44
55# Test cases
66hitTestCases = [
7- ([0 , 0 , 0 , 0 , 0 ], "rectangle" , 1 ),
8- ([b'a' , 0 , 0 , 0 , 0 ], "rectangle" , 2 ),
9- ([b'd' , 0 , 0 , 0 , 0 ], "rectangle" , 3 ),
10- ([b'd' , 0 , b'a' , 0 , 0 ], "lowBar" , 4 ),
11- ([b'd' , 0 , b'a' , 0 , b'd' ], "lowBar" , 4 ),
12- ([b'd' , 0 , b'a' , b'w' , 0 ], "lowBar " , 5 ),
13- ([b'd' , 0 , b'a' , b'w' , b'w' ], "lowBar " , 5 ),
7+ ([0 , 0 , 0 , 0 , 0 ], "rectangle" , 0.01 , 100 ),
8+ ([b'a' , 0 , 0 , 0 , 0 ], "rectangle" , 0.02 , 100 ),
9+ ([b'd' , 0 , 0 , 0 , 0 ], "rectangle" , 0.03 , 100 ),
10+ ([b'd' , 0 , b'a' , 0 , 0 ], "lowBar" , 0.04 , 100 ),
11+ ([b'd' , 0 , b'a' , 0 , b'd' ], "lowBar" , 0.04 , 100 ),
12+ ([b'd' , 0 , b'a' , b'w' , 0 ], "highBar " , 0.05 , 100 ),
13+ ([b'd' , 0 , b'a' , b'w' , b'w' ], "highBar " , 0.05 , 100 ),
1414]
1515# Tests collision with obstacle
16- # There is a problem in the test
17- # Does not take into account the fact that obstacles are destroyed when shapeType stack is empty
18- # Object is destroyed before it can be registered to hit
19- # This is due to high speeds used in the tests
20- # Maybe check hit before moving objects?
21- @pytest .mark .parametrize ("actions, obstacleType, speed" , hitTestCases )
22- def test_Hit (actions , obstacleType , speed ):
16+ @pytest .mark .parametrize ("actions, obstacleType, speed, speedMultiplier" , hitTestCases )
17+ def test_Hit (actions , obstacleType , speed , speedMultiplier ):
2318 # Initialize game
2419 gameState = gameStateClass (speed , "test" )
2520 # Saves all hits or no-hits
@@ -32,7 +27,11 @@ def test_Hit(actions, obstacleType, speed):
3227 if action != 0 :
3328 gameState .keyboard (action )
3429
35- # Move obstacles and check if game was won
30+ # Move objects by speed * (speedMultiplier - 1) distance
31+ for i in range (1 , speedMultiplier ):
32+ gameState .stage .moveAllObs (speed )
33+
34+ # Move obstacles once more and check if game was won
3635 win = gameState .stage .moveAllObs (speed )
3736 winList .append (win )
3837 # Check if player collided with obstacle
@@ -51,12 +50,12 @@ def test_Hit(actions, obstacleType, speed):
5150 gameState = None
5251
5352winTestCases = [
54- ([b'd' , 0 , b'a' , b'w' , b'w' ], 5 ),
55- ([b'a' , b'd' , 0 , b'w' , b's' ], 5 )
53+ ([b'd' , 0 , b'a' , b'w' , b'w' ], 0.05 , 100 ),
54+ ([b'a' , b'd' , 0 , b'w' , b's' ], 0.05 , 100 )
5655]
5756# Tests collision with obstacle
58- @pytest .mark .parametrize ("actions, speed" , winTestCases )
59- def test_Win (actions , speed ):
57+ @pytest .mark .parametrize ("actions, speed, speedMultiplier " , winTestCases )
58+ def test_Win (actions , speed , speedMultiplier ):
6059 # Initialize game
6160 gameState = gameStateClass (speed , "test" )
6261 # Saves all hits or no-hits
@@ -69,7 +68,11 @@ def test_Win(actions, speed):
6968 if action != 0 :
7069 gameState .keyboard (action )
7170
72- # Move obstacles and check if game was won
71+ # Move objects by speed * (speedMultiplier - 1) distance
72+ for i in range (1 , speedMultiplier ):
73+ gameState .stage .moveAllObs (speed )
74+
75+ # Move obstacles once more and check if game was won
7376 win = gameState .stage .moveAllObs (speed )
7477 winList .append (win )
7578 # Check if player collided with obstacle
@@ -187,21 +190,21 @@ def test_Keyboard():
187190 (0.05 )
188191]
189192# Test that idle function moves objects and registeres hits correctly
190- # Here problem is that obstacles list is not a copy of the state.obstacles after initialization, it is still a reference
191- # Hence stage.obstacles = obstacles
192193@pytest .mark .parametrize ("speed" , idleTestCases )
193194def test_Idle (speed ):
194195 gameState = gameStateClass (speed , "test" )
195- # Objects should move
196- obstacles = gameState .stage .obstacles .copy ()
196+ # Test stage object starting positions where to compare object movement
197+ startPos = [5 , 10 , 15 ]
198+ # Move objects by "speed" amount, once
197199 gameState .idle ()
198- for i in range (0 , len (obstacles )):
199- assert (obstacles [i ]. z == gameState .stage .obstacles [i ].z - speed )
200+ for i in range (0 , len (gameState . stage . obstacles )):
201+ assert (startPos [i ] == gameState .stage .obstacles [i ].z + speed )
200202
201203 # Player should be hit by the rectangle at x = 0
202- startZ = obstacles [0 ].z
203- # Steps until hit, minus one
204- steps = int (startZ / speed ) - 2
204+ startZ = 5
205+ # Steps until hit, must take into account the rounding of obstacle z position when registering hits
206+ # round(obs.z, 0) means that object at z = 0.49 is considered hitting the player
207+ steps = int ((startZ - 0.51 ) / speed )
205208 for i in range (0 , steps ):
206209 gameState .idle ()
207210 assert (gameState .gameLost == False )
@@ -274,26 +277,27 @@ def test_Move(shape, z, speed):
274277
275278moveAllTestCases = [
276279 (3 , ["rectangle" , "rectangle" , "rectangle" , "lowBar" , "highBar" ], [0 , 1 , - 1 ], 0.02 ),
277- (3 , ["lowBar" , "rectangle" , "highBar" , "highBar" , "rectangle" , "rectangle" , "rectangle" , "highBar" , "highBar" , "lowBar" , "rectangle" , "lowBar" ], [0 , 1 , 0 , - 1 , 0 ], 0.03 ),
278- (3 , ["highBar" , "highBar" , "rectangle" , "rectangle" , "rectangle" , "highBar" , "lowBar" , "rectangle" , "highBar" , "highBar" ], [0 , - 1 , 1 , 0 ], 0.05 )
280+ (4 , ["lowBar" , "rectangle" , "highBar" , "highBar" , "rectangle" , "rectangle" , "rectangle" , "highBar" , "highBar" , "lowBar" , "rectangle" , "lowBar" ], [0 , 1 , 0 , - 1 , 0 ], 0.03 ),
281+ (5 , ["highBar" , "highBar" , "rectangle" , "rectangle" , "rectangle" , "highBar" , "lowBar" , "rectangle" , "highBar" , "highBar" ], [0 , - 1 , 1 , 0 ], 0.05 )
279282]
280283# Test moving all obstacles
281284@pytest .mark .parametrize ("nObs, obstacleTypes, recXPositions, speed" , moveAllTestCases )
282285def test_MoveAll (nObs , obstacleTypes , recXPositions , speed ):
283286 # Initialize test
284287 course = obstacleCourse (nObs , obstacleTypes , recXPositions )
285- # Get positions of all obs
286- obs = course .obstacles
288+ # Starting positions of all obs, which are by default placed 5 distance units apart
289+ # start = 5, step = 5, exclusive end = (nObs + 1) * 5 => end = nObs * 5
290+ obsStart = range (5 , (nObs + 1 ) * 5 , 5 )
287291 # Move all obs
288292 course .moveAllObs (speed )
289293 # Check that all obs moved
290- for i in range (0 , len ( obs ) ):
291- assert (obs [i ]. z != course .obstacles [i ].z )
294+ for i in range (0 , nObs ):
295+ assert (obsStart [i ] != course .obstacles [i ].z )
292296
293297relocateTestCases = [
294- (3 , ["rectangle " , "rectangle " , "rectangle" , "lowBar " , "highBar " ], [0 , 1 , - 1 ]),
295- (3 , ["lowBar" , "rectangle " , "highBar" , "highBar" , "rectangle" , "rectangle" , "rectangle" , "highBar" , "highBar" , "lowBar" , "rectangle" , "lowBar" ], [0 , 1 , 0 , - 1 , 0 ]),
296- (3 , ["highBar" , "highBar" , "rectangle " , "rectangle" , "rectangle" , "highBar" , "lowBar" , "rectangle" , "highBar" , "highBar" ], [0 , - 1 , 1 , 0 ])
298+ (3 , ["lowBar " , "lowBar " , "lowBar" , " rectangle" , "rectangle " , "rectangle " ], [0 , 1 , - 1 ]),
299+ (3 , ["lowBar" , "lowBar " , "highBar" , "highBar" , "rectangle" , "rectangle" , "rectangle" , "highBar" , "highBar" , "lowBar" , "rectangle" , "lowBar" ], [1 , 0 , - 1 , 0 ]),
300+ (3 , ["highBar" , "highBar" , "highBar " , "rectangle" , "rectangle" , "highBar" , "lowBar" , "rectangle" , "highBar" , "highBar" ], [- 1 , 1 , 0 ])
297301]
298302# Test relocating an object in the x-axis
299303@pytest .mark .parametrize ("nObs, obstacleTypes, recXPositions" , relocateTestCases )
@@ -304,6 +308,9 @@ def test_Relocate(nObs, obstacleTypes, recXPositions):
304308 course = obstacleCourse (nObs , obstacleTypes , inputRecXPos )
305309 # Relocate all obstacles
306310 i = 0
311+ # Initializing obstacleCourse already relocates rectangles, so all of the rectangles in the starting screen will reduce the number of elements in recXPositions by one
312+ # Therefore if we relocate outside of the game, we need to make sure that the obstacleCourse has enough rectangle positions, otherwise we will be trying to pop from
313+ # an empty array. This can be done by having no rectangles in the first nObs of shapes
307314 for obs in course .obstacles :
308315 course .relocate (obs )
309316 # If obstacle was a rectangle, check that it got the correct position from recXPositions
0 commit comments