| 207 | |
| 208 | |
| 209 | class TestKeepingCommentsInSync: |
| 210 | def test_delete_pop_popitem_clear(self): |
| 211 | bx = Box(a=1, b=2, c=3, d=4) |
| 212 | for key in "abcd": |
| 213 | bx.set_comment(key, key) |
| 214 | del bx["a"] |
| 215 | bx.pop("b") |
| 216 | bx.popitem() |
| 217 | assert bx.get_comment("c") == {"eol": "c"} |
| 218 | bx["a"] = 1 |
| 219 | assert bx.get_comment("a") is None # a re-added key starts clean |
| 220 | bx.clear() |
| 221 | bx.c = 1 |
| 222 | assert bx.get_comment("c") is None |
| 223 | |
| 224 | def test_overwriting_a_value_keeps_the_comment(self): |
| 225 | bx = Box(a=1, b={"x": 1}) |
| 226 | bx.set_comment("a", "kept") |
| 227 | bx.set_comment("b", "kept too") |
| 228 | bx.a = 2 |
| 229 | bx.b = {"y": 2} |
| 230 | assert bx.get_comment("a") == {"eol": "kept"} |
| 231 | assert bx.get_comment("b") == {"eol": "kept too"} |
| 232 | |
| 233 | def test_merge_update_and_update_keep_existing(self): |
| 234 | bx = Box.from_yaml("a:\n b: 1 # keep\n") |
| 235 | bx.merge_update({"a": {"c": 2}}) |
| 236 | bx.update(z=1) |
| 237 | assert bx.a.get_comment("b") == {"eol": "keep"} |
| 238 | |
| 239 | def test_copy_deepcopy_pickle_and_rebuild(self): |
| 240 | bx = Box.from_yaml(DOCUMENT) |
| 241 | for other in (bx.copy(), copy.copy(bx), copy.deepcopy(bx), pickle.loads(pickle.dumps(bx)), Box(bx)): |
| 242 | assert other.to_yaml() == DOCUMENT |
| 243 | other = copy.deepcopy(bx) |
| 244 | other.server.set_comment("host", "changed") |
| 245 | assert bx.server.get_comment("host") == {"eol": "bind address"} |
| 246 | |
| 247 | def test_moving_a_subtree(self): |
| 248 | bx = Box.from_yaml(DOCUMENT) |
| 249 | target = Box() |
| 250 | target.settings = bx.server |
| 251 | assert target.settings.get_comment("host") == {"eol": "bind address"} |
| 252 | |
| 253 | def test_box_list_index_changes(self): |
| 254 | bl = BoxList(["a", "b", "c", "d"]) |
| 255 | for index in range(4): |
| 256 | bl.set_comment(index, "abcd"[index]) |
| 257 | |
| 258 | def comments(): |
| 259 | return [(bl.get_comment(i) or {}).get("eol") for i in range(len(bl))] |
| 260 | |
| 261 | bl.insert(1, "new") |
| 262 | assert comments() == ["a", None, "b", "c", "d"] |
| 263 | bl.pop(1) |
| 264 | assert comments() == ["a", "b", "c", "d"] |
| 265 | del bl[0] |
| 266 | assert comments() == ["b", "c", "d"] |
nothing calls this directly
no outgoing calls
no test coverage detected