test_scene.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. from os.path import join
  21. import pytest
  22. from pygame import Surface
  23. from jogai import paths, settings
  24. from jogai.scene import Scene, _load_scene
  25. class TestScene:
  26. def test_load_scene_empty(self):
  27. sc = _load_scene({})
  28. assert sc.image_filename == ''
  29. assert sc.image == None
  30. assert sc.background == (0, 0, 0)
  31. def test_load_scene_with_wrong_type(self):
  32. with pytest.raises(TypeError) as e:
  33. sc = _load_scene(-1)
  34. assert (
  35. str(e.value)
  36. == 'the scene data must be a dict, a string, a list or a tuple'
  37. )
  38. def test_load_scene_with_background(self):
  39. background = (255, 255, 255)
  40. sc = _load_scene(background)
  41. assert sc.image_filename == ''
  42. assert sc.image == None
  43. assert sc.background == background
  44. assert sc.boundaries == [
  45. None,
  46. settings.WINDOW_WIDTH,
  47. settings.WINDOW_HEIGHT,
  48. None,
  49. ]
  50. def test_load_scene_with_background_and_boundaries(self):
  51. background = (255, 255, 255)
  52. sc = _load_scene({'background': background, 'boundaries': False})
  53. assert sc.image_filename == ''
  54. assert sc.image == None
  55. assert sc.background == background
  56. assert sc.boundaries == [None] * 4
  57. def test_scene_object_with_existing_filename(self, scene_filename_test):
  58. sc = Scene(scene_filename_test)
  59. assert isinstance(sc.image, Surface) == True
  60. def test_create_scene_with_wrong_type(self):
  61. with pytest.raises(TypeError) as e:
  62. sc = Scene(-1)
  63. assert str(e.value) == 'the image filename must be a string'
  64. def test_create_empty_scene(self):
  65. sc = Scene()
  66. assert sc.image_filename == ''
  67. assert sc.background == settings.SCENE_DEFAULT_BACKGROUND
  68. def test_scene_background_property(self, videogame_test):
  69. background = (255, 255, 255)
  70. current_scene.background = background
  71. assert current_scene.background == background
  72. def test_scene_boundaries_property(self, videogame_test):
  73. boundaries = [50, 100, 500, 300]
  74. current_scene.boundaries = boundaries
  75. assert current_scene.boundaries == boundaries
  76. def test_height_property(self, videogame_test):
  77. assert current_scene.height == current_scene.rect.height
  78. def test_image_filename_property(self, scene_filename_test):
  79. sc = Scene(scene_filename_test)
  80. sc.image_filename == scene_filename_test
  81. def test_image_path_property(self, scene_filename_test):
  82. sc = Scene(scene_filename_test)
  83. assert sc.image_path == join(paths.SCENES_DIR, scene_filename_test)
  84. def test_height_is_a_non_negative_integer(self, videogame_test):
  85. assert current_scene.height > -1
  86. def test_width_is_a_non_negative_integer(self, videogame_test):
  87. assert current_scene.width > -1
  88. def test_x_coordinate_is_an_integer(self, videogame_test):
  89. assert isinstance(current_scene.x, int)
  90. def test_change_background_color_with_a_list(self, videogame_test):
  91. current_scene.change_background([255, 255, 255])
  92. assert isinstance(current_scene.background, tuple) == True
  93. assert current_scene.background == (255, 255, 255)
  94. @pytest.mark.parametrize('color', [-1, 'foo'])
  95. def test_change_background_color_wrong_type(self, videogame_test, color):
  96. with pytest.raises(TypeError) as e:
  97. current_scene.change_background(color)
  98. assert (
  99. str(e.value)
  100. == 'the background color must be a tuple with three integers (r,g,b) between 0 and 255'
  101. )
  102. def test_change_background_color_with_components_out_of_range(
  103. self, videogame_test
  104. ):
  105. with pytest.raises(ValueError) as e:
  106. current_scene.change_background((-1, -1, 300))
  107. assert (
  108. str(e.value)
  109. == 'the background color must be a tuple with three integers (r,g,b) between 0 and 255'
  110. )
  111. def test_change_background_color_with_different_args_length(
  112. self, videogame_test
  113. ):
  114. with pytest.raises(ValueError) as e:
  115. current_scene.change_background((0, 0))
  116. assert (
  117. str(e.value)
  118. == 'the background color must be a tuple with three integers (r,g,b) between 0 and 255'
  119. )
  120. def test_set_abscissa_position_with_wrong_type(self, videogame_test):
  121. with pytest.raises(TypeError) as e:
  122. current_scene._set_abscissa_position('foo')
  123. assert str(e.value) == 'the x coordinate must be an integer'
  124. @pytest.mark.parametrize(
  125. 'x', [0, settings.WINDOW_WIDTH // 4, settings.WINDOW_WIDTH // 2]
  126. )
  127. def test_set_abscissa_position_less_or_equal_than_middle_of_window(
  128. self, videogame_test, x
  129. ):
  130. current_scene._set_abscissa_position(x)
  131. assert current_scene.x == 0
  132. def test_set_abscissa_position_between_middle_of_window_and_scene_width_minus_window_width(
  133. self, videogame_test
  134. ):
  135. x = current_scene.width - settings.WINDOW_WIDTH
  136. current_scene._set_abscissa_position(x)
  137. assert current_scene.x == -x + settings.WINDOW_WIDTH // 2
  138. def test_set_abscissa_position_at_the_end(self, videogame_test):
  139. current_scene._set_abscissa_position(current_scene.width)
  140. assert current_scene.x == -current_scene.width + settings.WINDOW_WIDTH
  141. @pytest.mark.parametrize('boundaries', [-1, 'foo'])
  142. def test_set_boundaries_with_wrong_type(self, videogame_test, boundaries):
  143. with pytest.raises(TypeError) as e:
  144. current_scene.set_boundaries(boundaries)
  145. assert (
  146. str(e.value) == 'the scene boundaries must be a list or a boolean'
  147. )