-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_controller.py
128 lines (118 loc) · 3.05 KB
/
test_controller.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
"""
This module contains unit tests for the classes in controller.py
"""
import pygame
import pytest
from controller import Controller
# Test cases
controller_updates = [
# Check if the left arrow event returns correct movement
(
pygame.event.Event(
pygame.KEYDOWN,
{
"mod": 0,
"scancode": 30,
"key": pygame.K_LEFT,
"unicode": "left arrow",
},
),
-0.5,
False,
),
# Check if the right arrow event returns correct movement
(
pygame.event.Event(
pygame.KEYDOWN,
{
"mod": 0,
"scancode": 30,
"key": pygame.K_RIGHT,
"unicode": "right arrow",
},
),
0.5,
False,
),
# Check if the space bar event returns correct movement
(
pygame.event.Event(
pygame.KEYDOWN,
{
"mod": 0,
"scancode": 30,
"key": pygame.K_SPACE,
"unicode": "space",
},
),
0.0,
True,
),
# Check if left arrow is released movement halts
(
pygame.event.Event(
pygame.KEYUP,
{
"mod": 0,
"scancode": 30,
"key": pygame.K_LEFT,
"unicode": "left arrow",
},
),
0.0,
False,
),
# Check if right arrow is released movement halts
(
pygame.event.Event(
pygame.KEYUP,
{
"mod": 0,
"scancode": 30,
"key": pygame.K_RIGHT,
"unicode": "right arrow",
},
),
0.0,
False,
),
# Check if space bar is released movement halts
(
pygame.event.Event(
pygame.KEYUP,
{
"mod": 0,
"scancode": 30,
"key": pygame.K_SPACE,
"unicode": "space",
},
),
0.0,
False,
),
]
@pytest.mark.parametrize("keystroke,x_move,y_move", controller_updates)
def test_controller_updates(keystroke, x_move, y_move):
"""
Tests that a specified keystroke in the events queue will return the
correct set of movements.
Args:
keystroke: A pygame event representing a simulated pressed
key from the user.
x_move: A float representing the expected movement when pressing a key.
y_move: A bool representing the expected jump status when
pressing a key.
"""
pygame.init()
instance = Controller(None)
# post the event
pygame.event.post(keystroke)
instance.update_game()
x_move_result = instance.left_right
y_move_result = instance.jumping
# Check the correct type
assert isinstance(x_move_result, float)
assert isinstance(y_move_result, bool)
# Check the correct value result
assert x_move_result == x_move
assert y_move_result == y_move