-
Notifications
You must be signed in to change notification settings - Fork 8
/
gospin_test.go
189 lines (162 loc) · 6.77 KB
/
gospin_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
package gospin
import (
"github.com/stretchr/testify/assert"
"math/rand"
"testing"
)
func TestSpinner_Spin(t *testing.T) {
rand.Seed(1)
spinner := New(&Config{UseGlobalRand: true})
simple := "The {slow|quick} {brown|blue and {red|yellow}} {fox|deer} {gracefully |}jumps over the {sleeping|lazy} dog"
expected := "The slow blue and yellow deer jumps over the sleeping dog"
got, err := spinner.Spin(simple)
if err != nil {
assert.Error(t, err)
}
assert.Equal(t, expected, got, "should be equal")
escaped := "The \\{escaped\\} {slow|quick} {brown|blue and {red|yellow}} {fox|deer} {gracefully|} jumps over the {sleeping|lazy} dog"
expected = "The \\{escaped\\} slow brown deer gracefully jumps over the lazy dog"
got, err = spinner.Spin(escaped)
if err != nil {
assert.Error(t, err)
}
assert.Equal(t, expected, got, "should be equal")
escapedDelimiters := "The \\{escaped\\} {slow|quick} {brown|blue and {red|yellow}} {fox|deer|escaped\\|pipe} {gracefully|} jumps over the {sleeping|lazy} dog"
expected = "The \\{escaped\\} slow blue and red escaped\\pipe gracefully jumps over the lazy dog"
got, err = spinner.Spin(escapedDelimiters)
if err != nil {
assert.Error(t, err)
}
assert.Equal(t, expected, got, "should be equal")
}
func TestSpinner_Spin_UTF8(t *testing.T) {
rand.Seed(1)
spinner := New(&Config{UseGlobalRand: true})
simple := "{Медленный|Быстрый} {бурый|серый с {рыжими пятнами|золотистыми пятнами}} {лис|олень} {с легкостью |}перепрыгнул через {сонную|ленивую} собаку"
expected := "Медленный серый с золотистыми пятнами олень перепрыгнул через сонную собаку"
got, err := spinner.Spin(simple)
if err != nil {
assert.Error(t, err)
}
assert.Equal(t, expected, got, "should be equal")
escaped := "\\{Экранированный\\} {бурый|серый с {рыжими пятнами|золотистыми пятнами}} {лис|олень} {с легкостью |}перепрыгнул через {сонную|ленивую} собаку"
expected = "\\{Экранированный\\} бурый лис перепрыгнул через сонную собаку"
got, err = spinner.Spin(escaped)
if err != nil {
assert.Error(t, err)
}
assert.Equal(t, expected, got, "should be equal")
escapedDelimiters := "\\{Экранированный\\} {бурый|серый с {рыжими пятнами|золотистыми пятнами}} {экранированная\\|черта|олень} {с легкостью |}перепрыгнул через {сонную|ленивую} собаку"
expected = "\\{Экранированный\\} бурый экранированная\\черта перепрыгнул через ленивую собаку"
got, err = spinner.Spin(escapedDelimiters)
if err != nil {
assert.Error(t, err)
}
assert.Equal(t, expected, got, "should be equal")
}
func TestSpinner_Spin_EndCharFirstChar(t *testing.T) {
rand.Seed(1)
spinner := New(&Config{UseGlobalRand: true})
simple := "}The {slow|quick} {brown|blue and {red|yellow}} {fox|deer} {gracefully|} jumps over the {sleeping|lazy} dog"
_ = "The slow blue and yellow deer jumps over the sleeping dog"
_, err := spinner.Spin(simple)
if err == nil {
assert.Fail(t, "was expecting error")
return
}
assert.EqualError(t, err, errBracketsNotMatching, "should be equal")
}
func TestSpinner_Spin_BracketsNotMatchingErr(t *testing.T) {
rand.Seed(1)
spinner := New(&Config{UseGlobalRand: true})
simple := "The {slow|quick}} {brown|blue and {red|yellow}} {fox|deer} {gracefully|} jumps over the {sleeping|lazy} dog"
_, err := spinner.Spin(simple)
if err == nil {
assert.Fail(t, "was expecting error")
return
}
assert.EqualError(t, err, errBracketsNotMatching, "should be equal")
}
func TestSpinner_Spin_WithCustomConfig(t *testing.T) {
rand.Seed(1)
spinner := New(&Config{
StartChar: "[",
EndChar: "]",
EscapeChar: "@",
DelimiterChar: ";",
UseGlobalRand: true,
})
simple := "The [slow;quick] [brown;blue and [red;yellow]] [fox;deer] [gracefully ;]jumps over the [sleeping;lazy] dog"
expected := "The slow blue and yellow deer jumps over the sleeping dog"
got, err := spinner.Spin(simple)
if err != nil {
assert.Error(t, err)
}
assert.Equal(t, expected, got, "should be equal")
escaped := "The @[escaped@] [slow;quick] [brown;blue and [red;yellow]] [fox;deer] [gracefully;] jumps over the [sleeping;lazy] dog"
expected = "The @[escaped@] slow brown deer gracefully jumps over the lazy dog"
got, err = spinner.Spin(escaped)
if err != nil {
assert.Error(t, err)
}
assert.Equal(t, expected, got, "should be equal")
escapedDelimiters := "The @[escaped@] [slow;quick] [brown;blue and [red;yellow]] [fox;deer;escaped@;pipe] [gracefully;] jumps over the [sleeping;lazy] dog"
expected = "The @[escaped@] slow blue and red escaped@pipe gracefully jumps over the lazy dog"
got, err = spinner.Spin(escapedDelimiters)
if err != nil {
assert.Error(t, err)
}
assert.Equal(t, expected, got, "should be equal")
}
func TestSpinner_Spin_WithNilConfig(t *testing.T) {
spinner := New(nil)
assert.Equal(t, defaultEscapeChar, spinner.EscapeChar)
assert.Equal(t, defaultStartChar, spinner.StartChar)
assert.Equal(t, defaultEndChar, spinner.EndChar)
assert.Equal(t, defaultDelimiterChar, spinner.DelimiterChar)
}
func TestSpinner_SpinN(t *testing.T) {
rand.Seed(1)
spinner := New(&Config{UseGlobalRand: true})
simple := "The {slow|quick} {brown|blue and {red|yellow}} {fox|deer} {gracefully |}jumps over the {sleeping|lazy} dog"
expected := "The slow blue and yellow deer jumps over the sleeping dog"
got, err := spinner.SpinN(simple, 100)
if err != nil {
assert.Error(t, err)
}
assert.Equal(t, expected, got[0], "should be equal")
assert.Len(t, got, 100, "should be equal")
}
func TestSpinner_Spin_NoDuplicates(t *testing.T) {
spinner := New(nil)
simple := "The {slow|quick} {brown|blue and {red|yellow}} {fox|deer} {gracefully|} jumps over the {sleeping|lazy} dog"
got, err := spinner.SpinN(simple, 100)
if err != nil {
assert.Error(t, err)
}
assert.Len(t, got, 100, "should be equal")
ok := false
Exit:
for _, str1 := range got {
for _, str2 := range got {
if str1 != str2 {
ok = true
continue Exit
}
}
}
if !ok {
assert.Fail(t, "no unique values found")
}
}
func TestSpinner_SpinN_BracketsNotMatchingErr(t *testing.T) {
rand.Seed(1)
spinner := New(&Config{UseGlobalRand: true})
simple := "The {slow|quick}} {brown|blue and {red|yellow}} {fox|deer} {gracefully|} jumps over the {sleeping|lazy} dog"
_, err := spinner.SpinN(simple, 10)
if err == nil {
assert.Fail(t, "was expecting error")
return
}
assert.EqualError(t, err, errBracketsNotMatching, "should be equal")
}