-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_world.py
582 lines (426 loc) · 17.9 KB
/
test_world.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
# import pytest
import esper
from __main__ import __dict__, __name__
def check_raises(exception, function):
try:
function()
except exception:
return True
return False
# ECS test
# @pytest.fixture(autouse=True)
def _reset_to_zero():
# Wipe out all world contexts
# and re-create the default.
esper._context_map.clear()
esper.switch_world("default")
def test_create_entity():
entity1 = esper.create_entity()
entity2 = esper.create_entity()
assert isinstance(entity1, int)
assert isinstance(entity2, int)
assert entity1 < entity2
def test_create_entity_with_components():
entity1 = esper.create_entity(ComponentA())
entity2 = esper.create_entity(ComponentB(), ComponentC())
assert esper.has_component(entity1, ComponentA) is True
assert esper.has_component(entity1, ComponentB) is False
assert esper.has_component(entity1, ComponentC) is False
assert esper.has_component(entity2, ComponentA) is False
assert esper.has_component(entity2, ComponentB) is True
assert esper.has_component(entity2, ComponentC) is True
def test_adding_component_to_not_existing_entity_raises_error():
# with pytest.raises(KeyError):
assert check_raises(KeyError, lambda: esper.add_component(123, ComponentA()))
def test_create_entity_and_add_components():
entity1 = esper.create_entity()
esper.add_component(entity1, ComponentA())
esper.add_component(entity1, ComponentB())
assert esper.has_component(entity1, ComponentA) is True
assert esper.has_component(entity1, ComponentC) is False
def test_create_entity_and_add_components_with_alias():
entity = esper.create_entity()
esper.add_component(entity, ComponentA(), type_alias=ComponentF)
assert esper.has_component(entity, ComponentF) is True
assert esper.component_for_entity(entity, ComponentF).a == -66 # type: ignore[attr-defined]
def test_delete_entity():
esper.create_entity(ComponentA())
entity_b = esper.create_entity(ComponentB())
entity_c = esper.create_entity(ComponentC())
empty_entity = esper.create_entity()
assert entity_c == 3
esper.delete_entity(entity_b, immediate=True)
error_raised=False
assert check_raises(KeyError, lambda: esper.components_for_entity(entity_b))
assert check_raises(KeyError, lambda: esper.delete_entity(999, immediate=True))
esper.delete_entity(empty_entity, immediate=True)
def test_component_for_entity():
entity = esper.create_entity(ComponentC())
assert isinstance(esper.component_for_entity(entity, ComponentC), ComponentC)
assert check_raises(KeyError, lambda: esper.component_for_entity(entity, ComponentD))
def test_components_for_entity():
entity = esper.create_entity(ComponentA(), ComponentD(), ComponentE())
all_components: tuple[..., ...] = esper.components_for_entity(entity)
assert isinstance(all_components, tuple)
assert len(all_components) == 3
# with pytest.raises(KeyError):
assert check_raises(KeyError, lambda: esper.components_for_entity(999))
def test_has_component():
entity1 = esper.create_entity(ComponentA())
entity2 = esper.create_entity(ComponentB())
assert esper.has_component(entity1, ComponentA) is True
assert esper.has_component(entity1, ComponentB) is False
assert esper.has_component(entity2, ComponentA) is False
assert esper.has_component(entity2, ComponentB) is True
def test_has_components():
entity = esper.create_entity(ComponentA(), ComponentB(), ComponentC())
assert esper.has_components(entity, ComponentA, ComponentB) is True
assert esper.has_components(entity, ComponentB, ComponentC) is True
assert esper.has_components(entity, ComponentA, ComponentC) is True
assert esper.has_components(entity, ComponentA, ComponentD) is False
assert esper.has_components(entity, ComponentD) is False
def test_get_component():
create_entities(2000)
assert isinstance(esper.get_component(ComponentA), list)
# Confirm that the actually contains something:
assert len(esper.get_component(ComponentA)) > 0, "No Components Returned"
for ent, comp in esper.get_component(ComponentA):
assert isinstance(ent, int)
assert isinstance(comp, ComponentA)
def test_get_two_components():
create_entities(2000)
assert isinstance(esper.get_components(ComponentD, ComponentE), list)
# Confirm that the actually contains something:
assert len(esper.get_components(ComponentD, ComponentE)) > 0, "No Components Returned"
for ent, comps in esper.get_components(ComponentD, ComponentE):
assert isinstance(ent, int)
assert isinstance(comps, list)
assert len(comps) == 2
for ent, de in esper.get_components(ComponentD, ComponentE):
(d, e) = de
assert isinstance(ent, int)
assert isinstance(d, ComponentD)
assert isinstance(e, ComponentE)
def test_get_three_components():
create_entities(2000)
assert isinstance(esper.get_components(ComponentC, ComponentD, ComponentE), list)
for ent, comps in esper.get_components(ComponentC, ComponentD, ComponentE):
assert isinstance(ent, int)
assert isinstance(comps, list)
assert len(comps) == 3
for ent, cde in esper.get_components(ComponentC, ComponentD, ComponentE):
(c, d, e) = cde
assert isinstance(ent, int)
assert isinstance(c, ComponentC)
assert isinstance(d, ComponentD)
assert isinstance(e, ComponentE)
def test_try_component():
entity1 = esper.create_entity(ComponentA(), ComponentB())
one_item = esper.try_component(entity1, ComponentA)
assert isinstance(one_item, ComponentA)
zero_item = esper.try_component(entity1, ComponentC)
assert zero_item is None
def test_try_components():
entity1 = esper.create_entity(ComponentA(), ComponentB())
one_item = esper.try_components(entity1, ComponentA, ComponentB)
assert isinstance(one_item, list)
assert len(one_item) == 2
assert isinstance(one_item[0], ComponentA)
assert isinstance(one_item[1], ComponentB)
zero_item = esper.try_components(entity1, ComponentA, ComponentC)
assert zero_item is None
def test_clear_database():
create_entities(2000)
assert len(esper.get_component(ComponentA)) == 1000
esper.clear_database()
assert len(esper.get_component(ComponentA)) == 0
def test_clear_cache():
create_entities(2000)
assert len(esper.get_component(ComponentA)) == 1000
esper.clear_cache()
assert len(esper.get_component(ComponentA)) == 1000
def test_cache_results():
_______ = esper.create_entity(ComponentA(), ComponentB(), ComponentC())
entity2 = esper.create_entity(ComponentB(), ComponentC(), ComponentD())
assert len(esper.get_components(ComponentB, ComponentC)) == 2
esper.delete_entity(entity2, immediate=True)
assert len(esper.get_components(ComponentB, ComponentC)) == 1
class TestEntityExists:
def test_dead_entity(self):
dead_entity = esper.create_entity(ComponentB())
esper.delete_entity(dead_entity)
assert not esper.entity_exists(dead_entity)
def test_not_created_entity(self):
assert not esper.entity_exists(123)
def test_empty_entity(self):
empty_entity = esper.create_entity()
assert esper.entity_exists(empty_entity)
def test_entity_with_component(self):
entity_with_component = esper.create_entity(ComponentA())
assert esper.entity_exists(entity_with_component)
class TestRemoveComponent:
def test_remove_from_not_existing_entity_raises_key_error(self):
assert check_raises(KeyError, lambda: esper.remove_component(123, ComponentA))
def test_remove_not_existing_component_raises_key_error(self):
entity = esper.create_entity(ComponentB())
assert check_raises(KeyError, lambda: esper.remove_component(entity, ComponentA))
def test_remove_component_with_object_raises_key_error(self):
create_entities(2000)
entity = 2
component = ComponentD()
assert esper.has_component(entity, type(component))
assert check_raises(KeyError, lambda: esper.remove_component(entity, component))
def test_remove_component_returns_removed_instance(self):
component = ComponentA()
entity = esper.create_entity(component)
result = esper.remove_component(entity, type(component))
assert result is component
def test_remove_last_component_leaves_empty_entity(self):
entity = esper.create_entity()
esper.add_component(entity, ComponentA())
esper.remove_component(entity, ComponentA)
assert not esper.has_component(entity, ComponentA)
assert esper.entity_exists(entity)
def test_removing_one_component_leaves_other_intact(self):
component_a = ComponentA()
component_b = ComponentB()
component_c = ComponentC()
entity = esper.create_entity(component_a, component_b, component_c)
esper.remove_component(entity, ComponentB)
assert esper.component_for_entity(entity, ComponentA) is component_a
assert not esper.has_component(entity, ComponentB)
assert esper.component_for_entity(entity, ComponentC) is component_c
def test_clear_dead_entities():
component = ComponentA()
entity1 = esper.create_entity(component)
entity2 = esper.create_entity()
assert esper.entity_exists(entity1)
assert esper.entity_exists(entity2)
assert esper.has_component(entity1, ComponentA)
esper.delete_entity(entity1, immediate=False)
assert not esper.entity_exists(entity1)
assert esper.entity_exists(entity2)
assert esper.has_component(entity1, ComponentA)
esper.clear_dead_entities()
assert not esper.entity_exists(entity1)
assert esper.entity_exists(entity2)
assert check_raises(KeyError, lambda: esper.has_component(entity1, ComponentA))
def test_switch_world():
# The `create_entities` helper will add <number>/2 of
# 'ComponentA' to the World context. Make a new
# "left" context, and confirm this is True:
esper.switch_world("left")
assert len(esper.get_component(ComponentA)) == 0
create_entities(200)
assert len(esper.get_component(ComponentA)) == 100
# Switching to a new "right" World context, no
# 'ComponentA' Components should yet exist.
esper.switch_world("right")
assert len(esper.get_component(ComponentA)) == 0
create_entities(300)
assert len(esper.get_component(ComponentA)) == 150
# Switching back to the original "left" context,
# the original 100 Components should still exist.
# From there, 200 more should be added:
esper.switch_world("left")
assert len(esper.get_component(ComponentA)) == 100
create_entities(400)
assert len(esper.get_component(ComponentA)) == 300
##################################################
# Some helper functions and Component templates:
##################################################
def create_entities(number):
"""This function will create X number of entities.
The entities are created with a mix of Components,
so the World context will see an addition of
ComponentA * number * 1
ComponentB * number * 1
ComponentC * number * 2
ComponentD * number * 1
ComponentE * number * 1
"""
for _ in range(number // 2):
esper.create_entity(ComponentA(), ComponentB(), ComponentC())
esper.create_entity(ComponentC(), ComponentD(), ComponentE())
class ComponentA:
def __init__(self):
self.a = -66
self.b = 9999.99
class ComponentB:
def __init__(self):
self.attrib_a = True
self.attrib_b = False
self.attrib_c = False
self.attrib_d = True
class ComponentC:
def __init__(self):
self.x = 0
self.y = 0
self.z = None
class ComponentD:
def __init__(self):
self.direction = "left"
self.previous = "right"
class ComponentE:
def __init__(self):
self.items = {"itema": None, "itemb": 1000}
self.points = [a + 2 for a in list(range(44))]
class ComponentF:
pass
# Processor test
def test_add_processor():
create_entities(2000)
assert len(esper._processors) == 0
correct_processor_a = CorrectProcessorA()
assert isinstance(correct_processor_a, esper.Processor)
esper.add_processor(correct_processor_a)
assert len(esper._processors) == 1
assert isinstance(esper._processors[0], esper.Processor)
def test_remove_processor():
create_entities(2000)
assert len(esper._processors) == 0
correct_processor_a = CorrectProcessorA()
esper.add_processor(correct_processor_a)
assert len(esper._processors) == 1
esper.remove_processor(CorrectProcessorB)
assert len(esper._processors) == 1
esper.remove_processor(CorrectProcessorA)
assert len(esper._processors) == 0
def test_get_processor():
processor_a = CorrectProcessorA()
processor_b = CorrectProcessorB()
processor_c = CorrectProcessorC()
esper.add_processor(processor_a)
esper.add_processor(processor_b)
esper.add_processor(processor_c)
retrieved_proc_c = esper.get_processor(CorrectProcessorC)
retrieved_proc_b = esper.get_processor(CorrectProcessorB)
retrieved_proc_a = esper.get_processor(CorrectProcessorA)
assert type(retrieved_proc_a) == CorrectProcessorA
assert type(retrieved_proc_b) == CorrectProcessorB
assert type(retrieved_proc_c) == CorrectProcessorC
def test_processor_args():
esper.add_processor(ArgsProcessor())
assert check_raises(TypeError, lambda: esper.process()) ## missing arg
esper.process("arg")
def test_processor_kwargs():
esper.add_processor(KwargsProcessor())
assert check_raises(TypeError, lambda: esper.process()) # Missing argument
esper.process("spam", "eggs", "beans", "toast")
esper.process("spam", "eggs", "beans", toast="toast")
esper.process("spam", "eggs", beans="beans", toast="toast")
esper.process("spam", "eggs", toast="toast", beans="beans")
# Event dispatch test
def test_event_dispatch_no_handlers():
esper.dispatch_event("foo")
esper.dispatch_event("foo", 1)
esper.dispatch_event("foo", 1, 2)
esper.event_registry.clear()
def test_event_dispatch_one_arg():
esper.set_handler("foo", myhandler_onearg)
esper.dispatch_event("foo", 1)
esper.event_registry.clear()
def test_event_dispatch_two_args():
esper.set_handler("foo", myhandler_twoargs)
esper.dispatch_event("foo", 1, 2)
esper.event_registry.clear()
def test_event_dispatch_incorrect_args():
esper.set_handler("foo", myhandler_noargs)
assert check_raises(TypeError, lambda: esper.dispatch_event("foo", "arg1", "arg2"))
esper.event_registry.clear()
def test_set_methoad_as_handler_in_init():
class MyClass(esper.Processor):
def __init__(self):
esper.set_handler("foo", self._my_handler)
@staticmethod
def _my_handler():
print("OK")
def process(self, dt):
pass
_myclass = MyClass()
esper.dispatch_event("foo")
esper.event_registry.clear()
def test_set_instance_methoad_as_handler():
class MyClass(esper.Processor):
@staticmethod
def my_handler():
print("OK")
def process(self, dt):
pass
myclass = MyClass()
esper.set_handler("foo", myclass.my_handler)
esper.dispatch_event("foo")
esper.event_registry.clear()
test_event_handler_switch_world_called=0
def test_event_handler_switch_world():
global test_event_handler_switch_world_called
test_event_handler_switch_world_called=0
def handler():
# nonlocal called
global test_event_handler_switch_world_called
print("HELLO?")
test_event_handler_switch_world_called+=1
# Switch to a new "left" World context, and register
# an event handler. Confirm that it is being called
# by checking that the 'called' variable is incremented.
esper.switch_world("left")
esper.set_handler("foo", handler)
assert test_event_handler_switch_world_called == 0
esper.dispatch_event("foo")
assert test_event_handler_switch_world_called == 1
# Here we switch to a new "right" World context.
# The handler is registered to the "left" context only,
# so dispatching the event should have no effect. The
# handler is not attached, and so the 'called' value
# should not be incremented further.
esper.switch_world("right")
esper.dispatch_event("foo")
assert test_event_handler_switch_world_called == 1
# Switching back to the "left" context and dispatching
# the event, the handler should still be registered and
# the 'called' variable should be incremented by 1.
esper.switch_world("left")
esper.dispatch_event("foo")
assert test_event_handler_switch_world_called == 2
##################################################
# Some helper functions and Component templates:
##################################################
class CorrectProcessorA(esper.Processor):
def process(self):
pass
class CorrectProcessorB(esper.Processor):
def __init__(self, x=0, y=0):
self.x = x
self.y = y
def process(self):
pass
class CorrectProcessorC(esper.Processor):
def process(self):
pass
class ArgsProcessor(esper.Processor):
def process(self, spam):
pass
class KwargsProcessor(esper.Processor):
def process(self, spam, eggs, beans="", toast=""):
pass
class IncorrectProcessor:
def process(self):
pass
# Event handler templates:
def myhandler_noargs():
print("OK")
def myhandler_onearg(arg):
print("Arg:", arg)
def myhandler_twoargs(arg1, arg2):
print("Args:", arg1, arg2)
# print(__dict__)
for key, val in __dict__.items():
if callable(val) and key.startswith("test_"):
print(key, val)
print("running test", key)
_reset_to_zero()
val()
# if hasattr(val, "__module__") and val.__module__ == __name__:
# if callable(val):
# print(f"found callable:{val}")
# print(mappingproxy)