diff options
author | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2019-06-27 13:37:53 -0400 |
---|---|---|
committer | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2019-06-27 13:37:53 -0400 |
commit | 212301954c0584ff250883784463c65252ced63d (patch) | |
tree | 39f165f3ec0037cfba790f8854ee6432896e51aa | |
parent | 0dbb4d208a51b15ca814c00d2eceb43436d15dc1 (diff) | |
download | cmd2-git-212301954c0584ff250883784463c65252ced63d.tar.gz |
Added unit tests for ansi.py
-rw-r--r-- | tests/test_ansi.py | 71 | ||||
-rw-r--r-- | tests/test_cmd2.py | 4 | ||||
-rw-r--r-- | tests/test_utils.py | 32 |
3 files changed, 73 insertions, 34 deletions
diff --git a/tests/test_ansi.py b/tests/test_ansi.py new file mode 100644 index 00000000..0a48d7f5 --- /dev/null +++ b/tests/test_ansi.py @@ -0,0 +1,71 @@ +# coding=utf-8 +# flake8: noqa E302 +""" +Unit testing for cmd2/ansi.py module +""" +import pytest +from colorama import Fore, Back, Style + +import cmd2.ansi as ansi + +HELLO_WORLD = 'Hello, world!' + + +def test_strip_ansi(): + base_str = HELLO_WORLD + ansi_str = Fore.GREEN + base_str + Fore.RESET + assert base_str != ansi_str + assert base_str == ansi.strip_ansi(ansi_str) + + +def test_ansi_safe_wcswidth(): + base_str = HELLO_WORLD + ansi_str = Fore.GREEN + base_str + Fore.RESET + assert ansi.ansi_safe_wcswidth(ansi_str) != len(ansi_str) + + +def test_style_none(): + base_str = HELLO_WORLD + ansi_str = base_str + assert ansi.style(base_str) == ansi_str + + +def test_style_fg(): + base_str = HELLO_WORLD + ansi_str = Fore.BLUE + base_str + Fore.RESET + assert ansi.style(base_str, fg='blue') == ansi_str + + +def test_style_bg(): + base_str = HELLO_WORLD + ansi_str = Back.GREEN + base_str + Back.RESET + assert ansi.style(base_str, bg='green') == ansi_str + + +def test_style_bold(): + base_str = HELLO_WORLD + ansi_str = Style.BRIGHT + base_str + Style.NORMAL + assert ansi.style(base_str, bold=True) == ansi_str + + +def test_style_underline(): + base_str = HELLO_WORLD + ansi_str = ansi.UNDERLINE_ENABLE + base_str + ansi.UNDERLINE_DISABLE + assert ansi.style(base_str, underline=True) == ansi_str + + +def test_style_multi(): + base_str = HELLO_WORLD + ansi_str = Fore.BLUE + Back.GREEN + Style.BRIGHT + ansi.UNDERLINE_ENABLE + \ + base_str + Fore.RESET + Back.RESET + Style.NORMAL + ansi.UNDERLINE_DISABLE + assert ansi.style(base_str, fg='blue', bg='green', bold=True, underline=True) == ansi_str + + +def test_style_color_not_exist(): + base_str = HELLO_WORLD + + with pytest.raises(ValueError): + ansi.style(base_str, fg='fake', bg='green') + + with pytest.raises(ValueError): + ansi.style(base_str, fg='blue', bg='fake') diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py index f56555aa..c820292c 100644 --- a/tests/test_cmd2.py +++ b/tests/test_cmd2.py @@ -1457,7 +1457,7 @@ def test_poutput_none(outsim_app): expected = 'None\n' assert out == expected -def test_poutput_color_always(outsim_app): +def test_poutput_ansi_always(outsim_app): msg = 'Hello World' ansi.allow_ansi = ansi.ANSI_ALWAYS outsim_app.poutput(ansi.style(msg, fg='cyan')) @@ -1465,7 +1465,7 @@ def test_poutput_color_always(outsim_app): expected = Fore.CYAN + msg + Fore.RESET + '\n' assert out == expected -def test_poutput_color_never(outsim_app): +def test_poutput_ansi_never(outsim_app): msg = 'Hello World' ansi.allow_ansi = ansi.ANSI_NEVER outsim_app.poutput(ansi.style(msg, fg='cyan')) diff --git a/tests/test_utils.py b/tests/test_utils.py index a4db3132..db43c292 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -8,43 +8,11 @@ import sys import pytest -from colorama import Fore, Back import cmd2.utils as cu -import cmd2.ansi as ansi HELLO_WORLD = 'Hello, world!' -def test_strip_ansi(): - base_str = HELLO_WORLD - ansi_str = Fore.GREEN + base_str + Fore.RESET - assert base_str != ansi_str - assert base_str == ansi.strip_ansi(ansi_str) - -def test_ansi_safe_wcswidth(): - base_str = HELLO_WORLD - ansi_str = Fore.GREEN + base_str + Fore.RESET - assert ansi.ansi_safe_wcswidth(ansi_str) != len(ansi_str) - -def test_style(): - base_str = HELLO_WORLD - ansi_str = Fore.BLUE + Back.GREEN + base_str + Fore.RESET + Back.RESET - assert ansi.style(base_str, fg='blue', bg='green') == ansi_str - -def test_style_color_not_exist(): - base_str = HELLO_WORLD - try: - ansi.style(base_str, fg='fake', bg='green') - assert False - except ValueError: - assert True - - try: - ansi.style(base_str, fg='blue', bg='fake') - assert False - except ValueError: - assert True - def test_strip_quotes_no_quotes(): base_str = HELLO_WORLD stripped = cu.strip_quotes(base_str) |