Repository: MyreMylar/pygame_gui
Pull request #305 (2022-08-09 09:00:33)

Improve test coverage in core module
Like the other modules this will be done in batches trying to gradually improve testing coverage.

As in this first commit, improving core coverage may involve beefing up tests for other modules at the user facing level.


Patch:
diff --git a/pygame_gui/core/ui_element.py b/pygame_gui/core/ui_element.py
index 2615b6d..e066b57 100644
--- a/pygame_gui/core/ui_element.py
+++ b/pygame_gui/core/ui_element.py
@@ -47,7 +47,7 @@ class UIElement(GUISprite, IUIElementInterface):
                  *,
                  starting_height: int,
                  layer_thickness: int,
-                 anchors: Dict[str, str] = None,
+                 anchors: Dict[str, Union[str, 'UIElement']] = None,
                  visible: int = 1):
 
         self._layer = 0
diff --git a/pygame_gui/core/ui_font_dictionary.py b/pygame_gui/core/ui_font_dictionary.py
index bbb9eb0..a40cc55 100644
--- a/pygame_gui/core/ui_font_dictionary.py
+++ b/pygame_gui/core/ui_font_dictionary.py
@@ -153,8 +153,8 @@ class UIFontDictionary(IUIFontDictionaryInterface):
                 False))
 
         error = default_font_res.load()
-        if error is not None:
-            warnings.warn(str(error))
+        if error is not None and isinstance(error, Exception):
+            raise error
         self.loaded_fonts[self.default_font.idx] = default_font_res
 
         self.known_font_paths[self.default_font.name] = [
diff --git a/pygame_gui/elements/ui_button.py b/pygame_gui/elements/ui_button.py
index ad5fba5..c434d74 100644
--- a/pygame_gui/elements/ui_button.py
+++ b/pygame_gui/elements/ui_button.py
@@ -48,7 +48,7 @@ class UIButton(UIElement):
                  starting_height: int = 1,
                  parent_element: UIElement = None,
                  object_id: Union[ObjectID, str, None] = None,
-                 anchors: Dict[str, str] = None,
+                 anchors: Dict[str, Union[str, UIElement]] = None,
                  allow_double_clicks: bool = False,
                  generate_click_events_from: Iterable[int] = frozenset([pygame.BUTTON_LEFT]),
                  visible: int = 1
diff --git a/pygame_gui/elements/ui_drop_down_menu.py b/pygame_gui/elements/ui_drop_down_menu.py
index a4e0ad4..36e01cd 100644
--- a/pygame_gui/elements/ui_drop_down_menu.py
+++ b/pygame_gui/elements/ui_drop_down_menu.py
@@ -633,7 +633,7 @@ class UIDropDownMenu(UIContainer):
                  parent_element: UIElement = None,
                  object_id: Union[ObjectID, str, None] = None,
                  expansion_height_limit: Union[int, None] = None,
-                 anchors: Dict[str, str] = None,
+                 anchors: Dict[str, Union[str, UIElement]] = None,
                  visible: int = 1
                  ):
 
diff --git a/pygame_gui/elements/ui_horizontal_scroll_bar.py b/pygame_gui/elements/ui_horizontal_scroll_bar.py
index 7c9f554..1884d3a 100644
--- a/pygame_gui/elements/ui_horizontal_scroll_bar.py
+++ b/pygame_gui/elements/ui_horizontal_scroll_bar.py
@@ -35,7 +35,7 @@ class UIHorizontalScrollBar(UIElement):
                  container: Union[IContainerLikeInterface, None] = None,
                  parent_element: UIElement = None,
                  object_id: Union[ObjectID, str, None] = None,
-                 anchors: Dict[str, str] = None,
+                 anchors: Dict[str, Union[str, UIElement]] = None,
                  visible: int = 1):
 
         super().__init__(relative_rect, manager, container,
diff --git a/pygame_gui/elements/ui_horizontal_slider.py b/pygame_gui/elements/ui_horizontal_slider.py
index d7eb2f6..787b1e7 100644
--- a/pygame_gui/elements/ui_horizontal_slider.py
+++ b/pygame_gui/elements/ui_horizontal_slider.py
@@ -39,7 +39,7 @@ class UIHorizontalSlider(UIElement):
                  container: Union[IContainerLikeInterface, None] = None,
                  parent_element: UIElement = None,
                  object_id: Union[ObjectID, str, None] = None,
-                 anchors: Dict[str, str] = None,
+                 anchors: Dict[str, Union[str, UIElement]] = None,
                  visible: int = 1,
                  click_increment: Union[float, int] = 1
                  ):
diff --git a/pygame_gui/elements/ui_image.py b/pygame_gui/elements/ui_image.py
index 1ffacff..e069335 100644
--- a/pygame_gui/elements/ui_image.py
+++ b/pygame_gui/elements/ui_image.py
@@ -33,7 +33,7 @@ class UIImage(UIElement):
                  container: Union[IContainerLikeInterface, None] = None,
                  parent_element: UIElement = None,
                  object_id: Union[ObjectID, str, None] = None,
-                 anchors: Dict[str, str] = None,
+                 anchors: Dict[str, Union[str, UIElement]] = None,
                  visible: int = 1):
 
         super().__init__(relative_rect, manager, container,
diff --git a/pygame_gui/elements/ui_label.py b/pygame_gui/elements/ui_label.py
index 72d4eb2..6a9cf68 100644
--- a/pygame_gui/elements/ui_label.py
+++ b/pygame_gui/elements/ui_label.py
@@ -40,7 +40,7 @@ class UILabel(UIElement, IUITextOwnerInterface):
                  container: Union[IContainerLikeInterface, None] = None,
                  parent_element: UIElement = None,
                  object_id: Union[ObjectID, str, None] = None,
-                 anchors: Dict[str, str] = None,
+                 anchors: Dict[str, Union[str, UIElement]] = None,
                  visible: int = 1,
                  **text_kwargs: str):
 
diff --git a/pygame_gui/elements/ui_panel.py b/pygame_gui/elements/ui_panel.py
index 2252c2c..b5c6bae 100644
--- a/pygame_gui/elements/ui_panel.py
+++ b/pygame_gui/elements/ui_panel.py
@@ -49,7 +49,7 @@ class UIPanel(UIElement, IContainerLikeInterface):
                  container: Union[IContainerLikeInterface, None] = None,
                  parent_element: UIElement = None,
                  object_id: Union[ObjectID, str, None] = None,
-                 anchors: Dict[str, str] = None,
+                 anchors: Dict[str, Union[str, UIElement]] = None,
                  visible: int = 1
                  ):
 
diff --git a/pygame_gui/elements/ui_progress_bar.py b/pygame_gui/elements/ui_progress_bar.py
index 5398cb9..a0bf313 100644
--- a/pygame_gui/elements/ui_progress_bar.py
+++ b/pygame_gui/elements/ui_progress_bar.py
@@ -30,7 +30,7 @@ class UIProgressBar(UIStatusBar):
                  container: Union[IContainerLikeInterface, None] = None,
                  parent_element: UIElement = None,
                  object_id: Union[ObjectID, str, None] = None,
-                 anchors: Dict[str, str] = None,
+                 anchors: Dict[str, Union[str, UIElement]] = None,
                  visible: int = 1):
 
         self.current_progress = 0.0
diff --git a/pygame_gui/elements/ui_screen_space_health_bar.py b/pygame_gui/elements/ui_screen_space_health_bar.py
index 31e8b73..17e098a 100644
--- a/pygame_gui/elements/ui_screen_space_health_bar.py
+++ b/pygame_gui/elements/ui_screen_space_health_bar.py
@@ -33,7 +33,7 @@ class UIScreenSpaceHealthBar(UIStatusBar):
                  container: Union[IContainerLikeInterface, None] = None,
                  parent_element: UIElement = None,
                  object_id: Union[ObjectID, str, None] = None,
-                 anchors: Dict[str, str] = None,
+                 anchors: Dict[str, Union[str, UIElement]] = None,
                  visible: int = 1):
 
         # Setting this here because UIProgressBar doesn't accept a percent_method in __init__.
diff --git a/pygame_gui/elements/ui_selection_list.py b/pygame_gui/elements/ui_selection_list.py
index d075ef6..25c3407 100644
--- a/pygame_gui/elements/ui_selection_list.py
+++ b/pygame_gui/elements/ui_selection_list.py
@@ -57,7 +57,7 @@ class UISelectionList(UIElement):
                  starting_height: int = 1,
                  parent_element: UIElement = None,
                  object_id: Union[ObjectID, str, None] = None,
-                 anchors: Dict[str, str] = None,
+                 anchors: Dict[str, Union[str, UIElement]] = None,
                  visible: int = 1,
                  default_selection: Union[
                     str, Tuple[str, str],               # Single-selection lists
diff --git a/pygame_gui/elements/ui_status_bar.py b/pygame_gui/elements/ui_status_bar.py
index 8b3cefa..5b79abe 100644
--- a/pygame_gui/elements/ui_status_bar.py
+++ b/pygame_gui/elements/ui_status_bar.py
@@ -49,7 +49,7 @@ class UIStatusBar(UIElement):
                  container: Union[IContainerLikeInterface, None] = None,
                  parent_element: UIElement = None,
                  object_id: Union[ObjectID, str, None] = None,
-                 anchors: Dict[str, str] = None,
+                 anchors: Dict[str, Union[str, UIElement]] = None,
                  visible: int = 1):
 
         super().__init__(relative_rect, manager, container,
diff --git a/pygame_gui/elements/ui_text_box.py b/pygame_gui/elements/ui_text_box.py
index 41ee891..a0ab497 100644
--- a/pygame_gui/elements/ui_text_box.py
+++ b/pygame_gui/elements/ui_text_box.py
@@ -77,7 +77,7 @@ class UITextBox(UIElement, IUITextOwnerInterface):
                  container: Union[IContainerLikeInterface, None] = None,
                  parent_element: UIElement = None,
                  object_id: Union[ObjectID, str, None] = None,
-                 anchors: Dict[str, str] = None,
+                 anchors: Dict[str, Union[str, UIElement]] = None,
                  visible: int = 1):
 
         super().__init__(relative_rect, manager, container,
diff --git a/pygame_gui/elements/ui_text_entry_line.py b/pygame_gui/elements/ui_text_entry_line.py
index f318c0b..bd0b78c 100644
--- a/pygame_gui/elements/ui_text_entry_line.py
+++ b/pygame_gui/elements/ui_text_entry_line.py
@@ -67,7 +67,7 @@ class UITextEntryLine(UIElement):
                  container: Union[IContainerLikeInterface, None] = None,
                  parent_element: UIElement = None,
                  object_id: Union[ObjectID, str, None] = None,
-                 anchors: Dict[str, str] = None,
+                 anchors: Dict[str, Union[str, UIElement]] = None,
                  visible: int = 1):
 
         super().__init__(relative_rect, manager, container,
diff --git a/pygame_gui/elements/ui_tool_tip.py b/pygame_gui/elements/ui_tool_tip.py
index 5aa92e2..f72d645 100644
--- a/pygame_gui/elements/ui_tool_tip.py
+++ b/pygame_gui/elements/ui_tool_tip.py
@@ -37,7 +37,7 @@ class UITooltip(UIElement, IUITooltipInterface):
                  manager: IUIManagerInterface,
                  parent_element: UIElement = None,
                  object_id: Union[ObjectID, str, None] = None,
-                 anchors: Dict[str, str] = None):
+                 anchors: Dict[str, Union[str, UIElement]] = None):
 
         super().__init__(relative_rect=pygame.Rect((0, 0), (-1, -1)),
                          manager=manager,
diff --git a/pygame_gui/elements/ui_vertical_scroll_bar.py b/pygame_gui/elements/ui_vertical_scroll_bar.py
index cffcf30..024ff9f 100644
--- a/pygame_gui/elements/ui_vertical_scroll_bar.py
+++ b/pygame_gui/elements/ui_vertical_scroll_bar.py
@@ -35,7 +35,7 @@ class UIVerticalScrollBar(UIElement):
                  container: Union[IContainerLikeInterface, None] = None,
                  parent_element: UIElement = None,
                  object_id: Union[ObjectID, str, None] = None,
-                 anchors: Dict[str, str] = None,
+                 anchors: Dict[str, Union[str, UIElement]] = None,
                  visible: int = 1):
 
         super().__init__(relative_rect, manager, container,
diff --git a/pygame_gui/elements/ui_world_space_health_bar.py b/pygame_gui/elements/ui_world_space_health_bar.py
index 0659545..b11e510 100644
--- a/pygame_gui/elements/ui_world_space_health_bar.py
+++ b/pygame_gui/elements/ui_world_space_health_bar.py
@@ -50,7 +50,7 @@ class UIWorldSpaceHealthBar(UIStatusBar):
                  container: Union[IContainerLikeInterface, None] = None,
                  parent_element: UIElement = None,
                  object_id: Union[ObjectID, str, None] = None,
-                 anchors: Dict[str, str] = None,
+                 anchors: Dict[str, Union[str, UIElement]] = None,
                  visible: int = 1):
 
         if sprite_to_monitor is not None:


Test patch:
diff --git a/tests/test_core/test_surface_cache.py b/tests/test_core/test_surface_cache.py
index 9c132dd..c793a22 100644
--- a/tests/test_core/test_surface_cache.py
+++ b/tests/test_core/test_surface_cache.py
@@ -102,6 +102,9 @@ class TestSurfaceCache:
 
         assert cache.find_surface_in_cache('doop') is None
 
+        # remove item not in cache
+        cache.remove_user_and_request_clean_up_of_cached_item('Blep')
+
     def test_build_cache_id(self, _init_pygame):
 
         cache_id = SurfaceCache.build_cache_id(shape='rectangle',
@@ -114,6 +117,13 @@ class TestSurfaceCache:
 
         assert cache_id == 'rectangle_64_64_1_1_5_255_0_0_255_100_200_100_180'
 
+    def test_expand_lt_cache(self, _init_pygame):
+        cache = SurfaceCache()
+        assert not cache.low_on_space
+        cache.cache_surfaces = [1, 2, 3, 4, 5]
+        cache._expand_lt_cache()
+        assert cache.low_on_space
+
 
 if __name__ == '__main__':
     pytest.console_main()
diff --git a/tests/test_core/test_ui_font_dictionary.py b/tests/test_core/test_ui_font_dictionary.py
index 4bb929f..9a1a007 100644
--- a/tests/test_core/test_ui_font_dictionary.py
+++ b/tests/test_core/test_ui_font_dictionary.py
@@ -1,7 +1,7 @@
 import pytest
 
 from pygame_gui.core import BlockingThreadedResourceLoader
-from pygame_gui.core.ui_font_dictionary import UIFontDictionary
+from pygame_gui.core.ui_font_dictionary import UIFontDictionary, DefaultFontData
 
 
 class TestUIFontDictionary:
@@ -18,6 +18,20 @@ class TestUIFontDictionary:
 
         assert font_dictionary.loaded_fonts is not None
 
+    def test_load_bad_default_font(self, _init_pygame):
+        font_dictionary = UIFontDictionary(BlockingThreadedResourceLoader(), locale='derp')
+        bad_font = DefaultFontData(14,
+                                   'bad-font',
+                                   'regular',
+                                   'not_a_real_font.ttf',
+                                   'not_a_real_font.ttf',
+                                   'not_a_real_font.ttf',
+                                   'not_a_real_font.ttf')
+        font_dictionary.default_font_dictionary['zzap'] = bad_font
+
+        with pytest.raises(FileNotFoundError):
+            font_dictionary.set_locale('zzap')
+
     def test_find_font_unloaded_size(self, _init_pygame):
         font_dictionary = UIFontDictionary(BlockingThreadedResourceLoader(), locale='en')
 
diff --git a/tests/test_elements/test_ui_button.py b/tests/test_elements/test_ui_button.py
index b7830ee..0884882 100644
--- a/tests/test_elements/test_ui_button.py
+++ b/tests/test_elements/test_ui_button.py
@@ -216,11 +216,26 @@ class TestUIButton:
                           container=test_container,
                           manager=default_ui_manager)
 
+        button_2 = UIButton(relative_rect=pygame.Rect(200, 0, 50, 30),
+                            text="Test Button",
+                            tool_tip_text="This is a test of the button's tool tip functionality.",
+                            container=test_container,
+                            manager=default_ui_manager,
+                            anchors={'left_target': button})
+
+        assert button.rect.topleft == (100, 100)
+        assert button_2.rect.topleft == (450, 100)
+
         button.set_position(pygame.math.Vector2(150.0, 30.0))
 
+        assert button.rect.topleft == (150, 30)
+        assert button_2.rect.topleft == (500, 100)
+
         assert (button.relative_rect.topleft == (50, -70) and
                 button.drawable_shape.containing_rect.topleft == (150, 30))
 
+        assert button_2.relative_rect.topleft == (200, 0)
+
     def test_set_dimensions(self, _init_pygame, default_ui_manager,
                             _display_surface_return_none):
         button = UIButton(relative_rect=pygame.Rect(0, 0, 150, 30),
@@ -638,6 +653,29 @@ class TestUIButton:
 
         assert button.image is not None
 
+        manager = UIManager((800, 600))
+        button = UIButton(relative_rect=pygame.Rect(10, 10, 150, 30),
+                          text="Test Button",
+                          tool_tip_text="This is a test of the button's tool tip functionality.",
+                          manager=manager)
+        assert button.shape_corner_radius == 2
+        manager.get_theme().load_theme(os.path.join("tests", "data", "themes", "ui_button_non_default.json"))
+        button.rebuild_from_changed_theme_data()
+
+        assert button.shape_corner_radius == 10
+
+        manager = UIManager((800, 600))
+        button = UIButton(relative_rect=pygame.Rect(10, 10, 150, 30),
+                          text="Test Button",
+                          tool_tip_text="This is a test of the button's tool tip functionality.",
+                          manager=manager)
+        assert button.shape_corner_radius == 2
+        manager.get_theme().load_theme(os.path.join("tests", "data", "themes", "ui_button_non_default.json"))
+        button.drawable_shape.theming['shape_corner_radius'] = 10
+        button.set_dimensions((200, 30))
+
+        assert button.drawable_shape.shape_corner_radius == 10
+
     def test_rebuild_shape_ellipse(self, _init_pygame, _display_surface_return_none):
         manager = UIManager((800, 600),
                             os.path.join("tests", "data", "themes", "ui_button_non_default_2.json"))