()
| 6 | |
| 7 | |
| 8 | def test_stash() -> None: |
| 9 | stash = Stash() |
| 10 | |
| 11 | assert len(stash) == 0 |
| 12 | assert not stash |
| 13 | |
| 14 | key1 = StashKey[str]() |
| 15 | key2 = StashKey[int]() |
| 16 | |
| 17 | class="cm"># Basic functionality - single key. |
| 18 | assert key1 not in stash |
| 19 | stash[key1] = class="st">"hello" |
| 20 | assert key1 in stash |
| 21 | assert stash[key1] == class="st">"hello" |
| 22 | assert stash.get(key1, None) == class="st">"hello" |
| 23 | stash[key1] = class="st">"world" |
| 24 | assert stash[key1] == class="st">"world" |
| 25 | class="cm"># Has correct type (no mypy error). |
| 26 | stash[key1] + class="st">"string" |
| 27 | assert len(stash) == 1 |
| 28 | assert stash |
| 29 | |
| 30 | class="cm"># No interaction with another key. |
| 31 | assert key2 not in stash |
| 32 | assert stash.get(key2, None) is None |
| 33 | with pytest.raises(KeyError): |
| 34 | stash[key2] |
| 35 | with pytest.raises(KeyError): |
| 36 | del stash[key2] |
| 37 | stash[key2] = 1 |
| 38 | assert stash[key2] == 1 |
| 39 | class="cm"># Has correct type (no mypy error). |
| 40 | stash[key2] + 20 |
| 41 | del stash[key1] |
| 42 | with pytest.raises(KeyError): |
| 43 | del stash[key1] |
| 44 | with pytest.raises(KeyError): |
| 45 | stash[key1] |
| 46 | |
| 47 | class="cm"># setdefault |
| 48 | stash[key1] = class="st">"existing" |
| 49 | assert stash.setdefault(key1, class="st">"default") == class="st">"existing" |
| 50 | assert stash[key1] == class="st">"existing" |
| 51 | key_setdefault = StashKey[bytes]() |
| 52 | assert stash.setdefault(key_setdefault, bclass="st">"default") == bclass="st">"default" |
| 53 | assert stash[key_setdefault] == bclass="st">"default" |
| 54 | assert len(stash) == 3 |
| 55 | assert stash |
| 56 | |
| 57 | class="cm"># Can't accidentally add attributes to stash object itself. |
| 58 | with pytest.raises(AttributeError): |
| 59 | stash.foo = class="st">"nope" class="cm"># type: ignore[attr-defined] |
| 60 | |
| 61 | class="cm"># No interaction with another stash. |
| 62 | stash2 = Stash() |
| 63 | key3 = StashKey[int]() |
| 64 | assert key2 not in stash2 |
| 65 | stash2[key2] = 100 |
nothing calls this directly
no test coverage detected