Skip to main content

Restore the tests for consistent round-tripping

ID
679ae0f
date
2025-01-15 06:44:43+00:00
author
Alex Chan <alex@alexwlchan.net>
parent
88561f5
message
Restore the tests for consistent round-tripping
changed files
1 file, 43 additions

Changed files

tests/test_javascript_data_files.py (14431) → tests/test_javascript_data_files.py (16002)

diff --git a/tests/test_javascript_data_files.py b/tests/test_javascript_data_files.py
index 75e93f4..6130c33 100644
--- a/tests/test_javascript_data_files.py
+++ b/tests/test_javascript_data_files.py
@@ -340,6 +340,26 @@ class TestAppendToArray:
         with pytest.raises(IsADirectoryError):
             append_to_js_array(tmp_path, value="alex")
 
+    def test_indentation_is_consistent(self, tmp_path: pathlib.Path) -> None:
+        """
+        If you append to an array, the file looks as if you'd read and rewritten
+        the whole thing with ``write_js()``.
+        """
+        js_path1 = tmp_path / "data1.js"
+        js_path2 = tmp_path / "data2.js"
+
+        # We use deliberately large value, so they won't be compressed
+        # by the custom encoder.
+        value = ["1" * 10, "2" * 20, "3" * 30]
+        appended_value = ["4" * 40, "5" * 50, "6" * 60]
+
+        write_js(js_path1, varname="numbers", value=value)
+        append_to_js_array(js_path1, value=appended_value)
+
+        write_js(js_path2, varname="numbers", value=value + [appended_value])
+
+        assert js_path1.read_text() == js_path2.read_text()
+
 
 class TestAppendToObject:
     """
@@ -375,6 +395,29 @@ class TestAppendToObject:
             "sideLengths": [1, 2, 3, 4, 5],
         }
 
+    def test_indentation_is_consistent(self, tmp_path: pathlib.Path) -> None:
+        """
+        If you append to an object, the file looks as if you'd read and
+        rewritten the whole thing with ``write_js()``.
+        """
+        js_path1 = tmp_path / "data1.js"
+        js_path2 = tmp_path / "data2.js"
+
+        # We pick a deliberately large value, so it won't be compressed
+        # by the custom encoder.
+        value = ["1" * 10, "2" * 20, "3" * 30]
+
+        write_js(js_path1, varname="shape", value={"colour": "red"})
+        append_to_js_object(js_path1, key="sides", value=value)
+
+        write_js(
+            js_path2,
+            varname="shape",
+            value={"colour": "red", "sides": value},
+        )
+
+        assert js_path1.read_text() == js_path2.read_text()
+
     def test_error_if_file_doesnt_look_like_object(self, js_path: pathlib.Path) -> None:
         """
         Appending to a file which doesn't contain a JSON object throws