Debian Codegraph @

color_test.go

raw
1 //
2 // Copyright (c) 2016-2022 The Aurora Authors. All rights reserved.
3 // This program is free software. It comes without any warranty,
4 // to the extent permitted by applicable law. You can redistribute
5 // it and/or modify it under the terms of the Unlicense. See LICENSE
6 // file for more details or see below.
7 //
8
9 //
10 // This is free and unencumbered software released into the public domain.
11 //
12 // Anyone is free to copy, modify, publish, use, compile, sell, or
13 // distribute this software, either in source code form or as a compiled
14 // binary, for any purpose, commercial or non-commercial, and by any
15 // means.
16 //
17 // In jurisdictions that recognize copyright laws, the author or authors
18 // of this software dedicate any and all copyright interest in the
19 // software to the public domain. We make this dedication for the benefit
20 // of the public at large and to the detriment of our heirs and
21 // successors. We intend this dedication to be an overt act of
22 // relinquishment in perpetuity of all present and future rights to this
23 // software under copyright law.
24 //
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
28 // IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
29 // OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
30 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
31 // OTHER DEALINGS IN THE SOFTWARE.
32 //
33 // For more information, please refer to <http://unlicense.org/>
34 //
35
36 package aurora
37
38 import (
39 "math"
40 "strconv"
41 "testing"
42
43 "github.com/stretchr/testify/assert"
44 )
45
46 func TestColor_Nos(t *testing.T) {
47
48 for _, zero := range []bool{
49 false, true,
50 } {
51 for i, val := range []struct {
52 Nos string
53 Color Color
54 }{
55 {"1", BoldFm},
56 {"2", FaintFm},
57 {"3", ItalicFm},
58 {"4", UnderlineFm},
59 {"5", SlowBlinkFm},
60 {"6", RapidBlinkFm},
61 {"7", ReverseFm},
62 {"8", ConcealFm},
63 {"9", CrossedOutFm},
64
65 {"20", FrakturFm},
66 {"21", DoublyUnderlineFm},
67
68 {"51", FramedFm},
69 {"52", EncircledFm},
70 {"53", OverlinedFm},
71
72 {"30", BlackFg},
73 {"31", RedFg},
74 {"32", GreenFg},
75 {"33", YellowFg},
76 {"34", BlueFg},
77 {"35", MagentaFg},
78 {"36", CyanFg},
79 {"37", WhiteFg},
80
81 {"90", BlackFg | BrightFg},
82 {"91", RedFg | BrightFg},
83 {"92", GreenFg | BrightFg},
84 {"93", YellowFg | BrightFg},
85 {"94", BlueFg | BrightFg},
86 {"95", MagentaFg | BrightFg},
87 {"96", CyanFg | BrightFg},
88 {"97", WhiteFg | BrightFg},
89
90 {"90", BrightFg},
91
92 {"40", BlackBg},
93 {"41", RedBg},
94 {"42", GreenBg},
95 {"43", YellowBg},
96 {"44", BlueBg},
97 {"45", MagentaBg},
98 {"46", CyanBg},
99 {"47", WhiteBg},
100
101 {"100", BlackBg | BrightBg},
102 {"101", RedBg | BrightBg},
103 {"102", GreenBg | BrightBg},
104 {"103", YellowBg | BrightBg},
105 {"104", BlueBg | BrightBg},
106 {"105", MagentaBg | BrightBg},
107 {"106", CyanBg | BrightBg},
108 {"107", WhiteBg | BrightBg},
109
110 {"100", BrightBg},
111
112 // bold and faint
113
114 {"1", BoldFm | FaintFm},
115
116 // slow blink and rapid blink
117
118 {"5", SlowBlinkFm | RapidBlinkFm},
119
120 // index
121
122 {"38;5;100", (100 << shiftFg) | flagFg},
123 {"48;5;100", (100 << shiftBg) | flagBg},
124
125 // longest combination
126
127 {"1;3;4;5;7;8;9;20;21;51;52;53;38;5;123;48;5;231",
128 BoldFm | FaintFm |
129 ItalicFm | UnderlineFm |
130 SlowBlinkFm | RapidBlinkFm |
131 ReverseFm | ConcealFm |
132 CrossedOutFm | FrakturFm | DoublyUnderlineFm |
133 FramedFm | EncircledFm | OverlinedFm |
134 Color(123)<<shiftFg | flagFg |
135 Color(231)<<shiftBg | flagBg},
136 } {
137 var (
138 nos = val.Color.Nos(zero)
139 want = val.Nos
140 )
141 if zero {
142 if want != "" {
143 want = "0;" + want
144 } else {
145 want = "0"
146 }
147 }
148 assert.Equalf(t, want, nos, "%t %d: wrong nos string %q, want %q",
149 zero, i, nos, want)
150 }
151 }
152 }
153
154 func Test_itoa(t *testing.T) {
155 for i := 0; i < 256; i++ {
156 var a = itoa(byte(i))
157 assert.Equalf(t, a, strconv.Itoa(i), "wrong %q, want %d", a, i)
158 }
159 }
160
161 func TestColor_Reset(t *testing.T) {
162 var c Color = math.MaxUint // all bits set to 1
163 assert.Zero(t, c.Reset())
164 }
165
166 func TestColor_Bold(t *testing.T) {
167 assert.True(t, Color(0).Bold()&BoldFm != 0, "not a bold")
168 assert.True(t, Color(FaintFm).Bold()&FaintFm == 0, "contains faint")
169 }
170
171 func TestColor_Faint(t *testing.T) {
172 assert.True(t, Color(0).Faint()&FaintFm != 0, "not a faint")
173 assert.True(t, Color(BoldFm).Faint()&BoldFm == 0, "contains bold")
174 }
175
176 func TestColor_DoublyUnderline(t *testing.T) {
177 assert.True(t, Color(0).DoublyUnderline()&DoublyUnderlineFm != 0,
178 "not a doubly-underlined")
179 assert.True(t, Color(UnderlineFm).DoublyUnderline()&UnderlineFm == 0,
180 "contains underline")
181 }
182
183 func TestColor_Fraktur(t *testing.T) {
184 assert.True(t, Color(0).Fraktur()&FrakturFm != 0, "not a fraktur")
185 }
186
187 func TestColor_Italic(t *testing.T) {
188 assert.True(t, Color(0).Italic()&ItalicFm != 0, "not a italic")
189 }
190
191 func TestColor_Underline(t *testing.T) {
192 assert.True(t, Color(0).Underline()&UnderlineFm != 0, "not a underlined")
193 }
194
195 func TestColor_SlowBlink(t *testing.T) {
196 assert.True(t, Color(0).SlowBlink()&SlowBlinkFm != 0, "not a slow blinking")
197 }
198
199 func TestColor_RapidBlink(t *testing.T) {
200 assert.True(t, Color(0).RapidBlink()&RapidBlinkFm != 0,
201 "not a rapid blinking")
202 }
203
204 func TestColor_Blink(t *testing.T) {
205 assert.True(t, Color(0).Blink()&BlinkFm != 0, "not a blinking")
206 }
207
208 func TestColor_Reverse(t *testing.T) {
209 assert.True(t, Color(0).Reverse()&ReverseFm != 0, "not a reversed")
210 }
211
212 func TestColor_Inverse(t *testing.T) {
213 assert.True(t, Color(0).Inverse()&InverseFm != 0, "not a inversed")
214 }
215
216 func TestColor_Conceal(t *testing.T) {
217 assert.True(t, Color(0).Conceal()&ConcealFm != 0, "not a concealed")
218 }
219
220 func TestColor_Hidden(t *testing.T) {
221 assert.True(t, Color(0).Hidden()&HiddenFm != 0, "not a hidden")
222 }
223
224 func TestColor_CrossedOut(t *testing.T) {
225 assert.True(t, Color(0).CrossedOut()&CrossedOutFm != 0, "not a crossed out")
226 }
227
228 func TestColor_StrikeThrough(t *testing.T) {
229 assert.True(t, Color(0).StrikeThrough()&StrikeThroughFm != 0,
230 "not a striked through")
231 }
232
233 func TestColor_Framed(t *testing.T) {
234 assert.True(t, Color(0).Framed()&FramedFm != 0, "not a framed")
235 }
236
237 func TestColor_Encircled(t *testing.T) {
238 assert.True(t, Color(0).Encircled()&EncircledFm != 0, "not a encircled")
239 }
240
241 func TestColor_Overlined(t *testing.T) {
242 assert.True(t, Color(0).Overlined()&OverlinedFm != 0, "not a overlined")
243 }
244
245 func TestColor_Black(t *testing.T) {
246 assert.True(t, Color(0).Black()&BlackFg != 0, "not a black")
247 assert.True(t, Color(RedFg).Black()&RedFg == flagFg, "contains red")
248 }
249
250 func TestColor_Red(t *testing.T) {
251 assert.True(t, Color(0).Red()&RedFg != 0, "not a red")
252 assert.True(t, Color(BlackFg).Red()&BlackFg == flagFg, "contains black")
253 }
254
255 func TestColor_Green(t *testing.T) {
256 assert.True(t, Color(0).Green()&GreenFg != 0, "not a green")
257 assert.True(t, Color(BlackFg).Green()&BlackFg == flagFg, "contains black")
258 }
259
260 func TestColor_Yellow(t *testing.T) {
261 assert.True(t, Color(0).Yellow()&YellowFg != 0, "not a yellow")
262 assert.True(t, Color(BlackFg).Yellow()&BlackFg == flagFg, "contains black")
263 }
264
265 func TestColor_Blue(t *testing.T) {
266 assert.True(t, Color(0).Blue()&BlueFg != 0, "not a blue")
267 assert.True(t, Color(BlackFg).Blue()&BlackFg == flagFg, "contains black")
268 }
269
270 func TestColor_Magenta(t *testing.T) {
271 assert.True(t, Color(0).Magenta()&MagentaFg != 0, "not a magenta")
272 assert.True(t, Color(BlackFg).Magenta()&BlackFg == flagFg, "contains black")
273 }
274
275 func TestColor_Cyan(t *testing.T) {
276 assert.True(t, Color(0).Cyan()&CyanFg != 0, "not a cyan")
277 assert.True(t, Color(BlackFg).Cyan()&BlackFg == flagFg, "contains black")
278 }
279
280 func TestColor_White(t *testing.T) {
281 assert.True(t, Color(0).White()&WhiteFg != 0, "not a white")
282 assert.True(t, Color(BlackFg).White()&BlackFg == flagFg, "contains black")
283 }
284
285 func TestColor_BrightBlack(t *testing.T) {
286 assert.True(t, Color(0).BrightBlack()&(BrightFg|BlackFg) != 0,
287 "not a bright black")
288 assert.True(t, Color(RedFg).BrightBlack()&RedFg == flagFg, "contains red")
289 }
290
291 func TestColor_BrightRed(t *testing.T) {
292 assert.True(t, Color(0).BrightRed()&(BrightFg|RedFg) != 0,
293 "not a bright red")
294 assert.True(t, Color(BlackFg).BrightRed()&BlackFg == flagFg,
295 "contains black")
296 }
297
298 func TestColor_BrightGreen(t *testing.T) {
299 assert.True(t, Color(0).BrightGreen()&(BrightFg|GreenFg) != 0,
300 "not a bright green")
301 assert.True(t, Color(BlackFg).BrightGreen()&BlackFg == flagFg,
302 "contains black")
303 }
304
305 func TestColor_BrightYellow(t *testing.T) {
306 assert.True(t, Color(0).BrightYellow()&(BrightFg|YellowFg) != 0,
307 "not a bright yellow")
308 assert.True(t, Color(BlackFg).BrightYellow()&BlackFg == flagFg,
309 "contains black")
310 }
311
312 func TestColor_BrightBlue(t *testing.T) {
313 assert.True(t, Color(0).BrightBlue()&(BrightFg|BlueFg) != 0,
314 "not a bright blue")
315 assert.True(t, Color(BlackFg).BrightBlue()&BlackFg == flagFg,
316 "contains black")
317 }
318
319 func TestColor_BrightMagenta(t *testing.T) {
320 assert.True(t, Color(0).BrightMagenta()&(BrightFg|MagentaFg) != 0,
321 "not a bright blue")
322 assert.True(t, Color(BlackFg).BrightMagenta()&BlackFg == flagFg,
323 "contains black")
324 }
325
326 func TestColor_BrightCyan(t *testing.T) {
327 assert.True(t, Color(0).BrightCyan()&(BrightFg|CyanFg) != 0,
328 "not a bright cyan")
329 assert.True(t, Color(BlackFg).BrightCyan()&BlackFg == flagFg,
330 "contains black")
331 }
332
333 func TestColor_BrightWhite(t *testing.T) {
334 assert.True(t, Color(0).BrightWhite()&(BrightFg|WhiteFg) != 0,
335 "not a bright white")
336 assert.True(t, Color(BlackFg).BrightWhite()&BlackFg == flagFg,
337 "contains black")
338 }
339
340 func TestColor_Index(t *testing.T) {
341 for i := 0; i < 256; i++ {
342 var ci = ColorIndex(i)
343 assert.Truef(t, Color(0).Index(ci)&flagFg != 0,
344 "missing indexed color, color index %d", i)
345 assert.Truef(t, Color(BlackFg).Index(ci)&BlackFg == flagFg,
346 "contains black, color index %d", i)
347 }
348 }
349
350 func TestColor_Gray(t *testing.T) {
351 for i := GrayIndex(0); i < 25; i++ {
352 assert.Truef(t, Color(0).Gray(i)&flagFg != 0,
353 "missing indexed gray color, gray index %d", i)
354 assert.Truef(t, Color(BlackFg).Gray(i)&BlackFg == flagFg,
355 "contains black, gray index %d", i)
356 }
357 }
358
359 func TestColor_BgBlack(t *testing.T) {
360 assert.True(t, Color(0).BgBlack()&BlackBg != 0, "not a black background")
361 assert.True(t, Color(RedBg).BgBlack()&RedBg == flagBg,
362 "contains red background")
363 }
364
365 func TestColor_BgRed(t *testing.T) {
366 assert.True(t, Color(0).BgRed()&RedBg != 0, "not a red background")
367 assert.True(t, Color(BlackBg).BgRed()&BlackBg == flagBg,
368 "contains black background")
369 }
370
371 func TestColor_BgGreen(t *testing.T) {
372 assert.True(t, Color(0).BgGreen()&GreenBg != 0, "not a green background")
373 assert.True(t, Color(BlackBg).BgGreen()&BlackBg == flagBg,
374 "contains black background")
375 }
376
377 func TestColor_BgYellow(t *testing.T) {
378 assert.True(t, Color(0).BgYellow()&YellowBg != 0, "not a yellow background")
379 assert.True(t, Color(BlackBg).BgYellow()&BlackBg == flagBg,
380 "contains black background")
381 }
382
383 func TestColor_BgBlue(t *testing.T) {
384 assert.True(t, Color(0).BgBlue()&BlueBg != 0, "not a blue background")
385 assert.True(t, Color(BlackBg).BgBlue()&BlackBg == flagBg,
386 "contains black background")
387 }
388
389 func TestColor_BgMagenta(t *testing.T) {
390 assert.True(t, Color(0).BgMagenta()&MagentaBg != 0,
391 "not a magenta background")
392 assert.True(t, Color(BlackBg).BgMagenta()&BlackBg == flagBg,
393 "contains black background")
394 }
395
396 func TestColor_BgCyan(t *testing.T) {
397 assert.True(t, Color(0).BgCyan()&CyanBg != 0, "not a cyan background")
398 assert.True(t, Color(BlackBg).BgCyan()&BlackBg == flagBg,
399 "contains black background")
400 }
401
402 func TestColor_BgWhite(t *testing.T) {
403 assert.True(t, Color(0).BgWhite()&WhiteBg != 0, "not a white background")
404 assert.True(t, Color(BlackBg).BgWhite()&BlackBg == flagBg,
405 "contains black background")
406 }
407
408 func TestColor_BgBrightBlack(t *testing.T) {
409 assert.True(t, Color(0).BgBrightBlack()&(BrightBg|BlackBg) != 0,
410 "not a bright black background")
411 assert.True(t, Color(RedBg).BgBrightBlack()&RedBg == flagBg,
412 "contains red background")
413 }
414
415 func TestColor_BgBrightRed(t *testing.T) {
416 assert.True(t, Color(0).BgBrightRed()&(BrightBg|RedBg) != 0,
417 "not a bright red background")
418 assert.True(t, Color(BlackBg).BgBrightRed()&BlackBg == flagBg,
419 "contains black background")
420 }
421
422 func TestColor_BgBrightGreen(t *testing.T) {
423 assert.True(t, Color(0).BgBrightGreen()&(BrightBg|GreenBg) != 0,
424 "not a bright green background")
425 assert.True(t, Color(BlackBg).BgBrightGreen()&BlackBg == flagBg,
426 "contains black background")
427 }
428
429 func TestColor_BgBrightYellow(t *testing.T) {
430 assert.True(t, Color(0).BgBrightYellow()&(BrightBg|YellowBg) != 0,
431 "not a bright yellow background")
432 assert.True(t, Color(BlackBg).BgBrightYellow()&BlackBg == flagBg,
433 "contains black background")
434 }
435
436 func TestColor_BgBrightBlue(t *testing.T) {
437 assert.True(t, Color(0).BgBrightBlue()&(BrightBg|BlueBg) != 0,
438 "not a bright blue background")
439 assert.True(t, Color(BlackBg).BgBrightBlue()&BlackBg == flagBg,
440 "contains black background")
441 }
442
443 func TestColor_BgBrightMagenta(t *testing.T) {
444 assert.True(t, Color(0).BgBrightMagenta()&(BrightBg|MagentaBg) != 0,
445 "not a bright magenta background")
446 assert.True(t, Color(BlackBg).BgBrightMagenta()&BlackBg == flagBg,
447 "contains black background")
448 }
449
450 func TestColor_BgBrightCyan(t *testing.T) {
451 assert.True(t, Color(0).BgBrightCyan()&(BrightBg|CyanBg) != 0,
452 "not a bright cyan background")
453 assert.True(t, Color(BlackBg).BgBrightCyan()&BlackBg == flagBg,
454 "contains black background")
455 }
456
457 func TestColor_BgBrightWhite(t *testing.T) {
458 assert.True(t, Color(0).BgBrightWhite()&(BrightBg|WhiteBg) != 0,
459 "not a bright white background")
460 assert.True(t, Color(BlackBg).BgBrightWhite()&BlackBg == flagBg,
461 "contains black background")
462 }
463
464 func TestColor_BgIndex(t *testing.T) {
465 for i := 0; i < 256; i++ {
466 var ci = ColorIndex(i)
467 assert.True(t, Color(0).BgIndex(ci)&(flagBg) != 0,
468 "missing indexed background color")
469 assert.True(t, Color(BlackBg).BgIndex(ci)&BlackBg == flagBg,
470 "contains black background")
471 }
472 }
473
474 func TestColor_BgGray(t *testing.T) {
475 for i := GrayIndex(0); i <= 25; i++ {
476 assert.Truef(t, Color(0).BgGray(i)&(flagBg) != 0,
477 "missing indexed gray background color, gray index %d", i)
478 assert.Truef(t, Color(BlackBg).BgGray(i)&BlackBg == flagBg,
479 "contains black background, gray index %d", i)
480 }
481 }