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

Class TestBoxList

test/test_box_list.py:22–583  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

20
21
22class TestBoxList:
23 @pytest.fixture(autouse=True)
24 def temp_dir_cleanup(self):
25 shutil.rmtree(str(tmp_dir), ignore_errors=True)
26 try:
27 os.mkdir(str(tmp_dir))
28 except OSError:
29 pass
30 yield
31 shutil.rmtree(str(tmp_dir), ignore_errors=True)
32
33 def test_box_list(self):
34 new_list = BoxList({"item": x} for x in range(0, 10))
35 new_list.extend([{"item": 22}])
36 assert new_list[-1].item == 22
37 new_list.append([{"bad_item": 33}])
38 assert new_list[-1][0].bad_item == 33
39 new_list[-1].append([{"bad_item": 33}])
40 assert new_list[-1, -1, 0].bad_item == 33
41 bx = Box({0: {1: {2: {3: 3}}}, (0, 1, 2, 3): 4})
42 assert bx[0, 1, 2, 3] == 4
43 assert repr(new_list).startswith("BoxList(")
44 for x in new_list.to_list():
45 assert not isinstance(x, (BoxList, Box))
46 new_list.insert(0, {"test": 5})
47 new_list.insert(1, ["a", "b"])
48 new_list.append("x")
49 assert new_list[0].test == 5
50 assert isinstance(str(new_list), str)
51 assert isinstance(new_list[1], BoxList)
52 assert not isinstance(new_list.to_list(), BoxList)
53
54 @pytest.mark.parametrize("falsey", [False, True])
55 def test_box_list_does_not_require_iterable_truth_value(self, falsey):
56 # 2026-10-10: Iterable inputs can be falsey or have no boolean value.
57 class Rows:
58 def __iter__(self):
59 return iter([{"value": 1}])
60
61 def __bool__(self):
62 if falsey:
63 return False
64 raise ValueError("ambiguous truth value")
65
66 result = BoxList(Rows())
67 assert len(result) == 1
68 assert result[0].value == 1
69
70 @pytest.mark.parametrize("values", [[], [1], [1, 2], [{"SomeKey": 3}, [4]]])
71 def test_extend_self(self, values, monkeypatch):
72 data = BoxList(values, camel_killer_box=True)
73 expected = data.to_list() * 2
74 append = data.append
75 remaining = len(data)
76
77 def bounded_append(item):
78 nonlocal remaining
79 assert remaining > 0, "extend revisited an appended element"

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected