| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- # 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 pytest
- from jogai import *
- class TestGroups:
- def test_wrong_type_group_name(self, videogame_test):
- with pytest.raises(TypeError) as e:
- Group(-1, [protagonist])
- assert str(e.value) == 'group name must be a string'
- def test_wrong_type_group_characters(videogame_test):
- with pytest.raises(TypeError) as e:
- Group('protagonists', 'foo')
- assert str(e.value) == 'characters in group must be a list'
- def test_group_backward(self, videogame_test, square_group_test):
- initial = square_group_test[0].x
- offset = 10
- square_group_test.backward(offset)
- assert square_group_test[0].x == initial - offset
- def test_group_decrease(self, videogame_test, square_group_test):
- initial = square_group_test[0].scale
- offset = 0.1
- square_group_test.decrease(offset)
- assert square_group_test[0].scale == max(0, initial - offset)
- def test_group_move_with_key(self, videogame_test, square_group_test):
- initial_x = square_group_test[0].x
- initial_y = square_group_test[0].y
- offset = 5
- square_group_test.move(offset, offset, key='s')
- assert square_group_test[0].x == initial_x
- assert square_group_test[0].y == initial_y
- def test_group_flip(self, videogame_test, square_group_test):
- initial = square_group_test[0].flipped
- square_group_test.flip(True, False)
- assert square_group_test[0].flipped == {
- 'horizontal': True,
- 'vertical': False,
- }
- def test_group_decrease(self, videogame_test, square_group_test):
- initial = square_group_test[0].scale
- offset = 0.1
- square_group_test.decrease(offset)
- assert square_group_test[0].scale == max(0, initial - offset)
- def test_group_set_attribute(self, videogame_test, square_group_test):
- new_height = 10
- square_group_test.set_height(new_height)
- assert square_group_test[0].height == new_height
- def test_group_contains(
- self, videogame_test, simple_group_test, group_character_test
- ):
- assert group_character_test in simple_group_test
- def test_group_iteration(
- self, videogame_test, simple_group_test, group_character_test
- ):
- group_iterator = iter(simple_group_test)
- assert next(group_iterator) == group_character_test
- def test_group_name_property(
- self, videogame_test, simple_group_test, group_name_test
- ):
- assert simple_group_test.name == group_name_test
- def test_group_characters_remove(
- self, videogame_test, simple_group_test, group_character_test
- ):
- simple_group_test.remove(group_character_test)
- assert simple_group_test.characters == []
- def test_random_group_with_amount_wrong_type(self, videogame_test):
- with pytest.raises(TypeError) as e:
- RandomGroup('protagonists', protagonist, 'foo', (0, 200), (0, 200))
- assert (
- str(e.value)
- == 'the amount of characters must be a integer greater than 1'
- )
- def test_random_group_with_amount_not_greater_than_1(self, videogame_test):
- with pytest.raises(ValueError) as e:
- RandomGroup('protagonists', protagonist, -1, (0, 200), (0, 200))
- assert (
- str(e.value)
- == 'the amount of characters must be a integer greater than 1'
- )
- def test_random_group_with_ranges_wrong_type(self, videogame_test):
- with pytest.raises(TypeError) as e:
- RandomGroup('protagonists', protagonist, 2, 'bar', 'foo')
- assert str(e.value) == 'both x and y ranges must be tuples'
- def test_random_group_with_wrong_length_ranges(self, videogame_test):
- with pytest.raises(ValueError) as e:
- RandomGroup('protagonists', protagonist, 3, (1, 2, 3), (1, 2, 3))
- assert (
- str(e.value)
- == 'both x and y ranges must have only two items (min, max)'
- )
- def test_random_group_with_min_greater_than_max_range(
- self, videogame_test
- ):
- with pytest.raises(ValueError) as e:
- RandomGroup('protagonists', protagonist, 3, (100, 3), (100, 3))
- assert str(e.value) == 'both x and y ranges must be sorted (min, max)'
- def test_random_group_empty_range(self, videogame_test):
- rg = RandomGroup('protagonists', protagonist, 3, (), ())
- assert rg.x_range == (0, current_scene.width - protagonist.width)
- assert rg.y_range == (0, current_scene.height - protagonist.height)
- def test_random_group_range_lists(self, videogame_test):
- rg = RandomGroup('protagonists', protagonist, 3, [0, 200], [0, 200])
- assert isinstance(rg.x_range, tuple)
- assert rg.x_range == (0, 200)
- assert isinstance(rg.y_range, tuple)
- assert rg.y_range == (0, 200)
- def test_rectangle_group_with_wrong_sides_type(self, videogame_test):
- with pytest.raises(TypeError) as e:
- RectangleGroup('protagonists', protagonist, 'foo', 'bar')
- assert str(e.value) == 'both the base and the height must be integers'
- def test_rectangle_group_with_sides_out_of_range(self, videogame_test):
- with pytest.raises(ValueError) as e:
- RectangleGroup('protagonists', protagonist, -1, 3)
- assert str(e.value) == 'both the base and the height must be positive'
- def test_rectangle_group_with_both_sides_equal_to_1(self, videogame_test):
- with pytest.raises(ValueError) as e:
- RectangleGroup('protagonists', protagonist, 1, 1)
- assert (
- str(e.value)
- == 'at least one of the dimensions of the rectangle must be greater than 1'
- )
- def test_square_group_with_wrong_side_type(self, videogame_test):
- with pytest.raises(TypeError) as e:
- SquareGroup('protagonists', protagonist, 'foo')
- assert str(e.value) == 'the side of the square must be an integer'
- def test_square_group_with_side_out_of_range(self, videogame_test):
- with pytest.raises(ValueError) as e:
- SquareGroup('protagonists', protagonist, -1)
- assert str(e.value) == 'the side of the square must be greater than 1'
- def test_stairs_group(self, videogame_test):
- steps = 3
- sg = StairsGroup('protagonists', protagonist, steps)
- assert sg.amount == steps * (steps + 1) // 2
- def test_stairs_group_with_wrong_steps_type(self, videogame_test):
- with pytest.raises(TypeError) as e:
- StairsGroup('protagonists', protagonist, 'foo')
- assert str(e.value) == 'the steps of the stairs must be an integer'
- def test_stairs_group_with_steps_out_of_range(self, videogame_test):
- with pytest.raises(ValueError) as e:
- StairsGroup('protagonists', protagonist, -1)
- assert str(e.value) == 'the steps of the stairs must be greater than 1'
- def test_stairs_group_with_horizontal_and_vertical_flip_wrong_type(
- self, videogame_test
- ):
- with pytest.raises(TypeError) as e:
- StairsGroup('protagonists', protagonist, 3, 'foo', 'bar')
- assert (
- str(e.value)
- == 'both horizontal and vertical flips must be a boolean'
- )
|