Skip to content

Commit

Permalink
Yet again, more element test coverage fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
MyreMylar committed Aug 5, 2022
1 parent 87f061b commit 338c07b
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 4 deletions.
4 changes: 2 additions & 2 deletions pygame_gui/elements/ui_selection_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def __init__(self,
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:
"""
Expand Down Expand Up @@ -327,7 +327,7 @@ def set_item_list(self, new_item_list: Union[List[str], List[Tuple[str, str]]]):
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
43 changes: 43 additions & 0 deletions tests/test_elements/test_ui_selection_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ def test_creation(self, _init_pygame, default_ui_manager,
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),
Expand Down Expand Up @@ -150,6 +158,9 @@ def test_set_item_list(self, _init_pygame, default_ui_manager,
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),
Expand Down Expand Up @@ -917,6 +928,18 @@ def test_default_selection_sets_correctly(self):

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,
Expand Down Expand Up @@ -971,6 +994,26 @@ def test_default_selection_changes(
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()
11 changes: 10 additions & 1 deletion tests/test_elements/test_ui_status_bar.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,12 @@ def test_update(self, _init_pygame, default_ui_manager):
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,
Expand All @@ -65,6 +69,11 @@ def test_set_position(self, _init_pygame, default_ui_manager):

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),
Expand Down

0 comments on commit 338c07b

Please sign in to comment.