# 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 from os.path import join import pytest from pygame import Surface from jogai import paths, settings from jogai.scene import Scene, _load_scene class TestScene: def test_load_scene_empty(self): sc = _load_scene({}) assert sc.image_filename == '' assert sc.image == None assert sc.background == (0, 0, 0) def test_load_scene_with_wrong_type(self): with pytest.raises(TypeError) as e: sc = _load_scene(-1) assert ( str(e.value) == 'the scene data must be a dict, a string, a list or a tuple' ) def test_load_scene_with_background(self): background = (255, 255, 255) sc = _load_scene(background) assert sc.image_filename == '' assert sc.image == None assert sc.background == background assert sc.boundaries == [ None, settings.WINDOW_WIDTH, settings.WINDOW_HEIGHT, None, ] def test_load_scene_with_background_and_boundaries(self): background = (255, 255, 255) sc = _load_scene({'background': background, 'boundaries': False}) assert sc.image_filename == '' assert sc.image == None assert sc.background == background assert sc.boundaries == [None] * 4 def test_scene_object_with_existing_filename(self, scene_filename_test): sc = Scene(scene_filename_test) assert isinstance(sc.image, Surface) == True def test_create_scene_with_wrong_type(self): with pytest.raises(TypeError) as e: sc = Scene(-1) assert str(e.value) == 'the image filename must be a string' def test_create_empty_scene(self): sc = Scene() assert sc.image_filename == '' assert sc.background == settings.SCENE_DEFAULT_BACKGROUND def test_scene_background_property(self, videogame_test): background = (255, 255, 255) current_scene.background = background assert current_scene.background == background def test_scene_boundaries_property(self, videogame_test): boundaries = [50, 100, 500, 300] current_scene.boundaries = boundaries assert current_scene.boundaries == boundaries def test_height_property(self, videogame_test): assert current_scene.height == current_scene.rect.height def test_image_filename_property(self, scene_filename_test): sc = Scene(scene_filename_test) sc.image_filename == scene_filename_test def test_image_path_property(self, scene_filename_test): sc = Scene(scene_filename_test) assert sc.image_path == join(paths.SCENES_DIR, scene_filename_test) def test_height_is_a_non_negative_integer(self, videogame_test): assert current_scene.height > -1 def test_width_is_a_non_negative_integer(self, videogame_test): assert current_scene.width > -1 def test_x_coordinate_is_an_integer(self, videogame_test): assert isinstance(current_scene.x, int) def test_change_background_color_with_a_list(self, videogame_test): current_scene.change_background([255, 255, 255]) assert isinstance(current_scene.background, tuple) == True assert current_scene.background == (255, 255, 255) @pytest.mark.parametrize('color', [-1, 'foo']) def test_change_background_color_wrong_type(self, videogame_test, color): with pytest.raises(TypeError) as e: current_scene.change_background(color) assert ( str(e.value) == 'the background color must be a tuple with three integers (r,g,b) between 0 and 255' ) def test_change_background_color_with_components_out_of_range( self, videogame_test ): with pytest.raises(ValueError) as e: current_scene.change_background((-1, -1, 300)) assert ( str(e.value) == 'the background color must be a tuple with three integers (r,g,b) between 0 and 255' ) def test_change_background_color_with_different_args_length( self, videogame_test ): with pytest.raises(ValueError) as e: current_scene.change_background((0, 0)) assert ( str(e.value) == 'the background color must be a tuple with three integers (r,g,b) between 0 and 255' ) def test_set_abscissa_position_with_wrong_type(self, videogame_test): with pytest.raises(TypeError) as e: current_scene._set_abscissa_position('foo') assert str(e.value) == 'the x coordinate must be an integer' @pytest.mark.parametrize( 'x', [0, settings.WINDOW_WIDTH // 4, settings.WINDOW_WIDTH // 2] ) def test_set_abscissa_position_less_or_equal_than_middle_of_window( self, videogame_test, x ): current_scene._set_abscissa_position(x) assert current_scene.x == 0 def test_set_abscissa_position_between_middle_of_window_and_scene_width_minus_window_width( self, videogame_test ): x = current_scene.width - settings.WINDOW_WIDTH current_scene._set_abscissa_position(x) assert current_scene.x == -x + settings.WINDOW_WIDTH // 2 def test_set_abscissa_position_at_the_end(self, videogame_test): current_scene._set_abscissa_position(current_scene.width) assert current_scene.x == -current_scene.width + settings.WINDOW_WIDTH @pytest.mark.parametrize('boundaries', [-1, 'foo']) def test_set_boundaries_with_wrong_type(self, videogame_test, boundaries): with pytest.raises(TypeError) as e: current_scene.set_boundaries(boundaries) assert ( str(e.value) == 'the scene boundaries must be a list or a boolean' )