From 4c8e7f7964f7925253848dfb82533140aeacc301 Mon Sep 17 00:00:00 2001 From: Serj Date: Mon, 17 Apr 2023 22:13:48 +0300 Subject: [PATCH] Added tests for apply changes method --- tests/test_wolverine.py | 54 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 tests/test_wolverine.py diff --git a/tests/test_wolverine.py b/tests/test_wolverine.py new file mode 100644 index 0000000..0b44c69 --- /dev/null +++ b/tests/test_wolverine.py @@ -0,0 +1,54 @@ +import os +import pytest +import tempfile +from wolverine import apply_changes + + +@pytest.fixture(scope='function') +def temp_file(): + # Create a temporary file + with tempfile.NamedTemporaryFile(mode="w", delete=False) as f: + f.write("first line\nsecond line\nthird line") + file_path = f.name + yield file_path + # Clean up the temporary file + os.remove(file_path) + + +def test_apply_changes_replace(temp_file): + # Make a "replace" change to the second line + changes = [ + {"operation": "Replace", "line": 2, "content": "new second line"} + ] + apply_changes(temp_file, changes) + + # Check that the file was updated correctly + with open(temp_file) as f: + content = f.read() + assert content == "first line\nnew second line\nthird line" + + +def test_apply_changes_delete(temp_file): + # Make a "delete" change to the third line + changes = [ + {"operation": "Delete", "line": 3}, + ] + apply_changes(temp_file, changes) + + # Check that the file was updated correctly + with open(temp_file) as f: + content = f.read() + assert content == "first line\nsecond line" + + +def test_apply_changes_insert(temp_file): + # Make an "insert" change after the second line + changes = [ + {"operation": "InsertAfter", "line": 2, "content": "inserted line"}, + ] + apply_changes(temp_file, changes) + + # Check that the file was updated correctly + with open(temp_file) as f: + content = f.read() + assert content == "first line\nsecond line\ninserted line"