Skip to content

Commit

Permalink
[IMP] Improve the suite of tests. Use queues instead of stacks for th…
Browse files Browse the repository at this point in the history
…e simulated player actions and the test collection.
  • Loading branch information
japinol7 committed Apr 28, 2024
1 parent 9cd1bb8 commit f05a3f3
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 39 deletions.
32 changes: 32 additions & 0 deletions codemaster/tools/utils/queue.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from collections import deque


class Queue:
def __init__(self, iterable=(), maxlen=None, name=''):
self._container = deque(iterable=iterable, maxlen=maxlen)
self.name = name

def __iter__(self):
return iter(self._container)

@property
def is_empty(self):
return not self._container

def push(self, item):
self._container.append(item)

def pop(self):
return self._container.popleft()

def peek(self):
return self._container[0] if not self.is_empty else None

def __len__(self):
return len(self._container)

def __str__(self):
return f"Queue({repr(list(self._container))})"

def __repr__(self):
return f"Queue({repr(list(self._container))})"
33 changes: 33 additions & 0 deletions codemaster/tools/utils/stack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from collections import deque


class Stack:
def __init__(self, iterable=(), maxlen=None, name='', num=0):
self._container = deque(iterable=iterable, maxlen=maxlen)
self.name = name
self.num = num

def __iter__(self):
return iter(self._container)

@property
def is_empty(self):
return not self._container

def push(self, item):
self._container.append(item)

def pop(self):
return self._container.pop()

def peek(self):
return self._container[-1] if not self.is_empty else None

def __len__(self):
return len(self._container)

def __str__(self):
return f"stack({repr(list(self._container))})"

def __repr__(self):
return f"stack({repr(list(self._container))})"
80 changes: 41 additions & 39 deletions suiteoftests/test_code_master.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from codemaster.config.settings import Settings
from codemaster import levels
from codemaster.level_scroll_screen import level_scroll_shift_control
from codemaster.tools.utils.queue import Queue
from suiteoftests import levels as test_levels

PLAYER_HEALTH_SUPER_HERO = 90_000
Expand Down Expand Up @@ -85,7 +86,7 @@ def __init__(self):
self.text_msg_pc_sprites = None
self.player = None
self.players = None
self.player_actions = []
self.player_actions = Queue()
self.levels = []
self.levels_qty = None
self.level_no = None
Expand All @@ -95,40 +96,41 @@ def __init__(self):

def main(self):
pg.init()
tests = [
tests = Queue((
TestMethodWithSetupLevels(
self.test_bat_hit_with_enough_bullets_must_die_and_give_xp,
level_name_nums=[3], starting_level_n=0, skip=False,
self.test_fetch_two_apples,
level_name_nums=[1], starting_level_n=0, skip=False,
),
TestMethodWithSetupLevels(
self.test_big_jump_and_fetch_1_life_n_7_potions_power,
level_name_nums=[2], starting_level_n=0, skip=False,
self.test_big_jump_and_fetch_1_file_disk,
level_name_nums=[1], starting_level_n=0, skip=False,
),
TestMethodWithSetupLevels(
self.test_big_jump_and_fetch_3_batteries_n_1_disk,
level_name_nums=[1], starting_level_n=0, skip=False,
),
TestMethodWithSetupLevels(
self.test_big_jump_and_fetch_1_file_disk,
level_name_nums=[1], starting_level_n=0, skip=False,
self.test_big_jump_and_fetch_1_life_n_7_potions_power,
level_name_nums=[2], starting_level_n=0, skip=False,
),
TestMethodWithSetupLevels(
self.test_fetch_two_apples,
level_name_nums=[1], starting_level_n=0, skip=False,
self.test_bat_hit_with_enough_bullets_must_die_and_give_xp,
level_name_nums=[3], starting_level_n=0, skip=False,
),
]
))
self.tests_skipped = [test.test.__name__ for test in tests if test.skip]
self.test_skipped_count = len(self.tests_skipped)
self.tests_skipped_text = f". Tests skipped: {self.test_skipped_count}" if self.test_skipped_count else ''

tests = [test for test in tests if not test.skip]
self.tests_total = sum(1 for test in tests if not test.skip)

log.info(LOG_START_TEST_APP_MSG)
log.info(f"Total tests to pass: {self.tests_total}{self.tests_skipped_text}")
try:
for test_n in range(self.tests_total):
for test in tuple(tests):
test_method_with_setup_levels = tests.pop()
if test.skip:
continue
self.set_up(
level_name_nums=test_method_with_setup_levels.level_name_nums,
starting_level_n=test_method_with_setup_levels.starting_level_n)
Expand Down Expand Up @@ -246,9 +248,9 @@ def _player_move(self):
if not self.player_actions:
return

if self.player_actions[-1][1] > 1:
player_action = self.player_actions[-1][0]
self.player_actions[-1][1] -= 1
if self.player_actions.peek()[1] > 1:
player_action = self.player_actions.peek()[0]
self.player_actions.peek()[1] -= 1
else:
player_action = self.player_actions.pop()[0]

Expand Down Expand Up @@ -304,14 +306,14 @@ def test_fetch_two_apples(self):
self, time_in_secs=5)
self._init_clock_timer(time_in_secs=4)

self.player_actions = [
self.player_actions = Queue((
['go_right', 34],
['jump', 5],
['go_right', 148],
['stop', 1],
['go_left', 4],
['stop', 1],
['go_right', 148],
['jump', 5],
['go_right', 34],
]
))

self._game_loop()

Expand All @@ -331,12 +333,12 @@ def test_big_jump_and_fetch_1_file_disk(self):
self, time_in_secs=5)
self._init_clock_timer(time_in_secs=4)

self.player_actions = [
['stop', 1],
['go_left', 45],
['jump', 5],
self.player_actions = Queue((
['go_left', 15],
]
['jump', 5],
['go_left', 45],
['stop', 1],
))

self._game_loop()

Expand All @@ -356,12 +358,12 @@ def test_big_jump_and_fetch_3_batteries_n_1_disk(self):
self, time_in_secs=5)
self._init_clock_timer(time_in_secs=5)

self.player_actions = [
['stop', 1],
['go_right', 192],
['jump', 5],
self.player_actions = Queue((
['go_right', 22],
]
['jump', 5],
['go_right', 192],
['stop', 1],
))

self._game_loop()

Expand Down Expand Up @@ -390,15 +392,15 @@ def player_die_hard_mock():
self, time_in_secs=5)
self._init_clock_timer(time_in_secs=4)

self.player_actions = [
['stop', 1],
['go_right', 78],
self.player_actions = Queue((
['go_left', 22],
['jump', 5],
['go_right', 12],
['go_left', 64],
['go_right', 12],
['jump', 5],
['go_left', 22],
]
['go_right', 78],
['stop', 1],
))

self._game_loop()

Expand All @@ -418,9 +420,9 @@ def test_bat_hit_with_enough_bullets_must_die_and_give_xp(self):
self, time_in_secs=5)
self._init_clock_timer(time_in_secs=3)

self.player_actions = [
self.player_actions = Queue((
['shot_bullet_t3_photonic', 15],
]
))

bat_black = [npc for npc in self.level.npcs if npc.type == ActorType.BAT_BLACK][0]

Expand Down

0 comments on commit f05a3f3

Please sign in to comment.