test_character.py 5.4 KB

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