Repository: MyreMylar/pygame_gui
Pull request #292 (2022-08-05 11:25:28)

Further coverage elements improve
As title


Patch:
diff --git a/pygame_gui/elements/ui_selection_list.py b/pygame_gui/elements/ui_selection_list.py
index f47d9dd..4a43638 100644
--- a/pygame_gui/elements/ui_selection_list.py
+++ b/pygame_gui/elements/ui_selection_list.py
@@ -106,7 +106,7 @@ class UISelectionList(UIElement):
         self.rebuild_from_changed_theme_data()
 
         if self._default_selection is not None:
-            self.set_default_selection()
+            self._set_default_selection()
 
     def add_items(self, new_items: Union[List[str], List[Tuple[str, str]]]) -> None:
         """
@@ -327,7 +327,7 @@ class UISelectionList(UIElement):
             else:
                 break
 
-    def set_default_selection(self):
+    def _set_default_selection(self):
         """
         Set the default selection of the list. The default selection type must be a string or (str,
         str) tuple for single selection lists. For multi-selection lists, they can be a single


Test patch:
diff --git a/tests/data/themes/ui_status_bar_no_default.json b/tests/data/themes/ui_status_bar_non_default.json
similarity index 83%
rename from tests/data/themes/ui_status_bar_no_default.json
rename to tests/data/themes/ui_status_bar_non_default.json
index ec454c1..b5156a2 100644
--- a/tests/data/themes/ui_status_bar_no_default.json
+++ b/tests/data/themes/ui_status_bar_non_default.json
@@ -13,7 +13,8 @@
              "shape_corner_radius": "6",
              "hover_height": "5",
              "border_width": "0",
-             "shadow_width": "3"
+             "shadow_width": "3",
+             "follow_sprite_offset": "0, 32"
          }
     }
 }
\ No newline at end of file
diff --git a/tests/test_core/test_text/test_text_effects.py b/tests/test_core/test_text/test_text_effects.py
index a721c46..31592f4 100644
--- a/tests/test_core/test_text/test_text_effects.py
+++ b/tests/test_core/test_text/test_text_effects.py
@@ -31,22 +31,22 @@ class TestTypingAppearEffect:
         text_box = UITextBox('hello <font color=#FF0000>this is a</font> test',
                              pygame.Rect((10, 10), (200, 100)),
                              default_ui_manager)
-        typing_effect = TypingAppearEffect(text_owner=text_box)
+        text_box.set_active_effect(TEXT_EFFECT_TYPING_APPEAR)
 
-        assert typing_effect.text_progress == 0
+        assert text_box.active_text_effect.text_progress == 0
 
-        typing_effect.update(time_delta=0.06)
-        typing_effect.update(time_delta=0.06)
-        typing_effect.update(time_delta=0.06)
-        typing_effect.update(time_delta=0.06)
+        text_box.active_text_effect.update(time_delta=0.06)
+        text_box.active_text_effect.update(time_delta=0.06)
+        text_box.active_text_effect.update(time_delta=0.06)
+        text_box.active_text_effect.update(time_delta=0.06)
 
-        assert typing_effect.text_progress == 2
+        assert text_box.active_text_effect.text_progress == 2
 
         label = UILabel(pygame.Rect((10, 10), (200, 100)), 'Hell',
                         default_ui_manager)
-        label_typing_effect = TypingAppearEffect(text_owner=label)
+        label.set_active_effect(TEXT_EFFECT_TYPING_APPEAR)
 
-        assert label_typing_effect.text_progress == 0
+        assert label.active_text_effect.text_progress == 0
 
         label.update(time_delta=0.06)
         label.update(time_delta=0.06)
@@ -57,7 +57,11 @@ class TestTypingAppearEffect:
         label.update(time_delta=0.06)
         label.update(time_delta=0.06)
 
-        assert label_typing_effect.text_progress == 0
+        assert label.active_text_effect.text_progress == 4
+
+        label.stop_finished_effect()
+
+        assert label.active_text_effect is None
 
     def test_has_text_changed(self, _init_pygame, default_ui_manager: UIManager):
         text_box = UITextBox('hello <font color=#FF0000>this is a</font> test',
diff --git a/tests/test_elements/test_ui_horizontal_slider.py b/tests/test_elements/test_ui_horizontal_slider.py
index b7c8452..9edb3fb 100644
--- a/tests/test_elements/test_ui_horizontal_slider.py
+++ b/tests/test_elements/test_ui_horizontal_slider.py
@@ -79,9 +79,13 @@ class TestUIHorizontalSlider:
         # scroll down a bit then up again to exercise update
         slider.get_current_value()  # Clear has moved this turn
         slider.left_button.held = True
+        slider.update(0.01)
+        slider.update(0.01)
         slider.update(0.5)
         slider.left_button.held = False
         slider.right_button.held = True
+        slider.update(0.01)
+        slider.update(0.01)
         slider.update(0.5)
 
         assert slider.has_moved_recently is True
diff --git a/tests/test_elements/test_ui_progress_bar.py b/tests/test_elements/test_ui_progress_bar.py
new file mode 100644
index 0000000..9ed5fed
--- /dev/null
+++ b/tests/test_elements/test_ui_progress_bar.py
@@ -0,0 +1,43 @@
+import os
+import pytest
+import pygame
+
+from tests.shared_comparators import compare_surfaces
+
+from pygame_gui.ui_manager import UIManager
+from pygame_gui.elements.ui_progress_bar import UIProgressBar
+
+
+class TestUIProgressBar:
+
+    def test_creation(self, _init_pygame, default_ui_manager):
+        progress_bar = UIProgressBar(relative_rect=pygame.Rect(100, 100, 150, 30),
+                                     manager=default_ui_manager)
+        assert progress_bar.image is not None
+
+    def test_update(self, _init_pygame, default_ui_manager):
+        progress_bar = UIProgressBar(relative_rect=pygame.Rect(100, 100, 150, 30),
+                                     manager=default_ui_manager)
+        progress_bar.current_progress = 50.0
+        progress_bar.update(0.01)
+        assert progress_bar.image is not None and progress_bar.progress_percentage == 0.5
+
+    def test_set_current_progress(self, _init_pygame, default_ui_manager):
+        progress_bar = UIProgressBar(relative_rect=pygame.Rect(100, 100, 150, 30),
+                                     manager=default_ui_manager)
+
+        progress_bar.set_current_progress(75)
+
+        assert progress_bar.progress_percentage == 0.75
+
+    def test_status_text(self, _init_pygame, default_ui_manager):
+        progress_bar = UIProgressBar(relative_rect=pygame.Rect(100, 100, 150, 30),
+                                     manager=default_ui_manager)
+
+        progress_bar.set_current_progress(75)
+
+        assert progress_bar.status_text() == "75.0/100.0"
+
+
+if __name__ == '__main__':
+    pytest.console_main()
diff --git a/tests/test_elements/test_ui_selection_list.py b/tests/test_elements/test_ui_selection_list.py
index b597f66..1cf9608 100644
--- a/tests/test_elements/test_ui_selection_list.py
+++ b/tests/test_elements/test_ui_selection_list.py
@@ -19,6 +19,14 @@ class TestUISelectionList:
                         item_list=['green', 'eggs', 'and', 'ham'],
                         manager=default_ui_manager)
 
+    def test_rebuild(self, _init_pygame, default_ui_manager,
+                     _display_surface_return_none):
+        list = UISelectionList(relative_rect=pygame.Rect(50, 50, 150, 400),
+                               item_list=['green', 'eggs', 'and', 'ham'],
+                               manager=default_ui_manager)
+
+        list.rebuild()
+
     def test_addition(self, _init_pygame, default_ui_manager,
                       _display_surface_return_none):
         selection_list = UISelectionList(relative_rect=pygame.Rect(50, 50, 150, 400),
@@ -150,6 +158,9 @@ class TestUISelectionList:
                          if item['button_element'] is not None]
         assert visible_items == ['another item 1', 'another item 2']
 
+        with pytest.raises(ValueError):
+            selection_list.set_item_list([1, 2, 3])
+
     def test_process_event(self, _init_pygame, default_ui_manager,
                            _display_surface_return_none: None):
         selection_list = UISelectionList(relative_rect=pygame.Rect(50, 50, 150, 80),
@@ -917,6 +928,18 @@ class TestUISelectionList:
 
         assert selection == multi_list.get_multi_selection()
 
+        selection = [("Item 2", "#item_2"), ("Item 3", "#item_3")]
+        # tuple list
+        multi_list = UISelectionList(
+            relative_rect=pygame.Rect(100, 100, 400, 400),
+            item_list=[("Item 1", "#item_1"), ("Item 2", "#item_2"), ("Item 3", "#item_3")],
+            default_selection=selection,
+            allow_multi_select=True,
+            manager=manager
+        )
+
+        assert multi_list.get_multi_selection() == ["Item 2", "Item 3"]
+
     def test_default_selection_changes(
         self,
         _init_pygame,
@@ -971,6 +994,26 @@ class TestUISelectionList:
         assert lst[15] in final_vals
         assert lst[17] in final_vals
 
+    def test_set_default_selection(self, _init_pygame, default_ui_manager,
+                                   _display_surface_return_none):
+        selection_list = UISelectionList(relative_rect=pygame.Rect(50, 50, 150, 400),
+                                         item_list=['green', 'eggs', 'and', 'ham'],
+                                         manager=default_ui_manager,
+                                         allow_multi_select=True)
+
+        assert selection_list.get_multi_selection() == []
+
+        event_data = {'ui_element': selection_list.item_list_container.elements[0]}
+        press_list_item_event = pygame.event.Event(pygame_gui.UI_BUTTON_PRESSED, event_data)
+        default_ui_manager.process_events(press_list_item_event)
+
+        assert selection_list.get_multi_selection() == ['green']
+
+        selection_list._default_selection = ['eggs']
+        selection_list._set_default_selection()
+
+        assert selection_list.get_multi_selection() == ['green']
+
 
 if __name__ == '__main__':
     pytest.console_main()
diff --git a/tests/test_elements/test_ui_status_bar.py b/tests/test_elements/test_ui_status_bar.py
index 7a9f0fa..9e03204 100644
--- a/tests/test_elements/test_ui_status_bar.py
+++ b/tests/test_elements/test_ui_status_bar.py
@@ -37,8 +37,12 @@ class TestUIStatusBar:
         health_bar.update(0.01)
         assert health_bar.image is not None
 
+        health_bar.percent_full = 50
+
+        assert health_bar.percent_full == 0.5
+
     def test_rebuild_from_theme_data_non_default(self, _init_pygame):
-        manager = UIManager((800, 600), os.path.join("tests", "data", "themes", "ui_status_bar_no_default.json"))
+        manager = UIManager((800, 600), os.path.join("tests", "data", "themes", "ui_status_bar_non_default.json"))
         healthy_sprite = HealthySprite()
         health_bar = UIStatusBar(relative_rect=pygame.Rect(100, 100, 150, 30),
                                  sprite=healthy_sprite,
@@ -65,6 +69,11 @@ class TestUIStatusBar:
 
         assert health_bar.rect.topleft == (150, 30)
 
+        other_bar = UIStatusBar(relative_rect=pygame.Rect(100, 100, 150, 30),
+                                manager=default_ui_manager)
+
+        assert other_bar.position == (100, 100)
+
     def test_set_relative_position(self, _init_pygame, default_ui_manager):
         healthy_sprite = HealthySprite()
         health_bar = UIStatusBar(relative_rect=pygame.Rect(100, 100, 150, 30),