|
| 1 | +import sys |
| 2 | +import os |
| 3 | + |
| 4 | +sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..")) |
| 5 | + |
| 6 | +# conftest.py has already patched pygame.mixer before this import |
| 7 | +from game import Game |
| 8 | +from blocks import IBlock, JBlock, LBlock, OBlock, SBlock, TBlock, ZBlock |
| 9 | + |
| 10 | +ALL_BLOCK_TYPES = {IBlock, JBlock, LBlock, OBlock, SBlock, TBlock, ZBlock} |
| 11 | + |
| 12 | + |
| 13 | +def test_initial_score_is_zero(): |
| 14 | + game = Game() |
| 15 | + assert game.score == 0 |
| 16 | + |
| 17 | + |
| 18 | +def test_initial_game_over_is_false(): |
| 19 | + game = Game() |
| 20 | + assert game.game_over is False |
| 21 | + |
| 22 | + |
| 23 | +def test_update_score_one_line(): |
| 24 | + game = Game() |
| 25 | + game.update_score(1, 0) |
| 26 | + assert game.score == 100 |
| 27 | + |
| 28 | + |
| 29 | +def test_update_score_two_lines(): |
| 30 | + game = Game() |
| 31 | + game.update_score(2, 0) |
| 32 | + assert game.score == 300 |
| 33 | + |
| 34 | + |
| 35 | +def test_update_score_three_lines(): |
| 36 | + game = Game() |
| 37 | + game.update_score(3, 0) |
| 38 | + assert game.score == 500 |
| 39 | + |
| 40 | + |
| 41 | +def test_update_score_move_down(): |
| 42 | + game = Game() |
| 43 | + game.update_score(0, 5) |
| 44 | + assert game.score == 5 |
| 45 | + |
| 46 | + |
| 47 | +def test_update_score_no_lines(): |
| 48 | + game = Game() |
| 49 | + game.update_score(0, 0) |
| 50 | + assert game.score == 0 |
| 51 | + |
| 52 | + |
| 53 | +def test_get_random_block_removes_from_bag(): |
| 54 | + game = Game() |
| 55 | + # After __init__, two blocks have already been drawn (current_block and next_block). |
| 56 | + # Reset the bag to a full set of 7 to get a predictable starting count. |
| 57 | + from blocks import IBlock, JBlock, LBlock, OBlock, SBlock, TBlock, ZBlock |
| 58 | + game.blocks = [IBlock(), JBlock(), LBlock(), OBlock(), SBlock(), TBlock(), ZBlock()] |
| 59 | + game.get_random_block() |
| 60 | + assert len(game.blocks) == 6 |
| 61 | + |
| 62 | + |
| 63 | +def test_get_random_block_refills_bag(): |
| 64 | + game = Game() |
| 65 | + # Drain all 7 blocks from a fresh bag |
| 66 | + game.blocks = [IBlock(), JBlock(), LBlock(), OBlock(), SBlock(), TBlock(), ZBlock()] |
| 67 | + for _ in range(7): |
| 68 | + game.get_random_block() |
| 69 | + # Bag is now empty; next call must refill and return a block |
| 70 | + assert len(game.blocks) == 0 |
| 71 | + block = game.get_random_block() |
| 72 | + # After refill and one removal, bag has 6 items |
| 73 | + assert block is not None |
| 74 | + assert len(game.blocks) == 6 |
| 75 | + |
| 76 | + |
| 77 | +def test_get_random_block_all_7_types(): |
| 78 | + game = Game() |
| 79 | + # Start with a fresh, full bag |
| 80 | + game.blocks = [IBlock(), JBlock(), LBlock(), OBlock(), SBlock(), TBlock(), ZBlock()] |
| 81 | + drawn_types = set() |
| 82 | + for _ in range(7): |
| 83 | + block = game.get_random_block() |
| 84 | + drawn_types.add(type(block)) |
| 85 | + assert drawn_types == ALL_BLOCK_TYPES |
0 commit comments