Files
git-diff-explainer-cli/tests/test_code_analyzer.py
7000pctAUTO 4f3a17e3a6
Some checks failed
CI / test (3.10) (push) Has been cancelled
CI / test (3.11) (push) Has been cancelled
CI / test (3.12) (push) Has been cancelled
CI / lint (push) Has been cancelled
CI / build (push) Has been cancelled
fix: resolve CI linting errors - remove unused imports and update type annotations
2026-02-02 14:39:12 +00:00

152 lines
4.6 KiB
Python

"""Tests for the CodeAnalyzer module."""
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent.parent / 'src'))
from gdiffer.code_analyzer import analyze_code, summarize_change
class TestCodeAnalyzer:
"""Tests for CodeAnalyzer class."""
def test_analyze_empty_code(self, code_analyzer):
"""Test analyzing empty code."""
result = code_analyzer.analyze_code("")
assert result['language'] == "text"
assert result['change_summary'] == ""
def test_analyze_python_code(self, code_analyzer):
"""Test analyzing Python code."""
code = """def hello():
return "Hello"
class Greeter:
def greet(self, name):
return f"Hello, {name}"
"""
result = code_analyzer.analyze_code(code, "python")
assert result['language'] == "python"
assert 'functions' in result or 'ast_info' in result
def test_analyze_javascript_code(self, code_analyzer):
"""Test analyzing JavaScript code."""
code = """function add(a, b) {
return a + b;
}
const multiply = (x, y) => x * y;
"""
result = code_analyzer.analyze_code(code, "javascript")
assert result['language'] == "javascript"
def test_analyze_rust_code(self, code_analyzer):
"""Test analyzing Rust code."""
code = """fn main() {
println!("Hello");
}
struct Point {
x: i32,
y: i32,
}
"""
result = code_analyzer.analyze_code(code, "rust")
assert result['language'] == "rust"
def test_summarize_change_simple(self, code_analyzer):
"""Test summarizing simple code changes."""
old_code = "def hello():\n return 'Hello'"
new_code = "def hello():\n return 'Hello, World!'"
summary = code_analyzer.summarize_change(old_code, new_code, "python")
assert isinstance(summary, str)
assert len(summary) > 0
def test_summarize_change_added_function(self, code_analyzer):
"""Test summarizing when a function is added."""
old_code = ""
new_code = "def new_func():\n pass"
summary = code_analyzer.summarize_change(old_code, new_code, "python")
assert isinstance(summary, str)
assert len(summary) > 0
def test_summarize_change_removed_function(self, code_analyzer):
"""Test summarizing when a function is removed."""
old_code = "def old_func():\n pass"
new_code = ""
summary = code_analyzer.summarize_change(old_code, new_code, "python")
assert isinstance(summary, str)
def test_analyze_code_without_parser(self, code_analyzer):
"""Test analyzing code when tree-sitter parser is unavailable."""
code = "def test(): pass"
result = code_analyzer.analyze_code(code, "unknown_language")
assert 'change_summary' in result
def test_fallback_analysis_detects_functions(self, code_analyzer):
"""Test that fallback analysis can detect functions."""
code = """def calculate_sum(a, b):
return a + b
def multiply(x, y):
return x * y
"""
result = code_analyzer._analyze_without_parser(code)
assert isinstance(result, str)
assert "calculate_sum" in result or "multiply" in result or "function" in result.lower()
def test_fallback_analysis_detects_classes(self, code_analyzer):
"""Test that fallback analysis can detect classes."""
code = """class Calculator:
def add(self, a, b):
return a + b
"""
result = code_analyzer._analyze_without_parser(code)
assert "Calculator" in result or "class" in result.lower()
def test_fallback_analysis_line_count(self, code_analyzer):
"""Test that fallback analysis includes line count."""
old_code = "line1\nline2"
new_code = "line1\nline2\nline3"
result = code_analyzer._analyze_without_parser(new_code)
assert isinstance(result, str)
class TestAnalyzeCodeFunction:
"""Tests for the analyze_code convenience function."""
def test_analyze_code_function(self):
"""Test analyze_code convenience function."""
result = analyze_code("def test(): pass", "python")
assert 'language' in result
assert 'change_summary' in result
def test_analyze_code_empty(self):
"""Test analyze_code with empty code."""
result = analyze_code("", "text")
assert result['language'] == "text"
class TestSummarizeChangeFunction:
"""Tests for the summarize_change convenience function."""
def test_summarize_change_function(self):
"""Test summarize_change convenience function."""
result = summarize_change("old", "new", "text")
assert isinstance(result, str)