|
| 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