Debian Codegraph @

internal/color/color_test.go

raw
1 // Copyright (c) 2019, 2020 Maxime Soulé
2 // All rights reserved.
3 //
4 // This source code is licensed under the BSD-style license found in the
5 // LICENSE file in the root directory of this source tree.
6
7 package color_test
8
9 import (
10 "os"
11 "strings"
12 "testing"
13
14 "github.com/maxatome/go-testdeep/internal/color"
15 "github.com/maxatome/go-testdeep/internal/test"
16 )
17
18 func TestColor(t *testing.T) {
19 defer color.SaveState()()
20
21 // off
22 for _, flag := range []string{"off", "xxbad"} {
23 os.Setenv("TESTDEEP_COLOR", flag) //nolint: errcheck
24 os.Setenv("MY_TEST_COLOR", "green") //nolint: errcheck
25 light, bold, off := color.FromEnv("MY_TEST_COLOR", "red")
26 test.EqualStr(t, light, "")
27 test.EqualStr(t, bold, "")
28 test.EqualStr(t, off, "")
29
30 var b strings.Builder
31 color.AppendTestNameOn(&b)
32 test.EqualInt(t, b.Len(), 0)
33 color.AppendTestNameOff(&b)
34 test.EqualInt(t, b.Len(), 0)
35 }
36
37 // on
38 colorTestNameOnSave, colorTestNameOffSave := color.TestNameOn, color.TestNameOff
39 defer func() {
40 color.TestNameOn, color.TestNameOff = colorTestNameOnSave, colorTestNameOffSave
41 }()
42 for _, flag := range []string{"on", ""} {
43 os.Setenv("TESTDEEP_COLOR", flag) //nolint: errcheck
44 os.Setenv("MY_TEST_COLOR", "") //nolint: errcheck
45 light, bold, off := color.FromEnv("MY_TEST_COLOR", "red")
46 test.EqualStr(t, light, "\x1b[0;31m")
47 test.EqualStr(t, bold, "\x1b[1;31m")
48 test.EqualStr(t, off, "\x1b[0m")
49
50 // on + override
51 os.Setenv("MY_TEST_COLOR", "green") //nolint: errcheck
52 light, bold, off = color.FromEnv("MY_TEST_COLOR", "red")
53 test.EqualStr(t, light, "\x1b[0;32m")
54 test.EqualStr(t, bold, "\x1b[1;32m")
55 test.EqualStr(t, off, "\x1b[0m")
56
57 // on + override including background
58 os.Setenv("MY_TEST_COLOR", "green:magenta") //nolint: errcheck
59 light, bold, off = color.FromEnv("MY_TEST_COLOR", "red")
60 test.EqualStr(t, light, "\x1b[0;32m\x1b[45m")
61 test.EqualStr(t, bold, "\x1b[1;32m\x1b[45m")
62 test.EqualStr(t, off, "\x1b[0m")
63
64 // on + override including background only
65 os.Setenv("MY_TEST_COLOR", ":magenta") //nolint: errcheck
66 light, bold, off = color.FromEnv("MY_TEST_COLOR", "red")
67 test.EqualStr(t, light, "\x1b[45m")
68 test.EqualStr(t, bold, "\x1b[45m")
69 test.EqualStr(t, off, "\x1b[0m")
70
71 // on + bad colors
72 os.Setenv("MY_TEST_COLOR", "foo:bar") //nolint: errcheck
73 light, bold, off = color.FromEnv("MY_TEST_COLOR", "red")
74 test.EqualStr(t, light, "\x1b[0;31m") // red
75 test.EqualStr(t, bold, "\x1b[1;31m") // bold red
76 test.EqualStr(t, off, "\x1b[0m")
77
78 // Color test name
79 _, color.TestNameOn, color.TestNameOff = color.FromEnv(color.EnvColorTitle, "yellow")
80 var b strings.Builder
81 color.AppendTestNameOn(&b)
82 test.EqualStr(t, b.String(), "\x1b[1;33m")
83 color.AppendTestNameOff(&b)
84 test.EqualStr(t, b.String(), "\x1b[1;33m\x1b[0m")
85 }
86 }
87
88 func TestSaveState(t *testing.T) {
89 check := func(expected string) {
90 t.Helper()
91 test.EqualStr(t, os.Getenv("TESTDEEP_COLOR"), expected)
92 }
93
94 defer color.SaveState()()
95 check("off")
96
97 func() {
98 defer color.SaveState(true)()
99 check("on")
100 }()
101 check("off")
102
103 func() {
104 defer color.SaveState(false)()
105 check("off")
106 }()
107 check("off")
108
109 os.Unsetenv("TESTDEEP_COLOR") //nolint: errcheck
110 checkDoesNotExist := func() {
111 t.Helper()
112 _, exists := os.LookupEnv("TESTDEEP_COLOR")
113 test.IsFalse(t, exists)
114 }
115
116 func() {
117 defer color.SaveState(true)()
118 check("on")
119 }()
120 checkDoesNotExist()
121
122 func() {
123 defer color.SaveState(false)()
124 check("off")
125 }()
126 checkDoesNotExist()
127 }
128
129 func TestBad(t *testing.T) {
130 defer color.SaveState()()
131
132 test.EqualStr(t, color.Bad("test"), "test")
133 test.EqualStr(t, color.Bad("test %d", 123), "test 123")
134 }
135
136 func TestBadUsage(t *testing.T) {
137 defer color.SaveState()()
138
139 test.EqualStr(t,
140 color.BadUsage("Zzz(STRING)", nil, 1, true),
141 "usage: Zzz(STRING), but received nil as 1st parameter")
142 }
143
144 func TestTooManyParams(t *testing.T) {
145 defer color.SaveState()()
146
147 test.EqualStr(t, color.TooManyParams("Zzz(PARAM)"),
148 "usage: Zzz(PARAM), too many parameters")
149 }
150
151 func TestUnBad(t *testing.T) {
152 defer color.SaveState(true)()
153
154 const mesg = "test"
155 s := color.Bad(mesg)
156 if s == mesg {
157 t.Errorf("Bad should produce colored output: %s ≠ %s", s, mesg)
158 }
159 test.EqualStr(t, color.UnBad(s), mesg)
160 }