-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathtest_event.py
More file actions
73 lines (55 loc) · 2.38 KB
/
Copy pathtest_event.py
File metadata and controls
73 lines (55 loc) · 2.38 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
"""Tests for event parsing and handling."""
from typing import Any, Final
import pytest
import tcod.event
import tcod.sdl.sys
from tcod._internal import _check
from tcod.cffi import ffi, lib
from tcod.event import KeySym, Modifier, Scancode
EXPECTED_EVENTS: Final = (
tcod.event.Quit(),
tcod.event.KeyDown(scancode=Scancode.A, sym=KeySym.A, mod=Modifier(0), pressed=True),
tcod.event.KeyUp(scancode=Scancode.A, sym=KeySym.A, mod=Modifier(0), pressed=False),
)
"""Events to compare with after passing though the SDL event queue."""
def as_sdl_event(event: tcod.event.Event) -> dict[str, dict[str, Any]]:
"""Convert events into SDL_Event unions using cffi's union format."""
match event:
case tcod.event.Quit():
return {"quit": {"type": lib.SDL_EVENT_QUIT}}
case tcod.event.KeyboardEvent():
return {
"key": {
"type": (lib.SDL_EVENT_KEY_UP, lib.SDL_EVENT_KEY_DOWN)[event.pressed],
"scancode": event.scancode,
"key": event.sym,
"mod": event.mod,
"down": event.pressed,
"repeat": event.repeat,
}
}
raise AssertionError
EVENT_PACK: Final = ffi.new("SDL_Event[]", [as_sdl_event(_e) for _e in EXPECTED_EVENTS])
"""A custom C array of SDL_Event unions based on EXPECTED_EVENTS."""
def push_events() -> None:
"""Reset the SDL event queue to an expected list of events."""
tcod.sdl.sys.init(tcod.sdl.sys.Subsystem.EVENTS) # Ensure SDL event queue is enabled
lib.SDL_PumpEvents() # Clear everything from the queue
lib.SDL_FlushEvents(lib.SDL_EVENT_FIRST, lib.SDL_EVENT_LAST)
assert _check( # Fill the queue with EVENT_PACK
lib.SDL_PeepEvents(EVENT_PACK, len(EVENT_PACK), lib.SDL_ADDEVENT, lib.SDL_EVENT_FIRST, lib.SDL_EVENT_LAST)
) == len(EVENT_PACK)
def test_get_events() -> None:
push_events()
assert tuple(tcod.event.get()) == EXPECTED_EVENTS
assert tuple(tcod.event.get()) == ()
assert tuple(tcod.event.wait(timeout=0)) == ()
push_events()
assert tuple(tcod.event.wait()) == EXPECTED_EVENTS
def test_event_dispatch() -> None:
push_events()
with pytest.deprecated_call():
tcod.event.EventDispatch().event_wait(timeout=0)
push_events()
with pytest.deprecated_call():
tcod.event.EventDispatch().event_get()