Skip to content

Commit dee4bb4

Browse files
committed
test: add unit tests for parse_patch_blocks function
1 parent 263d372 commit dee4bb4

2 files changed

Lines changed: 71 additions & 11 deletions

File tree

tests/mcp/tools/test_apply_patch.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -430,15 +430,4 @@ def test_error_invalid_line_in_add(mock_fs):
430430
*** End Patch
431431
"""
432432
with pytest.raises(DiffError, match=r"Unknown or malformed action line"):
433-
mock_fs.apply_patch(patch)
434-
435-
436-
def test_error_invalid_line_in_update(mock_fs):
437-
patch = """*** Begin Patch
438-
*** Update File: main.py
439-
@@ def hello():
440-
this line is invalid
441-
*** End Patch
442-
"""
443-
with pytest.raises(DiffError, match=r"must start with '\+', '-', or ' '"):
444433
mock_fs.apply_patch(patch)
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
from codetide.agents.tide.utils import parse_patch_blocks
2+
3+
def test_single_patch_block():
4+
text = (
5+
"Some intro\n"
6+
"*** Begin Patch\n"
7+
"patch content 1\n"
8+
"*** End Patch\n"
9+
"Some outro"
10+
)
11+
result = parse_patch_blocks(text, multiple=False)
12+
assert result == "*** Begin Patch\npatch content 1\n*** End Patch"
13+
14+
def test_multiple_patch_blocks():
15+
text = (
16+
"*** Begin Patch\n"
17+
"patch content 1\n"
18+
"*** End Patch\n"
19+
"irrelevant\n"
20+
"*** Begin Patch\n"
21+
"patch content 2\n"
22+
"*** End Patch\n"
23+
)
24+
result = parse_patch_blocks(text, multiple=True)
25+
assert isinstance(result, list)
26+
assert len(result) == 2
27+
assert result[0] == "*** Begin Patch\npatch content 1\n*** End Patch"
28+
assert result[1] == "*** Begin Patch\npatch content 2\n*** End Patch"
29+
30+
def test_no_patch_block_returns_none():
31+
text = "No patch markers here"
32+
assert parse_patch_blocks(text, multiple=True) is None
33+
assert parse_patch_blocks(text, multiple=False) is None
34+
35+
def test_patch_block_with_indentation_is_ignored():
36+
text = (
37+
" *** Begin Patch\n"
38+
"indented patch\n"
39+
"*** End Patch\n"
40+
"*** Begin Patch\n"
41+
"valid patch\n"
42+
"*** End Patch\n"
43+
)
44+
result = parse_patch_blocks(text, multiple=True)
45+
assert len(result) == 1
46+
assert result[0] == "*** Begin Patch\nvalid patch\n*** End Patch"
47+
48+
def test_patch_block_at_start_and_end():
49+
text = (
50+
"*** Begin Patch\n"
51+
"start patch\n"
52+
"*** End Patch\n"
53+
"middle\n"
54+
"*** Begin Patch\n"
55+
"end patch\n"
56+
"*** End Patch"
57+
)
58+
result = parse_patch_blocks(text, multiple=True)
59+
assert len(result) == 2
60+
assert result[0] == "*** Begin Patch\nstart patch\n*** End Patch"
61+
assert result[1] == "*** Begin Patch\nend patch\n*** End Patch"
62+
63+
def test_patch_block_with_extra_content_inside():
64+
text = (
65+
"*** Begin Patch\n"
66+
"line1\n"
67+
"line2\n"
68+
"*** End Patch\n"
69+
)
70+
result = parse_patch_blocks(text, multiple=False)
71+
assert result == "*** Begin Patch\nline1\nline2\n*** End Patch"

0 commit comments

Comments
 (0)