-
Notifications
You must be signed in to change notification settings - Fork 1
/
tests.py
143 lines (96 loc) · 3.28 KB
/
tests.py
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
from uuid import uuid1
import pytest
from pyecs import Store
class ComponentA:
...
class ComponentB:
...
def test_entity_component_workflow():
store = Store()
ca = ComponentA()
e = store.add_entity(ca)
_e = store.get_entity(e.uuid)
assert e == _e
_e, *_ = store.get_entities()
assert e == _e
with pytest.raises(KeyError):
store.get_entity(uuid1())
assert e.get_component(ComponentA) == ca
assert e.get_components(ComponentA) == (ca,)
assert e.get_components() == (ca,)
assert e.get_component(ComponentA) == store.get_component(e.uuid, ComponentA)
assert e.get_components(ComponentA) == store.get_components(e.uuid, ComponentA)
assert e.get_components() == store.get_components(e.uuid)
with pytest.raises(KeyError):
e.get_component(ComponentB)
with pytest.raises(KeyError):
e.get_components(ComponentB)
cb = ComponentB()
e.add_components(cb)
assert e.get_components(ComponentA, ComponentB) == (ca, cb)
assert e.get_components(ComponentB, ComponentA) == (cb, ca)
assert e.get_components() in [(ca, cb), (cb, ca)]
assert e.get_components(ComponentA, ComponentB) == store.get_components(
e.uuid, ComponentA, ComponentB
)
assert e.get_components(ComponentB, ComponentB) == store.get_components(
e.uuid, ComponentB, ComponentB
)
assert e.get_components() == store.get_components(e.uuid)
_e1 = store.get_entities_with(ComponentA)[0]
_e2 = store.get_entities_with(ComponentB)[0]
_e3 = store.get_entities_with(ComponentA, ComponentB)[0]
assert e == _e1
assert _e1 == _e2
assert _e2 == _e3
e.remove_components(ComponentA)
with pytest.raises(KeyError):
e.get_component(ComponentA)
with pytest.raises(KeyError):
e.get_components(ComponentA)
assert e.get_component(ComponentB) == cb
assert e.get_components(ComponentB) == (cb,)
assert not store.get_entities_with(ComponentA)
with pytest.raises(KeyError):
store.add_entity(uuid=e.uuid)
e.remove()
assert not store.get_entities_with(ComponentB)
store.clear()
def test_entity_tree():
store = Store()
p = store.add_entity()
e = p.add_child()
assert e.get_parent() == p
assert p.get_children() == (e,)
e.remove()
assert p == store.get_entity(p.uuid)
e = store.add_entity(parent=p)
p.remove()
with pytest.raises(KeyError):
store.get_entity(e.uuid)
store.clear()
p = store.add_entity()
p.add_child()
e1 = p.add_child(ComponentA())
e2 = p.add_child(ComponentA(), ComponentB())
assert p.get_children_with(ComponentA) in [(e1, e2), (e2, e1)]
def test_delayed_removals():
store = Store()
ca = ComponentA()
e1 = store.add_entity(ca)
e2 = store.add_entity()
e1.remove_components(ComponentA, delay=True)
e2.remove(delay=True)
assert ca == e1.get_component(ComponentA)
assert e2 == store.get_entity(e2.uuid)
store.apply_removals()
with pytest.raises(KeyError):
e1.get_component(ComponentA)
with pytest.raises(KeyError):
store.get_entity(e2.uuid)
store.clear()
ca = ComponentA()
e1 = store.add_entity(ca)
e1.remove_components(ComponentA, delay=True)
e1.remove(delay=True)
store.apply_removals()