| 334 | |
| 335 | |
| 336 | class TestEdgeCases: |
| 337 | def test_missing_keys_and_paths(self): |
| 338 | with pytest.raises(BoxKeyError): |
| 339 | Box(camel_killer_box=True).set_comment("missingKey", "x") |
| 340 | dotted = Box({"a": 1, "l": [1]}, box_dots=True) |
| 341 | for key in ("nope.x", "a.x", "l[0].x", "l[5]", "l[x]", "l.x"): |
| 342 | with pytest.raises(BoxKeyError): |
| 343 | dotted.set_comment(key, "x") |
| 344 | with pytest.raises(BoxKeyError): |
| 345 | BoxList([1]).set_comment(1.5, "x") |
| 346 | |
| 347 | def test_delete_without_comments(self): |
| 348 | bx = Box(a=1) |
| 349 | bx.del_comment("a") |
| 350 | bl = BoxList([1]) |
| 351 | bl.del_comment(0) |
| 352 | bl.set_comment(0, "x") |
| 353 | bl.del_comment(0) |
| 354 | assert bl.get_comment(0) is None |
| 355 | |
| 356 | def test_box_list_document_and_positions(self): |
| 357 | bl = BoxList([1, 2]) |
| 358 | bl.set_comment(None, "top") |
| 359 | bl.set_comment(0, "above", position="before") |
| 360 | bl.set_comment(0, None, position="before") |
| 361 | assert bl.get_comment(0) is None |
| 362 | assert copy.copy(bl).get_comment(None) == {"start": "top"} |
| 363 | bl.del_comment() |
| 364 | assert bl.get_comment(None) is None |
| 365 | |
| 366 | def test_box_list_without_comments_keeps_working(self): |
| 367 | bl = BoxList([3, 1, 2]) |
| 368 | bl.sort() |
| 369 | bl.reverse() |
| 370 | del bl[0:1] |
| 371 | assert bl == [2, 1] |
| 372 | |
| 373 | def test_dotted_list_deletion_keeps_surviving_comments(self): |
| 374 | values = BoxList.from_yaml("- first # first comment\n- second # second comment\n", box_dots=True) |
| 375 | del values["[0]"] |
| 376 | assert values.to_yaml() == "- second # second comment\n" |
| 377 | |
| 378 | def test_sort_with_comments_evaluates_keys_once(self): |
| 379 | values = BoxList.from_yaml("- z # Z\n- a # A\n") |
| 380 | ranks = {"z": 2, "a": 1} |
| 381 | assert values.sort(key=ranks.pop) is None |
| 382 | assert ranks == {} |
| 383 | assert values.to_yaml() == "- a # A\n- z # Z\n" |
| 384 | |
| 385 | def test_slice_delete_drops_removed_comments_and_moves_the_rest(self): |
| 386 | bl = BoxList([1, 2, 3]) |
| 387 | bl.set_comment(0, "gone") |
| 388 | bl.set_comment(2, "x") |
| 389 | del bl[0:1] |
| 390 | assert bl.get_comment(0) is None |
| 391 | assert bl.get_comment(1) == {"eol": "x"} |
| 392 | |
| 393 | def test_empty_comment_and_missing_final_newline(self): |
nothing calls this directly
no outgoing calls
no test coverage detected