def test_add_or_rm_fav(shell, fixture, fake_stdscr): app = App(fake_stdscr) path = application.SHELLS[shell]["fav"] for value in (True, False): app.add_or_rm_fav("egg") favs = read(path) assert favs.__contains__("egg") == value
def test_delete_python_history(command, fake_stdscr, fake_standard, fake_readline, tmp_path): shutil.copyfile("tests/history/fake_python_history", tmp_path / "history") app = App(fake_stdscr) app.delete_python_history(command) assert command not in read("tests/history/fake_python_history") shutil.move(tmp_path / "history", "tests/history/fake_python_history")
def test_page_get_commands(fake_curses, fake_stdscr, fake_standard): page = Page(App(fake_stdscr)) assert page.get_commands() == [ 'print("SPAM")', 'assert hasattr(subprocess, \'run\'), "install a newer python lel"', '__import__("math").pi', 'print(sys.executable)', 'print(sys.argv)', 'from math import tau', '[x for x in range(100)]', ]
def test_page_turn( current, direction, expected, fake_curses, fake_stdscr, fake_standard, ): page = Page(App(fake_stdscr)) page.value = current page.turn(direction) assert page.value == expected
def test_selected_move( current_selected, direction, expected_selected, expected_page, fake_curses, fake_stdscr, fake_standard, ): page = Page(App(fake_stdscr)) page.selected = current_selected page.move_selected(direction) assert page.selected == expected_selected assert page.value == expected_page
def test_search( search_string, expected, regex_mode, case_sensitivity, fake_stdscr, fake_curses, fake_standard, ): app = App(fake_stdscr) app.search_string = search_string app.regex_mode = regex_mode app.case_sensitivity = case_sensitivity app.search() assert all(x in expected for x in app.commands[app.view])
def main(stdscr: _CursesWindow) -> None: # pylint: disable=too-many-statements app = App(stdscr) app.user_interface.init_color_pairs() app.user_interface.populate_screen() while True: try: user_input = app.stdscr.get_wch() except curses.error: app.stdscr.clear() app.user_interface.populate_screen() continue except KeyboardInterrupt: break # user_input is Union[int, str], sometimes isinstance needed to make mypy happy if user_input == CTRL_E: app.toggle_regex_mode() app.user_interface.page.selected = 0 app.user_interface.populate_screen() elif user_input == CTRL_F: command = app.user_interface.page.get_selected() if app.view == View.FAVORITES: app.user_interface.page.retain_selection() app.add_or_rm_fav(command) app.stdscr.clear() app.user_interface.populate_screen() elif user_input == TAB: command = app.user_interface.page.get_selected() echo(command) break elif user_input == ENTER: command = app.user_interface.page.get_selected() echo(command) echo("\n") break elif user_input == CTRL_T: app.toggle_case() app.user_interface.populate_screen() elif user_input == ESC: break elif user_input == CTRL_SLASH: app.toggle_view() app.user_interface.page.selected = 0 app.user_interface.page.value = 1 app.stdscr.clear() app.user_interface.populate_screen() elif user_input in {curses.KEY_UP, curses.KEY_DOWN}: assert isinstance(user_input, int) app.user_interface.page.move_selected(KEY_BINDINGS[user_input]) app.user_interface.populate_screen() elif user_input in {curses.KEY_NPAGE, curses.KEY_PPAGE}: assert isinstance(user_input, int) app.user_interface.page.turn(KEY_BINDINGS[user_input]) app.user_interface.populate_screen() elif user_input == curses.KEY_BACKSPACE: app.search_string = app.search_string[:-1] if not app.search_string: app.user_interface.page.selected = 0 app.commands = app.to_restore.copy() app.search() elif user_input == DEL: command = app.user_interface.page.get_selected() app.user_interface.prompt_for_deletion(command) answer = app.stdscr.getch() if answer == ord("y"): app.user_interface.page.retain_selection() app.delete_from_history(command) app.stdscr.clear() app.user_interface.populate_screen() elif isinstance(user_input, str): # not another special int character like curses.KEY_UP app.search_string += user_input app.commands = app.to_restore.copy() app.search() stdscr.clear() stdscr.refresh() curses.doupdate()
def test_prompt_for_deletion(fake_curses, fake_stdscr, fake_standard): user_interface = UserInterface(App(fake_stdscr)) command = '__import__("math").pi' user_interface.prompt_for_deletion(command) assert (1, 1, f"Do you want to delete all occurences of {command}? y/n", 0) in fake_stdscr.addstred
def test_create_search_regex_none(fake_stdscr): app = App(fake_stdscr) app.regex_mode = True app.search_string = "print(" assert app.create_search_regex() is None
def test_toggle_view(before, expected, fake_stdscr): app = App(fake_stdscr) app.view = before app.toggle_view() assert app.view == expected
def test_show_regex_error(fake_curses, fake_stdscr, fake_standard): user_interface = UserInterface(App(fake_stdscr)) user_interface.show_regex_error() assert (1, 1, "Invalid regex. Try again.", 0) in fake_stdscr.addstred
def test_get_history(shell, fixture, fake_stdscr): app = App(fake_stdscr) assert app.get_history() == read( f"tests/history/fake_{shell.value}_history")
def test_toggle_regex_mode(regex_mode, fake_stdscr): app = App(fake_stdscr) app.regex_mode = regex_mode app.toggle_regex_mode() assert app.regex_mode == (not regex_mode)
f"tests/history/fake_{shell.value}_history") @pytest.mark.all @pytest.mark.parametrize("regex_mode", [True, False]) def test_toggle_regex_mode(regex_mode, fake_stdscr): app = App(fake_stdscr) app.regex_mode = regex_mode app.toggle_regex_mode() assert app.regex_mode == (not regex_mode) @pytest.mark.all @pytest.mark.parametrize("case", [True, False]) def test_toggle_case(case, fake_stdscr): app = App(fake_stdscr) app.case_sensitivity = case app.toggle_case() assert app.case_sensitivity == (not case) @pytest.mark.all @pytest.mark.parametrize( "before, expected", [ [View.SORTED, View.FAVORITES], [View.FAVORITES, View.ALL], [View.ALL, View.SORTED], ], ) def test_toggle_view(before, expected, fake_stdscr):
def test_page_get_selected(fake_curses, fake_stdscr, fake_standard): page = Page(App(fake_stdscr)) page.selected = 2 assert page.get_selected() == '__import__("math").pi'
def test_page_get_size(fake_curses, fake_stdscr, fake_standard): page = Page(App(fake_stdscr)) assert page.get_size() == 7
def test_total_pages(fake_curses, fake_stdscr, fake_standard): user_interface = UserInterface(App(fake_stdscr)) assert user_interface.total_pages() == 4
def test_init_color_pairs(fake_curses, fake_stdscr): user_interface = UserInterface(App(fake_stdscr)) user_interface.init_color_pairs() for v in COLORS.values(): assert v != 0