diff --git a/Test b/Test new file mode 100644 index 0000000..f3b857b --- /dev/null +++ b/Test @@ -0,0 +1 @@ +tests file 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() 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))