test_groups.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. # jogai - Python Game Library
  2. #
  3. # This library is free software; you can redistribute it and/or
  4. # modify it under the terms of the GNU Library General Public
  5. # License as published by the Free Software Foundation; either
  6. # version 2 of the License, or (at your option) any later version.
  7. #
  8. # This library is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. # Library General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU Library General Public
  14. # License along with this library; if not, write to the Free
  15. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. #
  17. # Ramón Palleiro Rodríguez
  18. # Zero! Factorial Studio
  19. # info@zero.gal
  20. import pytest
  21. from jogai import *
  22. class TestGroups:
  23. def test_wrong_type_group_name(self, videogame_test):
  24. with pytest.raises(TypeError) as e:
  25. Group(-1, [protagonist])
  26. assert str(e.value) == 'group name must be a string'
  27. def test_wrong_type_group_characters(videogame_test):
  28. with pytest.raises(TypeError) as e:
  29. Group('protagonists', 'foo')
  30. assert str(e.value) == 'characters in group must be a list'
  31. def test_group_backward(self, videogame_test, square_group_test):
  32. initial = square_group_test[0].x
  33. offset = 10
  34. square_group_test.backward(offset)
  35. assert square_group_test[0].x == initial - offset
  36. def test_group_decrease(self, videogame_test, square_group_test):
  37. initial = square_group_test[0].scale
  38. offset = 0.1
  39. square_group_test.decrease(offset)
  40. assert square_group_test[0].scale == max(0, initial - offset)
  41. def test_group_move_with_key(self, videogame_test, square_group_test):
  42. initial_x = square_group_test[0].x
  43. initial_y = square_group_test[0].y
  44. offset = 5
  45. square_group_test.move(offset, offset, key='s')
  46. assert square_group_test[0].x == initial_x
  47. assert square_group_test[0].y == initial_y
  48. def test_group_flip(self, videogame_test, square_group_test):
  49. initial = square_group_test[0].flipped
  50. square_group_test.flip(True, False)
  51. assert square_group_test[0].flipped == {
  52. 'horizontal': True,
  53. 'vertical': False,
  54. }
  55. def test_group_decrease(self, videogame_test, square_group_test):
  56. initial = square_group_test[0].scale
  57. offset = 0.1
  58. square_group_test.decrease(offset)
  59. assert square_group_test[0].scale == max(0, initial - offset)
  60. def test_group_set_attribute(self, videogame_test, square_group_test):
  61. new_height = 10
  62. square_group_test.set_height(new_height)
  63. assert square_group_test[0].height == new_height
  64. def test_group_contains(
  65. self, videogame_test, simple_group_test, group_character_test
  66. ):
  67. assert group_character_test in simple_group_test
  68. def test_group_iteration(
  69. self, videogame_test, simple_group_test, group_character_test
  70. ):
  71. group_iterator = iter(simple_group_test)
  72. assert next(group_iterator) == group_character_test
  73. def test_group_name_property(
  74. self, videogame_test, simple_group_test, group_name_test
  75. ):
  76. assert simple_group_test.name == group_name_test
  77. def test_group_characters_remove(
  78. self, videogame_test, simple_group_test, group_character_test
  79. ):
  80. simple_group_test.remove(group_character_test)
  81. assert simple_group_test.characters == []
  82. def test_random_group_with_amount_wrong_type(self, videogame_test):
  83. with pytest.raises(TypeError) as e:
  84. RandomGroup('protagonists', protagonist, 'foo', (0, 200), (0, 200))
  85. assert (
  86. str(e.value)
  87. == 'the amount of characters must be a integer greater than 1'
  88. )
  89. def test_random_group_with_amount_not_greater_than_1(self, videogame_test):
  90. with pytest.raises(ValueError) as e:
  91. RandomGroup('protagonists', protagonist, -1, (0, 200), (0, 200))
  92. assert (
  93. str(e.value)
  94. == 'the amount of characters must be a integer greater than 1'
  95. )
  96. def test_random_group_with_ranges_wrong_type(self, videogame_test):
  97. with pytest.raises(TypeError) as e:
  98. RandomGroup('protagonists', protagonist, 2, 'bar', 'foo')
  99. assert str(e.value) == 'both x and y ranges must be tuples'
  100. def test_random_group_with_wrong_length_ranges(self, videogame_test):
  101. with pytest.raises(ValueError) as e:
  102. RandomGroup('protagonists', protagonist, 3, (1, 2, 3), (1, 2, 3))
  103. assert (
  104. str(e.value)
  105. == 'both x and y ranges must have only two items (min, max)'
  106. )
  107. def test_random_group_with_min_greater_than_max_range(
  108. self, videogame_test
  109. ):
  110. with pytest.raises(ValueError) as e:
  111. RandomGroup('protagonists', protagonist, 3, (100, 3), (100, 3))
  112. assert str(e.value) == 'both x and y ranges must be sorted (min, max)'
  113. def test_random_group_empty_range(self, videogame_test):
  114. rg = RandomGroup('protagonists', protagonist, 3, (), ())
  115. assert rg.x_range == (0, current_scene.width - protagonist.width)
  116. assert rg.y_range == (0, current_scene.height - protagonist.height)
  117. def test_random_group_range_lists(self, videogame_test):
  118. rg = RandomGroup('protagonists', protagonist, 3, [0, 200], [0, 200])
  119. assert isinstance(rg.x_range, tuple)
  120. assert rg.x_range == (0, 200)
  121. assert isinstance(rg.y_range, tuple)
  122. assert rg.y_range == (0, 200)
  123. def test_rectangle_group_with_wrong_sides_type(self, videogame_test):
  124. with pytest.raises(TypeError) as e:
  125. RectangleGroup('protagonists', protagonist, 'foo', 'bar')
  126. assert str(e.value) == 'both the base and the height must be integers'
  127. def test_rectangle_group_with_sides_out_of_range(self, videogame_test):
  128. with pytest.raises(ValueError) as e:
  129. RectangleGroup('protagonists', protagonist, -1, 3)
  130. assert str(e.value) == 'both the base and the height must be positive'
  131. def test_rectangle_group_with_both_sides_equal_to_1(self, videogame_test):
  132. with pytest.raises(ValueError) as e:
  133. RectangleGroup('protagonists', protagonist, 1, 1)
  134. assert (
  135. str(e.value)
  136. == 'at least one of the dimensions of the rectangle must be greater than 1'
  137. )
  138. def test_square_group_with_wrong_side_type(self, videogame_test):
  139. with pytest.raises(TypeError) as e:
  140. SquareGroup('protagonists', protagonist, 'foo')
  141. assert str(e.value) == 'the side of the square must be an integer'
  142. def test_square_group_with_side_out_of_range(self, videogame_test):
  143. with pytest.raises(ValueError) as e:
  144. SquareGroup('protagonists', protagonist, -1)
  145. assert str(e.value) == 'the side of the square must be greater than 1'
  146. def test_stairs_group(self, videogame_test):
  147. steps = 3
  148. sg = StairsGroup('protagonists', protagonist, steps)
  149. assert sg.amount == steps * (steps + 1) // 2
  150. def test_stairs_group_with_wrong_steps_type(self, videogame_test):
  151. with pytest.raises(TypeError) as e:
  152. StairsGroup('protagonists', protagonist, 'foo')
  153. assert str(e.value) == 'the steps of the stairs must be an integer'
  154. def test_stairs_group_with_steps_out_of_range(self, videogame_test):
  155. with pytest.raises(ValueError) as e:
  156. StairsGroup('protagonists', protagonist, -1)
  157. assert str(e.value) == 'the steps of the stairs must be greater than 1'
  158. def test_stairs_group_with_horizontal_and_vertical_flip_wrong_type(
  159. self, videogame_test
  160. ):
  161. with pytest.raises(TypeError) as e:
  162. StairsGroup('protagonists', protagonist, 3, 'foo', 'bar')
  163. assert (
  164. str(e.value)
  165. == 'both horizontal and vertical flips must be a boolean'
  166. )