diff --git a/scripts/test_diff_analyzer.py b/scripts/test_diff_analyzer.py new file mode 100644 index 0000000..636b5a2 --- /dev/null +++ b/scripts/test_diff_analyzer.py @@ -0,0 +1,189 @@ +#!/usr/bin/env python3 +"""Tests for scripts/diff_analyzer.py — 10 tests.""" + +import sys +import os +sys.path.insert(0, os.path.dirname(__file__) or ".") + +import importlib.util +spec = importlib.util.spec_from_file_location("da", os.path.join(os.path.dirname(__file__) or ".", "diff_analyzer.py")) +mod = importlib.util.module_from_spec(spec) +spec.loader.exec_module(mod) +DiffAnalyzer = mod.DiffAnalyzer +ChangeCategory = mod.ChangeCategory + + +SAMPLE_ADD = """diff --git a/new.py b/new.py +new file mode 100644 +--- /dev/null ++++ b/new.py +@@ -0,0 +1,3 @@ ++def hello(): ++ print("world") ++ return True +""" + +SAMPLE_DELETE = """diff --git a/old.py b/old.py +deleted file mode 100644 +--- a/old.py ++++ /dev/null +@@ -1,2 +0,0 @@ +-def goodbye(): +- pass +""" + +SAMPLE_MODIFY = """diff --git a/app.py b/app.py +--- a/app.py ++++ b/app.py +@@ -1,3 +1,4 @@ + def main(): +- print("old") ++ print("new") ++ print("extra") + return 0 +""" + +SAMPLE_RENAME = """diff --git a/old_name.py b/new_name.py +rename from old_name.py +rename to new_name.py +--- a/old_name.py ++++ b/new_name.py +@@ -1,1 +1,1 @@ +-old content ++new content +""" + +SAMPLE_MULTI = """diff --git a/a.py b/a.py +--- a/a.py ++++ b/a.py +@@ -1,1 +1,2 @@ + existing ++added line +diff --git b/b.py b/b.py +new file mode 100644 +--- /dev/null ++++ b/b.py +@@ -0,0 +1,1 @@ ++new file +""" + +SAMPLE_BINARY = """diff --git a/img.png b/img.png +Binary files a/img.png and b/img.png differ +""" + + +def test_empty(): + a = DiffAnalyzer() + s = a.analyze("") + assert s.total_files_changed == 0 + print("PASS: test_empty") + +def test_addition(): + a = DiffAnalyzer() + s = a.analyze(SAMPLE_ADD) + assert s.total_files_changed == 1 + assert s.total_added == 3 + assert s.total_deleted == 0 + assert s.new_files == 1 + assert s.files[0].hunks[0].category == ChangeCategory.ADDED + print("PASS: test_addition") + +def test_deletion(): + a = DiffAnalyzer() + s = a.analyze(SAMPLE_DELETE) + assert s.total_deleted == 2 + assert s.deleted_files == 1 + assert s.files[0].hunks[0].category == ChangeCategory.DELETED + print("PASS: test_deletion") + +def test_modification(): + a = DiffAnalyzer() + s = a.analyze(SAMPLE_MODIFY) + assert s.total_added == 2 + assert s.total_deleted == 1 + assert s.files[0].hunks[0].category == ChangeCategory.MODIFIED + print("PASS: test_modification") + +def test_rename(): + a = DiffAnalyzer() + s = a.analyze(SAMPLE_RENAME) + assert s.renamed_files == 1 + assert s.files[0].old_path == "old_name.py" + assert s.files[0].path == "new_name.py" + assert s.files[0].is_renamed == True + print("PASS: test_rename") + +def test_multiple_files(): + a = DiffAnalyzer() + s = a.analyze(SAMPLE_MULTI) + assert s.total_files_changed == 2 + assert s.new_files == 1 + print("PASS: test_multiple_files") + +def test_binary(): + a = DiffAnalyzer() + s = a.analyze(SAMPLE_BINARY) + assert s.binary_files == 1 + assert s.files[0].is_binary == True + assert len(s.files[0].hunks) == 0 + print("PASS: test_binary") + +def test_to_dict(): + a = DiffAnalyzer() + s = a.analyze(SAMPLE_MODIFY) + d = s.to_dict() + assert "total_files_changed" in d + assert "files" in d + assert isinstance(d["files"], list) + print("PASS: test_to_dict") + +def test_context_only(): + diff = """diff --git a/f.py b/f.py +--- a/f.py ++++ b/f.py +@@ -1,3 +1,3 @@ + line1 +-old ++new + line3 +""" + a = DiffAnalyzer() + s = a.analyze(diff) + # Has both added and deleted = MODIFIED + assert s.files[0].hunks[0].category == ChangeCategory.MODIFIED + print("PASS: test_context_only") + +def test_multi_hunk(): + diff = """diff --git a/f.py b/f.py +--- a/f.py ++++ b/f.py +@@ -1,1 +1,2 @@ + existing ++first addition +@@ -10,1 +11,2 @@ + more ++second addition +""" + a = DiffAnalyzer() + s = a.analyze(diff) + assert s.total_hunks == 2 + assert s.total_added == 2 + print("PASS: test_multi_hunk") + + +def run_all(): + test_empty() + test_addition() + test_deletion() + test_modification() + test_rename() + test_multiple_files() + test_binary() + test_to_dict() + test_context_only() + test_multi_hunk() + print("\nAll 10 tests passed!") + + +if __name__ == "__main__": + run_all()