1
from unittest import TestCase
3
from django.utils.termcolors import parse_color_setting, PALETTES, DEFAULT_PALETTE, LIGHT_PALETTE, DARK_PALETTE, NOCOLOR_PALETTE
5
class TermColorTests(TestCase):
7
def test_empty_string(self):
8
self.assertEquals(parse_color_setting(''), PALETTES[DEFAULT_PALETTE])
10
def test_simple_palette(self):
11
self.assertEquals(parse_color_setting('light'), PALETTES[LIGHT_PALETTE])
12
self.assertEquals(parse_color_setting('dark'), PALETTES[DARK_PALETTE])
13
self.assertEquals(parse_color_setting('nocolor'), None)
16
self.assertEquals(parse_color_setting('error=green'),
17
dict(PALETTES[NOCOLOR_PALETTE],
18
ERROR={'fg':'green'}))
21
self.assertEquals(parse_color_setting('error=green/blue'),
22
dict(PALETTES[NOCOLOR_PALETTE],
23
ERROR={'fg':'green', 'bg':'blue'}))
25
def test_fg_opts(self):
26
self.assertEquals(parse_color_setting('error=green,blink'),
27
dict(PALETTES[NOCOLOR_PALETTE],
28
ERROR={'fg':'green', 'opts': ('blink',)}))
29
self.assertEquals(parse_color_setting('error=green,bold,blink'),
30
dict(PALETTES[NOCOLOR_PALETTE],
31
ERROR={'fg':'green', 'opts': ('blink','bold')}))
33
def test_fg_bg_opts(self):
34
self.assertEquals(parse_color_setting('error=green/blue,blink'),
35
dict(PALETTES[NOCOLOR_PALETTE],
36
ERROR={'fg':'green', 'bg':'blue', 'opts': ('blink',)}))
37
self.assertEquals(parse_color_setting('error=green/blue,bold,blink'),
38
dict(PALETTES[NOCOLOR_PALETTE],
39
ERROR={'fg':'green', 'bg':'blue', 'opts': ('blink','bold')}))
41
def test_override_palette(self):
42
self.assertEquals(parse_color_setting('light;error=green'),
43
dict(PALETTES[LIGHT_PALETTE],
44
ERROR={'fg':'green'}))
46
def test_override_nocolor(self):
47
self.assertEquals(parse_color_setting('nocolor;error=green'),
48
dict(PALETTES[NOCOLOR_PALETTE],
49
ERROR={'fg': 'green'}))
51
def test_reverse_override(self):
52
self.assertEquals(parse_color_setting('error=green;light'), PALETTES[LIGHT_PALETTE])
54
def test_multiple_roles(self):
55
self.assertEquals(parse_color_setting('error=green;sql_field=blue'),
56
dict(PALETTES[NOCOLOR_PALETTE],
58
SQL_FIELD={'fg':'blue'}))
60
def test_override_with_multiple_roles(self):
61
self.assertEquals(parse_color_setting('light;error=green;sql_field=blue'),
62
dict(PALETTES[LIGHT_PALETTE],
64
SQL_FIELD={'fg':'blue'}))
66
def test_empty_definition(self):
67
self.assertEquals(parse_color_setting(';'), None)
68
self.assertEquals(parse_color_setting('light;'), PALETTES[LIGHT_PALETTE])
69
self.assertEquals(parse_color_setting(';;;'), None)
71
def test_empty_options(self):
72
self.assertEquals(parse_color_setting('error=green,'),
73
dict(PALETTES[NOCOLOR_PALETTE],
74
ERROR={'fg':'green'}))
75
self.assertEquals(parse_color_setting('error=green,,,'),
76
dict(PALETTES[NOCOLOR_PALETTE],
77
ERROR={'fg':'green'}))
78
self.assertEquals(parse_color_setting('error=green,,blink,,'),
79
dict(PALETTES[NOCOLOR_PALETTE],
80
ERROR={'fg':'green', 'opts': ('blink',)}))
82
def test_bad_palette(self):
83
self.assertEquals(parse_color_setting('unknown'), None)
85
def test_bad_role(self):
86
self.assertEquals(parse_color_setting('unknown='), None)
87
self.assertEquals(parse_color_setting('unknown=green'), None)
88
self.assertEquals(parse_color_setting('unknown=green;sql_field=blue'),
89
dict(PALETTES[NOCOLOR_PALETTE],
90
SQL_FIELD={'fg':'blue'}))
92
def test_bad_color(self):
93
self.assertEquals(parse_color_setting('error='), None)
94
self.assertEquals(parse_color_setting('error=;sql_field=blue'),
95
dict(PALETTES[NOCOLOR_PALETTE],
96
SQL_FIELD={'fg':'blue'}))
97
self.assertEquals(parse_color_setting('error=unknown'), None)
98
self.assertEquals(parse_color_setting('error=unknown;sql_field=blue'),
99
dict(PALETTES[NOCOLOR_PALETTE],
100
SQL_FIELD={'fg':'blue'}))
101
self.assertEquals(parse_color_setting('error=green/unknown'),
102
dict(PALETTES[NOCOLOR_PALETTE],
103
ERROR={'fg':'green'}))
104
self.assertEquals(parse_color_setting('error=green/blue/something'),
105
dict(PALETTES[NOCOLOR_PALETTE],
106
ERROR={'fg':'green', 'bg': 'blue'}))
107
self.assertEquals(parse_color_setting('error=green/blue/something,blink'),
108
dict(PALETTES[NOCOLOR_PALETTE],
109
ERROR={'fg':'green', 'bg': 'blue', 'opts': ('blink',)}))
111
def test_bad_option(self):
112
self.assertEquals(parse_color_setting('error=green,unknown'),
113
dict(PALETTES[NOCOLOR_PALETTE],
114
ERROR={'fg':'green'}))
115
self.assertEquals(parse_color_setting('error=green,unknown,blink'),
116
dict(PALETTES[NOCOLOR_PALETTE],
117
ERROR={'fg':'green', 'opts': ('blink',)}))
119
def test_role_case(self):
120
self.assertEquals(parse_color_setting('ERROR=green'),
121
dict(PALETTES[NOCOLOR_PALETTE],
122
ERROR={'fg':'green'}))
123
self.assertEquals(parse_color_setting('eRrOr=green'),
124
dict(PALETTES[NOCOLOR_PALETTE],
125
ERROR={'fg':'green'}))
127
def test_color_case(self):
128
self.assertEquals(parse_color_setting('error=GREEN'),
129
dict(PALETTES[NOCOLOR_PALETTE],
130
ERROR={'fg':'green'}))
131
self.assertEquals(parse_color_setting('error=GREEN/BLUE'),
132
dict(PALETTES[NOCOLOR_PALETTE],
133
ERROR={'fg':'green', 'bg':'blue'}))
135
self.assertEquals(parse_color_setting('error=gReEn'),
136
dict(PALETTES[NOCOLOR_PALETTE],
137
ERROR={'fg':'green'}))
138
self.assertEquals(parse_color_setting('error=gReEn/bLuE'),
139
dict(PALETTES[NOCOLOR_PALETTE],
140
ERROR={'fg':'green', 'bg':'blue'}))
142
def test_opts_case(self):
143
self.assertEquals(parse_color_setting('error=green,BLINK'),
144
dict(PALETTES[NOCOLOR_PALETTE],
145
ERROR={'fg':'green', 'opts': ('blink',)}))
147
self.assertEquals(parse_color_setting('error=green,bLiNk'),
148
dict(PALETTES[NOCOLOR_PALETTE],
149
ERROR={'fg':'green', 'opts': ('blink',)}))