Debian Codegraph @

aurora_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 "testing"
40
41 "github.com/stretchr/testify/assert"
42 )
43
44 func isColor(v Value, clr Color) bool {
45 return v.Color() == clr
46 }
47
48 func Test_New(t *testing.T) {
49 var (
50 dconf = NewConfig() // default configurations
51 a = New()
52 )
53 assert.Equal(t, dconf, a.conf, "non-default configurations")
54 // options
55 a = New(WithColors(true), WithHyperlinks(true))
56 assert.True(t, a.conf.Colors)
57 assert.True(t, a.conf.Hyperlinks)
58 // colors
59 a = New(WithColors(false), WithHyperlinks(false))
60 assert.False(t, a.conf.Colors)
61 assert.False(t, a.conf.Hyperlinks)
62
63 }
64
65 func TestAurora_Config(t *testing.T) {
66 assert.Equal(t, NewConfig(), New().Config())
67 }
68
69 func TestAurora_no_colors(t *testing.T) {
70
71 var a = New(WithColors(false), WithHyperlinks(false))
72
73 var test = func(mn string, v Value) {
74 t.Helper()
75 t.Log(mn)
76 assert.Zero(t, v.Color(), "colored")
77 assert.Equal(t, "x", v.Value(), "wrong value")
78 }
79
80 test("Reset", a.Reset("x"))
81 test("Clear", a.Clear("x"))
82
83 test("Bold", a.Bold("x"))
84 test("Faint", a.Faint("x"))
85 test("DoublyUnderline", a.DoublyUnderline("x"))
86 test("Fraktur", a.Fraktur("x"))
87 test("Italic", a.Italic("x"))
88 test("Underline", a.Underline("x"))
89 test("SlowBlink", a.SlowBlink("x"))
90 test("RapidBlink", a.RapidBlink("x"))
91 test("Blink", a.Blink("x"))
92 test("Reverse", a.Reverse("x"))
93 test("Inverse", a.Inverse("x"))
94 test("Conceal", a.Conceal("x"))
95 test("Hidden", a.Hidden("x"))
96 test("CrossedOut", a.CrossedOut("x"))
97 test("StrikeThrough", a.StrikeThrough("x"))
98 test("Framed", a.Framed("x"))
99 test("Encircled", a.Encircled("x"))
100 test("Overlined", a.Overlined("x"))
101
102 test("Black", a.Black("x"))
103 test("Red", a.Red("x"))
104 test("Green", a.Green("x"))
105 test("Yellow", a.Yellow("x"))
106 test("Blue", a.Blue("x"))
107 test("Magenta", a.Magenta("x"))
108 test("Cyan", a.Cyan("x"))
109 test("White", a.White("x"))
110 test("BrightBlack", a.BrightBlack("x"))
111 test("BrightRed", a.BrightRed("x"))
112 test("BrightGreen", a.BrightGreen("x"))
113 test("BrightYellow", a.BrightYellow("x"))
114 test("BrightBlue", a.BrightBlue("x"))
115 test("BrightMagenta", a.BrightMagenta("x"))
116 test("BrightCyan", a.BrightCyan("x"))
117 test("BrightWhite", a.BrightWhite("x"))
118 test("Index", a.Index(178, "x"))
119 test("Gray", a.Gray(14, "x"))
120
121 test("BgBlack", a.BgBlack("x"))
122 test("BgRed", a.BgRed("x"))
123 test("BgGreen", a.BgGreen("x"))
124 test("BgYellow", a.BgYellow("x"))
125 test("BgBlue", a.BgBlue("x"))
126 test("BgMagenta", a.BgMagenta("x"))
127 test("BgCyan", a.BgCyan("x"))
128 test("BgWhite", a.BgWhite("x"))
129 test("BgBrightBlack", a.BgBrightBlack("x"))
130 test("BgBrightRed", a.BgBrightRed("x"))
131 test("BgBrightGreen", a.BgBrightGreen("x"))
132 test("BgBrightYellow", a.BgBrightYellow("x"))
133 test("BgBrightBlue", a.BgBrightBlue("x"))
134 test("BgBrightMagenta", a.BgBrightMagenta("x"))
135 test("BgBrightCyan", a.BgBrightCyan("x"))
136 test("BgBrightWhite", a.BgBrightWhite("x"))
137 test("BgIndex", a.BgIndex(187, "x"))
138 test("BgGray", a.BgGray(15, "x"))
139
140 test("Colorize", a.Colorize("x", RedFg|BlueBg|BrightBg|BoldFm))
141
142 }
143
144 func Test_noColors_sprintf(t *testing.T) {
145 var au = New(WithColors(false))
146 assert.Equal(t, "x: 2", au.Sprintf(au.Black("x: %d"), au.Blue(2)))
147 assert.Equal(t, "x: 2", au.Sprintf("x: %d", au.Blue(2)))
148 }
149
150 func TestAurora_colored(t *testing.T) {
151
152 var a = New() // with colors, with hyperlinks
153
154 var test = func(mn string, v Value, clr Color) {
155 t.Helper()
156 t.Log(mn)
157 assert.Equal(t, clr, v.Color())
158 assert.Equal(t, "x", v.Value(), "wrong value")
159 }
160 test("Reset", a.Reset("x"), 0)
161
162 test("Bold", a.Bold("x"), BoldFm)
163 test("Faint", a.Faint("x"), FaintFm)
164 test("DoublyUnderline", a.DoublyUnderline("x"), DoublyUnderlineFm)
165 test("Fraktur", a.Fraktur("x"), FrakturFm)
166 test("Italic", a.Italic("x"), ItalicFm)
167 test("Underline", a.Underline("x"), UnderlineFm)
168 test("SlowBlink", a.SlowBlink("x"), SlowBlinkFm)
169 test("RapidBlink", a.RapidBlink("x"), RapidBlinkFm)
170 test("Blink", a.Blink("x"), BlinkFm)
171 test("Reverse", a.Reverse("x"), ReverseFm)
172 test("Inverse", a.Inverse("x"), InverseFm)
173 test("Conceal", a.Conceal("x"), ConcealFm)
174 test("Hidden", a.Hidden("x"), HiddenFm)
175 test("CrossedOut", a.CrossedOut("x"), CrossedOutFm)
176 test("StrikeThrough", a.StrikeThrough("x"), StrikeThroughFm)
177 test("Framed", a.Framed("x"), FramedFm)
178 test("Encircled", a.Encircled("x"), EncircledFm)
179 test("Overlined", a.Overlined("x"), OverlinedFm)
180
181 test("Black", a.Black("x"), BlackFg)
182 test("Red", a.Red("x"), RedFg)
183 test("Green", a.Green("x"), GreenFg)
184 test("Yellow", a.Yellow("x"), YellowFg)
185 test("Blue", a.Blue("x"), BlueFg)
186 test("Magenta", a.Magenta("x"), MagentaFg)
187 test("Cyan", a.Cyan("x"), CyanFg)
188 test("White", a.White("x"), WhiteFg)
189 test("BrightBlack", a.BrightBlack("x"), BrightFg|BlackFg)
190 test("BrightRed", a.BrightRed("x"), BrightFg|RedFg)
191 test("BrightGreen", a.BrightGreen("x"), BrightFg|GreenFg)
192 test("BrightYellow", a.BrightYellow("x"), BrightFg|YellowFg)
193 test("BrightBlue", a.BrightBlue("x"), BrightFg|BlueFg)
194 test("BrightMagenta", a.BrightMagenta("x"), BrightFg|MagentaFg)
195 test("BrightCyan", a.BrightCyan("x"), BrightFg|CyanFg)
196 test("BrightWhite", a.BrightWhite("x"), BrightFg|WhiteFg)
197 test("Index", a.Index(178, "x"), (Color(178)<<shiftFg)|flagFg)
198 test("Gray", a.Gray(14, "x"), (Color(232+14)<<shiftFg)|flagFg)
199
200 test("BgBlack", a.BgBlack("x"), BlackBg)
201 test("BgRed", a.BgRed("x"), RedBg)
202 test("BgGreen", a.BgGreen("x"), GreenBg)
203 test("BgYellow", a.BgYellow("x"), YellowBg)
204 test("BgBlue", a.BgBlue("x"), BlueBg)
205 test("BgMagenta", a.BgMagenta("x"), MagentaBg)
206 test("BgCyan", a.BgCyan("x"), CyanBg)
207 test("BgWhite", a.BgWhite("x"), WhiteBg)
208 test("BgBrightBlack", a.BgBrightBlack("x"), BrightBg|BlackBg)
209 test("BgBrightRed", a.BgBrightRed("x"), BrightBg|RedBg)
210 test("BgBrightGreen", a.BgBrightGreen("x"), BrightBg|GreenBg)
211 test("BgBrightYellow", a.BgBrightYellow("x"), BrightBg|YellowBg)
212 test("BgBrightBlue", a.BgBrightBlue("x"), BrightBg|BlueBg)
213 test("BgBrightMagenta", a.BgBrightMagenta("x"), BrightBg|MagentaBg)
214 test("BgBrightCyan", a.BgBrightCyan("x"), BrightBg|CyanBg)
215 test("BgBrightWhite", a.BgBrightWhite("x"), BrightBg|WhiteBg)
216 test("BgIndex", a.BgIndex(187, "x"), (Color(187)<<shiftBg)|flagBg)
217 test("BgGray", a.BgGray(15, "x"), (Color(15+232)<<shiftBg)|flagBg)
218
219 test("Colorize", a.Colorize("x", RedFg|BlueBg|BrightBg|BoldFm),
220 RedFg|BlueBg|BrightBg|BoldFm)
221 }
222
223 func TestAurora_Sprintf(t *testing.T) {
224 var a = New()
225 assert.Equal(t, "\033[30mx: \033[0;34m2\033[0;30mB\033[0m",
226 a.Sprintf(a.Black("x: %dB"), a.Blue(2)))
227 assert.Equal(t, "x: \033[34m2\033[0mB",
228 a.Sprintf("x: %dB", a.Blue(2)))
229 }