mirror of
https://github.com/Aider-AI/aider.git
synced 2025-05-29 16:54:59 +00:00
added basic test coverage for find_diffs
This commit is contained in:
parent
cf83d5ecd2
commit
15c915e481
1 changed files with 66 additions and 36 deletions
|
@ -1,43 +1,73 @@
|
||||||
import unittest
|
import unittest
|
||||||
from unittest.mock import MagicMock, patch
|
|
||||||
from pathlib import Path
|
from aider.coders.udiff_coder import find_diffs
|
||||||
from aider.coders.udiff_coder import UnifiedDiffCoder, do_replace, apply_hunk, find_diffs
|
from aider.dump import dump # noqa: F401
|
||||||
from aider.io import InputOutput
|
|
||||||
|
|
||||||
class TestUnifiedDiffCoder(unittest.TestCase):
|
class TestUnifiedDiffCoder(unittest.TestCase):
|
||||||
def setUp(self):
|
|
||||||
self.coder = UnifiedDiffCoder(io=InputOutput())
|
|
||||||
|
|
||||||
def test_do_replace_new_file(self):
|
|
||||||
# Test do_replace when it should create a new file
|
|
||||||
hunk = [
|
|
||||||
"--- /dev/null",
|
|
||||||
"+++ newfile.txt",
|
|
||||||
"@@ -0,0 +1 @@",
|
|
||||||
"+New content\n"
|
|
||||||
]
|
|
||||||
result = do_replace('newfile.txt', None, hunk)
|
|
||||||
self.assertEqual(result, 'New content\n')
|
|
||||||
|
|
||||||
def test_apply_hunk_with_changes(self):
|
|
||||||
# Test apply_hunk with actual changes
|
|
||||||
content = "Original line 1\nOriginal line 2\n"
|
|
||||||
hunk = [
|
|
||||||
"@@ -1,2 +1,2 @@",
|
|
||||||
" Original line 1",
|
|
||||||
"-Original line 2",
|
|
||||||
"+Modified line 2"
|
|
||||||
]
|
|
||||||
result = apply_hunk(content, hunk)
|
|
||||||
self.assertEqual(result, "Original line 1\nModified line 2\n")
|
|
||||||
|
|
||||||
def test_find_diffs_single_hunk(self):
|
def test_find_diffs_single_hunk(self):
|
||||||
# Test find_diffs with a single hunk
|
# Test find_diffs with a single hunk
|
||||||
content = "```diff\n--- a/file.txt\n+++ b/file.txt\n@@ -1,2 +1,2 @@\n-Original\n+Modified\n```\n"
|
content = """
|
||||||
result = find_diffs(content)
|
Some text...
|
||||||
self.assertEqual(len(result), 1)
|
|
||||||
self.assertEqual(result[0][0], 'b/file.txt')
|
|
||||||
self.assertEqual(result[0][1], ['@@ -1,2 +1,2 @@\n', '-Original\n', '+Modified\n'])
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
```diff
|
||||||
|
--- file.txt
|
||||||
|
+++ file.txt
|
||||||
|
@@ ... @@
|
||||||
|
-Original
|
||||||
|
+Modified
|
||||||
|
```
|
||||||
|
"""
|
||||||
|
edits = find_diffs(content)
|
||||||
|
dump(edits)
|
||||||
|
self.assertEqual(len(edits), 1)
|
||||||
|
|
||||||
|
edit = edits[0]
|
||||||
|
self.assertEqual(edit[0], "file.txt")
|
||||||
|
self.assertEqual(edit[1], ["-Original\n", "+Modified\n"])
|
||||||
|
|
||||||
|
def test_find_diffs_dev_null(self):
|
||||||
|
# Test find_diffs with a single hunk
|
||||||
|
content = """
|
||||||
|
Some text...
|
||||||
|
|
||||||
|
```diff
|
||||||
|
--- /dev/null
|
||||||
|
+++ file.txt
|
||||||
|
@@ ... @@
|
||||||
|
-Original
|
||||||
|
+Modified
|
||||||
|
```
|
||||||
|
"""
|
||||||
|
edits = find_diffs(content)
|
||||||
|
dump(edits)
|
||||||
|
self.assertEqual(len(edits), 1)
|
||||||
|
|
||||||
|
edit = edits[0]
|
||||||
|
self.assertEqual(edit[0], "file.txt")
|
||||||
|
self.assertEqual(edit[1], ["-Original\n", "+Modified\n"])
|
||||||
|
|
||||||
|
def test_find_diffs_dirname_with_spaces(self):
|
||||||
|
# Test find_diffs with a single hunk
|
||||||
|
content = """
|
||||||
|
Some text...
|
||||||
|
|
||||||
|
```diff
|
||||||
|
--- dir name with spaces/file.txt
|
||||||
|
+++ dir name with spaces/file.txt
|
||||||
|
@@ ... @@
|
||||||
|
-Original
|
||||||
|
+Modified
|
||||||
|
```
|
||||||
|
"""
|
||||||
|
edits = find_diffs(content)
|
||||||
|
dump(edits)
|
||||||
|
self.assertEqual(len(edits), 1)
|
||||||
|
|
||||||
|
edit = edits[0]
|
||||||
|
self.assertEqual(edit[0], "dir name with spaces/file.txt")
|
||||||
|
self.assertEqual(edit[1], ["-Original\n", "+Modified\n"])
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue