Skip to content

Commit 456f1a5

Browse files
committed
Add exception checking to test_corpus.py;
Fix bug in 'restrict_search_range'
1 parent 916c5f4 commit 456f1a5

File tree

7 files changed

+55
-12
lines changed

7 files changed

+55
-12
lines changed

src/cedarscript_editor/cedarscript_editor.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,10 @@ def restrict_search_range(
325325
return inner_boundaries.location_to_search_range(BodyOrWhole.WHOLE)
326326
case RangeSpec() as inner_range_spec:
327327
return inner_range_spec
328+
case None:
329+
raise ValueError(f"Unable to find {region}")
330+
case _ as invalid:
331+
raise ValueError(f'Invalid: {invalid}')
328332
case Segment() as segment:
329333
return segment.to_search_range(lines, identifier_boundaries.whole if identifier_boundaries is not None else None)
330334
case _ as invalid:
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def i_exist():
2+
pass
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
def something():
2+
pass
3+
def i_exist():
4+
pass
5+
def something():
6+
pass
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<no-train>
2+
```CEDARScript
3+
UPDATE FILE "2.py" DELETE FUNCTION "i_exist";
4+
```
5+
<throws value="Unable to find function 'does-not-exist'">
6+
```CEDARScript
7+
UPDATE FILE "1.py" DELETE FUNCTION "does-not-exist";
8+
```
9+
</throws>
10+
</no-train>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def i_exist():
2+
pass
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
def something():
2+
pass
3+
def something():
4+
pass

tests/test_corpus.py

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import os
1+
import re
22
import shutil
33
import tempfile
44
import pytest
@@ -65,23 +65,38 @@ def copy_files(src_dir: Path, dst_dir: Path):
6565
# Find and apply commands
6666
commands = list(find_commands(chat_xml))
6767
assert commands, "No commands found in chat.xml"
68-
editor.apply_commands(commands)
68+
69+
# Check if test expects an exception
70+
throws_match = re.search(r'<throws value="([^"]+)">', chat_xml)
71+
if throws_match:
72+
expected_error = throws_match.group(1)
73+
with pytest.raises(Exception) as excinfo:
74+
editor.apply_commands(commands)
75+
# TODO excinfo.value is '<description>Unable to find function 'does-not-exist'</description>'
76+
actual_error = str(excinfo.value)
77+
match re.search(r'<description>(.+)</description>', actual_error):
78+
case None:
79+
pass
80+
case _ as found:
81+
actual_error = found.group(1)
82+
assert actual_error == expected_error, f"Expected error '{expected_error}', but got '{actual_error}'"
83+
else:
84+
editor.apply_commands(commands)
6985

70-
# For each file in the scratch directory, check its expected version
7186
def check_expected_files(dir_path: Path):
7287
for path in dir_path.iterdir():
7388
if path.is_dir():
7489
check_expected_files(path)
75-
else:
76-
# Find corresponding expected file in test directory
77-
rel_path = path.relative_to(editor.root_path)
78-
expected_file = test_dir / f"expected.{rel_path}"
79-
assert expected_file.exists(), f"Expected file not found: {expected_file}"
90+
continue
91+
# Find corresponding expected file in test directory
92+
rel_path = path.relative_to(editor.root_path)
93+
expected_file = test_dir / f"expected.{rel_path}"
94+
assert expected_file.exists(), f"Expected file not found: {expected_file}"
8095

81-
expected_content = f"[{rel_path}] \n" + expected_file.read_text()
82-
result_content = f"[{rel_path}] \n" + path.read_text()
83-
assert result_content.strip() == expected_content.strip(), \
84-
f"Output does not match expected content for {rel_path}"
96+
expected_content = f"[{rel_path}] \n" + expected_file.read_text()
97+
result_content = f"[{rel_path}] \n" + path.read_text()
98+
assert result_content.strip() == expected_content.strip(), \
99+
f"Output does not match expected content for {rel_path}"
85100

86101
check_expected_files(editor.root_path)
87102

0 commit comments

Comments
 (0)