71 lines
2.6 KiB
Python
71 lines
2.6 KiB
Python
import json
|
|
import os
|
|
import sys
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
os.environ.setdefault('SDL_VIDEODRIVER', 'dummy')
|
|
os.environ.setdefault('SDL_AUDIODRIVER', 'dummy')
|
|
ROOT = Path(__file__).resolve().parent.parent
|
|
sys.path.insert(0, str(ROOT))
|
|
|
|
import das_tetris_gui as game_module # noqa: E402
|
|
import tetris_config as cfg # noqa: E402
|
|
|
|
|
|
class ConfigFilesTest(unittest.TestCase):
|
|
def test_all_config_files_are_valid_json(self):
|
|
for path in (ROOT / 'configs').glob('*.json'):
|
|
with self.subTest(path=path.name):
|
|
json.loads(path.read_text(encoding='utf-8'))
|
|
|
|
def test_default_config_used_when_local_missing(self):
|
|
missing = os.path.join(cfg.CONFIG_DIR, 'nonexistent.json')
|
|
self.assertIsNone(cfg._config_path(missing))
|
|
self.assertTrue(cfg._config_path(cfg.CONFIG_JSON).endswith('.json'))
|
|
|
|
|
|
class TetrisGameTest(unittest.TestCase):
|
|
def setUp(self):
|
|
self.game = game_module.TetrisGame(width=10)
|
|
|
|
def test_random_block_respects_min_score(self):
|
|
self.game.score = 0
|
|
for _ in range(200):
|
|
self.assertEqual(self.game.get_block_min_score(self.game.choice_random_block()), 0)
|
|
|
|
def test_random_block_excludes_given_when_possible(self):
|
|
current = self.game.current_block
|
|
for _ in range(200):
|
|
self.assertNotEqual(self.game.choice_random_block(exclude=current), current)
|
|
|
|
def test_random_block_with_single_allowed_block_does_not_hang(self):
|
|
only = next(b for b in game_module.BLOCK_TYPE if b.value['min_score'] == 0)
|
|
original = self.game.get_block_min_score
|
|
self.game.get_block_min_score = lambda b: 0 if b == only else 10 ** 9
|
|
try:
|
|
self.assertEqual(self.game.choice_random_block(exclude=only), only)
|
|
finally:
|
|
self.game.get_block_min_score = original
|
|
|
|
def test_clear_lines_scoring(self):
|
|
self.game.field[-1] = [1] * 10
|
|
self.game.field[-2] = [1] * 10
|
|
self.assertEqual(self.game.clear_lines(), 2)
|
|
self.assertEqual(self.game.score, 300)
|
|
self.assertEqual(self.game.field[-1], [0] * 10)
|
|
|
|
def test_fall_speed_thresholds(self):
|
|
cases = {6000: (0.45, 0.55), 11000: (0.4, 0.6), 16000: (0.35, 0.65)}
|
|
for score, (normal, inc) in cases.items():
|
|
for flag, expected in ((False, normal), (True, inc)):
|
|
with self.subTest(score=score, flag=flag):
|
|
cfg.HIGH_SCORE_INC_SPEED = flag
|
|
self.game.score = score
|
|
self.game.update(self.game.last_time)
|
|
self.assertEqual(self.game.fall_speed, expected)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|