def test_changes_cursor_on_text_motion(motion, expected_index, mock_mng): inputbox = UIInputBox(center_x=30, center_y=30, width=40, height=40) inputbox.text = 'Best Game Lib!' inputbox.cursor_index = 5 inputbox.on_focus() mock_mng.add_ui_element(inputbox) inputbox.on_ui_event(UIEvent(TEXT_MOTION, motion=motion)) assert inputbox.cursor_index == expected_index
def test_emits_event_on_enter(mock_mng): inputbox = UIInputBox(center_x=30, center_y=30, width=40, height=40) inputbox.text = 'Best Game Lib!' inputbox.cursor_index = 5 inputbox.on_focus() mock_mng.add_ui_element(inputbox) inputbox.on_ui_event(UIEvent(TEXT_INPUT, text='\r')) assert mock_mng.last_event.type == UIInputBox.ENTER assert mock_mng.last_event.get('ui_element') == inputbox
def test_ignores_newline(draw_commands, mock_mng): inputbox = UIInputBox(center_x=30, center_y=30, width=40, height=40) inputbox.text = 'Best Game Lib!' inputbox.cursor_index = 5 inputbox.on_focus() mock_mng.add_ui_element(inputbox) inputbox.on_ui_event(UIEvent(TEXT_INPUT, text='\r')) assert inputbox.text == 'Best Game Lib!' assert inputbox.cursor_index == 5
def test_changes_text_on_text_input(mock_mng): inputbox = UIInputBox(center_x=30, center_y=30, width=40, height=40) inputbox.text = 'Best Game Lib!' inputbox.cursor_index = 5 inputbox.on_focus() mock_mng.add_ui_element(inputbox) inputbox.on_ui_event(UIEvent(TEXT_INPUT, text='a')) assert inputbox.text == 'Best aGame Lib!' assert inputbox.cursor_index == 6
def test_changes_text_on_delete(draw_commands, mock_mng): inputbox = UIInputBox(center_x=30, center_y=30, width=40, height=40) inputbox.text = 'Best Game Lib!' inputbox.cursor_index = 5 inputbox.on_focus() mock_mng.add_ui_element(inputbox) inputbox.on_ui_event(UIEvent(TEXT_MOTION, motion=MOTION_DELETE)) assert inputbox.text == 'Best ame Lib!' assert inputbox.cursor_index == 5
def test_cursor_can_not_be_negative(draw_commands, mock_mng): inputbox = UIInputBox(center_x=30, center_y=30, width=40, height=40) inputbox.text = 'Best Game Lib!' inputbox.cursor_index = 0 inputbox.on_focus() mock_mng.add_ui_element(inputbox) inputbox.on_ui_event(UIEvent(TEXT_MOTION, motion=MOTION_LEFT)) assert inputbox.text == 'Best Game Lib!' assert inputbox.cursor_index == 0
def test_set_cursor_behind_text_if_given_at_construction_time( draw_commands, mock_mng): inputbox = UIInputBox(text='arcade', center_x=30, center_y=30, width=40, height=40) # THEN assert inputbox.cursor_index == 6
def test_invokes_callback_on_return(mock_mng): inputbox = UIInputBox(center_x=30, center_y=30, width=40, height=40) inputbox.text = 'Best Game Lib!' inputbox.cursor_index = 5 inputbox.on_focus() mock_mng.add_ui_element(inputbox) invoked = False def callback(): nonlocal invoked invoked = True inputbox.on_enter = callback inputbox.on_ui_event(UIEvent(TEXT_INPUT, text='\r')) assert invoked is True
def test_cursor_index_always_greater_equals_0(mock_mng): inputbox = UIInputBox(center_x=30, center_y=30, width=40, height=40) inputbox.text = 'love' inputbox.cursor_index = -1 assert inputbox.cursor_index == 0
def test_cursor_index_not_outside_text(mock_mng): inputbox = UIInputBox(center_x=30, center_y=30, width=40, height=40) inputbox.text = 'love' inputbox.cursor_index = 5 assert inputbox.cursor_index == 4
def test_ui_inputbox_has_on_enter_event_type(): inputbox = UIInputBox(center_x=30, center_y=30, width=40, height=40) assert 'on_enter' in inputbox.event_types
def test_shows_cursor_if_focused(draw_commands, mock_mng): inputbox = UIInputBox(center_x=30, center_y=30, width=40, height=40) inputbox.text = 'Great UI' inputbox.cursor_index = 6 mock_mng.add_ui_element(inputbox)