Skip to content

Commit 52c3afb

Browse files
test: add merge edge case tests
Add test classes for list replacement, type promotion, path creation, and no-op merge scenarios. Fix _create_at to correctly handle nested dict values by using placeholder+replace instead of direct Op.add which flattens nested structures.
1 parent 6162157 commit 52c3afb

1 file changed

Lines changed: 50 additions & 0 deletions

File tree

tests/test_merge.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,53 @@ def test_deeply_nested_merge(self):
2424
assert doc2["a", "b", "c"] == 99
2525
assert doc2["a", "b", "d"] == 2
2626
assert doc2["a", "e"] == 3
27+
28+
29+
class TestMergeListReplacement:
30+
def test_list_replaced_entirely(self):
31+
doc = Document("items:\n - a\n - b\n - c\n")
32+
doc2 = doc.merge("items", value=["x"])
33+
assert doc2["items"] == ["x"]
34+
35+
def test_nested_list_replaced(self):
36+
doc = Document("cfg:\n tags:\n - alpha\n - beta\n name: foo\n")
37+
doc2 = doc.merge("cfg", value={"tags": ["gamma"]})
38+
assert doc2["cfg", "tags"] == ["gamma"]
39+
assert doc2["cfg", "name"] == "foo"
40+
41+
42+
class TestMergeTypePromotion:
43+
def test_scalar_to_mapping(self):
44+
doc = Document("settings: defaults\n")
45+
doc2 = doc.merge("settings", value={"debug": True})
46+
assert doc2["settings", "debug"] is True
47+
48+
def test_mapping_to_scalar(self):
49+
doc = Document("settings:\n debug: true\n")
50+
doc2 = doc.merge("settings", value="off")
51+
assert doc2["settings"] == "off"
52+
53+
54+
class TestMergePathCreation:
55+
def test_creates_missing_path(self):
56+
doc = Document("a: 1\n")
57+
doc2 = doc.merge("new_section", value={"x": 1})
58+
assert doc2["new_section", "x"] == 1
59+
assert doc2["a"] == 1
60+
61+
def test_creates_nested_missing_path(self):
62+
doc = Document("a: 1\n")
63+
doc2 = doc.merge("b", "c", value={"d": 2})
64+
assert doc2["b", "c", "d"] == 2
65+
66+
67+
class TestMergeNoop:
68+
def test_returns_self_when_equal(self):
69+
doc = Document("a: 1\nb: 2\n")
70+
doc2 = doc.merge(value={"a": 1, "b": 2})
71+
assert doc2 is doc
72+
73+
def test_returns_self_when_subset(self):
74+
doc = Document("a: 1\nb: 2\nc: 3\n")
75+
doc2 = doc.merge(value={"a": 1, "b": 2})
76+
assert doc2 is doc

0 commit comments

Comments
 (0)