فهرست منبع

New tests added and bugs corrections.

mdo 1 سال پیش
والد
کامیت
309900a44c
7فایلهای تغییر یافته به همراه69 افزوده شده و 6 حذف شده
  1. 1 0
      data/no_characters.yaml
  2. 9 4
      jogai/character.py
  3. 2 1
      jogai/videogame.py
  4. 1 1
      scripts/basics.py
  5. 33 0
      tests/test_character.py
  6. 17 0
      tests/test_scene.py
  7. 6 0
      tests/test_videogame.py

+ 1 - 0
data/no_characters.yaml

@@ -0,0 +1 @@
+scene: test_large.png

+ 9 - 4
jogai/character.py

@@ -259,7 +259,7 @@ class Character(pygame.sprite.Sprite):
             >>> ch.decrease(0.1)
         """
         if isinstance(decrement, int):
-            increment = float(increment)
+            decrement = float(decrement)
 
         if not isinstance(decrement, float):
             raise TypeError(_('The decrement must be a float number.'))
@@ -285,7 +285,10 @@ class Character(pygame.sprite.Sprite):
             raise TypeError(_('The increment must be a float number.'))
 
         if not key or _get_keys()[utils.eval_key(key)]:
-            self.scale_to(self.scale_factor + increment)
+            if self.scale_factor + increment < 0:
+                self.scale_to(0)
+            else:
+                self.scale_to(self.scale_factor + increment)
 
     def scale_to(self, scale_factor: float = 1.0):
         """
@@ -304,9 +307,11 @@ class Character(pygame.sprite.Sprite):
             raise TypeError(
                 _('The scale factor must be a positive number (float or int).')
             )
-        if scale_factor <= 0:
+        if scale_factor < 0:
             raise ValueError(
-                _('The scale factor must be a positive number (float or int).')
+                _(
+                    'The scale factor must be a non-negative number (float or int).'
+                )
             )
 
         self._image = pygame.transform.scale(

+ 2 - 1
jogai/videogame.py

@@ -235,7 +235,8 @@ def update():
     """
     global _clock, _window, vg_scene, vg_characters
     if vg_scene:
-        vg_scene.set_position(vg_characters[0].x)
+        if vg_characters:
+            vg_scene.set_position(vg_characters[0].x)
         paint(vg_scene)
     if vg_characters:
         for pos, vg_character in enumerate(vg_characters):

+ 1 - 1
scripts/basics.py

@@ -5,7 +5,7 @@ def test_example01():
     def move():
         ...
 
-    prepare('02.yaml')
+    prepare('no_characters.yaml')
 
     while is_running():
         update()

+ 33 - 0
tests/test_character.py

@@ -36,6 +36,11 @@ def test_wrong_predefined_keys_type():
         ch = character.Character('protagonist.png', predefined_keys=[])
 
 
+def test_update_rect_position_with_wrong_type(prepare_videogame):
+    with pytest.raises(TypeError):
+        protagonist._update_rect_position(vgscene, 'foo')
+
+
 def test_move_with_wrong_coordinates(prepare_videogame):
     with pytest.raises(TypeError):
         protagonist.move('foo', 'bar')
@@ -46,6 +51,24 @@ def test_move_with_wrong_key(prepare_videogame):
         protagonist.move(5, 3, 1)
 
 
+def test_decrease_with_integer_argument(prepare_videogame):
+    protagonist.decrease(2)
+
+
+def test_decrease_with_wrong_type(prepare_videogame):
+    with pytest.raises(TypeError):
+        protagonist.decrease('foo')
+
+
+def test_increase_with_integer_argument(prepare_videogame):
+    protagonist.increase(2)
+
+
+def test_increase_with_wrong_type(prepare_videogame):
+    with pytest.raises(TypeError):
+        protagonist.increase('foo')
+
+
 def test_scale_with_wrong_factor_type(prepare_videogame):
     with pytest.raises(TypeError):
         protagonist.scale_to('foo')
@@ -66,11 +89,21 @@ def test_set_height_with_a_negative_amount(prepare_videogame):
         protagonist.set_height(-1)
 
 
+def test_set_keys_with_wrong_type(prepare_videogame):
+    with pytest.raises(TypeError):
+        protagonist.set_keys('foo')
+
+
 def test_set_position_with_wrong_types(prepare_videogame):
     with pytest.raises(TypeError):
         protagonist.set_position('foo', 'bar')
 
 
+def test_set_position_with_wrong_number_of_parameters(prepare_videogame):
+    with pytest.raises(ValueError):
+        protagonist.set_position((2, 3, 4))
+
+
 def test_set_width_with_wrong_type(prepare_videogame):
     with pytest.raises(TypeError):
         protagonist.set_width('a')

+ 17 - 0
tests/test_scene.py

@@ -5,6 +5,18 @@ import pytest
 from jogai import *
 
 
+def test_height_is_a_non_negative_integer(prepare_videogame):
+    assert vgscene.height > -1
+
+
+def test_width_is_a_non_negative_integer(prepare_videogame):
+    assert vgscene.width > -1
+
+
+def test_x_coordinate_is_an_integer(prepare_videogame):
+    assert isinstance(vgscene.x, int)
+
+
 def test_change_background_color_with_components_out_of_range():
     with pytest.raises(ValueError):
         sc = Scene()
@@ -26,3 +38,8 @@ def test_change_background_color_wrong_type():
 def test_create_scene_with_wrong_type():
     with pytest.raises(TypeError):
         sc = Scene(-1)
+
+
+def test_set_position_with_wrong_type(prepare_videogame):
+    with pytest.raises(TypeError):
+        vgscene.set_position('foo')

+ 6 - 0
tests/test_videogame.py

@@ -47,6 +47,12 @@ def test_prepare_02_yaml():
     vg.quit()
 
 
+def test_load_yaml_with_no_characters():
+    vg.init()
+    vg.prepare('no_characters.yaml')
+    vg.quit()
+
+
 def test_add_character_with_wrong_type():
     with pytest.raises(TypeError):
         vg.add_character('foo')