From b5714c4ebb086d65bfdda1b1b7af3d45b2b2774d Mon Sep 17 00:00:00 2001 From: zehava-bard Date: Sun, 20 Jul 2025 19:14:58 +0300 Subject: [PATCH 1/5] Add files via upload some game tests --- conftest.py | 33 +++++++++++++++++++++++++ test_block_rotation.py | 19 +++++++++++++++ test_game_logic.py | 55 ++++++++++++++++++++++++++++++++++++++++++ test_grid.py | 29 ++++++++++++++++++++++ 4 files changed, 136 insertions(+) create mode 100644 conftest.py create mode 100644 test_block_rotation.py create mode 100644 test_game_logic.py create mode 100644 test_grid.py diff --git a/conftest.py b/conftest.py new file mode 100644 index 0000000..d3d9bbc --- /dev/null +++ b/conftest.py @@ -0,0 +1,33 @@ +import pytest +import pygame +from game import Game +from grid import Grid +from blocks import TBlock +import pygame + + +@pytest.fixture(scope="function") +def game_instance(monkeypatch): + monkeypatch.setattr(pygame.mixer, "Sound", lambda x: DummySound()) + monkeypatch.setattr(pygame.mixer.music, "load", lambda x: None) + monkeypatch.setattr(pygame.mixer.music, "play", lambda x: None) + + return Game() + +class DummySound: + def play(self): pass + +@pytest.fixture +def empty_grid(): + return Grid() + +@pytest.fixture +def full_row_grid(): + grid = Grid() + for col in range(grid.num_cols): + grid.grid[19][col] = 1 # שורה מלאה בתחתית + return grid + +@pytest.fixture +def t_block(): + return TBlock() \ No newline at end of file diff --git a/test_block_rotation.py b/test_block_rotation.py new file mode 100644 index 0000000..a33b06c --- /dev/null +++ b/test_block_rotation.py @@ -0,0 +1,19 @@ +def test_tblock_rotation_cycles(t_block): + original_positions = t_block.get_cell_positions() + for _ in range(4): + t_block.rotate() + cycled_positions = t_block.get_cell_positions() + # אחרי 4 סיבובים זה אמור לחזור למצב ההתחלתי + for p1, p2 in zip(original_positions, cycled_positions): + assert p1.row == p2.row + assert p1.column == p2.column + +def test_tblock_move_and_undo(t_block): + original = [(pos.row, pos.column) for pos in t_block.get_cell_positions()] + t_block.move(1, -2) + moved = [(pos.row, pos.column) for pos in t_block.get_cell_positions()] + assert any(o != m for o, m in zip(original, moved)) + + t_block.move(-1, 2) + restored = [(pos.row, pos.column) for pos in t_block.get_cell_positions()] + assert original == restored diff --git a/test_game_logic.py b/test_game_logic.py new file mode 100644 index 0000000..599ea4f --- /dev/null +++ b/test_game_logic.py @@ -0,0 +1,55 @@ +# Tests/test_game_logic.py +def test_game_initial(game_instance): + assert game_instance.grid is not None + assert game_instance.current_block is not None + assert game_instance.next_block is not None + assert game_instance.score == 0 + assert not game_instance.game_over + + +def test_block_inside_on_start(game_instance): + assert game_instance.block_inside() + +def test_rotate_block_keeps_inside(game_instance): + block_before = game_instance.current_block + game_instance.rotate() + assert game_instance.block_inside() + assert game_instance.current_block == block_before + +def test_block_can_move_left(game_instance): + col_before = game_instance.current_block.column_offset + game_instance.move_left() + col_after = game_instance.current_block.column_offset + assert col_after <= col_before + +def test_block_can_move_right(game_instance): + col_before = game_instance.current_block.column_offset + game_instance.move_right() + col_after = game_instance.current_block.column_offset + assert col_after >= col_before + +def test_move_left_and_back(game_instance): + old_pos = game_instance.current_block.column_offset, game_instance.current_block.column_offset + game_instance.move_left() + game_instance.move_right() + new_pos = game_instance.current_block.column_offset, game_instance.current_block.column_offset + assert new_pos == old_pos + +def test_score_after_line_clear(game_instance, monkeypatch): + row_to_clear = game_instance.grid.num_rows - 1 + for col in range(game_instance.grid.num_cols): + game_instance.grid.grid[row_to_clear][col] = 1 + + game_instance.lock_block() + assert game_instance.score >= 100 + + +def test_rotate_plays_sound(game_instance, monkeypatch): + played = {"called": False} + + def fake_play(): + played["called"] = True + + monkeypatch.setattr(game_instance.rotate_sound, "play", fake_play) + game_instance.rotate() + assert played["called"] diff --git a/test_grid.py b/test_grid.py new file mode 100644 index 0000000..20ef242 --- /dev/null +++ b/test_grid.py @@ -0,0 +1,29 @@ +def test_grid_initialization(empty_grid): + assert empty_grid.num_rows == 20 + assert empty_grid.num_cols == 10 + for row in empty_grid.grid: + assert all(cell == 0 for cell in row) + +def test_grid_is_inside(empty_grid): + assert empty_grid.is_inside(0, 0) + assert empty_grid.is_inside(19, 9) + assert not empty_grid.is_inside(-1, 0) + assert not empty_grid.is_inside(20, 0) + assert not empty_grid.is_inside(0, 10) + +def test_grid_is_empty(empty_grid): + assert empty_grid.is_empty(0, 0) + empty_grid.grid[0][0] = 1 + assert not empty_grid.is_empty(0, 0) + +def test_grid_clear_row(empty_grid): + row = 5 + for col in range(empty_grid.num_cols): + empty_grid.grid[row][col] = 3 + empty_grid.clear_row(row) + assert all(empty_grid.grid[row][col] == 0 for col in range(empty_grid.num_cols)) + +def test_clear_full_rows(full_row_grid): + rows_cleared = full_row_grid.clear_full_rows() + assert rows_cleared == 1 + assert all(full_row_grid.grid[19][col] == 0 for col in range(full_row_grid.num_cols)) From 0d4958d1105d0fad0c9f661aba426c9bedb45dd3 Mon Sep 17 00:00:00 2001 From: zehava-bard Date: Sun, 20 Jul 2025 19:21:22 +0300 Subject: [PATCH 2/5] Create Test test file --- Test | 1 + 1 file changed, 1 insertion(+) create mode 100644 Test diff --git a/Test b/Test new file mode 100644 index 0000000..f3b857b --- /dev/null +++ b/Test @@ -0,0 +1 @@ +tests file From 3fc66b1e3d8242a257fb5b8c8b0bb030380e58fe Mon Sep 17 00:00:00 2001 From: zehava-bard Date: Sun, 20 Jul 2025 19:22:49 +0300 Subject: [PATCH 3/5] Update test_block_rotation.py --- test_block_rotation.py | 1 - 1 file changed, 1 deletion(-) diff --git a/test_block_rotation.py b/test_block_rotation.py index a33b06c..6fe5575 100644 --- a/test_block_rotation.py +++ b/test_block_rotation.py @@ -3,7 +3,6 @@ def test_tblock_rotation_cycles(t_block): for _ in range(4): t_block.rotate() cycled_positions = t_block.get_cell_positions() - # אחרי 4 סיבובים זה אמור לחזור למצב ההתחלתי for p1, p2 in zip(original_positions, cycled_positions): assert p1.row == p2.row assert p1.column == p2.column From 17c3ca8d0527667bd514b5f653db0382811877bd Mon Sep 17 00:00:00 2001 From: zehava-bard Date: Sun, 20 Jul 2025 19:25:49 +0300 Subject: [PATCH 4/5] Create conftest conftest --- Tests/conftest | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 Tests/conftest diff --git a/Tests/conftest b/Tests/conftest new file mode 100644 index 0000000..436d493 --- /dev/null +++ b/Tests/conftest @@ -0,0 +1,33 @@ +import pytest +import pygame +from game import Game +from grid import Grid +from blocks import TBlock +import pygame + + +@pytest.fixture(scope="function") +def game_instance(monkeypatch): + monkeypatch.setattr(pygame.mixer, "Sound", lambda x: DummySound()) + monkeypatch.setattr(pygame.mixer.music, "load", lambda x: None) + monkeypatch.setattr(pygame.mixer.music, "play", lambda x: None) + + return Game() + +class DummySound: + def play(self): pass + +@pytest.fixture +def empty_grid(): + return Grid() + +@pytest.fixture +def full_row_grid(): + grid = Grid() + for col in range(grid.num_cols): + grid.grid[19][col] = 1 # שורה מלאה בתחתית + return grid + +@pytest.fixture +def t_block(): + return TBlock() From 2a36c332cff49ddb3b0df0b94469f04567184fe4 Mon Sep 17 00:00:00 2001 From: zehava-bard Date: Wed, 20 May 2026 17:09:13 +0300 Subject: [PATCH 5/5] Add pytest tests for game logic --- test_block_rotation.py | 1 + 1 file changed, 1 insertion(+) diff --git a/test_block_rotation.py b/test_block_rotation.py index 6fe5575..a33b06c 100644 --- a/test_block_rotation.py +++ b/test_block_rotation.py @@ -3,6 +3,7 @@ def test_tblock_rotation_cycles(t_block): for _ in range(4): t_block.rotate() cycled_positions = t_block.get_cell_positions() + # אחרי 4 סיבובים זה אמור לחזור למצב ההתחלתי for p1, p2 in zip(original_positions, cycled_positions): assert p1.row == p2.row assert p1.column == p2.column