From ed0dd9b7fabb777896b18c3c8c2a873e70a52e31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E4=BC=9F=E9=9F=AC?= Date: Fri, 13 Sep 2024 20:15:26 +0800 Subject: [PATCH] update test editor --- tests/metagpt/tools/libs/test_editor.py | 72 ++++++++++++------------- 1 file changed, 33 insertions(+), 39 deletions(-) diff --git a/tests/metagpt/tools/libs/test_editor.py b/tests/metagpt/tools/libs/test_editor.py index a716b3de4..07efd8132 100644 --- a/tests/metagpt/tools/libs/test_editor.py +++ b/tests/metagpt/tools/libs/test_editor.py @@ -25,7 +25,7 @@ def test_function_for_fm(): # this is the 7th line """.strip() -WINDOW = 100 +WINDOW = 200 @pytest.fixture @@ -196,11 +196,11 @@ def test_open_file(temp_file_path): expected = ( f"[File: {temp_file_path} (5 lines total)]\n" "(this is the beginning of the file)\n" - "1|Line 1\n" - "2|Line 2\n" - "3|Line 3\n" - "4|Line 4\n" - "5|Line 5\n" + "001|Line 1\n" + "002|Line 2\n" + "003|Line 3\n" + "004|Line 4\n" + "005|Line 5\n" "(this is the end of the file)" ) assert result.split("\n") == expected.split("\n") @@ -215,11 +215,11 @@ def test_open_file_with_indentation(temp_file_path): expected = ( f"[File: {temp_file_path} (5 lines total)]\n" "(this is the beginning of the file)\n" - "1|Line 1\n" - "2| Line 2\n" - "3|Line 3\n" - "4|Line 4\n" - "5|Line 5\n" + "001|Line 1\n" + "002| Line 2\n" + "003|Line 3\n" + "004|Line 4\n" + "005|Line 5\n" "(this is the end of the file)" ) assert result.split("\n") == expected.split("\n") @@ -235,7 +235,7 @@ def test_open_file_long(temp_file_path): expected = f"[File: {temp_file_path} (1000 lines total)]\n" expected += "(this is the beginning of the file)\n" for i in range(1, 51): - expected += f"{i}|Line {i}\n" + expected += f"{i:03d}|Line {i}\n" expected += "(950 more lines below)" assert result.split("\n") == expected.split("\n") @@ -245,7 +245,7 @@ def test_open_file_long_with_lineno(temp_file_path): content = "\n".join([f"Line {i}" for i in range(1, 1001)]) temp_file_path.write_text(content) - cur_line = 100 + cur_line = 300 result = editor.open_file(str(temp_file_path), cur_line) assert result is not None @@ -256,7 +256,7 @@ def test_open_file_long_with_lineno(temp_file_path): else: expected += f"({start - 1} more lines above)\n" for i in range(start, end + 1): - expected += f"{i}|Line {i}\n" + expected += f"{i:03d}|Line {i}\n" if end == 1000: expected += "(this is the end of the file)\n" else: @@ -290,7 +290,7 @@ def test_goto_line(temp_file_path): expected = f"[File: {temp_file_path} ({total_lines} lines total)]\n" expected += "(this is the beginning of the file)\n" for i in range(1, WINDOW + 1): - expected += f"{i}|Line {i}\n" + expected += f"{i:03d}|Line {i}\n" expected += f"({total_lines - WINDOW} more lines below)" assert result.split("\n") == expected.split("\n") @@ -306,7 +306,7 @@ def test_goto_line(temp_file_path): else: expected += f"({start - 1} more lines above)\n" for i in range(start, end + 1): - expected += f"{i}|Line {i}\n" + expected += f"{i:03d}|Line {i}\n" if end == total_lines: expected += "(this is the end of the file)\n" else: @@ -349,7 +349,7 @@ def test_scroll_down(temp_file_path): else: expected += f"({start - 1} more lines above)\n" for i in range(start, end + 1): - expected += f"{i}|Line {i}\n" + expected += f"{i:03d}|Line {i}\n" if end == total_lines: expected += "(this is the end of the file)" else: @@ -367,7 +367,7 @@ def test_scroll_down(temp_file_path): else: expected += f"({start - 1} more lines above)\n" for i in range(start, end + 1): - expected += f"{i}|Line {i}\n" + expected += f"{i:03d}|Line {i}\n" if end == total_lines: expected += "(this is the end of the file)\n" else: @@ -381,7 +381,7 @@ def test_scroll_up(temp_file_path): content = "\n".join([f"Line {i}" for i in range(1, total_lines + 1)]) temp_file_path.write_text(content) - cur_line = 300 + cur_line = 500 result = editor.open_file(str(temp_file_path), cur_line) assert result is not None @@ -393,11 +393,12 @@ def test_scroll_up(temp_file_path): else: expected += f"({start - 1} more lines above)\n" for i in range(start, end + 1): - expected += f"{i}|Line {i}\n" + expected += f"{i:03d}|Line {i}\n" if end == total_lines: expected += "(this is the end of the file)\n" else: expected += f"({total_lines - end} more lines below)" + assert result.split("\n") == expected.split("\n") result = editor.scroll_up() assert result is not None @@ -411,11 +412,13 @@ def test_scroll_up(temp_file_path): else: expected += f"({start - 1} more lines above)\n" for i in range(start, end + 1): - expected += f"{i}|Line {i}\n" + expected += f"{i:03d}|Line {i}\n" if end == total_lines: expected += "(this is the end of the file)\n" else: expected += f"({total_lines - end} more lines below)" + print(result) + print(expected) assert result.split("\n") == expected.split("\n") @@ -430,7 +433,7 @@ def test_scroll_down_edge(temp_file_path): expected = f"[File: {temp_file_path} (9 lines total)]\n" expected += "(this is the beginning of the file)\n" for i in range(1, 10): - expected += f"{i}|Line {i}\n" + expected += f"{i:03d}|Line {i}\n" expected += "(this is the end of the file)" result = editor.scroll_down() @@ -450,7 +453,7 @@ def test_print_window_internal(temp_file_path): window = 2 result = editor._print_window(temp_file_path, current_line, window) - expected = "(48 more lines above)\n" "49|Line `49`\n" "50|Line `50`\n" "51|Line `51`\n" "(49 more lines below)" + expected = "(48 more lines above)\n" "049|Line `49`\n" "050|Line `50`\n" "051|Line `51`\n" "(49 more lines below)" assert result == expected @@ -533,21 +536,12 @@ def test_function_for_fm(): def test_edit_file_by_replace(temp_py_file): editor = Editor() - editor.edit_file_by_replace(file_name=str(temp_py_file), to_replace=" b = 2", new_content=" b = 9") + editor.edit_file_by_replace(file_name=str(temp_py_file), start_line=5, end_line=5, new_content=" b = 9") with open(temp_py_file, "r") as f: new_content = f.read() assert new_content.strip() == EXPECTED_CONTENT_AFTER_REPLACE_TEXT.strip() -def test_edit_file_by_replace_to_palce_empty(empty_file): - editor = Editor() - with pytest.raises(ValueError) as exc_info: - editor.edit_file_by_replace( - file_name=str(empty_file), to_replace="", new_content=EXPECTED_CONTENT_AFTER_REPLACE_TEXT.strip() - ) - assert "is empty. Use the append method to add content." in str(exc_info.value) - - def test_append_file(temp_file_path): editor = Editor() # 写入初始内容 @@ -571,13 +565,13 @@ def test_append_file(temp_file_path): expected_output = ( f"[File: {temp_file_path.resolve()} (5 lines total after edit)]\n" "(this is the beginning of the file)\n" - "1|Line 1\n" - "2|Line 2\n" - "3|Line 3\n" - "4|Line 4\n" - "5|Line 5\n" + "001|Line 1\n" + "002|Line 2\n" + "003|Line 3\n" + "004|Line 4\n" + "005|Line 5\n" "(this is the end of the file)\n" - "[File updated (edited at line 3). Please review the changes and make sure they are correct (correct indentation, no duplicate lines, etc). Edit the file again if necessary.]" + "[File updated (edited at line 3)]." ) assert result.split("\n") == expected_output.split("\n")