diff --git a/tests/test_code_analyzer.py b/tests/test_code_analyzer.py new file mode 100644 index 0000000..ee4ad4c --- /dev/null +++ b/tests/test_code_analyzer.py @@ -0,0 +1,87 @@ +"""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 CodeAnalyzer, 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" + + def test_analyze_javascript_code(self, code_analyzer): + code = """function add(a, b) { + return a + b; +}""" + result = code_analyzer.analyze_code(code, "javascript") + assert result['language'] == "javascript" + + 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_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) + + 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() + + +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)