-
Notifications
You must be signed in to change notification settings - Fork 0
/
libgosay_test.go
237 lines (189 loc) · 5.67 KB
/
libgosay_test.go
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
234
235
236
237
package libgosay
import (
"bytes"
"fmt"
"testing"
"text/template"
)
func tError(r bool, s string, t *testing.T) {
if r {
t.Errorf("%s", s)
}
}
func TestErrorfuc(t *testing.T) {
var p Pimp
var err error
_, err = p.Say("")
tError(err == nil, "\nIf Say() is call with an Column size zero, we expect to catch an err", t)
p.Column = 10
_, err = p.Say("")
tError(err == nil, "\nIf Say() is call with an empty p.Body, we expect to catch an err", t)
p.setEyes()
tError(p.EyeL != "" || p.EyeR != "", "\nWe expect EyeL and EyeR are empty", t)
p.Eyes = "0Oo"
p.setEyes()
tError(p.EyeL != "0" || p.EyeR != "O", fmt.Sprintf("\nWe expect to EyeL == 0 and EyeR == O :\np.Eyes = %s\np.EyeL = %s && p.EyeL = %s", p.Eyes, p.EyeL, p.EyeR), t)
p.Eyes = "--"
p.setEyes()
tError(p.EyeL != "0" || p.EyeR != "O", fmt.Sprintf("\nWe expect Eye* not change to -- :\np.Eyes = %s\np.EyeL = %s && p.EyeL = %s", p.Eyes, p.EyeL, p.EyeR), t)
p.Tail = "o"
p.setTail()
tError(p.Tail != "o", fmt.Sprintf("We expect p.Tail == o but p.Tail == %s", p.Tail), t)
}
func TestStrfunc(t *testing.T) {
ss := splitStringToLen("Go for the eyes, Boo, go for the eyes!", 10)
tError(len(ss) != 4, fmt.Sprint("\nStringSplit error:\nExpect-4 give-", len(ss), "\n", ss), t)
ml := maxLen(ss)
tError(ml != 10, fmt.Sprint("\nMax array len error:\nExpect-10 give-", ml), t)
i := spaceBefore("toto", "___toto")
tError(i != 3, fmt.Sprint("\nSpace before object:\nExpect-3 give-", i), t)
}
func TestSay(t *testing.T) {
var g = Create()
se := 128
s, e := g.Say("")
tError(e != nil, fmt.Sprint("DefaultSay error:", e), t)
tError(len(s) != se, fmt.Sprint("\nDefaultSay render len don't match\nExpected-", se, " Rended-", len(s), s), t)
se = 199
s, e = g.Say("Heya, it's me Imoen")
tError(e != nil, fmt.Sprint("DefaultSay error:", e), t)
tError(len(s) != se, fmt.Sprint("\nDefaultSay render len don't match\nExpected-", se, " Rended-", len(s), s), t)
se = 479
s, e = g.Say("I've seen things you people wouldn't believe. Attack ships on fire off the shoulder of Orion. I watched C-beams glitter in the dark near the Tannhauser gate. All those moments will be lost in time, like tears in rain. Time to die.")
tError(e != nil, fmt.Sprint("DefaultSay error:", e), t)
tError(len(s) != se, fmt.Sprint("\nDefaultSay render len don't match\nExpected-", se, " Rended-", len(s), s), t)
}
func TestCustomSay(t *testing.T) {
var g = Create()
g.Body = `{{- /* From https://github.com/paulkaefer/cowsay-files.git */ -}}
{{- /* An owl */ -}}
{{.Said}}
{{.Tail}}
{{.Tail}}
___
(o o)
( V )
/--m-m-`
expect := ` _____________________
< Heya, it's me Imoen >
---------------------
\
\
___
(o o)
( V )
/--m-m-`
out, err := g.Say("Heya, it's me Imoen")
tError(err != nil, fmt.Sprint("CustomSay error:", err), t)
tError(out != expect, fmt.Sprint("Bad output formating:\n", out), t)
}
func TestBubble(t *testing.T) {
var b BubbleDef
s := splitStringToLen("Les méchants, je les démolis. Tout ce qui brille, je me le garde. Devant moi, mes ennemis détalent. Je suis née pour gagner !", 40)
er := ` __________________________________________
/ Les méchants, je les démolis. Tout ce \
| qui brille, je me le garde. Devant moi, |
| mes ennemis détalent. Je suis née pour |
\ gagner ! /
------------------------------------------ `
b.Speak()
gr := bubbleMyStrings(s, b, 0)
tError(gr != er, fmt.Sprint("Bad bubble formating:\n", gr), t)
s = splitStringToLen("I am the law!", 15)
er = ` _______________
< I am the law! >
--------------- `
b.Speak()
gr = bubbleMyStrings(s, b, 0)
tError(gr != er, fmt.Sprint("Bad bubble formating:\n", gr), t)
er = ` _______________
( I am the law! )
--------------- `
b.Think()
gr = bubbleMyStrings(s, b, 0)
tError(gr != er, fmt.Sprint("Bad bubble formating:\n", gr), t)
er = ` ...............
: I am the law! :
............... `
b.Whisper()
gr = bubbleMyStrings(s, b, 0)
tError(gr != er, fmt.Sprint("Bad bubble formating:\n", gr), t)
er = `=================
| I am the law! |
=================`
b.Narrative()
gr = bubbleMyStrings(s, b, 0)
tError(gr != er, fmt.Sprint("Bad bubble formating:\n", gr), t)
er = ` =================
| I am the law! |
=================`
b.Narrative()
gr = bubbleMyStrings(s, b, 5)
tError(gr != er, fmt.Sprint("Bad spacer size:\n", gr), t)
}
var gs Pimp
var bfInt int
var bfStr string
var bfStrt []string
const gSay = "Les méchants, je les démolis. Tout ce qui brille, je me le garde. Devant moi, mes ennemis détalent. Je suis née pour gagner !"
func BenchmarkCreate(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
gs = Create()
}
_ = gs
}
func BenchmarkSay(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
gs.Say(gSay)
}
}
func BenchmarkBms(b *testing.B) {
var s string
b.ResetTimer()
for i := 0; i < b.N; i++ {
bubbleMyStrings([]string{gSay}, gs.Bubble, 0)
}
bfStr = s
}
func BenchmarkSstl(b *testing.B) {
var s []string
b.ResetTimer()
for i := 0; i < b.N; i++ {
s = splitStringToLen(gSay, gs.Column)
}
bfStrt = s
}
func BenchmarkMl(b *testing.B) {
var m int
b.ResetTimer()
for i := 0; i < b.N; i++ {
m = maxLen([]string{gSay})
}
bfInt = m
}
func BenchmarkBubble(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
gs.Bubble.Speak()
gs.Bubble.Think()
gs.Bubble.ThinkUTF8()
gs.Bubble.Whisper()
gs.Bubble.WhisperUTF8()
gs.Bubble.Narrative()
gs.Bubble.BigBoxUTF8()
gs.Bubble.AsianBoxUTF8()
}
}
func BenchmarkTpl(b *testing.B) {
var s string
b.ResetTimer()
for i := 0; i < b.N; i++ {
var out bytes.Buffer
body, _ := template.New("body").Parse(gs.Body)
body.Execute(&out, gs)
s = out.String()
}
bfStr = s
}