Coverage for tests/test_prototypers.py: 100%
32 statements
« prev ^ index » next coverage.py v7.14.1, created at 2026-10-30 22:54 +0000
« prev ^ index » next coverage.py v7.14.1, created at 2026-10-30 22:54 +0000
1"""Tests the `wonka` prototyper factories."""
3from __future__ import annotations
5import dataclasses
7from conftest import Thing
9import wonka
12@dataclasses.dataclass
13class Clone(wonka.Scribe):
14 """`Scribe` with default contents.
16 Args:
17 contents: stored contents.
19 """
21 contents: dict[str, str] = dataclasses.field(
22 default_factory = lambda: {'tree': 'house', 'ghost': 'town'})
25def test_scribe() -> None:
26 """Tests that a `Scribe` clones itself without an item."""
27 clone_class = Clone.create()
28 clone_instance = clone_class()
29 assert clone_instance.contents['tree'] == 'house'
30 assert isinstance(clone_instance, Clone)
31 new_clone_instance = Clone.create(parameters = {})
32 assert new_clone_instance.contents['ghost'] == 'town'
33 assert isinstance(new_clone_instance, Clone)
36def test_scribe_none_clones_self() -> None:
37 """Tests that a deep copy of a class is the class itself."""
38 assert Clone.create() is Clone
41def test_scribe_clones_falsy_items() -> None:
42 """Tests that falsy items are cloned instead of the `Scribe`."""
43 assert wonka.Scribe.create(0) == 0
44 assert wonka.Scribe.create('') == ''
45 assert wonka.Scribe.create([]) == []
48def test_scribe_deep_copies() -> None:
49 """Tests that a `Scribe` makes a deep copy."""
50 original = [[1], [2]]
51 copied = wonka.Scribe.create(original)
52 assert copied == original
53 assert copied is not original
54 assert copied[0] is not original[0]
57def test_scribe_injects_parameters_into_copy() -> None:
58 """Tests that `parameters` change the copy but not the original."""
59 original = Thing(value = 1)
60 copied = wonka.Scribe.create(original, parameters = {'value': 2})
61 assert copied.value == 2
62 assert original.value == 1
65if __name__ == '__main__':
66 test_scribe()