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: def test_analyze_empty_code(self, code_analyzer): result = code_analyzer.analyze_code("") assert result['language'] == "text" assert result['change_summary'] == "" def test_analyze_python_code(self, code_analyzer): 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): 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): 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): 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): 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): 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): 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): 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): 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): new_code = "line1\nline2\nline3" result = code_analyzer._analyze_without_parser(new_code) assert isinstance(result, str) class TestAnalyzeCodeFunction: def test_analyze_code_function(self): result = analyze_code("def test(): pass", "python") assert 'language' in result assert 'change_summary' in result def test_analyze_code_empty(self): result = analyze_code("", "text") assert result['language'] == "text" class TestSummarizeChangeFunction: def test_summarize_change_function(self): result = summarize_change("old", "new", "text") assert isinstance(result, str)