-
Notifications
You must be signed in to change notification settings - Fork 0
/
rainbow_merge_test.py
149 lines (110 loc) · 4.88 KB
/
rainbow_merge_test.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
import copy
import pytest
from rainbow_merge_game import (
handle_merges,
handle_falls,
)
# Sample game board for testing
pre_list = [
[" ", " ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " ", " "],
]
blocks_list = copy.deepcopy(pre_list)
def test_handle_merges():
def merge_function(x, y, blocks_list):
return False # Ensure no merges occur for this test
merged_block = handle_merges(blocks_list, merge_function)
assert merged_block is None # No merges should occur
def test_handle_falls():
fallen_block = handle_falls(blocks_list)
assert fallen_block is None # No falls should occur
import pytest
import pygame
from rainbow_merge_block import Block, MovingBlock
from rainbow_merge_controller import P1Controller
from rainbow_merge_game import (
handle_merges,
handle_falls,
check_structure_changed,
check_first_column,
check_for_black,
win_screen,
game_over_screen,
main,
)
# Initialize Pygame to avoid errors when calling Pygame functions
pygame.init()
pygame.display.set_mode((800, 600))
# Setting up a simple game board with two blocks
blocks_list = [[None for _ in range(8)] for _ in range(8)]
block1 = Block(0, 0, 0) # Red block at position (0,0)
block2 = Block(1, 1, 1) # Orange block at position (1,1)
blocks_list[0][0] = block1
blocks_list[1][1] = block2
# Tests for Block class
def test_block_get_color():
# Test to ensure that the get_color method returns the correct color for the block.
assert block1.get_color() == (181, 3, 3) # Red block color
def test_block_draw():
# Test to verify that the block can be drawn on a Pygame surface without errors.
# This does not check the actual rendering but ensures that the method can be called.
screen = pygame.display.get_surface()
block1.draw(screen) # Test drawing the block
def test_vertical_merge():
# Test that no merge occurs when no block is below the current block.
assert not block1.vertical_merge(0, 0, blocks_list)
# Then, test that a merge does occur when a block of the same color is placed below.
blocks_list[0][1] = Block(0, 1, 0)
assert block1.vertical_merge(0, 0, blocks_list)
def test_horizontal_merge():
# Test that no merge occurs when no block is to the right of the current block.
assert not block1.horizontal_merge(0, 0, blocks_list)
# Then, test that a merge does occur when a block of the same color is placed to the right.
blocks_list[1][0] = Block(1, 0, 0)
assert block1.horizontal_merge(0, 0, blocks_list)
def test_fall():
# Test the fall functionality by clearing the space below the block and ensuring it can fall.
blocks_list[0][1] = None
assert block1.fall(0, 0, blocks_list)
# Tests for MovingBlock class
def test_moving_block_init():
# Test that a MovingBlock object initializes with the correct player number and controller.
controller = P1Controller()
moving_block = MovingBlock(1, controller)
assert moving_block.player_num == 1
def test_move_block():
# Test the ability of a MovingBlock to process a 'drop' command and update the blocks_list accordingly.
controller = P1Controller()
moving_block = MovingBlock(1, controller)
moving_block.move_block("drop", blocks_list)
# Tests for P1Controller
def test_p1_controller_input():
# Test to ensure the P1Controller correctly interprets and returns user input based on key presses.
# Actual key presses would be mocked in a more thorough testing environment.
controller = P1Controller()
# Tests for game logic functions
def test_handle_merges():
# Test that handle_merges returns None when no possible merges are detected.
assert handle_merges(blocks_list, lambda x, y, bl: False) is None
def test_handle_falls():
# Test that handle_falls returns None when no possible falls are detected.
assert handle_falls(blocks_list) is None
def test_check_structure_changed():
# Test to ensure that changes in the structure of blocks_list are correctly detected.
new_blocks_list = copy.deepcopy(blocks_list)
new_blocks_list[0][0] = None
assert check_structure_changed(blocks_list, new_blocks_list)
def test_check_first_column():
# Test to check if the first column of the block list is not empty.
assert check_first_column(blocks_list)
def test_check_for_black():
# Test to check if a black block is present in the blocks_list; expects False initially.
assert not check_for_black(blocks_list)
# Additional game logic and Pygame-dependent functions like win_screen, game_over_screen, and main
# would typically require more integration-style testing or mocking to properly test.