Skip to content

Commit 7d90b87

Browse files
authored
Merge pull request #2 from IBM/BetterTestCov
Better test cov
2 parents 3cadd79 + 8bb5302 commit 7d90b87

File tree

5 files changed

+144
-64
lines changed

5 files changed

+144
-64
lines changed

scriptit/color.py

Lines changed: 72 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -19,74 +19,74 @@
1919

2020
# Standard
2121
from enum import Enum
22-
from typing import Union
22+
from typing import Dict, Union
2323

2424
## Public ######################################################################
2525

2626

2727
class Colors(Enum):
28-
black = "black"
29-
dark_gray = "dark_gray"
30-
red = "red"
31-
light_red = "light_red"
32-
green = "green"
33-
light_green = "light_green"
34-
brown = "brown"
35-
orange = "orange"
36-
yellow = "yellow"
37-
blue = "blue"
38-
light_blue = "light_blue"
39-
purple = "purple"
40-
light_purple = "light_purple"
41-
cyan = "cyan"
42-
light_cyan = "light_cyan"
43-
light_gray = "light_gray"
44-
white = "white"
28+
BLACK = "black"
29+
DARK_GRAY = "dark_gray"
30+
RED = "red"
31+
LIGHT_RED = "light_red"
32+
GREEN = "green"
33+
LIGHT_GREEN = "light_green"
34+
BROWN = "brown"
35+
ORANGE = "orange"
36+
YELLOW = "yellow"
37+
BLUE = "blue"
38+
LIGHT_BLUE = "light_blue"
39+
PURPLE = "purple"
40+
LIGHT_PURPLE = "light_purple"
41+
CYAN = "cyan"
42+
LIGHT_CYAN = "light_cyan"
43+
LIGHT_GRAY = "light_gray"
44+
WHITE = "white"
4545

4646

4747
## Mapping from color name to ansi escape code for foreground color
48-
FG_COLOR_CODES = {
49-
Colors.black: "0;30",
50-
Colors.dark_gray: "1;30",
51-
Colors.red: "0;31",
52-
Colors.light_red: "1;31",
53-
Colors.green: "0;32",
54-
Colors.light_green: "1;32",
55-
Colors.brown: "0;33",
56-
Colors.orange: "0;33",
57-
Colors.yellow: "1;33",
58-
Colors.blue: "0;34",
59-
Colors.light_blue: "1;34",
60-
Colors.purple: "0;35",
61-
Colors.light_purple: "1;35",
62-
Colors.cyan: "0;36",
63-
Colors.light_cyan: "1;36",
64-
Colors.light_gray: "0;37",
65-
Colors.white: "1;37",
48+
FG_COLOR_CODES: Dict[Union[Colors, str], str] = {
49+
Colors.BLACK: "0;30",
50+
Colors.DARK_GRAY: "1;30",
51+
Colors.RED: "0;31",
52+
Colors.LIGHT_RED: "1;31",
53+
Colors.GREEN: "0;32",
54+
Colors.LIGHT_GREEN: "1;32",
55+
Colors.BROWN: "0;33",
56+
Colors.ORANGE: "0;33",
57+
Colors.YELLOW: "1;33",
58+
Colors.BLUE: "0;34",
59+
Colors.LIGHT_BLUE: "1;34",
60+
Colors.PURPLE: "0;35",
61+
Colors.LIGHT_PURPLE: "1;35",
62+
Colors.CYAN: "0;36",
63+
Colors.LIGHT_CYAN: "1;36",
64+
Colors.LIGHT_GRAY: "0;37",
65+
Colors.WHITE: "1;37",
6666
}
67-
FG_COLOR_CODES.update(**{key.value: val for key, val in FG_COLOR_CODES.items()})
67+
FG_COLOR_CODES.update(**{entry.value: FG_COLOR_CODES[entry] for entry in Colors})
6868

6969
## Mapping from color name to ansi escape code for background color
70-
BG_COLOR_CODES = {
71-
Colors.black: "0;40",
72-
Colors.dark_gray: "1;40",
73-
Colors.red: "0;41",
74-
Colors.light_red: "1;41",
75-
Colors.green: "0;42",
76-
Colors.light_green: "1;42",
77-
Colors.brown: "0;43",
78-
Colors.orange: "0;43",
79-
Colors.yellow: "1;43",
80-
Colors.blue: "0;44",
81-
Colors.light_blue: "1;44",
82-
Colors.purple: "0;45",
83-
Colors.light_purple: "1;45",
84-
Colors.cyan: "0;46",
85-
Colors.light_cyan: "1;46",
86-
Colors.light_gray: "0;47",
87-
Colors.white: "1;47",
70+
BG_COLOR_CODES: Dict[Union[Colors, str], str] = {
71+
Colors.BLACK: "0;40",
72+
Colors.DARK_GRAY: "1;40",
73+
Colors.RED: "0;41",
74+
Colors.LIGHT_RED: "1;41",
75+
Colors.GREEN: "0;42",
76+
Colors.LIGHT_GREEN: "1;42",
77+
Colors.BROWN: "0;43",
78+
Colors.ORANGE: "0;43",
79+
Colors.YELLOW: "1;43",
80+
Colors.BLUE: "0;44",
81+
Colors.LIGHT_BLUE: "1;44",
82+
Colors.PURPLE: "0;45",
83+
Colors.LIGHT_PURPLE: "1;45",
84+
Colors.CYAN: "0;46",
85+
Colors.LIGHT_CYAN: "1;46",
86+
Colors.LIGHT_GRAY: "0;47",
87+
Colors.WHITE: "1;47",
8888
}
89-
BG_COLOR_CODES.update(**{key.value: val for key, val in BG_COLOR_CODES.items()})
89+
BG_COLOR_CODES.update(**{entry.value: BG_COLOR_CODES[entry] for entry in Colors})
9090

9191
COLOR_START = "\033["
9292
COLOR_END = "\033[0m"
@@ -102,8 +102,7 @@ def colorize(x: str, color: Union[Colors, str]) -> str:
102102
Returns:
103103
x_color (str): The input string with color applied
104104
"""
105-
assert color in FG_COLOR_CODES
106-
return f"{COLOR_START}{FG_COLOR_CODES[color]}m{x}{COLOR_END}"
105+
return _apply_color(x, color, FG_COLOR_CODES)
107106

108107

109108
def bg_colorize(x: str, color: Union[Colors, str]) -> str:
@@ -116,8 +115,7 @@ def bg_colorize(x: str, color: Union[Colors, str]) -> str:
116115
Returns:
117116
x_color (str): The input string with color applied
118117
"""
119-
assert color in BG_COLOR_CODES
120-
return f"{COLOR_START}{BG_COLOR_CODES[color]}m{x}{COLOR_END}"
118+
return _apply_color(x, color, BG_COLOR_CODES)
121119

122120

123121
def decolorize(x: str) -> str:
@@ -135,3 +133,17 @@ def decolorize(x: str) -> str:
135133
x = x.replace(COLOR_END, "")
136134
x = x.replace("0m", "") # TODO: Make this not prone to removing "1.0mb"
137135
return x
136+
137+
138+
## Impl ########################################################################
139+
140+
141+
def _apply_color(
142+
x: str,
143+
color: Union[Colors, str],
144+
color_dict: Dict[Union[Colors, str], str],
145+
) -> str:
146+
"""Apply the color or raise a ValueError"""
147+
if color_code := color_dict.get(color):
148+
return f"{COLOR_START}{color_code}m{x}{COLOR_END}"
149+
raise ValueError(f"Invalid color: {color}")

scriptit/shape.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,11 @@ def progress_bar(
4949
Returns:
5050
progress_bar_str (str): String for the progress bar
5151
"""
52-
assert len(done_char) == 1
53-
assert len(undone_char) == 1
54-
assert len(head_char) == 1
52+
if len(done_char) != 1 or len(undone_char) != 1 or len(head_char) != 1:
53+
raise ValueError("All char args must have length 1")
54+
# Flatten to [0, 1]. This is done instead of raising since this may be
55+
# computed and should not break
56+
complete_pct = max(min(1.0, complete_pct), 0.0)
5557
if width is None:
5658
width = shutil.get_terminal_size().columns
5759
n_done = int((width - 3) * complete_pct)

scripts/run_tests.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ set -e
44
BASE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
55
cd "$BASE_DIR"
66

7-
fail_under=${FAIL_UNDER:-"29"}
7+
fail_under=${FAIL_UNDER:-"36.5"}
88
PYTHONPATH="${BASE_DIR}:$PYTHONPATH" python3 -m pytest \
99
--cov-config=.coveragerc \
1010
--cov=scriptit \

tests/test_color.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"""
2+
Tests for the color tools
3+
"""
4+
5+
# Third Party
6+
import pytest
7+
8+
# Local
9+
from scriptit import color
10+
11+
12+
@pytest.mark.parametrize("color_fn", [color.colorize, color.bg_colorize])
13+
def test_color_round_trip(color_fn):
14+
"""Make sure that the colorize functions round trip with decolorize"""
15+
text = "Hello there world!"
16+
colorized = color_fn(text, "red")
17+
assert colorized != text
18+
assert colorized.startswith(color.COLOR_START)
19+
assert colorized.endswith(color.COLOR_END)
20+
round_trip = color.decolorize(colorized)
21+
assert round_trip == text
22+
23+
24+
@pytest.mark.parametrize("enum_val", color.Colors)
25+
def test_colorize_enum_or_name(enum_val):
26+
"""Make sure that the enum value and the string name can both be used"""
27+
txt = "Lorum ipsum"
28+
assert color.colorize(txt, enum_val) == color.colorize(txt, enum_val.value)
29+
assert color.bg_colorize(txt, enum_val) == color.bg_colorize(txt, enum_val.value)
30+
31+
32+
def test_invalid_color():
33+
"""Make sure that a ValueError is raised on an invalid color"""
34+
with pytest.raises(ValueError):
35+
color.colorize("hey there", "not valid")

tests/test_shape.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""
2+
Tests for the shape module
3+
"""
4+
# Standard
5+
import re
6+
7+
# Third Party
8+
import pytest
9+
10+
# Local
11+
from scriptit import shape
12+
13+
14+
def test_progress_bar():
15+
"""Test various progress_bar configurations"""
16+
assert shape.progress_bar(0.5, 10) == "[===>----]"
17+
assert (
18+
shape.progress_bar(0.5, 10, done_char=">", undone_char=".", head_char="|")
19+
== "[>>>|....]"
20+
)
21+
assert shape.progress_bar(-1, 10) == "[>-------]"
22+
assert shape.progress_bar(2, 10) == "[=======>]"
23+
match = re.match(r"\[(=+)>(-)+\]", shape.progress_bar(0.75))
24+
assert match
25+
assert len(match.group(1)) > len(match.group(2))
26+
with pytest.raises(ValueError):
27+
shape.progress_bar(0.5, 10, done_char="xx")
28+
29+
30+
def test_box():
31+
"""Test various"""

0 commit comments

Comments
 (0)