-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_lazy.py
More file actions
50 lines (37 loc) · 1.33 KB
/
Copy pathtest_lazy.py
File metadata and controls
50 lines (37 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
"""Behavioral tests for the pattern's Lazy global."""
from __future__ import annotations
from patterns.python.global_object import Lazy
class TestLazy:
def test_construction_does_not_run_the_factory(self) -> None:
runs: list[int] = []
def factory() -> str:
runs.append(1)
return "value"
lazy = Lazy(factory)
assert runs == []
assert lazy.initialized is False
def test_first_get_builds_exactly_once(self) -> None:
runs: list[int] = []
def factory() -> str:
runs.append(1)
return "value"
lazy = Lazy(factory)
assert lazy.get() == "value"
assert lazy.get() == "value"
assert runs == [1]
assert lazy.initialized is True
def test_reset_forces_a_rebuild(self) -> None:
counter = iter(range(100))
lazy = Lazy(lambda: next(counter))
assert lazy.get() == 0
lazy.reset()
assert lazy.initialized is False
assert lazy.get() == 1
def test_none_is_a_legitimate_lazy_value(self) -> None:
runs: list[int] = []
def factory() -> None:
runs.append(1)
lazy: Lazy[None] = Lazy(factory)
assert lazy.get() is None
assert lazy.get() is None
assert runs == [1] # a stored None is not mistaken for "unbuilt"