@@ -170,6 +170,11 @@ def test_str(self):
170170
171171
172172class TestCustomLoader (unittest .TestCase ):
173+ """Test the CustomLoader class"""
174+
175+ def load (self , s : str ):
176+ return yaml .load (s , Loader = CustomLoader )
177+
173178 def test_construct_mapping (self ):
174179 """test the CustomLoader construct_mapping method"""
175180 envfile = os .path .join (envpath , "project.env" )
@@ -178,8 +183,56 @@ def test_construct_mapping(self):
178183 mapping = loader .construct_mapping (node )
179184 self .assertIsInstance (mapping , dict )
180185
186+ def test_keys_stay_strings_booleanish (self ):
187+ """test that keys that look like booleans are not converted to bools
188+ by the resolver"""
189+ data = self .load ("YES: 1\n NO: 2\n ON: 3\n OFF: 4\n true: 5\n False: 6\n " )
190+ assert "YES" in data and data ["YES" ] == 1
191+ assert "NO" in data and data ["NO" ] == 2
192+ assert "ON" in data and data ["ON" ] == 3
193+ assert "OFF" in data and data ["OFF" ] == 4
194+ assert "true" in data and data ["true" ] == 5
195+ assert "False" in data and data ["False" ] == 6
196+ # ensure the resolver didn't produce bool keys
197+ assert True not in data
198+ assert False not in data
199+
200+ def test_keys_stay_strings_nullish (self ):
201+ """test that keys that look like nulls are not converted to None by
202+ the resolver"""
203+ data = self .load ("NULL: 1\n Null: 2\n null: 3\n ~: 4\n " )
204+ assert data ["NULL" ] == 1
205+ assert data ["Null" ] == 2
206+ assert data ["null" ] == 3
207+ assert "~" in data and data ["~" ] == 4
208+ assert None not in data
209+
210+ def test_keys_stay_strings_timestampish (self ):
211+ """test that keys that look like timestamps are not converted to
212+ datetime objects"""
213+ # YAML 1.1 can infer timestamps
214+ data = self .load ("2024-01-02: 1\n 2024-01-02T03:04:05Z: 2\n " )
215+ assert data ["2024-01-02" ] == 1
216+ assert data ["2024-01-02T03:04:05Z" ] == 2
217+
218+ def test_yaml_merge_key_works (self ):
219+ """test that the YAML merge key (<<) works correctly with anchors and
220+ aliases"""
221+ s = """
222+ all: &all
223+ A: 1
224+ dev:
225+ <<: *all
226+ B: 2
227+ """
228+ data = self .load (s )
229+ assert data ["dev" ]["A" ] == 1
230+ assert data ["dev" ]["B" ] == 2
231+
181232
182233class TestCustomDumper (unittest .TestCase ):
234+ """Test the CustomDumper class"""
235+
183236 def test_init (self ):
184237 """test the CustomDumper __init__ method"""
185238 dumper = CustomDumper (None )
0 commit comments