test_character.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 pygame
  21. import pytest
  22. from jogai import character, settings
  23. from jogai import videogame as vg
  24. from jogai.translations import gettext as _
  25. def test_load_data_with_none_argument(prepare_videogame):
  26. protagonist._load_data(None)
  27. def test_load_data_with_wrong_type(prepare_videogame):
  28. with pytest.raises(TypeError):
  29. protagonist._load_data('foo')
  30. def test_height_is_a_non_negative_integer(prepare_videogame):
  31. assert protagonist.height > -1
  32. def test_width_is_a_non_negative_integer(prepare_videogame):
  33. assert protagonist.width > -1
  34. def test_x_coordinate_is_an_integer(prepare_videogame):
  35. assert isinstance(protagonist.x, int)
  36. def test_y_coordinate_is_an_integer(prepare_videogame):
  37. assert isinstance(protagonist.y, int)
  38. def test_wrong_character_name():
  39. with pytest.raises(TypeError):
  40. protagonist = character.Character('protagonist.png', name=1)
  41. def test_repr_character(prepare_videogame):
  42. print(protagonist.__repr__)
  43. def test_wrong_predefined_keys_type():
  44. with pytest.raises(TypeError):
  45. protagonist = character.Character(
  46. 'protagonist.png', predefined_keys=[]
  47. )
  48. def test_move_with_wrong_coordinates(prepare_videogame):
  49. with pytest.raises(TypeError):
  50. protagonist.move('foo', 'bar')
  51. def test_move_with_wrong_key_type(prepare_videogame):
  52. with pytest.raises(TypeError):
  53. protagonist.move(5, 3, key=-1)
  54. def test_rotate_with_wrong_type(prepare_videogame):
  55. with pytest.raises(TypeError):
  56. protagonist.rotate('foo')
  57. def test_decrease_with_integer_argument(prepare_videogame):
  58. protagonist.decrease(2)
  59. def test_decrease_with_wrong_type(prepare_videogame):
  60. with pytest.raises(TypeError):
  61. protagonist.decrease('foo')
  62. def test_increase_with_integer_argument(prepare_videogame):
  63. protagonist.increase(2)
  64. def test_increase_with_wrong_type(prepare_videogame):
  65. with pytest.raises(TypeError):
  66. protagonist.increase('foo')
  67. def test_scale_with_wrong_factor_type(prepare_videogame):
  68. with pytest.raises(TypeError):
  69. protagonist.scale_to('foo')
  70. def test_scale_with_a_negative_factor(prepare_videogame):
  71. with pytest.raises(ValueError):
  72. protagonist.scale_to(-1)
  73. def test_flip_with_wrong_type(prepare_videogame):
  74. with pytest.raises(TypeError):
  75. protagonist.flip('foo', 'bar')
  76. def test_set_angle_with_wrong_type(prepare_videogame):
  77. with pytest.raises(TypeError):
  78. protagonist.set_angle('foo')
  79. def test_set_height_with_wrong_type(prepare_videogame):
  80. with pytest.raises(TypeError):
  81. protagonist.set_height('foo')
  82. def test_set_height_with_a_negative_amount(prepare_videogame):
  83. with pytest.raises(ValueError):
  84. protagonist.set_height(-1)
  85. def test_set_keys_with_wrong_type(prepare_videogame):
  86. with pytest.raises(TypeError):
  87. protagonist.set_keys('foo')
  88. def test_set_position_with_wrong_types(prepare_videogame):
  89. with pytest.raises(TypeError):
  90. protagonist.set_position('foo', 'bar')
  91. def test_set_position_with_wrong_number_of_parameters(prepare_videogame):
  92. with pytest.raises(ValueError):
  93. protagonist.set_position((2, 3, 4))
  94. def test_set_width_with_wrong_type(prepare_videogame):
  95. with pytest.raises(TypeError):
  96. protagonist.set_width('a')
  97. def test_set_width_with_a_negative_amount(prepare_videogame):
  98. with pytest.raises(ValueError):
  99. protagonist.set_width(-1)
  100. def test_touch_with_wrong_characters_type(prepare_videogame):
  101. with pytest.raises(TypeError):
  102. protagonist.touch('foo')
  103. def test_touch_with_list_not_all_characters(prepare_videogame):
  104. with pytest.raises(ValueError):
  105. protagonist.touch([secondary, 'foo'])
  106. def test_touch_with_wrong_key_type(prepare_videogame):
  107. with pytest.raises(TypeError):
  108. protagonist.touch(secondary, key=-1)
  109. def test_touch_without_sprites_colliding(prepare_videogame):
  110. protagonist.move_to(0, 0)
  111. secondary.move_to(5000, 500)
  112. vg.update()
  113. assert protagonist.touch(secondary) == ''
  114. def test_jump_with_force_as_integer(prepare_videogame):
  115. protagonist.jump(5)
  116. def test_jump_with_wrong_force_type(prepare_videogame):
  117. with pytest.raises(TypeError):
  118. protagonist.jump('foo')
  119. def test_jump_with_negative_force(prepare_videogame):
  120. with pytest.raises(ValueError):
  121. protagonist.jump(-5)
  122. def test_jump_with_wrong_key_type(prepare_videogame):
  123. with pytest.raises(TypeError):
  124. protagonist.jump(5, key=-1)
  125. def test_jump_counter_when_updating_jump(prepare_videogame):
  126. protagonist.jump()
  127. previous_jump_counter = protagonist._jump_counter
  128. protagonist._update_jump()
  129. assert protagonist._jump_counter == previous_jump_counter - 1
  130. def test_jump_counter_when_jump_finishes():
  131. protagonist.jump()
  132. while protagonist.is_jumping:
  133. protagonist._update_jump()
  134. assert protagonist._jump_counter == settings.DEFAULT_JUMP_FORCE
  135. def test_update_rect_position_with_wrong_traking_type(prepare_videogame):
  136. with pytest.raises(TypeError):
  137. protagonist._update_rect_position(tracking='foo')
  138. def test_update_rect_position_when_character_moved(prepare_videogame):
  139. protagonist.move_to(1000, 0)
  140. protagonist._update_rect_position(tracking=True)
  141. protagonist.move_to(3000, 0)
  142. protagonist._update_rect_position(tracking=True)