summaryrefslogtreecommitdiff
path: root/tests/utils_tests/test_termcolors.py
blob: 7ee4f9b0b20c5dfc565d1656e853981306ae6d4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import unittest

from django.utils.termcolors import (
    DARK_PALETTE,
    DEFAULT_PALETTE,
    LIGHT_PALETTE,
    NOCOLOR_PALETTE,
    PALETTES,
    colorize,
    parse_color_setting,
)


class TermColorTests(unittest.TestCase):
    def test_empty_string(self):
        self.assertEqual(parse_color_setting(""), PALETTES[DEFAULT_PALETTE])

    def test_simple_palette(self):
        self.assertEqual(parse_color_setting("light"), PALETTES[LIGHT_PALETTE])
        self.assertEqual(parse_color_setting("dark"), PALETTES[DARK_PALETTE])
        self.assertIsNone(parse_color_setting("nocolor"))

    def test_fg(self):
        self.assertEqual(
            parse_color_setting("error=green"),
            dict(PALETTES[NOCOLOR_PALETTE], ERROR={"fg": "green"}),
        )

    def test_fg_bg(self):
        self.assertEqual(
            parse_color_setting("error=green/blue"),
            dict(PALETTES[NOCOLOR_PALETTE], ERROR={"fg": "green", "bg": "blue"}),
        )

    def test_fg_opts(self):
        self.assertEqual(
            parse_color_setting("error=green,blink"),
            dict(PALETTES[NOCOLOR_PALETTE], ERROR={"fg": "green", "opts": ("blink",)}),
        )
        self.assertEqual(
            parse_color_setting("error=green,bold,blink"),
            dict(
                PALETTES[NOCOLOR_PALETTE],
                ERROR={"fg": "green", "opts": ("blink", "bold")},
            ),
        )

    def test_fg_bg_opts(self):
        self.assertEqual(
            parse_color_setting("error=green/blue,blink"),
            dict(
                PALETTES[NOCOLOR_PALETTE],
                ERROR={"fg": "green", "bg": "blue", "opts": ("blink",)},
            ),
        )
        self.assertEqual(
            parse_color_setting("error=green/blue,bold,blink"),
            dict(
                PALETTES[NOCOLOR_PALETTE],
                ERROR={"fg": "green", "bg": "blue", "opts": ("blink", "bold")},
            ),
        )

    def test_override_palette(self):
        self.assertEqual(
            parse_color_setting("light;error=green"),
            dict(PALETTES[LIGHT_PALETTE], ERROR={"fg": "green"}),
        )

    def test_override_nocolor(self):
        self.assertEqual(
            parse_color_setting("nocolor;error=green"),
            dict(PALETTES[NOCOLOR_PALETTE], ERROR={"fg": "green"}),
        )

    def test_reverse_override(self):
        self.assertEqual(
            parse_color_setting("error=green;light"), PALETTES[LIGHT_PALETTE]
        )

    def test_multiple_roles(self):
        self.assertEqual(
            parse_color_setting("error=green;sql_field=blue"),
            dict(
                PALETTES[NOCOLOR_PALETTE],
                ERROR={"fg": "green"},
                SQL_FIELD={"fg": "blue"},
            ),
        )

    def test_override_with_multiple_roles(self):
        self.assertEqual(
            parse_color_setting("light;error=green;sql_field=blue"),
            dict(
                PALETTES[LIGHT_PALETTE], ERROR={"fg": "green"}, SQL_FIELD={"fg": "blue"}
            ),
        )

    def test_empty_definition(self):
        self.assertIsNone(parse_color_setting(";"))
        self.assertEqual(parse_color_setting("light;"), PALETTES[LIGHT_PALETTE])
        self.assertIsNone(parse_color_setting(";;;"))

    def test_empty_options(self):
        self.assertEqual(
            parse_color_setting("error=green,"),
            dict(PALETTES[NOCOLOR_PALETTE], ERROR={"fg": "green"}),
        )
        self.assertEqual(
            parse_color_setting("error=green,,,"),
            dict(PALETTES[NOCOLOR_PALETTE], ERROR={"fg": "green"}),
        )
        self.assertEqual(
            parse_color_setting("error=green,,blink,,"),
            dict(PALETTES[NOCOLOR_PALETTE], ERROR={"fg": "green", "opts": ("blink",)}),
        )

    def test_bad_palette(self):
        self.assertIsNone(parse_color_setting("unknown"))

    def test_bad_role(self):
        self.assertIsNone(parse_color_setting("unknown="))
        self.assertIsNone(parse_color_setting("unknown=green"))
        self.assertEqual(
            parse_color_setting("unknown=green;sql_field=blue"),
            dict(PALETTES[NOCOLOR_PALETTE], SQL_FIELD={"fg": "blue"}),
        )

    def test_bad_color(self):
        self.assertIsNone(parse_color_setting("error="))
        self.assertEqual(
            parse_color_setting("error=;sql_field=blue"),
            dict(PALETTES[NOCOLOR_PALETTE], SQL_FIELD={"fg": "blue"}),
        )
        self.assertIsNone(parse_color_setting("error=unknown"))
        self.assertEqual(
            parse_color_setting("error=unknown;sql_field=blue"),
            dict(PALETTES[NOCOLOR_PALETTE], SQL_FIELD={"fg": "blue"}),
        )
        self.assertEqual(
            parse_color_setting("error=green/unknown"),
            dict(PALETTES[NOCOLOR_PALETTE], ERROR={"fg": "green"}),
        )
        self.assertEqual(
            parse_color_setting("error=green/blue/something"),
            dict(PALETTES[NOCOLOR_PALETTE], ERROR={"fg": "green", "bg": "blue"}),
        )
        self.assertEqual(
            parse_color_setting("error=green/blue/something,blink"),
            dict(
                PALETTES[NOCOLOR_PALETTE],
                ERROR={"fg": "green", "bg": "blue", "opts": ("blink",)},
            ),
        )

    def test_bad_option(self):
        self.assertEqual(
            parse_color_setting("error=green,unknown"),
            dict(PALETTES[NOCOLOR_PALETTE], ERROR={"fg": "green"}),
        )
        self.assertEqual(
            parse_color_setting("error=green,unknown,blink"),
            dict(PALETTES[NOCOLOR_PALETTE], ERROR={"fg": "green", "opts": ("blink",)}),
        )

    def test_role_case(self):
        self.assertEqual(
            parse_color_setting("ERROR=green"),
            dict(PALETTES[NOCOLOR_PALETTE], ERROR={"fg": "green"}),
        )
        self.assertEqual(
            parse_color_setting("eRrOr=green"),
            dict(PALETTES[NOCOLOR_PALETTE], ERROR={"fg": "green"}),
        )

    def test_color_case(self):
        self.assertEqual(
            parse_color_setting("error=GREEN"),
            dict(PALETTES[NOCOLOR_PALETTE], ERROR={"fg": "green"}),
        )
        self.assertEqual(
            parse_color_setting("error=GREEN/BLUE"),
            dict(PALETTES[NOCOLOR_PALETTE], ERROR={"fg": "green", "bg": "blue"}),
        )
        self.assertEqual(
            parse_color_setting("error=gReEn"),
            dict(PALETTES[NOCOLOR_PALETTE], ERROR={"fg": "green"}),
        )
        self.assertEqual(
            parse_color_setting("error=gReEn/bLuE"),
            dict(PALETTES[NOCOLOR_PALETTE], ERROR={"fg": "green", "bg": "blue"}),
        )

    def test_opts_case(self):
        self.assertEqual(
            parse_color_setting("error=green,BLINK"),
            dict(PALETTES[NOCOLOR_PALETTE], ERROR={"fg": "green", "opts": ("blink",)}),
        )
        self.assertEqual(
            parse_color_setting("error=green,bLiNk"),
            dict(PALETTES[NOCOLOR_PALETTE], ERROR={"fg": "green", "opts": ("blink",)}),
        )

    def test_colorize_empty_text(self):
        self.assertEqual(colorize(text=None), "\x1b[m\x1b[0m")
        self.assertEqual(colorize(text=""), "\x1b[m\x1b[0m")

        self.assertEqual(colorize(text=None, opts=("noreset",)), "\x1b[m")
        self.assertEqual(colorize(text="", opts=("noreset",)), "\x1b[m")

    def test_colorize_reset(self):
        self.assertEqual(colorize(text="", opts=("reset",)), "\x1b[0m")

    def test_colorize_fg_bg(self):
        self.assertEqual(colorize(text="Test", fg="red"), "\x1b[31mTest\x1b[0m")
        self.assertEqual(colorize(text="Test", bg="red"), "\x1b[41mTest\x1b[0m")
        # Ignored kwarg.
        self.assertEqual(colorize(text="Test", other="red"), "\x1b[mTest\x1b[0m")

    def test_colorize_opts(self):
        self.assertEqual(
            colorize(text="Test", opts=("bold", "underscore")),
            "\x1b[1;4mTest\x1b[0m",
        )
        self.assertEqual(
            colorize(text="Test", opts=("blink",)),
            "\x1b[5mTest\x1b[0m",
        )
        # Ignored opts.
        self.assertEqual(
            colorize(text="Test", opts=("not_an_option",)),
            "\x1b[mTest\x1b[0m",
        )