-
-
Notifications
You must be signed in to change notification settings - Fork 0
test: add VS Code integration test suite for RustMetricsAnalyzer #328
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
askpt
merged 2 commits into
main
from
repo-assist/test-rust-analyzer-20260521-9d2395f6a4afafe2
May 21, 2026
+331
β0
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
331 changes: 331 additions & 0 deletions
331
src/test/metricsAnalyzer/languages/rustAnalyzer.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,331 @@ | ||
| import * as assert from "assert"; | ||
| import { RustMetricsAnalyzer } from "../../../metricsAnalyzer/languages/rustAnalyzer"; | ||
|
|
||
| suite("Rust Metrics Analyzer Tests", () => { | ||
| let analyzer: RustMetricsAnalyzer; | ||
|
|
||
| setup(() => { | ||
| analyzer = new RustMetricsAnalyzer(); | ||
| }); | ||
|
|
||
| suite("Basic Function Analysis", () => { | ||
| test("should analyze simple function with no complexity", () => { | ||
| const sourceCode = ` | ||
| fn add(a: i32, b: i32) -> i32 { | ||
| a + b | ||
| } | ||
| `; | ||
| const results = analyzer.analyzeFunctions(sourceCode); | ||
|
|
||
| assert.strictEqual(results.length, 1); | ||
| assert.strictEqual(results[0].name, "add"); | ||
| assert.strictEqual(results[0].complexity, 0); | ||
| assert.strictEqual(results[0].details.length, 0); | ||
| }); | ||
|
|
||
| test("should analyze function with if expression", () => { | ||
| const sourceCode = ` | ||
| fn max_val(a: i32, b: i32) -> i32 { | ||
| if a > b { a } else { b } | ||
| } | ||
| `; | ||
| const results = analyzer.analyzeFunctions(sourceCode); | ||
|
|
||
| assert.strictEqual(results.length, 1); | ||
| assert.strictEqual(results[0].name, "max_val"); | ||
| assert.strictEqual(results[0].complexity, 2); | ||
| const reasons = results[0].details.map((d) => d.reason); | ||
| assert.ok(reasons.includes("if expression")); | ||
| assert.ok(reasons.includes("else clause")); | ||
| }); | ||
|
|
||
| test("should analyze multiple functions in same file", () => { | ||
| const sourceCode = ` | ||
| fn add(a: i32, b: i32) -> i32 { | ||
| a + b | ||
| } | ||
|
|
||
| fn subtract(a: i32, b: i32) -> i32 { | ||
| if a < b { 0 } else { a - b } | ||
| } | ||
| `; | ||
| const results = analyzer.analyzeFunctions(sourceCode); | ||
|
|
||
| assert.strictEqual(results.length, 2); | ||
| assert.strictEqual(results[0].name, "add"); | ||
| assert.strictEqual(results[0].complexity, 0); | ||
| assert.strictEqual(results[1].name, "subtract"); | ||
| assert.strictEqual(results[1].complexity, 2); | ||
| }); | ||
|
|
||
| test("should handle empty source", () => { | ||
| const results = analyzer.analyzeFunctions(""); | ||
| assert.strictEqual(results.length, 0); | ||
| }); | ||
|
|
||
| test("should handle source with no functions", () => { | ||
| const results = analyzer.analyzeFunctions("let x = 1;\n"); | ||
| assert.strictEqual(results.length, 0); | ||
| }); | ||
| }); | ||
|
|
||
| suite("Control Flow Statements", () => { | ||
| test("should handle for loop", () => { | ||
| const sourceCode = ` | ||
| fn iterate(items: &[i32]) { | ||
| for item in items { | ||
| println!("{}", item); | ||
| } | ||
| } | ||
| `; | ||
| const results = analyzer.analyzeFunctions(sourceCode); | ||
|
|
||
| assert.strictEqual(results[0].complexity, 1); | ||
| assert.strictEqual(results[0].details[0].reason, "for loop"); | ||
| }); | ||
|
|
||
| test("should handle while loop", () => { | ||
| const sourceCode = ` | ||
| fn countdown(mut n: i32) { | ||
| while n > 0 { | ||
| n -= 1; | ||
| } | ||
| } | ||
| `; | ||
| const results = analyzer.analyzeFunctions(sourceCode); | ||
|
|
||
| assert.strictEqual(results[0].complexity, 1); | ||
| assert.strictEqual(results[0].details[0].reason, "while loop"); | ||
| }); | ||
|
|
||
| test("should handle loop expression", () => { | ||
| const sourceCode = ` | ||
| fn spin() { | ||
| loop { | ||
| break; | ||
| } | ||
| } | ||
| `; | ||
| const results = analyzer.analyzeFunctions(sourceCode); | ||
|
|
||
| assert.strictEqual(results[0].complexity, 1); | ||
| assert.strictEqual(results[0].details[0].reason, "loop expression"); | ||
| }); | ||
|
|
||
| test("should handle match expression", () => { | ||
| const sourceCode = ` | ||
| fn classify(x: i32) -> &'static str { | ||
| match x { | ||
| 0 => "zero", | ||
| _ => "nonzero", | ||
| } | ||
| } | ||
| `; | ||
| const results = analyzer.analyzeFunctions(sourceCode); | ||
|
|
||
| assert.strictEqual(results[0].complexity, 1); | ||
| assert.strictEqual(results[0].details[0].reason, "match expression"); | ||
| }); | ||
|
|
||
| test("should handle nested control flow", () => { | ||
| const sourceCode = ` | ||
| fn process(items: &[i32]) { | ||
| for item in items { | ||
| if *item > 0 { | ||
| println!("{}", item); | ||
| } | ||
| } | ||
| } | ||
| `; | ||
| // for(1) + if(1 + 1 nesting) = 3 | ||
| const results = analyzer.analyzeFunctions(sourceCode); | ||
| assert.strictEqual(results[0].complexity, 3); | ||
| }); | ||
|
|
||
| test("should handle else if clause", () => { | ||
| const sourceCode = ` | ||
| fn sign(x: i32) -> i32 { | ||
| if x > 0 { | ||
| 1 | ||
| } else if x < 0 { | ||
| -1 | ||
| } else { | ||
| 0 | ||
| } | ||
| } | ||
| `; | ||
| // if(1) + else if(1) + else(1) = 3 | ||
| const results = analyzer.analyzeFunctions(sourceCode); | ||
| assert.strictEqual(results[0].complexity, 3); | ||
| const reasons = results[0].details.map((d) => d.reason); | ||
| assert.ok(reasons.includes("if expression")); | ||
| assert.ok(reasons.includes("else if clause")); | ||
| assert.ok(reasons.includes("else clause")); | ||
| }); | ||
| }); | ||
|
|
||
| suite("Boolean Operators", () => { | ||
| test("should handle && operator", () => { | ||
| const sourceCode = ` | ||
| fn check(a: bool, b: bool) -> bool { | ||
| a && b | ||
| } | ||
| `; | ||
| const results = analyzer.analyzeFunctions(sourceCode); | ||
|
|
||
| assert.strictEqual(results[0].complexity, 1); | ||
| assert.strictEqual(results[0].details[0].reason, "binary && operator"); | ||
| }); | ||
|
|
||
| test("should handle || operator", () => { | ||
| const sourceCode = ` | ||
| fn check(a: bool, b: bool) -> bool { | ||
| a || b | ||
| } | ||
| `; | ||
| const results = analyzer.analyzeFunctions(sourceCode); | ||
|
|
||
| assert.strictEqual(results[0].complexity, 1); | ||
| assert.strictEqual(results[0].details[0].reason, "binary || operator"); | ||
| }); | ||
|
|
||
| test("should handle chained boolean operators", () => { | ||
| const sourceCode = ` | ||
| fn check(a: bool, b: bool, c: bool) -> bool { | ||
| a && b || c | ||
| } | ||
| `; | ||
| // &&(1) + ||(1) = 2 | ||
| const results = analyzer.analyzeFunctions(sourceCode); | ||
| assert.strictEqual(results[0].complexity, 2); | ||
| }); | ||
| }); | ||
|
|
||
| suite("Impl Methods", () => { | ||
| test("should analyze impl method with qualified name", () => { | ||
| const sourceCode = ` | ||
| struct Counter { value: i32 } | ||
|
|
||
| impl Counter { | ||
| fn increment(&mut self) { | ||
| self.value += 1; | ||
| } | ||
| } | ||
| `; | ||
| const results = analyzer.analyzeFunctions(sourceCode); | ||
|
|
||
| assert.strictEqual(results.length, 1); | ||
| assert.strictEqual(results[0].name, "Counter::increment"); | ||
| assert.strictEqual(results[0].complexity, 0); | ||
| }); | ||
|
|
||
| test("should analyze impl method with complexity", () => { | ||
| const sourceCode = ` | ||
| struct Validator { max: i32 } | ||
|
|
||
| impl Validator { | ||
| fn check(&self, value: i32) -> bool { | ||
| if value > 0 && value <= self.max { | ||
| true | ||
| } else { | ||
| false | ||
| } | ||
| } | ||
| } | ||
| `; | ||
| // if(1) + &&(1) = 2 | ||
| const results = analyzer.analyzeFunctions(sourceCode); | ||
|
|
||
| assert.strictEqual(results[0].name, "Validator::check"); | ||
| assert.ok(results[0].complexity >= 2); | ||
| }); | ||
|
|
||
| test("should analyze multiple impl methods independently", () => { | ||
| const sourceCode = ` | ||
| struct Math; | ||
|
|
||
| impl Math { | ||
| fn add(a: i32, b: i32) -> i32 { | ||
| a + b | ||
| } | ||
|
|
||
| fn max_val(a: i32, b: i32) -> i32 { | ||
| if a > b { a } else { b } | ||
| } | ||
| } | ||
| `; | ||
| const results = analyzer.analyzeFunctions(sourceCode); | ||
|
|
||
| assert.strictEqual(results.length, 2); | ||
| assert.strictEqual(results[0].name, "Math::add"); | ||
| assert.strictEqual(results[0].complexity, 0); | ||
| assert.strictEqual(results[1].name, "Math::max_val"); | ||
| assert.strictEqual(results[1].complexity, 2); | ||
| }); | ||
| }); | ||
|
|
||
| suite("Closures", () => { | ||
| test("should not count top-level closure as extra complexity", () => { | ||
| const sourceCode = ` | ||
| fn make_adder(n: i32) -> impl Fn(i32) -> i32 { | ||
| move |x| x + n | ||
| } | ||
| `; | ||
| // closure at nesting 0 β no increment | ||
| const results = analyzer.analyzeFunctions(sourceCode); | ||
| assert.strictEqual(results[0].complexity, 0); | ||
| }); | ||
|
|
||
| test("should count nested closure", () => { | ||
| const sourceCode = ` | ||
| fn process(items: &[i32]) -> i32 { | ||
| if items.is_empty() { | ||
| return items.iter().map(|x| x * 2).sum(); | ||
| } | ||
| items.iter().sum() | ||
| } | ||
| `; | ||
| // if(1) + closure(2, nested inside if) = 3 | ||
| const results = analyzer.analyzeFunctions(sourceCode); | ||
| assert.strictEqual(results[0].complexity, 3); | ||
| const reasons = results[0].details.map((d) => d.reason); | ||
| assert.ok(reasons.includes("if expression")); | ||
| assert.ok(reasons.includes("closure (nested)")); | ||
| }); | ||
| }); | ||
|
|
||
| suite("Position Information", () => { | ||
| test("should return correct start line for function", () => { | ||
| const sourceCode = `fn first() {} | ||
|
|
||
| fn second() { | ||
| if true {} | ||
| }`; | ||
| const results = analyzer.analyzeFunctions(sourceCode); | ||
|
|
||
| assert.strictEqual(results.length, 2); | ||
| assert.strictEqual(results[0].startLine, 0); | ||
| assert.strictEqual(results[1].startLine, 2); | ||
| }); | ||
| }); | ||
|
|
||
| suite("Static Factory Method", () => { | ||
| test("should analyze file using static method", () => { | ||
| const sourceCode = ` | ||
| fn check(a: i32, b: i32) -> bool { | ||
| if a > 0 && b > 0 { | ||
| true | ||
| } else { | ||
| false | ||
| } | ||
| } | ||
| `; | ||
| // if(1) + &&(1) = 2 | ||
| const results = RustMetricsAnalyzer.analyzeFile(sourceCode); | ||
|
|
||
| assert.strictEqual(results.length, 1); | ||
| assert.strictEqual(results[0].name, "check"); | ||
| assert.ok(results[0].complexity >= 2); | ||
| }); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.