# jogai - Python Game Library # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Library General Public # License as published by the Free Software Foundation; either # version 2 of the License, or (at your option) any later version. # # This library is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Library General Public License for more details. # # You should have received a copy of the GNU Library General Public # License along with this library; if not, write to the Free # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # # Ramón Palleiro Rodríguez # Zero! Factorial Studio # info@zero.gal import pygame import pytest from pytest import fixture, mark from jogai import _globals, settings from jogai.character import Character from jogai.exceptions import KeyPressedNotFoundError from jogai.groups import RectangleGroup from jogai.texts import Chronometer, Scoreboard, Text from jogai.widgets import LevelBar class TestVideogame: def test_load_data_with_a_predefined_attribute_allowed( self, videogame_test ): videogame_test._load_data({'caption': 'foo'}) assert pygame.display.get_caption()[0] == 'foo' def test_load_data_with_wrong_type(self, videogame_test): with pytest.raises(TypeError) as e: videogame_test._load_data('foo') assert ( str(e.value) == 'the attributes in the input schema must be a dict' ) @mark.parametrize( ('key, down'), [('a', False), ('ESCAPE', True)], ) def test_simulate_key_press(self, videogame_test, key, down): videogame_test.simulate_key_press(key, down) events = pygame.event.get() for event in events: if event.type == pygame.KEYUP or event.type == pygame.KEYDOWN: assert pygame.key.name(event.key) == key.lower() @mark.parametrize( ('key, down'), [(-1, False), (None, True)], ) def test_simulate_wrong_key_press(self, videogame_test, key, down): with pytest.raises(KeyPressedNotFoundError) as e: videogame_test.simulate_key_press(key, down) assert str(e.value) == 'the key pressed was not found' def test_add_character(self, videogame_test): character = Character('protagonist.png') videogame_test.add_character(character) assert character in _globals.vg_characters def test_add_character_with_wrong_type(self, videogame_test): with pytest.raises(TypeError) as e: videogame_test.add_character('foo') assert str(e.value) == 'only Character instances can be added' def test_add_chronometer(self, videogame_test): chronometer = Chronometer(30) videogame_test.add_chronometer(chronometer) assert chronometer in _globals.vg_chronometers def test_add_chronometer_with_wrong_type(self, videogame_test): with pytest.raises(TypeError) as e: videogame_test.add_chronometer('foo') assert str(e.value) == 'only Chronometer instances can be added' def test_add_group(self, videogame_test, square_group_test): videogame_test.add_group(square_group_test) assert square_group_test in _globals.vg_groups def test_add_group_with_wrong_type(self, videogame_test): with pytest.raises(TypeError) as e: videogame_test.add_group('foo') assert str(e.value) == 'only Group instances can be added' def test_add_levelbar(self, videogame_test): levelbar = LevelBar(3) videogame_test.add_levelbar(levelbar) assert levelbar in _globals.vg_levelbars def test_add_levelbar_with_wrong_type(self, videogame_test): with pytest.raises(TypeError) as e: videogame_test.add_levelbar('foo') assert str(e.value) == 'only LevelBar instances can be added' def test_add_scoreboard(self, videogame_test): scoreboard = Scoreboard('foo') videogame_test.add_scoreboard(scoreboard) assert scoreboard in _globals.vg_scoreboards def test_add_scoreboard_with_wrong_type(self, videogame_test): with pytest.raises(TypeError) as e: videogame_test.add_scoreboard('foo') assert str(e.value) == 'only Scoreboard instances can be added' def test_add_text(self, videogame_test): text = Text('foo') videogame_test.add_text(text) assert text in _globals.vg_texts def test_add_text_with_wrong_type(self, videogame_test): with pytest.raises(TypeError) as e: videogame_test.add_text('foo') assert str(e.value) == 'only Text instances can be added' def test_listen_quit_event(self, videogame_test): quit_event = pygame.event.Event( pygame.QUIT, mod=pygame.locals.KMOD_NONE, ) pygame.event.post(quit_event) videogame_test.listen_events() assert pygame.display.get_init() == False videogame_test.init() videogame_test.load_environment('test') def test_listen_escape_event(self, videogame_test): videogame_test.simulate_key_press('ESCAPE', True) videogame_test.listen_events() assert pygame.display.get_init() == False videogame_test.init() videogame_test.load_environment('test') def test_get_color(self, videogame_test): color = videogame_test.get_color( videogame_test.settings.WINDOW_WIDTH - 1, videogame_test.settings.WINDOW_HEIGHT - 1, ) assert len(color) == 3 assert all([True if 0 <= c <= 255 else False for c in color]) def test_get_color_with_wrong_type(self, videogame_test): with pytest.raises(TypeError) as e: videogame_test.get_color('foo', 'bar') assert ( str(e.value) == 'the coordinates of the point must be a non-negative integers' ) def test_get_color_out_of_range_values(self, videogame_test): with pytest.raises(ValueError) as e: videogame_test.get_color(-3, 100000) assert ( str(e.value) == 'the coordinates of the point must be a non-negative integers' ) @mark.parametrize('wait', [0, 1, -1, None, 'foo']) def test_quit_including_wrong_wait(self, videogame_test, wait): videogame_test.quit(wait) assert pygame.display.get_init() == False videogame_test.init() videogame_test.load_environment('test') def test_set_caption(self, videogame_test): videogame_test.set_caption('foo') assert pygame.display.get_caption()[0] == 'foo' @mark.parametrize('caption', [1, -1, None]) def test_set_caption_with_wrong_type(self, videogame_test, caption): with pytest.raises(TypeError) as e: videogame_test.set_caption(caption) assert str(e.value) == 'the caption for the videogame must be a string' def test_set_gravity(self, videogame_test): gravity = 5 videogame_test.set_gravity(gravity) assert _globals._gravity == gravity @mark.parametrize('gravity', ['foo', 1.2, None]) def test_set_gravity_with_wrong_type(self, videogame_test, gravity): with pytest.raises(TypeError) as e: videogame_test.set_gravity(gravity) assert str(e.value) == 'the gravity must be an integer' def test_set_marks(self, videogame_test): videogame_test.set_marks() assert _globals.marks.x_color == settings.MARKS_DEFAULT_X_COLOR assert _globals.marks.y_color == settings.MARKS_DEFAULT_Y_COLOR assert _globals.marks.width == settings.MARKS_DEFAULT_WIDTH """ def test_prepare_02_yaml(self, videogame_test): videogame_test.prepare('02.yaml') def test_load_yaml_with_no_characters(self, videogame_test): videogame_test.prepare('no_characters.yaml') def test_update(self, videogame_test): videogame_test.update() """