MCPcopy Create free account
hub / github.com/cdgriffith/Box / TestRuamelScalarConversion

Class TestRuamelScalarConversion

test/test_box.py:2362–2396  ·  view source on GitHub ↗

from_yaml returns builtin numbers and bools without disturbing strings (#264, PR #321).

Source from the content-addressed store, hash-verified

2360
2361
2362class TestRuamelScalarConversion:
2363 """from_yaml returns builtin numbers and bools without disturbing strings (#264, PR #321)."""
2364
2365 def test_anchored_boolean_stays_bool(self):
2366 bx = Box.from_yaml("a: &x true\nb: *x\nc: false\n")
2367 assert bx.a is True and bx.b is True and bx.c is False
2368
2369 def test_anchored_boolean_round_trips_as_bool(self):
2370 assert Box.from_yaml("a: &x true\nb: *x\n").to_yaml() == "a: true\nb: true\n"
2371 assert Box.from_yaml("a: &x true\n").to_json() == '{"a": true}'
2372
2373 def test_multiline_string_dump_unchanged(self):
2374 bx = Box.from_yaml("lit: |\n line1\n line2\n")
2375 assert bx.to_yaml() == "lit: |\n line1\n line2\n"
2376
2377 def test_preserve_quotes_attr_still_effective(self):
2378 bx = Box.from_yaml("a: 'x'\n", ruamel_attrs={"preserve_quotes": True})
2379 assert bx.to_yaml() == "a: 'x'\n"
2380
2381 def test_numbers_become_builtin(self):
2382 bx = Box.from_yaml("f: 1.10\ni: 0x1F\nu: 1_000\no: 0o17\nb: 0b101\n")
2383 assert [type(v) for v in bx.values()] == [float, int, int, int, int]
2384
2385 def test_keys_are_builtin(self):
2386 bx = Box.from_yaml("1.5: a\n", conversion_box=False)
2387 assert [type(k) for k in bx.keys()] == [float]
2388
2389 def test_pyyaml_backend_unaffected(self, monkeypatch):
2390 import box.converters as cv
2391
2392 if not cv.pyyaml_available:
2393 pytest.skip("PyYAML is not installed")
2394 monkeypatch.setattr(cv, "ruamel_available", False)
2395 bx = Box.from_yaml("a: 1.10\nb: true\n")
2396 assert bx.to_dict() == {"a": 1.1, "b": True}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected