diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/conftest.py | 2 | ||||
-rw-r--r-- | tests/test_ansi.py | 231 | ||||
-rwxr-xr-x | tests/test_cmd2.py | 89 | ||||
-rwxr-xr-x | tests/test_completion.py | 28 | ||||
-rw-r--r-- | tests/test_table_creator.py | 25 | ||||
-rw-r--r-- | tests/test_utils.py | 43 | ||||
-rw-r--r-- | tests/transcripts/regex_set.txt | 2 |
7 files changed, 265 insertions, 155 deletions
diff --git a/tests/conftest.py b/tests/conftest.py index 0829da2f..de74de46 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -109,7 +109,7 @@ SET_TXT = ( "Name Value Description \n" "==================================================================================================================\n" "allow_style Terminal Allow ANSI text style sequences in output (valid values: \n" - " Terminal, Always, Never) \n" + " Always, Never, Terminal) \n" "always_show_hint False Display tab completion hint even when completion suggestions\n" " print \n" "debug False Show full traceback on exception \n" diff --git a/tests/test_ansi.py b/tests/test_ansi.py index 1797a047..307d9ad7 100644 --- a/tests/test_ansi.py +++ b/tests/test_ansi.py @@ -5,21 +5,23 @@ Unit testing for cmd2/ansi.py module """ import pytest -import cmd2.ansi as ansi +from cmd2 import ( + ansi, +) HELLO_WORLD = 'Hello, world!' def test_strip_style(): base_str = HELLO_WORLD - ansi_str = ansi.style(base_str, fg='green') + ansi_str = ansi.style(base_str, fg=ansi.StdFg.GREEN) assert base_str != ansi_str assert base_str == ansi.strip_style(ansi_str) def test_style_aware_wcswidth(): base_str = HELLO_WORLD - ansi_str = ansi.style(base_str, fg='green') + ansi_str = ansi.style(base_str, fg=ansi.StdFg.GREEN) assert ansi.style_aware_wcswidth(HELLO_WORLD) == ansi.style_aware_wcswidth(ansi_str) assert ansi.style_aware_wcswidth('i have a tab\t') == -1 @@ -27,7 +29,7 @@ def test_style_aware_wcswidth(): def test_widest_line(): - text = ansi.style('i have\n3 lines\nThis is the longest one', fg='green') + text = ansi.style('i have\n3 lines\nThis is the longest one', fg=ansi.StdFg.GREEN) assert ansi.widest_line(text) == ansi.style_aware_wcswidth("This is the longest one") text = "I'm just one line" @@ -42,99 +44,111 @@ def test_style_none(): assert ansi.style(base_str) == ansi_str -def test_style_fg(): +@pytest.mark.parametrize('fg_color', [ansi.StdFg.BLUE, ansi.EightBitFg.AQUAMARINE_1A, ansi.RgbFg(0, 2, 4)]) +def test_style_fg(fg_color): base_str = HELLO_WORLD - fg_color = 'blue' - ansi_str = ansi.fg[fg_color].value + base_str + ansi.FG_RESET + ansi_str = fg_color + base_str + ansi.StdFg.RESET assert ansi.style(base_str, fg=fg_color) == ansi_str -def test_style_bg(): +@pytest.mark.parametrize('bg_color', [ansi.StdBg.BLUE, ansi.EightBitBg.AQUAMARINE_1A, ansi.RgbBg(0, 2, 4)]) +def test_style_bg(bg_color): base_str = HELLO_WORLD - bg_color = 'green' - ansi_str = ansi.bg[bg_color].value + base_str + ansi.BG_RESET + ansi_str = bg_color + base_str + ansi.StdBg.RESET assert ansi.style(base_str, bg=bg_color) == ansi_str def test_style_bold(): base_str = HELLO_WORLD - ansi_str = ansi.INTENSITY_BRIGHT + base_str + ansi.INTENSITY_NORMAL + ansi_str = ansi.TextStyle.INTENSITY_BOLD + base_str + ansi.TextStyle.INTENSITY_NORMAL assert ansi.style(base_str, bold=True) == ansi_str def test_style_dim(): base_str = HELLO_WORLD - ansi_str = ansi.INTENSITY_DIM + base_str + ansi.INTENSITY_NORMAL + ansi_str = ansi.TextStyle.INTENSITY_DIM + base_str + ansi.TextStyle.INTENSITY_NORMAL assert ansi.style(base_str, dim=True) == ansi_str -def test_style_underline(): +def test_style_italic(): base_str = HELLO_WORLD - ansi_str = ansi.UNDERLINE_ENABLE + base_str + ansi.UNDERLINE_DISABLE - assert ansi.style(base_str, underline=True) == ansi_str + ansi_str = ansi.TextStyle.ITALIC_ENABLE + base_str + ansi.TextStyle.ITALIC_DISABLE + assert ansi.style(base_str, italic=True) == ansi_str -def test_style_multi(): +def test_style_overline(): base_str = HELLO_WORLD - fg_color = 'blue' - bg_color = 'green' - ansi_str = ( - ansi.fg[fg_color].value - + ansi.bg[bg_color].value - + ansi.INTENSITY_BRIGHT - + ansi.INTENSITY_DIM - + ansi.UNDERLINE_ENABLE - + base_str - + ansi.FG_RESET - + ansi.BG_RESET - + ansi.INTENSITY_NORMAL - + ansi.INTENSITY_NORMAL - + ansi.UNDERLINE_DISABLE - ) - assert ansi.style(base_str, fg=fg_color, bg=bg_color, bold=True, dim=True, underline=True) == ansi_str + ansi_str = ansi.TextStyle.OVERLINE_ENABLE + base_str + ansi.TextStyle.OVERLINE_DISABLE + assert ansi.style(base_str, overline=True) == ansi_str -def test_style_color_not_exist(): +def test_style_strikethrough(): base_str = HELLO_WORLD + ansi_str = ansi.TextStyle.STRIKETHROUGH_ENABLE + base_str + ansi.TextStyle.STRIKETHROUGH_DISABLE + assert ansi.style(base_str, strikethrough=True) == ansi_str - with pytest.raises(ValueError): - ansi.style(base_str, fg='fake', bg='green') - - with pytest.raises(ValueError): - ansi.style(base_str, fg='blue', bg='fake') - - -def test_fg_lookup_exist(): - fg_color = 'green' - assert ansi.fg_lookup(fg_color) == ansi.fg_lookup(ansi.fg.green) - -def test_fg_lookup_nonexist(): - with pytest.raises(ValueError): - ansi.fg_lookup('foo') - - -def test_bg_lookup_exist(): - bg_color = 'red' - assert ansi.bg_lookup(bg_color) == ansi.bg_lookup(ansi.bg.red) +def test_style_underline(): + base_str = HELLO_WORLD + ansi_str = ansi.TextStyle.UNDERLINE_ENABLE + base_str + ansi.TextStyle.UNDERLINE_DISABLE + assert ansi.style(base_str, underline=True) == ansi_str -def test_bg_lookup_nonexist(): - with pytest.raises(ValueError): - ansi.bg_lookup('bar') +def test_style_multi(): + base_str = HELLO_WORLD + fg_color = ansi.StdFg.LIGHT_BLUE + bg_color = ansi.StdBg.LIGHT_GRAY + ansi_str = ( + fg_color + + bg_color + + ansi.TextStyle.INTENSITY_BOLD + + ansi.TextStyle.INTENSITY_DIM + + ansi.TextStyle.ITALIC_ENABLE + + ansi.TextStyle.OVERLINE_ENABLE + + ansi.TextStyle.STRIKETHROUGH_ENABLE + + ansi.TextStyle.UNDERLINE_ENABLE + + base_str + + ansi.StdFg.RESET + + ansi.StdBg.RESET + + ansi.TextStyle.INTENSITY_NORMAL + + ansi.TextStyle.INTENSITY_NORMAL + + ansi.TextStyle.ITALIC_DISABLE + + ansi.TextStyle.OVERLINE_DISABLE + + ansi.TextStyle.STRIKETHROUGH_DISABLE + + ansi.TextStyle.UNDERLINE_DISABLE + ) + assert ( + ansi.style( + base_str, + fg=fg_color, + bg=bg_color, + bold=True, + dim=True, + italic=True, + overline=True, + strikethrough=True, + underline=True, + ) + == ansi_str + ) -def test_set_title_str(): - OSC = '\033]' - BEL = '\007' +def test_set_title(): title = HELLO_WORLD - assert ansi.set_title_str(title) == OSC + '2;' + title + BEL + assert ansi.set_title(title) == ansi.OSC + '2;' + title + ansi.BEL @pytest.mark.parametrize( 'cols, prompt, line, cursor, msg, expected', [ - (127, '(Cmd) ', 'help his', 12, ansi.style('Hello World!', fg='magenta'), '\x1b[2K\r\x1b[35mHello World!\x1b[39m'), + ( + 127, + '(Cmd) ', + 'help his', + 12, + ansi.style('Hello World!', fg=ansi.StdFg.MAGENTA), + '\x1b[2K\r\x1b[35mHello World!\x1b[39m', + ), (127, '\n(Cmd) ', 'help ', 5, 'foo', '\x1b[2K\x1b[1A\x1b[2K\rfoo'), ( 10, @@ -151,40 +165,83 @@ def test_async_alert_str(cols, prompt, line, cursor, msg, expected): assert alert_str == expected -def test_cast_color_as_str(): - assert str(ansi.fg.blue) == ansi.fg.blue.value - assert str(ansi.bg.blue) == ansi.bg.blue.value +def test_clear_screen(): + clear_type = 2 + assert ansi.clear_screen(clear_type) == f"{ansi.CSI}{clear_type}J" + clear_type = -1 + with pytest.raises(ValueError): + ansi.clear_screen(clear_type) -def test_color_str_building(): - from cmd2.ansi import ( - bg, - fg, - ) + clear_type = 4 + with pytest.raises(ValueError): + ansi.clear_screen(clear_type) - assert fg.blue + "hello" == fg.blue.value + "hello" - assert bg.blue + "hello" == bg.blue.value + "hello" - assert fg.blue + "hello" + fg.reset == fg.blue.value + "hello" + fg.reset.value - assert bg.blue + "hello" + bg.reset == bg.blue.value + "hello" + bg.reset.value - assert ( - fg.blue + bg.white + "hello" + fg.reset + bg.reset - == fg.blue.value + bg.white.value + "hello" + fg.reset.value + bg.reset.value - ) +def test_clear_line(): + clear_type = 2 + assert ansi.clear_line(clear_type) == f"{ansi.CSI}{clear_type}K" + + clear_type = -1 + with pytest.raises(ValueError): + ansi.clear_line(clear_type) + + clear_type = 3 + with pytest.raises(ValueError): + ansi.clear_line(clear_type) -def test_color_nonunique_values(): - class Matching(ansi.ColorBase): - magenta = ansi.fg_lookup('magenta') - purple = ansi.fg_lookup('magenta') - assert sorted(Matching.colors()) == ['magenta', 'purple'] +def test_cursor(): + count = 1 + assert ansi.Cursor.UP(count) == f"{ansi.CSI}{count}A" + assert ansi.Cursor.DOWN(count) == f"{ansi.CSI}{count}B" + assert ansi.Cursor.FORWARD(count) == f"{ansi.CSI}{count}C" + assert ansi.Cursor.BACK(count) == f"{ansi.CSI}{count}D" + x = 4 + y = 5 + assert ansi.Cursor.SET_POS(x, y) == f"{ansi.CSI}{y};{x}H" -def test_color_enum(): - assert ansi.fg_lookup('bright_red') == ansi.fg_lookup(ansi.fg.bright_red) - assert ansi.bg_lookup('green') == ansi.bg_lookup(ansi.bg.green) + +@pytest.mark.parametrize( + 'ansi_sequence', + [ + ansi.fg.green, + ansi.bg.blue, + ansi.StdFg.MAGENTA, + ansi.StdBg.LIGHT_GRAY, + ansi.EightBitBg.CHARTREUSE_2A, + ansi.EightBitBg.MEDIUM_PURPLE, + ansi.RgbFg(0, 5, 22), + ansi.RgbBg(100, 150, 222), + ansi.TextStyle.OVERLINE_ENABLE, + ], +) +def test_sequence_str_building(ansi_sequence): + """This tests __add__(), __radd__(), and __str__() methods for AnsiSequences""" + assert ansi_sequence + ansi_sequence == str(ansi_sequence) + str(ansi_sequence) -def test_colors_list(): - assert list(ansi.fg.__members__.keys()) == ansi.fg.colors() - assert list(ansi.bg.__members__.keys()) == ansi.bg.colors() +@pytest.mark.parametrize( + 'r, g, b, valid', + [ + (0, 0, 0, True), + (255, 255, 255, True), + (-1, 0, 0, False), + (256, 255, 255, False), + (0, -1, 0, False), + (255, 256, 255, False), + (0, 0, -1, False), + (255, 255, 256, False), + ], +) +def test_rgb_bounds(r, g, b, valid): + + if valid: + ansi.RgbFg(r, g, b) + ansi.RgbBg(r, g, b) + else: + with pytest.raises(ValueError): + ansi.RgbFg(r, g, b) + with pytest.raises(ValueError): + ansi.RgbBg(r, g, b) diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py index 0f022849..45e07603 100755 --- a/tests/test_cmd2.py +++ b/tests/test_cmd2.py @@ -198,18 +198,18 @@ def test_set_no_settables(base_app): @pytest.mark.parametrize( 'new_val, is_valid, expected', [ - (ansi.STYLE_NEVER, True, ansi.STYLE_NEVER), - ('neVeR', True, ansi.STYLE_NEVER), - (ansi.STYLE_TERMINAL, True, ansi.STYLE_TERMINAL), - ('TeRMInal', True, ansi.STYLE_TERMINAL), - (ansi.STYLE_ALWAYS, True, ansi.STYLE_ALWAYS), - ('AlWaYs', True, ansi.STYLE_ALWAYS), - ('invalid', False, ansi.STYLE_TERMINAL), + (ansi.AllowStyle.NEVER, True, ansi.AllowStyle.NEVER), + ('neVeR', True, ansi.AllowStyle.NEVER), + (ansi.AllowStyle.TERMINAL, True, ansi.AllowStyle.TERMINAL), + ('TeRMInal', True, ansi.AllowStyle.TERMINAL), + (ansi.AllowStyle.ALWAYS, True, ansi.AllowStyle.ALWAYS), + ('AlWaYs', True, ansi.AllowStyle.ALWAYS), + ('invalid', False, ansi.AllowStyle.TERMINAL), ], ) def test_set_allow_style(base_app, new_val, is_valid, expected): # Initialize allow_style for this test - ansi.allow_style = ansi.STYLE_TERMINAL + ansi.allow_style = ansi.AllowStyle.TERMINAL # Use the set command to alter it out, err = run_cmd(base_app, 'set allow_style {}'.format(new_val)) @@ -219,10 +219,29 @@ def test_set_allow_style(base_app, new_val, is_valid, expected): assert ansi.allow_style == expected if is_valid: assert not err - assert "now: {!r}".format(new_val.capitalize()) in out[1] + assert out # Reset allow_style to its default since it's an application-wide setting that can affect other unit tests - ansi.allow_style = ansi.STYLE_TERMINAL + ansi.allow_style = ansi.AllowStyle.TERMINAL + + +def test_set_with_choices(base_app): + """Test choices validation of Settables""" + fake_choices = ['valid', 'choices'] + base_app.fake = fake_choices[0] + + fake_settable = cmd2.Settable('fake', type(base_app.fake), "fake description", base_app, choices=fake_choices) + base_app.add_settable(fake_settable) + + # Try a valid choice + out, err = run_cmd(base_app, f'set fake {fake_choices[1]}') + assert base_app.last_result is True + assert not err + + # Try an invalid choice + out, err = run_cmd(base_app, 'set fake bad_value') + assert base_app.last_result is False + assert err[0].startswith("Error setting fake: invalid choice") class OnChangeHookApp(cmd2.Cmd): @@ -1031,7 +1050,7 @@ def test_escaping_prompt(): assert rl_escape_prompt(prompt) == prompt # This prompt has color which needs to be escaped - color = 'cyan' + color = ansi.StdFg.CYAN prompt = ansi.style('InColor', fg=color) escape_start = "\x01" @@ -1042,8 +1061,8 @@ def test_escaping_prompt(): # PyReadline on Windows doesn't need to escape invisible characters assert escaped_prompt == prompt else: - assert escaped_prompt.startswith(escape_start + ansi.fg_lookup(color) + escape_end) - assert escaped_prompt.endswith(escape_start + ansi.FG_RESET + escape_end) + assert escaped_prompt.startswith(escape_start + color + escape_end) + assert escaped_prompt.endswith(escape_start + ansi.StdFg.RESET + escape_end) assert rl_unescape_prompt(escaped_prompt) == prompt @@ -1743,8 +1762,8 @@ def test_poutput_none(outsim_app): def test_poutput_ansi_always(outsim_app): msg = 'Hello World' - ansi.allow_style = ansi.STYLE_ALWAYS - colored_msg = ansi.style(msg, fg='cyan') + ansi.allow_style = ansi.AllowStyle.ALWAYS + colored_msg = ansi.style(msg, fg=ansi.StdFg.CYAN) outsim_app.poutput(colored_msg) out = outsim_app.stdout.getvalue() expected = colored_msg + '\n' @@ -1754,8 +1773,8 @@ def test_poutput_ansi_always(outsim_app): def test_poutput_ansi_never(outsim_app): msg = 'Hello World' - ansi.allow_style = ansi.STYLE_NEVER - colored_msg = ansi.style(msg, fg='cyan') + ansi.allow_style = ansi.AllowStyle.NEVER + colored_msg = ansi.style(msg, fg=ansi.StdFg.CYAN) outsim_app.poutput(colored_msg) out = outsim_app.stdout.getvalue() expected = msg + '\n' @@ -2182,7 +2201,7 @@ def test_nonexistent_macro(base_app): def test_perror_style(base_app, capsys): msg = 'testing...' end = '\n' - ansi.allow_style = ansi.STYLE_ALWAYS + ansi.allow_style = ansi.AllowStyle.ALWAYS base_app.perror(msg) out, err = capsys.readouterr() assert err == ansi.style_error(msg) + end @@ -2191,7 +2210,7 @@ def test_perror_style(base_app, capsys): def test_perror_no_style(base_app, capsys): msg = 'testing...' end = '\n' - ansi.allow_style = ansi.STYLE_ALWAYS + ansi.allow_style = ansi.AllowStyle.ALWAYS base_app.perror(msg, apply_style=False) out, err = capsys.readouterr() assert err == msg + end @@ -2200,7 +2219,7 @@ def test_perror_no_style(base_app, capsys): def test_pwarning_style(base_app, capsys): msg = 'testing...' end = '\n' - ansi.allow_style = ansi.STYLE_ALWAYS + ansi.allow_style = ansi.AllowStyle.ALWAYS base_app.pwarning(msg) out, err = capsys.readouterr() assert err == ansi.style_warning(msg) + end @@ -2209,7 +2228,7 @@ def test_pwarning_style(base_app, capsys): def test_pwarning_no_style(base_app, capsys): msg = 'testing...' end = '\n' - ansi.allow_style = ansi.STYLE_ALWAYS + ansi.allow_style = ansi.AllowStyle.ALWAYS base_app.pwarning(msg, apply_style=False) out, err = capsys.readouterr() assert err == msg + end @@ -2217,7 +2236,7 @@ def test_pwarning_no_style(base_app, capsys): def test_pexcept_style(base_app, capsys): msg = Exception('testing...') - ansi.allow_style = ansi.STYLE_ALWAYS + ansi.allow_style = ansi.AllowStyle.ALWAYS base_app.pexcept(msg) out, err = capsys.readouterr() @@ -2226,7 +2245,7 @@ def test_pexcept_style(base_app, capsys): def test_pexcept_no_style(base_app, capsys): msg = Exception('testing...') - ansi.allow_style = ansi.STYLE_ALWAYS + ansi.allow_style = ansi.AllowStyle.ALWAYS base_app.pexcept(msg, apply_style=False) out, err = capsys.readouterr() @@ -2236,7 +2255,7 @@ def test_pexcept_no_style(base_app, capsys): def test_pexcept_not_exception(base_app, capsys): # Pass in a msg that is not an Exception object msg = False - ansi.allow_style = ansi.STYLE_ALWAYS + ansi.allow_style = ansi.AllowStyle.ALWAYS base_app.pexcept(msg) out, err = capsys.readouterr() @@ -2268,9 +2287,9 @@ def test_ppaged_none(outsim_app): def test_ppaged_strips_ansi_when_redirecting(outsim_app): msg = 'testing...' end = '\n' - ansi.allow_style = ansi.STYLE_TERMINAL + ansi.allow_style = ansi.AllowStyle.TERMINAL outsim_app._redirecting = True - outsim_app.ppaged(ansi.style(msg, fg='red')) + outsim_app.ppaged(ansi.style(msg, fg=ansi.StdFg.RED)) out = outsim_app.stdout.getvalue() assert out == msg + end @@ -2278,9 +2297,9 @@ def test_ppaged_strips_ansi_when_redirecting(outsim_app): def test_ppaged_strips_ansi_when_redirecting_if_always(outsim_app): msg = 'testing...' end = '\n' - ansi.allow_style = ansi.STYLE_ALWAYS + ansi.allow_style = ansi.AllowStyle.ALWAYS outsim_app._redirecting = True - colored_msg = ansi.style(msg, fg='red') + colored_msg = ansi.style(msg, fg=ansi.StdFg.RED) outsim_app.ppaged(colored_msg) out = outsim_app.stdout.getvalue() assert out == colored_msg + end @@ -2464,14 +2483,14 @@ class AnsiApp(cmd2.Cmd): self.perror(args) def do_echo_error(self, args): - self.poutput(ansi.style(args, fg='red')) + self.poutput(ansi.style(args, fg=ansi.StdFg.RED)) # perror uses colors by default self.perror(args) def test_ansi_pouterr_always_tty(mocker, capsys): app = AnsiApp() - ansi.allow_style = ansi.STYLE_ALWAYS + ansi.allow_style = ansi.AllowStyle.ALWAYS mocker.patch.object(app.stdout, 'isatty', return_value=True) mocker.patch.object(sys.stderr, 'isatty', return_value=True) @@ -2494,7 +2513,7 @@ def test_ansi_pouterr_always_tty(mocker, capsys): def test_ansi_pouterr_always_notty(mocker, capsys): app = AnsiApp() - ansi.allow_style = ansi.STYLE_ALWAYS + ansi.allow_style = ansi.AllowStyle.ALWAYS mocker.patch.object(app.stdout, 'isatty', return_value=False) mocker.patch.object(sys.stderr, 'isatty', return_value=False) @@ -2517,7 +2536,7 @@ def test_ansi_pouterr_always_notty(mocker, capsys): def test_ansi_terminal_tty(mocker, capsys): app = AnsiApp() - ansi.allow_style = ansi.STYLE_TERMINAL + ansi.allow_style = ansi.AllowStyle.TERMINAL mocker.patch.object(app.stdout, 'isatty', return_value=True) mocker.patch.object(sys.stderr, 'isatty', return_value=True) @@ -2539,7 +2558,7 @@ def test_ansi_terminal_tty(mocker, capsys): def test_ansi_terminal_notty(mocker, capsys): app = AnsiApp() - ansi.allow_style = ansi.STYLE_TERMINAL + ansi.allow_style = ansi.AllowStyle.TERMINAL mocker.patch.object(app.stdout, 'isatty', return_value=False) mocker.patch.object(sys.stderr, 'isatty', return_value=False) @@ -2554,7 +2573,7 @@ def test_ansi_terminal_notty(mocker, capsys): def test_ansi_never_tty(mocker, capsys): app = AnsiApp() - ansi.allow_style = ansi.STYLE_NEVER + ansi.allow_style = ansi.AllowStyle.NEVER mocker.patch.object(app.stdout, 'isatty', return_value=True) mocker.patch.object(sys.stderr, 'isatty', return_value=True) @@ -2569,7 +2588,7 @@ def test_ansi_never_tty(mocker, capsys): def test_ansi_never_notty(mocker, capsys): app = AnsiApp() - ansi.allow_style = ansi.STYLE_NEVER + ansi.allow_style = ansi.AllowStyle.NEVER mocker.patch.object(app.stdout, 'isatty', return_value=False) mocker.patch.object(sys.stderr, 'isatty', return_value=False) diff --git a/tests/test_completion.py b/tests/test_completion.py index c61a0eec..2eebaaeb 100755 --- a/tests/test_completion.py +++ b/tests/test_completion.py @@ -271,6 +271,34 @@ def test_cmd2_help_completion_nomatch(cmd2_app): assert first_match is None +def test_set_allow_style_completion(cmd2_app): + """Confirm that completing allow_style presents AllowStyle strings""" + text = '' + line = 'set allow_style'.format(text) + endidx = len(line) + begidx = endidx - len(text) + + expected = [val.name.lower() for val in cmd2.ansi.AllowStyle] + + first_match = complete_tester(text, line, begidx, endidx, cmd2_app) + assert first_match + assert cmd2_app.completion_matches == sorted(expected, key=cmd2_app.default_sort_key) + + +def test_set_bool_completion(cmd2_app): + """Confirm that completing a boolean Settable presents true and false strings""" + text = '' + line = 'set debug'.format(text) + endidx = len(line) + begidx = endidx - len(text) + + expected = ['false', 'true'] + + first_match = complete_tester(text, line, begidx, endidx, cmd2_app) + assert first_match + assert cmd2_app.completion_matches == sorted(expected, key=cmd2_app.default_sort_key) + + def test_shell_command_completion_shortcut(cmd2_app): # Made sure ! runs a shell command and all matches start with ! since there # isn't a space between ! and the shell command. Display matches won't diff --git a/tests/test_table_creator.py b/tests/test_table_creator.py index 5d8fefe9..33f36984 100644 --- a/tests/test_table_creator.py +++ b/tests/test_table_creator.py @@ -6,6 +6,9 @@ Unit testing for cmd2/table_creator.py module import pytest from cmd2 import ( + StdBg, + StdFg, + TextStyle, ansi, ) from cmd2.table_creator import ( @@ -45,7 +48,7 @@ def test_column_creation(): assert tc.cols[0].width == 1 # No width specified, label isn't blank but has no width - c = Column(ansi.style('', fg=ansi.fg.green)) + c = Column(ansi.style('', fg=StdFg.GREEN)) assert c.width < 0 tc = TableCreator([c]) assert tc.cols[0].width == 1 @@ -229,25 +232,25 @@ def test_wrap_long_word(): row_data = list() # Long word should start on the first line (style should not affect width) - row_data.append(ansi.style("LongerThan10", fg=ansi.fg.green)) + row_data.append(ansi.style("LongerThan10", fg=StdFg.GREEN)) # Long word should start on the second line row_data.append("Word LongerThan10") row = tc.generate_row(row_data=row_data) expected = ( - ansi.RESET_ALL - + ansi.fg.green + TextStyle.RESET_ALL + + StdFg.GREEN + "LongerThan" - + ansi.RESET_ALL + + TextStyle.RESET_ALL + " Word \n" - + ansi.RESET_ALL - + ansi.fg.green + + TextStyle.RESET_ALL + + StdFg.GREEN + "10" - + ansi.fg.reset - + ansi.RESET_ALL + + StdFg.RESET + + TextStyle.RESET_ALL + ' ' - + ansi.RESET_ALL + + TextStyle.RESET_ALL + ' LongerThan\n' ' 10 ' ) @@ -598,7 +601,7 @@ def test_alternating_table_creation(): ) # Other bg colors - at = AlternatingTable([column_1, column_2], bg_odd=ansi.bg.bright_blue, bg_even=ansi.bg.green) + at = AlternatingTable([column_1, column_2], bg_odd=StdBg.LIGHT_BLUE, bg_even=StdBg.GREEN) table = at.generate_table(row_data) assert table == ( '╔═════════════════╤═════════════════╗\n' diff --git a/tests/test_utils.py b/tests/test_utils.py index c14c2f07..18be61c8 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -393,11 +393,12 @@ def test_truncate_line_tabs(): def test_truncate_with_style(): from cmd2 import ( - ansi, + StdFg, + TextStyle, ) - before_style = ansi.fg.blue + ansi.UNDERLINE_ENABLE - after_style = ansi.fg.reset + ansi.UNDERLINE_DISABLE + before_style = StdFg.BLUE + TextStyle.UNDERLINE_ENABLE + after_style = StdFg.RESET + TextStyle.UNDERLINE_DISABLE # Style only before truncated text line = before_style + 'long' @@ -428,46 +429,48 @@ def test_align_text_fill_char_is_tab(): def test_align_text_with_style(): from cmd2 import ( - ansi, + StdFg, + TextStyle, + style, ) # Single line with only left fill - text = ansi.style('line1', fg=ansi.fg.bright_blue) - fill_char = ansi.style('-', fg=ansi.fg.bright_yellow) + text = style('line1', fg=StdFg.LIGHT_BLUE) + fill_char = style('-', fg=StdFg.LIGHT_YELLOW) width = 6 aligned = cu.align_text(text, cu.TextAlignment.RIGHT, fill_char=fill_char, width=width) - left_fill = ansi.RESET_ALL + fill_char + ansi.RESET_ALL - right_fill = ansi.RESET_ALL - line_1_text = ansi.fg.bright_blue + 'line1' + ansi.FG_RESET + left_fill = TextStyle.RESET_ALL + fill_char + TextStyle.RESET_ALL + right_fill = TextStyle.RESET_ALL + line_1_text = StdFg.LIGHT_BLUE + 'line1' + StdFg.RESET assert aligned == (left_fill + line_1_text + right_fill) # Single line with only right fill - text = ansi.style('line1', fg=ansi.fg.bright_blue) - fill_char = ansi.style('-', fg=ansi.fg.bright_yellow) + text = style('line1', fg=StdFg.LIGHT_BLUE) + fill_char = style('-', fg=StdFg.LIGHT_YELLOW) width = 6 aligned = cu.align_text(text, cu.TextAlignment.LEFT, fill_char=fill_char, width=width) - left_fill = ansi.RESET_ALL - right_fill = ansi.RESET_ALL + fill_char + ansi.RESET_ALL - line_1_text = ansi.fg.bright_blue + 'line1' + ansi.FG_RESET + left_fill = TextStyle.RESET_ALL + right_fill = TextStyle.RESET_ALL + fill_char + TextStyle.RESET_ALL + line_1_text = StdFg.LIGHT_BLUE + 'line1' + StdFg.RESET assert aligned == (left_fill + line_1_text + right_fill) # Multiple lines to show that style is preserved across all lines. Also has left and right fill. - text = ansi.style('line1\nline2', fg=ansi.fg.bright_blue) - fill_char = ansi.style('-', fg=ansi.fg.bright_yellow) + text = style('line1\nline2', fg=StdFg.LIGHT_BLUE) + fill_char = style('-', fg=StdFg.LIGHT_YELLOW) width = 7 aligned = cu.align_text(text, cu.TextAlignment.CENTER, fill_char=fill_char, width=width) - left_fill = ansi.RESET_ALL + fill_char + ansi.RESET_ALL - right_fill = ansi.RESET_ALL + fill_char + ansi.RESET_ALL - line_1_text = ansi.fg.bright_blue + 'line1' - line_2_text = ansi.fg.bright_blue + 'line2' + ansi.FG_RESET + left_fill = TextStyle.RESET_ALL + fill_char + TextStyle.RESET_ALL + right_fill = TextStyle.RESET_ALL + fill_char + TextStyle.RESET_ALL + line_1_text = StdFg.LIGHT_BLUE + 'line1' + line_2_text = StdFg.LIGHT_BLUE + 'line2' + StdFg.RESET assert aligned == (left_fill + line_1_text + right_fill + '\n' + left_fill + line_2_text + right_fill) diff --git a/tests/transcripts/regex_set.txt b/tests/transcripts/regex_set.txt index 68e61e30..c2a0f091 100644 --- a/tests/transcripts/regex_set.txt +++ b/tests/transcripts/regex_set.txt @@ -13,7 +13,7 @@ now: 'vim' Name Value Description/ +/ ================================================================================================================== allow_style Terminal Allow ANSI text style sequences in output (valid values:/ +/ - Terminal, Always, Never)/ +/ + Always, Never, Terminal)/ +/ always_show_hint False Display tab completion hint even when completion suggestions print/ +/ debug False Show full traceback on exception/ +/ |