summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKevin Van Brunt <kmvanbrunt@gmail.com>2021-10-27 12:10:45 -0400
committerKevin Van Brunt <kmvanbrunt@gmail.com>2021-10-27 12:10:45 -0400
commitd2c97338d395f4ce2439cf6ccde0aabac413d959 (patch)
tree1a898cd927629345a948955c6b453147e6acffcf
parent0d60017ba650ce2c8eedf2de7317477292e80056 (diff)
downloadcmd2-git-d2c97338d395f4ce2439cf6ccde0aabac413d959.tar.gz
Added type checks to ansi.style()
-rw-r--r--cmd2/ansi.py6
-rw-r--r--tests/test_ansi.py12
2 files changed, 17 insertions, 1 deletions
diff --git a/cmd2/ansi.py b/cmd2/ansi.py
index 5517ecf0..29d09fff 100644
--- a/cmd2/ansi.py
+++ b/cmd2/ansi.py
@@ -970,6 +970,8 @@ def style(
:param overline: apply the overline style if True. Defaults to False.
:param strikethrough: apply the strikethrough style if True. Defaults to False.
:param underline: apply the underline style if True. Defaults to False.
+ :raises: TypeError if fg isn't None or a subclass of FgColor
+ :raises: TypeError if bg isn't None or a subclass of BgColor
:return: the stylized string
"""
# List of strings that add style
@@ -980,10 +982,14 @@ def style(
# Process the style settings
if fg is not None:
+ if not isinstance(fg, FgColor):
+ raise TypeError("fg must be a subclass of FgColor")
additions.append(fg)
removals.append(Fg.RESET)
if bg is not None:
+ if not isinstance(bg, BgColor):
+ raise TypeError("bg must a subclass of BgColor")
additions.append(bg)
removals.append(Bg.RESET)
diff --git a/tests/test_ansi.py b/tests/test_ansi.py
index d94fd91a..bed60eab 100644
--- a/tests/test_ansi.py
+++ b/tests/test_ansi.py
@@ -58,6 +58,17 @@ def test_style_bg(bg_color):
assert ansi.style(base_str, bg=bg_color) == ansi_str
+# noinspection PyTypeChecker
+def test_style_invalid_types():
+ # Use a BgColor with fg
+ with pytest.raises(TypeError):
+ ansi.style('test', fg=ansi.Bg.BLUE)
+
+ # Use a FgColor with bg
+ with pytest.raises(TypeError):
+ ansi.style('test', bg=ansi.Fg.BLUE)
+
+
def test_style_bold():
base_str = HELLO_WORLD
ansi_str = ansi.TextStyle.INTENSITY_BOLD + base_str + ansi.TextStyle.INTENSITY_NORMAL
@@ -236,7 +247,6 @@ def test_sequence_str_building(ansi_sequence):
],
)
def test_rgb_bounds(r, g, b, valid):
-
if valid:
ansi.RgbFg(r, g, b)
ansi.RgbBg(r, g, b)