From c98deb138675e10f8a9e1357af5e3e62e335060a Mon Sep 17 00:00:00 2001 From: Dan Lawrence Date: Wed, 3 Aug 2022 18:54:24 +0100 Subject: [PATCH 1/3] Improve test coverage - batch of fixes --- tests/test_core/test_layered_gui_group.py | 15 +++++++++++--- tests/test_elements/test_ui_button.py | 20 +++++++++++++++++++ tests/test_elements/test_ui_panel.py | 18 +++++++++++++++++ .../test_ui_screen_space_health_bar.py | 9 +++++++++ 4 files changed, 59 insertions(+), 3 deletions(-) diff --git a/tests/test_core/test_layered_gui_group.py b/tests/test_core/test_layered_gui_group.py index 658196ee..aad5d3da 100644 --- a/tests/test_core/test_layered_gui_group.py +++ b/tests/test_core/test_layered_gui_group.py @@ -14,15 +14,16 @@ def update(self, time_delta: float): pass -class MyDodgySprite1(pygame.sprite.Sprite): +class MyDodgySprite1(GUISprite): def __init__(self, *groups): super().__init__(*groups) + self.blendmode = 0 def update(self, time_delta: float): pass -class MyDodgySprite2(pygame.sprite.Sprite): +class MyDodgySprite2(GUISprite): def __init__(self, *groups): super().__init__(*groups) self.visible = 1 @@ -42,12 +43,20 @@ class TestUIElement: def test_remove_sprite_from_group(self, _init_pygame, default_ui_manager): group = LayeredGUIGroup() + group_2 = LayeredGUIGroup() proper_sprite = MyProperSprite(group) + proper_sprite_2 = MyProperSprite([group]) + + proper_sprite_3 = MyProperSprite([[group], [group_2]]) + proper_sprite.remove(group) + proper_sprite_2.remove([group]) + proper_sprite_3.remove([[group], [group_2]]) assert len(group.sprites()) == 0 + assert len(group_2.sprites()) == 0 def test_add_dodgy_sprites(self, _init_pygame, default_ui_manager): group = LayeredGUIGroup() @@ -88,4 +97,4 @@ def test_print_sprite(self, _init_pygame, default_ui_manager): if __name__ == '__main__': - pytest.console_main() \ No newline at end of file + pytest.console_main() diff --git a/tests/test_elements/test_ui_button.py b/tests/test_elements/test_ui_button.py index f5517728..c7dda56a 100644 --- a/tests/test_elements/test_ui_button.py +++ b/tests/test_elements/test_ui_button.py @@ -726,6 +726,26 @@ def test_class_theming_id(self, _init_pygame, _display_surface_return_none): '@test_class', 'button'] + def test_change_locale(self, _init_pygame, default_ui_manager, _display_surface_return_none): + button = UIButton(relative_rect=pygame.Rect(100, 100, 150, 30), + text="Test Button", + tool_tip_text="This is a test of the button's tool tip functionality.", + manager=default_ui_manager) + + default_ui_manager.set_locale('fr') + default_ui_manager.set_locale('ja') + + assert button.text == "Test Button" + + dynamic_width_button = UIButton(relative_rect=pygame.Rect(100, 100, -1, 30), + text="Test Button", + tool_tip_text="This is a test of the button's tool tip functionality.", + manager=default_ui_manager) + + default_ui_manager.set_locale('fr') + default_ui_manager.set_locale('ja') + + assert dynamic_width_button.text == "Test Button" if __name__ == '__main__': pytest.console_main() diff --git a/tests/test_elements/test_ui_panel.py b/tests/test_elements/test_ui_panel.py index 5922b957..7f61d714 100644 --- a/tests/test_elements/test_ui_panel.py +++ b/tests/test_elements/test_ui_panel.py @@ -380,6 +380,24 @@ def test_set_dimensions(self, _init_pygame, default_ui_manager): 'top': 'top', 'bottom': 'bottom'}) + button = UIButton(relative_rect=pygame.Rect(0, 40, 10, 10), text="A", + manager=default_ui_manager, + container=element_5, + anchors={'left': 'left', + 'right': 'left', + 'top': 'bottom', + 'bottom': 'bottom'} + ) + + button_2 = UIButton(relative_rect=pygame.Rect(40, 0, 10, 10), text="A", + manager=default_ui_manager, + container=element_5, + anchors={'left': 'right', + 'right': 'right', + 'top': 'top', + 'bottom': 'top'} + ) + assert element_5.relative_right_margin == 240 assert element_5.relative_bottom_margin == 240 diff --git a/tests/test_elements/test_ui_screen_space_health_bar.py b/tests/test_elements/test_ui_screen_space_health_bar.py index 3f50d92a..63795809 100644 --- a/tests/test_elements/test_ui_screen_space_health_bar.py +++ b/tests/test_elements/test_ui_screen_space_health_bar.py @@ -91,6 +91,15 @@ def test_update(self, _init_pygame, default_ui_manager): health_bar.update(0.01) assert health_bar.image is not None + def test_health_percentage(self, _init_pygame, default_ui_manager): + healthy_sprite = HealthySprite() + health_bar = UIScreenSpaceHealthBar(relative_rect=pygame.Rect(100, 100, 150, 30), + sprite_to_monitor=healthy_sprite, + manager=default_ui_manager) + healthy_sprite.current_health = 50 + health_bar.update(0.01) + assert health_bar.health_percentage == 0.5 + def test_rebuild_from_theme_data_non_default(self, _init_pygame): manager = UIManager((800, 600), os.path.join("tests", "data", "themes", "ui_screen_health_bar_non_default.json")) From b23cb224133e4a59f4bd812e7fd7fcda0d669268 Mon Sep 17 00:00:00 2001 From: Dan Lawrence Date: Wed, 3 Aug 2022 19:15:42 +0100 Subject: [PATCH 2/3] Fix layered UI Group test --- tests/test_core/test_layered_gui_group.py | 36 +++++++++++++++++++---- 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/tests/test_core/test_layered_gui_group.py b/tests/test_core/test_layered_gui_group.py index aad5d3da..1aed6a08 100644 --- a/tests/test_core/test_layered_gui_group.py +++ b/tests/test_core/test_layered_gui_group.py @@ -14,29 +14,53 @@ def update(self, time_delta: float): pass -class MyDodgySprite1(GUISprite): +class MyDodgySprite1: def __init__(self, *groups): - super().__init__(*groups) self.blendmode = 0 + self.__g = {} + if groups: + self.add(*groups) + + def add(self, *groups): + has = self.__g.__contains__ + for group in groups: + if hasattr(group, '_spritegroup'): + if not has(group): + group.add_internal(self) + else: + self.add(*group) + def update(self, time_delta: float): pass -class MyDodgySprite2(GUISprite): +class MyDodgySprite2: def __init__(self, *groups): - super().__init__(*groups) self.visible = 1 + self.__g = {} + if groups: + self.add(*groups) + + def add(self, *groups): + has = self.__g.__contains__ + for group in groups: + if hasattr(group, '_spritegroup'): + if not has(group): + group.add_internal(self) + else: + self.add(*group) + def update(self, time_delta: float): pass class MyDodgySprite3(pygame.sprite.Sprite): def __init__(self, *groups): - super().__init__(*groups) self.blendmode = 0 self.visible = 1 + super().__init__(*groups) class TestUIElement: @@ -67,7 +91,7 @@ def test_add_dodgy_sprites(self, _init_pygame, default_ui_manager): with pytest.raises(AttributeError): MyDodgySprite2(group) - with pytest.raises(AttributeError): + with pytest.raises(TypeError): MyDodgySprite3(group) def test_sprite_set_layer_before_add(self, _init_pygame, default_ui_manager): From 62cdfa4d38d46a7be2bc5e0d689d8ce792f1720e Mon Sep 17 00:00:00 2001 From: Dan Lawrence Date: Wed, 3 Aug 2022 19:54:28 +0100 Subject: [PATCH 3/3] UI Button test coverage improvements --- tests/data/themes/ui_button_bad_values_2.json | 12 ++++++ tests/data/themes/ui_button_non_default.json | 2 + tests/test_elements/test_ui_button.py | 41 ++++++++++++++++++- 3 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 tests/data/themes/ui_button_bad_values_2.json diff --git a/tests/data/themes/ui_button_bad_values_2.json b/tests/data/themes/ui_button_bad_values_2.json new file mode 100644 index 00000000..39459677 --- /dev/null +++ b/tests/data/themes/ui_button_bad_values_2.json @@ -0,0 +1,12 @@ +{ + "button": + { + "misc": + { + "state_transitions": + { + "normal_hovered": "dog" + } + } + } + } \ No newline at end of file diff --git a/tests/data/themes/ui_button_non_default.json b/tests/data/themes/ui_button_non_default.json index 76fd182b..529a26a2 100644 --- a/tests/data/themes/ui_button_non_default.json +++ b/tests/data/themes/ui_button_non_default.json @@ -61,6 +61,8 @@ "text_vert_alignment": "top", "text_horiz_alignment_padding": "6", "text_vert_alignment_padding": "7", + "text_shadow_size": "3", + "text_shadow_offset": "0,2", "state_transitions": { diff --git a/tests/test_elements/test_ui_button.py b/tests/test_elements/test_ui_button.py index c7dda56a..b7830ee3 100644 --- a/tests/test_elements/test_ui_button.py +++ b/tests/test_elements/test_ui_button.py @@ -259,6 +259,7 @@ def test_process_event_double_click(self, _init_pygame: None, default_ui_manager consumed_event_1 = button.process_event(pygame.event.Event(pygame.MOUSEBUTTONDOWN, {'button': pygame.BUTTON_LEFT, 'pos': button.rect.center})) + button.update(0.001) consumed_event_2 = button.process_event(pygame.event.Event(pygame.MOUSEBUTTONDOWN, {'button': pygame.BUTTON_LEFT, @@ -505,6 +506,14 @@ def test_set_text(self, _init_pygame: None, default_ui_manager: UIManager, assert (empty_queue == 0 and full_queue != 0 and button.drawable_shape.theming['text'] == 'Ipsum' and button.text == 'Ipsum') + dynamic_width_button = UIButton(relative_rect=pygame.Rect(10, 10, -1, 30), + text="Test Button", + tool_tip_text="This is a test of the button's tool tip functionality.", + manager=default_ui_manager) + + dynamic_width_button.set_text('Ipsum') + assert dynamic_width_button.text == "Ipsum" + def test_set_text_same(self, _init_pygame: None, default_ui_manager: UIManager, _display_surface_return_none): button = UIButton(relative_rect=pygame.Rect(10, 10, 150, 30), @@ -606,6 +615,18 @@ def test_rebuild_from_changed_theme_data_bad_values(self, _init_pygame, assert button.image is not None + def test_rebuild_from_changed_theme_data_bad_values_2(self, _init_pygame, + _display_surface_return_none): + manager = UIManager((800, 600), + os.path.join("tests", "data", "themes", "ui_button_bad_values_2.json")) + + 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.state_transitions[("normal", "hovered")] == 0.0 + def test_rebuild_shape(self, _init_pygame, _display_surface_return_none): manager = UIManager((800, 600), os.path.join("tests", "data", "themes", "ui_button_non_default.json")) @@ -628,6 +649,20 @@ def test_rebuild_shape_ellipse(self, _init_pygame, _display_surface_return_none) assert button.image is not None + def test_rebuild_anchors_dynamic_dimensions(self, _init_pygame, default_ui_manager, _display_surface_return_none): + + button = UIButton(relative_rect=pygame.Rect(10, 10, 150, -1), + text="Test Button", + tool_tip_text="This is a test of the button's tool tip functionality.", + manager=default_ui_manager, + anchors={'top': 'bottom', 'bottom': 'bottom'}) + + assert button.dynamic_height + + button.rebuild() + + assert button.image is not None + def test_show(self, _init_pygame, default_ui_manager, _display_surface_return_none): button = UIButton(relative_rect=pygame.Rect(100, 100, 150, 30), text="Test Button", @@ -737,15 +772,19 @@ def test_change_locale(self, _init_pygame, default_ui_manager, _display_surface_ assert button.text == "Test Button" + default_ui_manager.set_locale('en') + dynamic_width_button = UIButton(relative_rect=pygame.Rect(100, 100, -1, 30), text="Test Button", tool_tip_text="This is a test of the button's tool tip functionality.", manager=default_ui_manager) + assert dynamic_width_button.dynamic_width + default_ui_manager.set_locale('fr') - default_ui_manager.set_locale('ja') assert dynamic_width_button.text == "Test Button" + if __name__ == '__main__': pytest.console_main()