| 425 | |
| 426 | |
| 427 | class TestFrozen: |
| 428 | def test_hash_equality_and_unhashable_mutable(self): |
| 429 | one = Box({"a": [1, {"b": 2}], "c": "x"}, frozen_box=True) |
| 430 | two = Box({"a": [1, {"b": 2}], "c": "x"}, frozen_box=True) |
| 431 | assert one == two and hash(one) == hash(two) |
| 432 | assert {one: 1}[two] == 1 |
| 433 | assert len({one, two}) == 1 |
| 434 | with pytest.raises(BoxTypeError): |
| 435 | hash(Box(a=1)) |
| 436 | with pytest.raises(TypeError): |
| 437 | hash(BoxList([1])) |
| 438 | assert hash(BoxList([1, 2], frozen_box=True)) == hash(BoxList([1, 2], frozen_box=True)) |
| 439 | |
| 440 | def test_every_mutator_is_refused_and_leaves_data_intact(self): |
| 441 | bx = Box({"a": 1, "n": {"m": 1}}, frozen_box=True) |
| 442 | attempts = [ |
| 443 | lambda: bx.__setitem__("b", 1), |
| 444 | lambda: setattr(bx, "b", 1), |
| 445 | lambda: delattr(bx, "a"), |
| 446 | lambda: bx.__delitem__("a"), |
| 447 | lambda: bx.pop("a"), |
| 448 | lambda: bx.popitem(), |
| 449 | lambda: bx.clear(), |
| 450 | lambda: bx.update(b=1), |
| 451 | lambda: bx.merge_update({"b": 1}), |
| 452 | lambda: bx.merge_update({"n": {"m": 2}}), |
| 453 | lambda: bx.setdefault("zz", 1), |
| 454 | lambda: bx.n.__setitem__("m", 2), |
| 455 | lambda: bx.n.update({"m": 2}), |
| 456 | ] |
| 457 | for attempt in attempts: |
| 458 | with pytest.raises(BoxError): |
| 459 | attempt() |
| 460 | with pytest.raises(BoxError): |
| 461 | bx |= {"b": 1} |
| 462 | with pytest.raises(BoxError): |
| 463 | bx += {"b": 1} |
| 464 | assert bx == {"a": 1, "n": {"m": 1}} |
| 465 | |
| 466 | def test_frozen_boxlist_refuses_every_mutator(self): |
| 467 | bl = BoxList([3, 1, {"a": 1}], frozen_box=True) |
| 468 | attempts = [ |
| 469 | lambda: bl.append(1), |
| 470 | lambda: bl.extend([1]), |
| 471 | lambda: bl.insert(0, 1), |
| 472 | lambda: bl.pop(), |
| 473 | lambda: bl.remove(3), |
| 474 | lambda: bl.reverse(), |
| 475 | lambda: bl.sort(key=str), |
| 476 | lambda: bl.clear(), |
| 477 | lambda: bl.__setitem__(0, 1), |
| 478 | lambda: bl.__setitem__(slice(0, 1), [1]), |
| 479 | lambda: bl.__delitem__(0), |
| 480 | lambda: bl.__iadd__([1]), |
| 481 | lambda: bl.__imul__(2), |
| 482 | ] |
| 483 | for attempt in attempts: |
| 484 | with pytest.raises(BoxError): |
nothing calls this directly
no outgoing calls
no test coverage detected