| 1 |
import unittest |
| 2 |
|
| 3 |
from django.utils.termcolors import ( |
| 4 |
DARK_PALETTE, |
| 5 |
DEFAULT_PALETTE, |
| 6 |
LIGHT_PALETTE, |
| 7 |
NOCOLOR_PALETTE, |
| 8 |
PALETTES, |
| 9 |
colorize, |
| 10 |
parse_color_setting, |
| 11 |
) |
| 12 |
|
| 13 |
|
| 14 |
class TermColorTests(unittest.TestCase): |
| 15 |
def test_empty_string(self): |
| 16 |
self.assertEqual(parse_color_setting(""), PALETTES[DEFAULT_PALETTE]) |
| 17 |
|
| 18 |
def test_simple_palette(self): |
| 19 |
self.assertEqual(parse_color_setting("light"), PALETTES[LIGHT_PALETTE]) |
| 20 |
self.assertEqual(parse_color_setting("dark"), PALETTES[DARK_PALETTE]) |
| 21 |
self.assertIsNone(parse_color_setting("nocolor")) |
| 22 |
|
| 23 |
def test_fg(self): |
| 24 |
self.assertEqual( |
| 25 |
parse_color_setting("error=green"), |
| 26 |
dict(PALETTES[NOCOLOR_PALETTE], ERROR={"fg": "green"}), |
| 27 |
) |
| 28 |
|
| 29 |
def test_fg_bg(self): |
| 30 |
self.assertEqual( |
| 31 |
parse_color_setting("error=green/blue"), |
| 32 |
dict(PALETTES[NOCOLOR_PALETTE], ERROR={"fg": "green", "bg": "blue"}), |
| 33 |
) |
| 34 |
|
| 35 |
def test_fg_opts(self): |
| 36 |
self.assertEqual( |
| 37 |
parse_color_setting("error=green,blink"), |
| 38 |
dict(PALETTES[NOCOLOR_PALETTE], ERROR={"fg": "green", "opts": ("blink",)}), |
| 39 |
) |
| 40 |
self.assertEqual( |
| 41 |
parse_color_setting("error=green,bold,blink"), |
| 42 |
dict( |
| 43 |
PALETTES[NOCOLOR_PALETTE], |
| 44 |
ERROR={"fg": "green", "opts": ("blink", "bold")}, |
| 45 |
), |
| 46 |
) |
| 47 |
|
| 48 |
def test_fg_bg_opts(self): |
| 49 |
self.assertEqual( |
| 50 |
parse_color_setting("error=green/blue,blink"), |
| 51 |
dict( |
| 52 |
PALETTES[NOCOLOR_PALETTE], |
| 53 |
ERROR={"fg": "green", "bg": "blue", "opts": ("blink",)}, |
| 54 |
), |
| 55 |
) |
| 56 |
self.assertEqual( |
| 57 |
parse_color_setting("error=green/blue,bold,blink"), |
| 58 |
dict( |
| 59 |
PALETTES[NOCOLOR_PALETTE], |
| 60 |
ERROR={"fg": "green", "bg": "blue", "opts": ("blink", "bold")}, |
| 61 |
), |
| 62 |
) |
| 63 |
|
| 64 |
def test_override_palette(self): |
| 65 |
self.assertEqual( |
| 66 |
parse_color_setting("light;error=green"), |
| 67 |
dict(PALETTES[LIGHT_PALETTE], ERROR={"fg": "green"}), |
| 68 |
) |
| 69 |
|
| 70 |
def test_override_nocolor(self): |
| 71 |
self.assertEqual( |
| 72 |
parse_color_setting("nocolor;error=green"), |
| 73 |
dict(PALETTES[NOCOLOR_PALETTE], ERROR={"fg": "green"}), |
| 74 |
) |
| 75 |
|
| 76 |
def test_reverse_override(self): |
| 77 |
self.assertEqual( |
| 78 |
parse_color_setting("error=green;light"), PALETTES[LIGHT_PALETTE] |
| 79 |
) |
| 80 |
|
| 81 |
def test_multiple_roles(self): |
| 82 |
self.assertEqual( |
| 83 |
parse_color_setting("error=green;sql_field=blue"), |
| 84 |
dict( |
| 85 |
PALETTES[NOCOLOR_PALETTE], |
| 86 |
ERROR={"fg": "green"}, |
| 87 |
SQL_FIELD={"fg": "blue"}, |
| 88 |
), |
| 89 |
) |
| 90 |
|
| 91 |
def test_override_with_multiple_roles(self): |
| 92 |
self.assertEqual( |
| 93 |
parse_color_setting("light;error=green;sql_field=blue"), |
| 94 |
dict( |
| 95 |
PALETTES[LIGHT_PALETTE], ERROR={"fg": "green"}, SQL_FIELD={"fg": "blue"} |
| 96 |
), |
| 97 |
) |
| 98 |
|
| 99 |
def test_empty_definition(self): |
| 100 |
self.assertIsNone(parse_color_setting(";")) |
| 101 |
self.assertEqual(parse_color_setting("light;"), PALETTES[LIGHT_PALETTE]) |
| 102 |
self.assertIsNone(parse_color_setting(";;;")) |
| 103 |
|
| 104 |
def test_empty_options(self): |
| 105 |
self.assertEqual( |
| 106 |
parse_color_setting("error=green,"), |
| 107 |
dict(PALETTES[NOCOLOR_PALETTE], ERROR={"fg": "green"}), |
| 108 |
) |
| 109 |
self.assertEqual( |
| 110 |
parse_color_setting("error=green,,,"), |
| 111 |
dict(PALETTES[NOCOLOR_PALETTE], ERROR={"fg": "green"}), |
| 112 |
) |
| 113 |
self.assertEqual( |
| 114 |
parse_color_setting("error=green,,blink,,"), |
| 115 |
dict(PALETTES[NOCOLOR_PALETTE], ERROR={"fg": "green", "opts": ("blink",)}), |
| 116 |
) |
| 117 |
|
| 118 |
def test_bad_palette(self): |
| 119 |
self.assertIsNone(parse_color_setting("unknown")) |
| 120 |
|
| 121 |
def test_bad_role(self): |
| 122 |
self.assertIsNone(parse_color_setting("unknown=")) |
| 123 |
self.assertIsNone(parse_color_setting("unknown=green")) |
| 124 |
self.assertEqual( |
| 125 |
parse_color_setting("unknown=green;sql_field=blue"), |
| 126 |
dict(PALETTES[NOCOLOR_PALETTE], SQL_FIELD={"fg": "blue"}), |
| 127 |
) |
| 128 |
|
| 129 |
def test_bad_color(self): |
| 130 |
self.assertIsNone(parse_color_setting("error=")) |
| 131 |
self.assertEqual( |
| 132 |
parse_color_setting("error=;sql_field=blue"), |
| 133 |
dict(PALETTES[NOCOLOR_PALETTE], SQL_FIELD={"fg": "blue"}), |
| 134 |
) |
| 135 |
self.assertIsNone(parse_color_setting("error=unknown")) |
| 136 |
self.assertEqual( |
| 137 |
parse_color_setting("error=unknown;sql_field=blue"), |
| 138 |
dict(PALETTES[NOCOLOR_PALETTE], SQL_FIELD={"fg": "blue"}), |
| 139 |
) |
| 140 |
self.assertEqual( |
| 141 |
parse_color_setting("error=green/unknown"), |
| 142 |
dict(PALETTES[NOCOLOR_PALETTE], ERROR={"fg": "green"}), |
| 143 |
) |
| 144 |
self.assertEqual( |
| 145 |
parse_color_setting("error=green/blue/something"), |
| 146 |
dict(PALETTES[NOCOLOR_PALETTE], ERROR={"fg": "green", "bg": "blue"}), |
| 147 |
) |
| 148 |
self.assertEqual( |
| 149 |
parse_color_setting("error=green/blue/something,blink"), |
| 150 |
dict( |
| 151 |
PALETTES[NOCOLOR_PALETTE], |
| 152 |
ERROR={"fg": "green", "bg": "blue", "opts": ("blink",)}, |
| 153 |
), |
| 154 |
) |
| 155 |
|
| 156 |
def test_bad_option(self): |
| 157 |
self.assertEqual( |
| 158 |
parse_color_setting("error=green,unknown"), |
| 159 |
dict(PALETTES[NOCOLOR_PALETTE], ERROR={"fg": "green"}), |
| 160 |
) |
| 161 |
self.assertEqual( |
| 162 |
parse_color_setting("error=green,unknown,blink"), |
| 163 |
dict(PALETTES[NOCOLOR_PALETTE], ERROR={"fg": "green", "opts": ("blink",)}), |
| 164 |
) |
| 165 |
|
| 166 |
def test_role_case(self): |
| 167 |
self.assertEqual( |
| 168 |
parse_color_setting("ERROR=green"), |
| 169 |
dict(PALETTES[NOCOLOR_PALETTE], ERROR={"fg": "green"}), |
| 170 |
) |
| 171 |
self.assertEqual( |
| 172 |
parse_color_setting("eRrOr=green"), |
| 173 |
dict(PALETTES[NOCOLOR_PALETTE], ERROR={"fg": "green"}), |
| 174 |
) |
| 175 |
|
| 176 |
def test_color_case(self): |
| 177 |
self.assertEqual( |
| 178 |
parse_color_setting("error=GREEN"), |
| 179 |
dict(PALETTES[NOCOLOR_PALETTE], ERROR={"fg": "green"}), |
| 180 |
) |
| 181 |
self.assertEqual( |
| 182 |
parse_color_setting("error=GREEN/BLUE"), |
| 183 |
dict(PALETTES[NOCOLOR_PALETTE], ERROR={"fg": "green", "bg": "blue"}), |
| 184 |
) |
| 185 |
self.assertEqual( |
| 186 |
parse_color_setting("error=gReEn"), |
| 187 |
dict(PALETTES[NOCOLOR_PALETTE], ERROR={"fg": "green"}), |
| 188 |
) |
| 189 |
self.assertEqual( |
| 190 |
parse_color_setting("error=gReEn/bLuE"), |
| 191 |
dict(PALETTES[NOCOLOR_PALETTE], ERROR={"fg": "green", "bg": "blue"}), |
| 192 |
) |
| 193 |
|
| 194 |
def test_opts_case(self): |
| 195 |
self.assertEqual( |
| 196 |
parse_color_setting("error=green,BLINK"), |
| 197 |
dict(PALETTES[NOCOLOR_PALETTE], ERROR={"fg": "green", "opts": ("blink",)}), |
| 198 |
) |
| 199 |
self.assertEqual( |
| 200 |
parse_color_setting("error=green,bLiNk"), |
| 201 |
dict(PALETTES[NOCOLOR_PALETTE], ERROR={"fg": "green", "opts": ("blink",)}), |
| 202 |
) |
| 203 |
|
| 204 |
def test_colorize_empty_text(self): |
| 205 |
self.assertEqual(colorize(text=None), "\x1b[m\x1b[0m") |
| 206 |
self.assertEqual(colorize(text=""), "\x1b[m\x1b[0m") |
| 207 |
|
| 208 |
self.assertEqual(colorize(text=None, opts=("noreset",)), "\x1b[m") |
| 209 |
self.assertEqual(colorize(text="", opts=("noreset",)), "\x1b[m") |
| 210 |
|
| 211 |
def test_colorize_reset(self): |
| 212 |
self.assertEqual(colorize(text="", opts=("reset",)), "\x1b[0m") |
| 213 |
|
| 214 |
def test_colorize_fg_bg(self): |
| 215 |
self.assertEqual(colorize(text="Test", fg="red"), "\x1b[31mTest\x1b[0m") |
| 216 |
self.assertEqual(colorize(text="Test", bg="red"), "\x1b[41mTest\x1b[0m") |
| 217 |
# Ignored kwarg. |
| 218 |
self.assertEqual(colorize(text="Test", other="red"), "\x1b[mTest\x1b[0m") |
| 219 |
|
| 220 |
def test_colorize_opts(self): |
| 221 |
self.assertEqual( |
| 222 |
colorize(text="Test", opts=("bold", "underscore")), |
| 223 |
"\x1b[1;4mTest\x1b[0m", |
| 224 |
) |
| 225 |
self.assertEqual( |
| 226 |
colorize(text="Test", opts=("blink",)), |
| 227 |
"\x1b[5mTest\x1b[0m", |
| 228 |
) |
| 229 |
# Ignored opts. |
| 230 |
self.assertEqual( |
| 231 |
colorize(text="Test", opts=("not_an_option",)), |
| 232 |
"\x1b[mTest\x1b[0m", |
| 233 |
) |