File: style_test.go

package info (click to toggle)
golang-github-gookit-color 1.5.4-3
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 2,708 kB
  • sloc: sh: 101; makefile: 7
file content (229 lines) | stat: -rw-r--r-- 5,039 bytes parent folder | download
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
package color

import (
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestNew(t *testing.T) {
	is := assert.New(t)

	s := New(Red)
	is.Equal("31", s.String())

	s.Add(OpBold)
	is.Equal("31;1", s.String())

	s.Add(BgCyan)
	is.Equal("31;1;46", s.String())
}

func TestStyle(t *testing.T) {
	// force open color render for testing
	buf := forceOpenColorRender()
	defer resetColorRender()

	is := assert.New(t)

	// IsEmpty
	s := Style{}
	is.True(s.IsEmpty())
	is.Equal("", s.String())

	is.Equal("97;40", Light.Code())
	is.Equal("97;40", Light.String())
	str := Light.Render("msg")
	is.Contains(str, "97")

	str = Danger.Sprint("msg")
	is.Contains(str, FgRed.String())

	str = Question.Render("msg")
	is.Contains(str, FgMagenta.String())

	str = Question.Render("msg", "More")
	is.Contains(str, FgMagenta.String())
	is.Contains(str, "msgMore")

	str = Question.Renderln("msg", "More")
	is.Contains(str, FgMagenta.String())
	is.Contains(str, "msg More")

	str = Secondary.Sprintf("m%s", "sg")
	is.Contains(str, FgDarkGray.String())

	// Style.Print
	Info.Print("MSG")
	is.Equal("\x1b[0;32mMSG\x1b[0m", buf.String())
	buf.Reset()

	// Style.Printf
	Info.Printf("A %s", "MSG")
	is.Equal("\x1b[0;32mA MSG\x1b[0m", buf.String())
	buf.Reset()

	// Style.Println
	Info.Println("MSG")
	is.Equal("\x1b[0;32mMSG\x1b[0m\n", buf.String())
	buf.Reset()

	Info.Println("MSG", "OK")
	is.Equal("\x1b[0;32mMSG OK\x1b[0m\n", buf.String())
	buf.Reset()

	s = GetStyle("err")
	is.False(s.IsEmpty())

	if isLikeInCmd {
		s.Print("msg")
		s.Printf("M%s", "sg")
		s.Println("Msg")
		is.Equal("\x1b[97;41mmsg\x1b[0m\x1b[97;41mMsg\x1b[0m\x1b[97;41mMsg\x1b[0m\n", buf.String())
		buf.Reset()
	}

	// add new
	s = GetStyle("new0")
	is.True(s.IsEmpty())
	AddStyle("new0", Style{OpFastBlink})
	s = GetStyle("new0")
	is.False(s.IsEmpty())
	delete(Styles, "new0")

	// add new
	s = GetStyle("new1")
	is.True(s.IsEmpty())
	New(OpStrikethrough).Save("new1")
	s = GetStyle("new1")
	is.False(s.IsEmpty())
	delete(Styles, "new1")
}

func TestThemes(t *testing.T) {
	// force open color render for testing
	buf := forceOpenColorRender()
	defer resetColorRender()

	is := assert.New(t)

	// Theme.Tips
	Info.Tips("MSG")
	is.Equal("\x1b[0;32mINFO: \x1b[0mMSG\n", buf.String())
	buf.Reset()

	// Theme.Prompt
	Info.Prompt("MSG")
	is.Equal("\x1b[0;32mINFO: MSG\x1b[0m\n", buf.String())
	buf.Reset()

	// Theme.Block
	Info.Block("MSG")
	is.Equal("\x1b[0;32mINFO:\n MSG\x1b[0m\n", buf.String())
	buf.Reset()

	theme := GetTheme("info")
	is.NotNil(theme)
	theme = GetTheme("not-exist")
	is.Nil(theme)

	// add new
	AddTheme("new0", Style{OpFastBlink})
	theme = GetTheme("new0")
	is.NotNil(theme)
	delete(Themes, "new0")
	theme = GetTheme("new0")
	is.Nil(theme)

	// add new
	theme = GetTheme("new1")
	is.Nil(theme)

	theme = NewTheme("new1", Style{OpFastBlink})
	theme.Save()
	theme = GetTheme("new1")
	is.NotNil(theme)

	delete(Themes, "new1")
	theme = GetTheme("new1")
	is.Nil(theme)
}

func TestStyleFunc(t *testing.T) {
	// force open color render for testing
	buf := forceOpenColorRender()
	defer resetColorRender()

	Infop("color message")
	assert.Equal(t, "\x1b[0;32mcolor message\x1b[0m", buf.String())
	buf.Reset()

	Infoln("color message")
	assert.Equal(t, "\x1b[0;32mcolor message\x1b[0m\n", buf.String())
	buf.Reset()

	Infof("color %s", "message")
	assert.Equal(t, "\x1b[0;32mcolor message\x1b[0m", buf.String())
	buf.Reset()

	Successp("color message")
	assert.Equal(t, "\x1b[1;32mcolor message\x1b[0m", buf.String())
	buf.Reset()

	Successln("color message")
	assert.Equal(t, "\x1b[1;32mcolor message\x1b[0m\n", buf.String())
	buf.Reset()

	Successf("color %s", "message")
	assert.Equal(t, "\x1b[1;32mcolor message\x1b[0m", buf.String())
	buf.Reset()

	Warnp("color message")
	assert.Equal(t, "\x1b[1;33mcolor message\x1b[0m", buf.String())
	buf.Reset()

	Warnln("color message")
	assert.Equal(t, "\x1b[1;33mcolor message\x1b[0m\n", buf.String())
	buf.Reset()

	Warnf("color %s", "message")
	assert.Equal(t, "\x1b[1;33mcolor message\x1b[0m", buf.String())
	buf.Reset()

	Errorp("color message")
	assert.Equal(t, "\x1b[97;41mcolor message\x1b[0m", buf.String())
	buf.Reset()

	Errorln("color message")
	assert.Equal(t, "\x1b[97;41mcolor message\x1b[0m\n", buf.String())
	buf.Reset()

	Errorf("color %s", "message")
	assert.Equal(t, "\x1b[97;41mcolor message\x1b[0m", buf.String())
	buf.Reset()
}

func TestSimplePrinter_Print(t *testing.T) {
	sp := &SimplePrinter{}
	sp.Printf("simple %s\n", "printer")
	sp.Infof("simple %s\n", "printer")
	sp.Warnf("simple %s\n", "printer")
	sp.Errorf("simple %s\n", "printer")
	sp.Print("simple printer\n")
	sp.Println("simple printer")
	sp.Infoln("simple printer")
	sp.Warnln("simple printer")
	sp.Errorln("simple printer")
}

func TestNewScheme(t *testing.T) {
	cs := NewDefaultScheme("test")

	cs.Infof("color %s\n", "scheme")
	cs.Warnf("color %s\n", "scheme")
	cs.Errorf("color %s\n", "scheme")
	cs.Infoln("color scheme")
	cs.Warnln("color scheme")
	cs.Errorln("color scheme")
	cs.Style("info").Println("color scheme")
}