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

Class TestOperators

test/test_edge_cases.py:585–640  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

583
584
585class TestOperators:
586 def test_add_merges_deeply_and_right_side_wins(self):
587 left = Box({"a": {"x": 1, "y": 1}, "k": 1})
588 right = {"a": {"y": 2}, "z": 3}
589 assert left + right == {"a": {"x": 1, "y": 2}, "k": 1, "z": 3}
590 assert right + left == {"a": {"x": 1, "y": 1}, "k": 1, "z": 3}
591 assert list(left + right) == ["a", "k", "z"]
592 assert left == {"a": {"x": 1, "y": 1}, "k": 1}
593 assert right == {"a": {"y": 2}, "z": 3}
594
595 def test_sub_is_recursive_and_requires_a_mapping(self):
596 bx = Box({"a": {"x": 1, "y": 2}, "b": 1, "c": {"z": 1}})
597 assert bx - {"a": {"x": 1}, "b": 1, "c": {"z": 1}} == {"a": {"y": 2}}
598 assert bx - Box() == bx
599 assert bx - bx == {}
600 with pytest.raises(BoxError):
601 bx - [("a", 1)]
602 assert bx == {"a": {"x": 1, "y": 2}, "b": 1, "c": {"z": 1}}
603
604 def test_non_mappings_are_rejected_with_type_error(self):
605 bx = Box(a=1)
606 for other in ([("b", 1)], "text", 5, None, (("b", 1),)):
607 with pytest.raises(BoxTypeError):
608 bx + other
609 with pytest.raises(BoxTypeError):
610 other + bx
611 with pytest.raises(BoxTypeError):
612 bx | other
613 with pytest.raises(BoxTypeError):
614 other | bx
615 with pytest.raises(BoxTypeError):
616 bx.__iadd__(other)
617 with pytest.raises(BoxTypeError):
618 bx.__ior__(other)
619 assert bx == {"a": 1}
620
621 def test_inplace_operators_keep_identity_and_class(self):
622 bx = Box({"a": {"x": 1}})
623 ident = id(bx)
624 nested = bx.a
625 bx += {"a": {"y": 2}}
626 assert id(bx) == ident and bx.a is nested and nested == {"x": 1, "y": 2}
627 bx |= {"a": {"z": 3}}
628 assert id(bx) == ident and bx.a == {"z": 3}
629 assert type(bx.a) is Box
630
631 def test_results_keep_the_class_and_options_of_the_box_operand(self):
632 cfg = ConfigBox({"Key": {"Inner": 1}})
633 for result in (cfg + {"other": {"x": 1}}, cfg | {"other": {"x": 1}}, {"o": {"x": 1}} + cfg, {"o": 1} | cfg):
634 assert type(result) is ConfigBox
635 assert result.key.inner == 1
636 sbx = SBox({"a": 1}, default_box=True)
637 for result in (sbx + {"b": {"c": 1}}, sbx | {"b": {"c": 1}}):
638 assert type(result) is SBox and type(result.b) is SBox
639 assert result.missing == {}
640 assert "missing" not in result
641
642

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected