Skip to content

Commit

Permalink
Merge pull request #292 from MyreMylar/further-coverage-elements-improve
Browse files Browse the repository at this point in the history
Further coverage elements improve
  • Loading branch information
MyreMylar authored Aug 5, 2022
2 parents 2c9c718 + 338c07b commit f4907e9
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 14 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"
}
}
}
24 changes: 14 additions & 10 deletions tests/test_core/test_text/test_text_effects.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,22 @@ def test_update(self, _init_pygame, default_ui_manager: UIManager):
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)
Expand All @@ -57,7 +57,11 @@ def test_update(self, _init_pygame, default_ui_manager: UIManager):
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',
Expand Down
4 changes: 4 additions & 0 deletions tests/test_elements/test_ui_horizontal_slider.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,13 @@ def test_check_update_buttons(self, _init_pygame, default_ui_manager,
# 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
Expand Down
43 changes: 43 additions & 0 deletions tests/test_elements/test_ui_progress_bar.py
Original file line number Diff line number Diff line change
@@ -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()
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 f4907e9

Please sign in to comment.