Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/unit/unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import * as assert from "assert";
import type Parser from "tree-sitter";
import { CSharpMetricsAnalyzer } from "../metricsAnalyzer/languages/csharpAnalyzer";
import { GoMetricsAnalyzer } from "../metricsAnalyzer/languages/goAnalyzer";
import { JavaMetricsAnalyzer } from "../metricsAnalyzer/languages/javaAnalyzer";
Expand All @@ -22,6 +23,7 @@ import {
} from "../metricsAnalyzer/metricsAnalyzerFactory";
import { SampleCSharpCode } from "../test/testUtils";
import { LruCache } from "../lruCache";
import { isOutermostInSameOperatorChain } from "../metricsAnalyzer/languages/complexityHelpers";

describe("Core Logic Unit Tests (Node.js)", () => {
describe("LruCache", () => {
Expand Down Expand Up @@ -109,6 +111,47 @@ describe("Core Logic Unit Tests (Node.js)", () => {
});
});

describe("complexityHelpers: isOutermostInSameOperatorChain", () => {
it("returns true when the node has no parent (root of tree)", () => {
const node = { parent: null, type: "binary_expression" } as unknown as Parser.SyntaxNode;
const result = isOutermostInSameOperatorChain(node, "&&", "binary_expression", () => "&&");
assert.strictEqual(result, true);
});

it("returns false when the parent is the same kind with the same operator (inner node of a chain)", () => {
const parent = { type: "binary_expression" } as unknown as Parser.SyntaxNode;
const node = { parent, type: "binary_expression" } as unknown as Parser.SyntaxNode;
const result = isOutermostInSameOperatorChain(node, "&&", "binary_expression", () => "&&");
assert.strictEqual(result, false);
});

it("returns true when the parent is the same kind but with a different operator", () => {
const parent = { type: "binary_expression" } as unknown as Parser.SyntaxNode;
const node = { parent, type: "binary_expression" } as unknown as Parser.SyntaxNode;
const result = isOutermostInSameOperatorChain(node, "&&", "binary_expression", () => "||");
assert.strictEqual(result, true);
});

it("returns true when the parent is a different node type entirely", () => {
const parent = { type: "if_statement" } as unknown as Parser.SyntaxNode;
const node = { parent, type: "binary_expression" } as unknown as Parser.SyntaxNode;
const result = isOutermostInSameOperatorChain(node, "&&", "binary_expression", () => "&&");
assert.strictEqual(result, true);
});

it("supports an array of sameKindTypes", () => {
const parent = { type: "logical_expression" } as unknown as Parser.SyntaxNode;
const node = { parent, type: "binary_expression" } as unknown as Parser.SyntaxNode;
const result = isOutermostInSameOperatorChain(
node,
"&&",
["binary_expression", "logical_expression"],
() => "&&"
);
assert.strictEqual(result, false);
});
});

describe("Go Analyzer Core Logic", () => {
it("should analyze simple function correctly", () => {
const analyzer = new GoMetricsAnalyzer();
Expand Down