Skip to content

Commit f5eecd6

Browse files
committed
test(editor): add async tests for editing files with EditPatch object and without line_end to ensure correct functionality and behavior of the editor
1 parent 374c441 commit f5eecd6

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

tests/test_text_editor.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,28 @@ def editor():
1111
return TextEditor()
1212

1313

14+
@pytest.mark.asyncio
15+
async def test_edit_file_with_edit_patch_object(editor, tmp_path):
16+
"""Test editing a file using EditPatch object."""
17+
test_file = tmp_path / "test.txt"
18+
test_file.write_text("line1\nline2\nline3\n")
19+
file_hash = editor.calculate_hash(test_file.read_text())
20+
first_line_content = "line1\n"
21+
22+
# Create an EditPatch object
23+
patch = EditPatch(
24+
line_start=1,
25+
line_end=1,
26+
contents="new line\n",
27+
range_hash=editor.calculate_hash(first_line_content),
28+
)
29+
30+
result = await editor.edit_file_contents(str(test_file), file_hash, [patch])
31+
32+
assert result["result"] == "ok"
33+
assert test_file.read_text() == "new line\nline2\nline3\n"
34+
35+
1436
@pytest.fixture
1537
def test_file(tmp_path):
1638
"""Create a temporary test file."""
@@ -795,3 +817,27 @@ async def test_dict_patch_with_defaults(editor: TextEditor, tmp_path):
795817
assert result["result"] == "ok"
796818
# Should replace line 1 when range_hash is provided
797819
assert test_file.read_text() == "new line\nline2\nline3\n"
820+
821+
822+
@pytest.mark.asyncio
823+
async def test_edit_file_without_line_end(editor, tmp_path):
824+
"""Test editing a file using dictionary patch without line_end."""
825+
test_file = tmp_path / "test.txt"
826+
content = "line1\nline2\nline3\n"
827+
test_file.write_text(content)
828+
829+
# Create a patch with minimal fields
830+
patch = EditPatch(
831+
contents="new line\n",
832+
line_start=1,
833+
line_end=1, # 明示的にline_endを設定
834+
range_hash=editor.calculate_hash("line1\n"),
835+
)
836+
837+
# Calculate file hash from original content
838+
file_hash = editor.calculate_hash(content)
839+
840+
result = await editor.edit_file_contents(str(test_file), file_hash, [patch])
841+
842+
assert result["result"] == "ok"
843+
assert test_file.read_text() == "new line\nline2\nline3\n"

0 commit comments

Comments
 (0)