test_character.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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(prepare_videogame):
  33. with pytest.raises(TypeError):
  34. protagonist.move(5, 3, 1)
  35. def test_decrease_with_integer_argument(prepare_videogame):
  36. protagonist.decrease(2)
  37. def test_decrease_with_wrong_type(prepare_videogame):
  38. with pytest.raises(TypeError):
  39. protagonist.decrease('foo')
  40. def test_increase_with_integer_argument(prepare_videogame):
  41. protagonist.increase(2)
  42. def test_increase_with_wrong_type(prepare_videogame):
  43. with pytest.raises(TypeError):
  44. protagonist.increase('foo')
  45. def test_scale_with_wrong_factor_type(prepare_videogame):
  46. with pytest.raises(TypeError):
  47. protagonist.scale_to('foo')
  48. def test_scale_with_a_negative_factor(prepare_videogame):
  49. with pytest.raises(ValueError):
  50. protagonist.scale_to(-1)
  51. def test_set_height_with_wrong_type(prepare_videogame):
  52. with pytest.raises(TypeError):
  53. protagonist.set_height('foo')
  54. def test_set_height_with_a_negative_amount(prepare_videogame):
  55. with pytest.raises(ValueError):
  56. protagonist.set_height(-1)
  57. def test_set_keys_with_wrong_type(prepare_videogame):
  58. with pytest.raises(TypeError):
  59. protagonist.set_keys('foo')
  60. def test_set_position_with_wrong_types(prepare_videogame):
  61. with pytest.raises(TypeError):
  62. protagonist.set_position('foo', 'bar')
  63. def test_set_position_with_wrong_number_of_parameters(prepare_videogame):
  64. with pytest.raises(ValueError):
  65. protagonist.set_position((2, 3, 4))
  66. def test_set_width_with_wrong_type(prepare_videogame):
  67. with pytest.raises(TypeError):
  68. protagonist.set_width('a')
  69. def test_set_width_with_a_negative_amount(prepare_videogame):
  70. with pytest.raises(ValueError):
  71. protagonist.set_width(-1)
  72. def test_touch_with_wrong_characters_type(prepare_videogame):
  73. with pytest.raises(TypeError):
  74. protagonist.touch('foo')
  75. def test_touch_with_list_not_all_characters(prepare_videogame):
  76. with pytest.raises(ValueError):
  77. protagonist.touch([secondary, 'foo'])
  78. def test_touch_with_wrong_key_type(prepare_videogame):
  79. with pytest.raises(TypeError):
  80. protagonist.touch(secondary, -1)
  81. def test_touch_without_sprites_colliding(prepare_videogame):
  82. protagonist.move_to(0, 0)
  83. secondary.move_to(5000, 500)
  84. vg.update()
  85. assert protagonist.touch(secondary) == False
  86. def test_jump_with_force_as_integer(prepare_videogame):
  87. protagonist.jump(5)
  88. def test_jump_with_wrong_type(prepare_videogame):
  89. with pytest.raises(TypeError):
  90. protagonist.jump('foo')
  91. def test_jump_with_negative_force(prepare_videogame):
  92. with pytest.raises(ValueError):
  93. protagonist.jump(-5)
  94. def test_jump_counter_when_updating_jump(prepare_videogame):
  95. protagonist.jump()
  96. previous_jump_counter = protagonist._jump_counter
  97. protagonist._update_jump()
  98. assert protagonist._jump_counter == previous_jump_counter - 1
  99. def test_jump_counter_when_jump_finishes():
  100. protagonist.jump()
  101. while protagonist.is_jumping:
  102. protagonist._update_jump()
  103. assert protagonist._jump_counter == settings.DEFAULT_JUMP_FORCE
  104. def test_update_rect_position_with_wrong_traking_type(prepare_videogame):
  105. with pytest.raises(TypeError):
  106. protagonist._update_rect_position(vgscene, tracking='foo')
  107. def test_update_rect_position_with_no_scene(prepare_videogame):
  108. protagonist._update_rect_position(None)
  109. def test_update_rect_position_when_character_moved(prepare_videogame):
  110. protagonist.move_to(1000, 0)
  111. protagonist._update_rect_position(vgscene, tracking=True)
  112. protagonist.move_to(3000, 0)
  113. protagonist._update_rect_position(vgscene, tracking=True)