class TitleScreenTests(TestCase): def setUp(self): self.events = EventGenerator() self.result = '' def one(self): self.result += '1' def two(self): self.result += '2' def three(self): self.result += '3' def test_show_title(self, screen): """Displays a main menu.""" menu = [ ('One', self.one), ('Two', self.two), ('Three', self.three), ] title = TitleScreen(screen, self.events, TITLE_RECT, TITLE_IMAGE, menu, MENU_RECT, VERTICAL_MOVES) self.events.add_scripted_keys([ K_UP, K_RETURN, K_DOWN, K_DOWN, K_SPACE, K_RETURN, K_UP, K_RETURN, K_UP ], converter=int) self.events.add_scripted_event(QUIT_EVENT) title.run() self.assertEqual(self.result, '3')
class TitleScreenTests(TestCase): def setUp(self): self.events = EventGenerator() self.result = '' def one(self): self.result += '1' def two(self): self.result += '2' def three(self): self.result += '3' def test_show_title(self, screen): """Displays a main menu.""" menu = [ ('One', self.one), ('Two', self.two), ('Three', self.three), ] title = TitleScreen(screen, self.events, TITLE_RECT, TITLE_IMAGE, menu, MENU_RECT, VERTICAL_MOVES) self.events.add_scripted_keys([K_UP, K_RETURN, K_DOWN, K_DOWN, K_SPACE, K_RETURN, K_UP, K_RETURN, K_UP], converter=int) self.events.add_scripted_event(QUIT_EVENT) title.run() self.assertEqual(self.result, '3')
def test_game_paused(self, screen): """Display pause box.""" frame = Frame(screen, Rect(100, 100, 260, 160)) egen = EventGenerator() egen.add_scripted_keys(['a']) gp = GamePausedBox(frame, config.PAUSE_IMAGE, text='test game paused signal', egen=egen) gp.draw() pygame.display.update() gp.activate()
def test_highscores(self): """Enters something into the highscore list.""" hl = HighscoreList(HIGHSCORE_FILE) events = EventGenerator() events.add_scripted_keys(["B", "C", chr(K_RETURN), chr(K_RETURN)]) events.add_scripted_event(QUIT_EVENT) TEST_GAME_CONTEXT.screen.clear() frame = Frame(TEST_GAME_CONTEXT.screen, HIGHSCORE_RECT) hs = HighscoreBox(frame, events, hl, HIGHSCORE_IMAGE, HIGHSCORE_TEXT_POS) hs.enter_score(333) self.assertEqual(hl.scores[0], (333, "BC")) hs.activate()
def test_highscores(self, screen): """Enters something into the highscore list.""" os.system('cp %s %s' % (HIGHSCORE_BACKUP, config.HIGHSCORE_FILE)) hl = HighscoreList(config.HIGHSCORE_FILE) events = EventGenerator() events.add_scripted_keys(['B', 'C', chr(K_RETURN), chr(K_RETURN)]) events.add_scripted_event(QUIT_EVENT) screen.clear() frame = Frame(screen, config.HIGHSCORE_RECT) hs = HighscoreBox(frame, events, hl, config.HIGHSCORE_IMAGE, config.HIGHSCORE_TEXT_POS) hs.enter_score(33333) hs.activate() assert hl.scores[0] == (33333, 'BC')
class TestAnyKeyListener: def callback(self): self.keypressed = True def test_anykey_negative(self): self.keypressed = False self.egen = EventGenerator() self.elis = AnyKeyListener(self.callback) self.egen.add_listener(self.elis) self.egen.add_scripted_event(QUIT_EVENT) self.egen.event_loop() assert not self.keypressed def test_anykey_positive(self): self.keypressed = False self.egen = EventGenerator() self.elis = AnyKeyListener(self.callback) self.egen.add_listener(self.elis) self.egen.add_scripted_keys('a') self.egen.add_scripted_event(QUIT_EVENT) self.egen.event_loop() assert self.keypressed
class AnyKeyListenerTests(TestCase): def callback(self): self.keypressed = True def test_anykey_negative(self): self.keypressed = False self.egen = EventGenerator() self.elis = AnyKeyListener(self.callback) self.egen.add_listener(self.elis) self.egen.add_scripted_event(QUIT_EVENT) self.egen.event_loop() self.assertFalse(self.keypressed) def test_anykey_positive(self): self.keypressed = False self.egen = EventGenerator() self.elis = AnyKeyListener(self.callback) self.egen.add_listener(self.elis) self.egen.add_scripted_keys('a') self.egen.add_scripted_event(QUIT_EVENT) self.egen.event_loop() self.assertTrue(self.keypressed)
class TestEvent: def hel(self): self.result += "Hel" def lo(self): self.result += "lo" def world(self): self.result += "World" def space(self): self.result += " " def setup(self): self.egen = EventGenerator() keymap = { pygame.K_a: self.hel, pygame.K_b: self.lo, pygame.K_c: self.world, pygame.K_d: self.space } self.elis = EventListener(keymap) self.egen.add_listener(self.elis) self.updated = 0 self.result = "" def update(self): """Counts how many times the method was called""" # callback function called from EventGenerator() self.updated += 1 def test_event_generator(self): """Generate some key events and check whether they arrive.""" self.egen.add_scripted_keys('acb') self.egen.add_scripted_event(QUIT_EVENT) self.egen.event_loop() assert self.result == "HelWorldlo" def test_callback(self): """Pass scripted keys as a string.""" self.egen.add_callback(self) self.egen.add_scripted_keys('abdc') self.egen.add_scripted_event(QUIT_EVENT) self.egen.event_loop() assert self.result == "Hello World" assert self.updated == 5 def test_scripted_repeat(self): """Pass scripted keys as a string.""" self.egen.add_callback(self) self.egen.add_scripted_keys('aaaa') self.egen.add_scripted_event(QUIT_EVENT) self.egen.event_loop() assert self.updated == 5
class EventTests(TestCase): def hel(self): self.result += "Hel" def lo(self): self.result += "lo" def world(self): self.result += "World" def space(self): self.result += " " def setUp(self): self.egen = EventGenerator() keymap = { 'a': self.hel, 'b': self.lo, 'c': self.world, 'd': self.space } self.elis = EventListener(keymap) self.egen.add_listener(self.elis) self.updated = 0 self.result = "" def update(self): """Counts how many times the method was called""" # callback function called from EventGenerator() self.updated += 1 def test_event_generator(self): """Generate some key events and check whether they arrive.""" self.egen.add_scripted_keys('acb') self.egen.add_scripted_event(QUIT_EVENT) self.egen.event_loop() self.assertEqual(self.result, "HelWorldlo") def test_callback(self): """Pass scripted keys as a string.""" self.egen.add_callback(self) self.egen.add_scripted_keys('abdc') self.egen.add_scripted_event(QUIT_EVENT) self.egen.event_loop() self.assertEqual(self.result, "Hello World") self.assertEqual(self.updated, 5) def test_scripted_repeat(self): """Pass scripted keys as a string.""" self.egen.add_callback(self) self.egen.add_scripted_keys('aaaa') self.egen.add_scripted_event(QUIT_EVENT) self.egen.event_loop() self.assertEqual(self.updated, 5)
class TextEnteringListenerTests(TestCase): def setUp(self): self.egen = EventGenerator() self.updated = 0 self.result = "" def text_finished(self, text): """Callback for listener.""" self.result = text def key_pressed(self, text): """Counts how many times the method was called""" self.updated += 1 def test_text_entering(self): """Generate some key events and check whether they arrive.""" elis = TextEnteringListener(self.key_pressed, self.text_finished) self.egen.add_listener(elis) self.egen.add_scripted_keys('acbd') self.egen.add_scripted_keys(chr(K_RETURN)) self.egen.add_scripted_event(QUIT_EVENT) self.egen.event_loop() self.assertEqual(self.result, "ACBD") self.assertEqual(self.updated, 4) def test_return_missing(self): """Generate some key events and check whether they arrive.""" elis = TextEnteringListener(self.key_pressed, self.text_finished) self.egen.add_listener(elis) self.egen.add_scripted_keys('acbd') self.egen.add_scripted_event(QUIT_EVENT) self.egen.event_loop() self.assertEqual(self.result, "") def test_delete(self): """Generate some key events and check whether they arrive.""" elis = TextEnteringListener(self.key_pressed, self.text_finished) self.egen.add_listener(elis) self.egen.add_scripted_keys('acbd') self.egen.add_scripted_keys(chr(K_DELETE)) self.egen.add_scripted_keys('efg') self.egen.add_scripted_keys(chr(K_RETURN)) self.egen.add_scripted_event(QUIT_EVENT) self.egen.event_loop() self.assertEqual(self.result, "ACBEFG") self.assertEqual(self.updated, 8) def test_no_upper(self): """Generate some key events and check whether they arrive.""" elis = TextEnteringListener(self.key_pressed, self.text_finished, False) self.egen.add_listener(elis) self.egen.add_scripted_keys('acbd') self.egen.add_scripted_keys(chr(K_RETURN)) self.egen.add_scripted_event(QUIT_EVENT) self.egen.event_loop() self.assertEqual(self.result, "acbd") self.assertEqual(self.updated, 4)
class TestTextEnteringListener: def setup(self): self.egen = EventGenerator() self.updated = 0 self.result = "" def text_finished(self, text): """Callback for listener.""" self.result = text def key_pressed(self, text): """Counts how many times the method was called""" self.updated += 1 def test_text_entering(self): """Generate some key events and check whether they arrive.""" elis = TextEnteringListener(self.key_pressed, self.text_finished) self.egen.add_listener(elis) self.egen.add_scripted_keys('acbd') self.egen.add_scripted_keys(chr(K_RETURN)) self.egen.add_scripted_event(QUIT_EVENT) self.egen.event_loop() assert self.result == "ACBD" assert self.updated == 4 def test_return_missing(self): """Generate some key events and check whether they arrive.""" elis = TextEnteringListener(self.key_pressed, self.text_finished) self.egen.add_listener(elis) self.egen.add_scripted_keys('acbd') self.egen.add_scripted_event(QUIT_EVENT) self.egen.event_loop() assert self.result == "" def test_delete(self): """Generate some key events and check whether they arrive.""" elis = TextEnteringListener(self.key_pressed, self.text_finished) self.egen.add_listener(elis) self.egen.add_scripted_keys('acbd') self.egen.add_scripted_keys(chr(K_DELETE)) self.egen.add_scripted_keys('efg') self.egen.add_scripted_keys(chr(K_RETURN)) self.egen.add_scripted_event(QUIT_EVENT) self.egen.event_loop() assert self.result == "ACBEFG" assert self.updated == 8 def test_no_upper(self): """Generate some key events and check whether they arrive.""" elis = TextEnteringListener(self.key_pressed, self.text_finished, False) self.egen.add_listener(elis) self.egen.add_scripted_keys('acbd') self.egen.add_scripted_keys(chr(K_RETURN)) self.egen.add_scripted_event(QUIT_EVENT) self.egen.event_loop() assert self.result == "acbd" assert self.updated == 4