-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_shared.py
More file actions
40 lines (30 loc) · 1018 Bytes
/
Copy pathtest_shared.py
File metadata and controls
40 lines (30 loc) · 1018 Bytes
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
"""Behavioral tests for the shared-instance accessor."""
from patterns.creational.singleton.pattern import Shared
class Counter:
built = 0
def __init__(self) -> None:
type(self).built += 1
class TestShared:
def setup_method(self) -> None:
Counter.built = 0
def test_same_instance_every_get(self) -> None:
shared = Shared(Counter)
assert shared.get() is shared.get()
def test_factory_runs_once(self) -> None:
shared = Shared(Counter)
shared.get()
shared.get()
assert Counter.built == 1
def test_build_is_lazy(self) -> None:
shared = Shared(Counter)
assert not shared.built
assert Counter.built == 0
shared.get()
assert shared.built
def test_reset_builds_fresh_next_time(self) -> None:
shared = Shared(Counter)
first = shared.get()
shared.reset()
assert not shared.built
assert shared.get() is not first
assert Counter.built == 2