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

Class TestBoxListBehavior

test/test_edge_cases.py:847–899  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

845
846
847class TestBoxListBehavior:
848 def test_every_insertion_path_converts_items(self):
849 bl = BoxList()
850 bl.append({"a": 1})
851 bl.insert(0, [{"b": 1}])
852 bl.extend([{"c": 1}, [{"d": 1}]])
853 bl[1:1] = [{"e": 1}]
854 bl[-1] = {"f": [{"g": 1}]}
855 assert type(bl[0]) is BoxList and type(bl[0][0]) is Box
856 assert type(bl[1]) is Box and bl[1].e == 1
857 assert type(bl[2]) is Box and type(bl[3]) is Box
858 assert type(bl[-1].f) is BoxList and type(bl[-1].f[0]) is Box
859
860 def test_iadd_converts_items_like_extend(self):
861 bl = BoxList()
862 bl += [{"a": 1}, [{"b": 2}]]
863 assert type(bl[0]) is Box
864 assert type(bl[1]) is BoxList and type(bl[1][0]) is Box
865
866 def test_box_class_propagates_to_nested_items(self):
867 class MyBox(Box):
868 pass
869
870 bl = BoxList([{"a": {"b": [{"c": 1}]}}], box_class=MyBox)
871 assert type(bl[0]) is MyBox
872 assert type(bl[0].a) is MyBox
873 assert type(bl[0].a.b) is BoxList and type(bl[0].a.b[0]) is MyBox
874 bl.append({"z": {}})
875 assert type(bl[1]) is MyBox and type(bl[1].z) is MyBox
876
877 def test_box_options_flow_from_parent_box_to_list_items(self):
878 bx = Box({"l": [{"a": 1}]}, box_dots=True, default_box=True, camel_killer_box=True)
879 bx.l.append({"CamelKey": {"X": 1}})
880 assert bx.l.box_options["box_dots"] is True
881 assert bx["l[1].camel_key.x"] == 1
882 assert bx.l[0].missing == {}
883 assert "missing" not in bx.l[0]
884 bx["l[2].fresh"] = 1
885 assert bx.l[2] == {"fresh": 1}
886
887 def test_boxlist_copy_is_shallow_deepcopy_is_independent(self):
888 bl = BoxList([{"a": [1]}], box_dots=True)
889 shallow = copy.copy(bl)
890 deep = copy.deepcopy(bl)
891 assert shallow == bl and deep == bl
892 assert shallow is not bl and deep is not bl
893 assert shallow.box_options["box_dots"] and deep.box_options["box_dots"]
894 assert shallow.box_options["box_class"] is deep.box_options["box_class"] is Box
895 deep[0].a.append(2)
896 deep.append({"n": 1})
897 assert bl == [{"a": [1]}]
898 assert deep["[0].a[1]"] == 2
899 assert deep[1]._box_config["box_dots"] is True
900
901
902class TestSubclassing:

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected