Skip to content

Commit

Permalink
Merge pull request #290 from MyreMylar/elements-coverage-improvements
Browse files Browse the repository at this point in the history
Improve test coverage - batch of fixes
  • Loading branch information
MyreMylar authored Aug 3, 2022
2 parents ce6f31d + 62cdfa4 commit 5892ebd
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 7 deletions.
12 changes: 12 additions & 0 deletions tests/data/themes/ui_button_bad_values_2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"button":
{
"misc":
{
"state_transitions":
{
"normal_hovered": "dog"
}
}
}
}
2 changes: 2 additions & 0 deletions tests/data/themes/ui_button_non_default.json
Original file line number Diff line number Diff line change
Expand Up @@ -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":
{
Expand Down
47 changes: 40 additions & 7 deletions tests/test_core/test_layered_gui_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,40 +14,73 @@ def update(self, time_delta: float):
pass


class MyDodgySprite1(pygame.sprite.Sprite):
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(pygame.sprite.Sprite):
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:
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()
Expand All @@ -58,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):
Expand Down Expand Up @@ -88,4 +121,4 @@ def test_print_sprite(self, _init_pygame, default_ui_manager):


if __name__ == '__main__':
pytest.console_main()
pytest.console_main()
59 changes: 59 additions & 0 deletions tests/test_elements/test_ui_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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"))
Expand All @@ -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",
Expand Down Expand Up @@ -726,6 +761,30 @@ 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"

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')

assert dynamic_width_button.text == "Test Button"


if __name__ == '__main__':
pytest.console_main()
18 changes: 18 additions & 0 deletions tests/test_elements/test_ui_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 9 additions & 0 deletions tests/test_elements/test_ui_screen_space_health_bar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down

0 comments on commit 5892ebd

Please sign in to comment.