Spaces:
Sleeping
Sleeping
Download tests/unit/test_code_analyzer.py from Divb30/intellicode-rag: direct link, hf CLI and curl.
- Browser
- Download file 4.74 kB
-
https://huggingface.co/spaces/Divb30/intellicode-rag/resolve/main/tests/unit/test_code_analyzer.py
- Command line
-
hf download hf://spaces/Divb30/intellicode-rag/tests/unit/test_code_analyzer.py
-
curl -L -o test_code_analyzer.py https://huggingface.co/spaces/Divb30/intellicode-rag/resolve/main/tests/unit/test_code_analyzer.py
4.74 kB
| """Unit tests for the AST code analyzer.""" | |
| from __future__ import annotations | |
| import pytest | |
| from intellicode.analysis import CodeAnalyzer | |
| def analyzer() -> CodeAnalyzer: | |
| return CodeAnalyzer() | |
| # ββ Individual anti-pattern detection ββββββββββββββββββββββββββββββββββββββββ | |
| def test_detects_pattern(analyzer, code, expected_type): | |
| """Each anti-pattern is flagged with the correct issue type.""" | |
| result = analyzer.analyze_code(code) | |
| assert result.success | |
| assert expected_type in {issue.type for issue in result.issues} | |
| def test_detects_unused_variable(analyzer): | |
| result = analyzer.analyze_code("def f():\n unused = 5\n return 10") | |
| assert "Unused variable" in {i.type for i in result.issues} | |
| def test_underscore_variable_not_flagged_as_unused(analyzer): | |
| """Names prefixed with underscore are intentionally-unused and ignored.""" | |
| result = analyzer.analyze_code("def f():\n _ignored = 5\n return 10") | |
| assert "Unused variable" not in {i.type for i in result.issues} | |
| # ββ Async support (the original bug) βββββββββββββββββββββββββββββββββββββββββ | |
| def test_async_function_mutable_default(analyzer): | |
| """AsyncFunctionDef must be analyzed, not skipped (regression guard).""" | |
| result = analyzer.analyze_code("async def fetch(cache=[]):\n return cache") | |
| assert "Mutable default argument" in {i.type for i in result.issues} | |
| def test_async_function_bare_except(analyzer): | |
| code = "async def run():\n try:\n await x()\n except:\n pass" | |
| result = analyzer.analyze_code(code) | |
| assert "Bare except clause" in {i.type for i in result.issues} | |
| # ββ Complexity & nesting βββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| def test_deep_nesting_detected(analyzer): | |
| code = ( | |
| "def f(data):\n" | |
| " for a in data:\n" | |
| " if a:\n" | |
| " while a:\n" | |
| " if a > 1:\n" | |
| " a -= 1" | |
| ) | |
| assert "Deep nesting" in {i.type for i in analyzer.analyze_code(code).issues} | |
| def test_high_complexity_detected(): | |
| analyzer = CodeAnalyzer(complexity_threshold=3) | |
| code = "def f(a, b, c):\n if a:\n pass\n if b:\n pass\n if c:\n pass" | |
| assert "High cyclomatic complexity" in {i.type for i in analyzer.analyze_code(code).issues} | |
| # ββ Metrics ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| def test_metrics_counts(analyzer): | |
| code = "# a comment\nclass A:\n def m(self):\n return 1\n\ndef g():\n return 2" | |
| result = analyzer.analyze_code(code) | |
| assert result.metrics["functions"] == 2 | |
| assert result.metrics["classes"] == 1 | |
| assert result.metrics["comment_lines"] == 1 | |
| # ββ Edge cases βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| def test_syntax_error_returns_failure(analyzer): | |
| result = analyzer.analyze_code("def f(:\n pass") | |
| assert not result.success | |
| assert "Syntax Error" in result.error | |
| def test_empty_code_succeeds_with_no_issues(analyzer): | |
| result = analyzer.analyze_code("") | |
| assert result.success | |
| assert result.issues == [] | |
| def test_clean_code_has_no_high_severity(analyzer, clean_code_path): | |
| result = analyzer.analyze_file(str(clean_code_path)) | |
| assert result.success | |
| assert all(i.severity != "HIGH" for i in result.issues) | |
| def test_missing_file_returns_failure(analyzer): | |
| result = analyzer.analyze_file("does_not_exist_12345.py") | |
| assert not result.success | |
| assert "not found" in result.error.lower() | |