Skip to content

Commit 16de994

Browse files
committed
Add MappingKey/Tagged interaction tests and complex round-trip fixtures
- Add hashing/equality coverage for MappingKey wrapping Tagged values and verify Tagged-scalar key round-trips. - Introduce fixtures for tagged outer mappings with tagged keys and a mixed YAML sample covering sequences/mappings/tagged keys/values, asserting parse and format_yaml round-trip.
1 parent 9fee429 commit 16de994

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

tests_py/test_mapping_keys.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from __future__ import annotations
22

3+
import textwrap
4+
35
import yaml12
46

57

@@ -82,6 +84,31 @@ def test_mapping_key_hashes_by_structure():
8284
assert hash(k1) == hash(k2)
8385

8486

87+
def test_mapping_key_with_tagged_value_hashes_and_compares():
88+
k1 = yaml12.MappingKey(yaml12.Tagged({"a": 1}, "!tag"))
89+
k2 = yaml12.MappingKey(yaml12.Tagged({"a": 1}, "!tag"))
90+
91+
assert k1 == k2
92+
assert hash(k1) == hash(k2)
93+
94+
mapping = {k1: "value"}
95+
assert mapping[k2] == "value"
96+
97+
98+
def test_mapping_key_tagged_round_trip_format_and_parse():
99+
key = yaml12.MappingKey(yaml12.Tagged("foo", "!k"))
100+
original = {key: "v"}
101+
102+
encoded = yaml12.format_yaml(original)
103+
reparsed = yaml12.parse_yaml(encoded)
104+
reparsed_key = next(iter(reparsed))
105+
106+
assert isinstance(reparsed_key, yaml12.Tagged)
107+
assert reparsed_key.tag == "!k"
108+
assert reparsed_key.value == "foo"
109+
assert reparsed == {yaml12.Tagged("foo", "!k"): "v"}
110+
111+
85112
def test_collection_values_stay_plain():
86113
parsed = yaml12.parse_yaml(
87114
"top:\n"
@@ -93,3 +120,61 @@ def test_collection_values_stay_plain():
93120
assert items[0] == [1, 2]
94121
assert isinstance(items[1], dict)
95122
assert not any(isinstance(k, yaml12.MappingKey) for k in items[1])
123+
124+
125+
def test_tagged_outer_mapping_with_tagged_keys_round_trip():
126+
yaml_text = "!outer\n!k1 foo: 1\n!k2 bar: 2\n"
127+
128+
parsed = yaml12.parse_yaml(yaml_text)
129+
assert isinstance(parsed, yaml12.Tagged)
130+
assert parsed.tag == "!outer"
131+
assert isinstance(parsed.value, dict)
132+
keys = list(parsed.value.keys())
133+
assert all(isinstance(k, yaml12.Tagged) for k in keys)
134+
assert {k.tag for k in keys} == {"!k1", "!k2"}
135+
assert parsed.value[keys[0]] in (1, 2)
136+
137+
encoded = yaml12.format_yaml(parsed)
138+
reparsed = yaml12.parse_yaml(encoded)
139+
assert reparsed == parsed
140+
141+
142+
def test_complex_tagged_and_untagged_mapping_keys_round_trip():
143+
yaml_text = textwrap.dedent(
144+
"""\
145+
? [a, b]
146+
: plain-seq
147+
? {foo: bar}
148+
: !val {x: 1}
149+
? !tagged-key scalar
150+
: [3, 4]
151+
? tagged_value_key
152+
: !tagged-seq [5, 6]
153+
"""
154+
)
155+
156+
parsed = yaml12.parse_yaml(yaml_text)
157+
assert isinstance(parsed, dict)
158+
assert len(parsed) == 4
159+
160+
keys = list(parsed.keys())
161+
assert any(isinstance(k, yaml12.MappingKey) for k in keys)
162+
163+
seq_key = yaml12.MappingKey(["a", "b"])
164+
map_key = yaml12.MappingKey({"foo": "bar"})
165+
tagged_scalar_key = yaml12.Tagged("scalar", "!tagged-key")
166+
plain_scalar_key = "tagged_value_key"
167+
168+
assert parsed[seq_key] == "plain-seq"
169+
assert isinstance(parsed[map_key], yaml12.Tagged)
170+
assert parsed[map_key].tag == "!val"
171+
assert parsed[map_key].value == {"x": 1}
172+
assert parsed[tagged_scalar_key] == [3, 4]
173+
tagged_value = parsed[plain_scalar_key]
174+
assert isinstance(tagged_value, yaml12.Tagged)
175+
assert tagged_value.tag == "!tagged-seq"
176+
assert tagged_value.value == [5, 6]
177+
178+
encoded = yaml12.format_yaml(parsed)
179+
reparsed = yaml12.parse_yaml(encoded)
180+
assert reparsed == parsed

0 commit comments

Comments
 (0)