-
-
Notifications
You must be signed in to change notification settings - Fork 263
/
helpers_test.go
320 lines (284 loc) · 8.58 KB
/
helpers_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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package gofakeit
import (
"fmt"
"testing"
)
// Test taking in two random int values and making sure the output is within the range
func TestRandIntRange(t *testing.T) {
// Create a set of structs to test
type testStruct struct {
min, max int
}
// Create a set of test values
tests := []testStruct{
{0, 0},
{1000, -1000},
{minInt, maxInt},
{minInt, minInt + 100}, // Test min
{maxInt - 100, maxInt}, // Test max
{maxInt - 20000, maxInt - 10000},
{minInt + 10000, maxInt - 10000},
}
// Add 10000 random values to the test set
for i := 0; i < 5000; i++ {
tests = append(tests, testStruct{
min: randIntRange(GlobalFaker, 0, maxInt),
max: randIntRange(GlobalFaker, 0, maxInt),
})
}
for i := 0; i < 5000; i++ {
tests = append(tests, testStruct{
min: randIntRange(GlobalFaker, minInt, 0),
max: randIntRange(GlobalFaker, 0, maxInt),
})
}
// Loop through the tests
for _, test := range tests {
// Get the result
result := randIntRange(GlobalFaker, test.min, test.max)
// Check the result
if test.min > test.max {
// Test if min is greater than max by reversing the check
if result > test.min && result < test.max {
t.Fatalf("The result should be between %d and %d. Got: %d", test.min, test.max, result)
}
} else if result < test.min || result > test.max {
t.Fatalf("The result should be between %d and %d. Got: %d", test.min, test.max, result)
}
}
}
// Test taking in two random uint values and making sure the output is within the range
func TestRandUintRange(t *testing.T) {
// Create a set of structs to test
type testStruct struct {
min, max uint
}
// Create a set of test values
tests := []testStruct{
{0, 0},
{100000, 100},
{minUint, maxUint},
{minUint + 10000, maxUint - 10000},
}
// Add 10000 random values to the test set
for i := 0; i < 5000; i++ {
tests = append(tests, testStruct{
min: randUintRange(GlobalFaker, 0, maxUint),
max: randUintRange(GlobalFaker, 0, maxUint),
})
}
for i := 0; i < 5000; i++ {
tests = append(tests, testStruct{
min: randUintRange(GlobalFaker, 0, maxUint/2),
max: randUintRange(GlobalFaker, maxUint/2, maxUint),
})
}
// Loop through the tests
for _, test := range tests {
// Get the result
result := randUintRange(GlobalFaker, test.min, test.max)
// Check the result
if test.min > test.max {
// Test if min is greater than max by reversing the check
if result > test.min && result < test.max {
t.Fatalf("The result should be between %d and %d. Got: %d", test.min, test.max, result)
}
} else if result < test.min || result > test.max {
t.Fatalf("The result should be between %d and %d. Got: %d", test.min, test.max, result)
}
}
}
func TestGetRandValueFail(t *testing.T) {
for _, test := range [][]string{nil, {}, {"not", "found"}, {"person", "notfound"}} {
if getRandValue(GlobalFaker, test) != "" {
t.Error("You should have gotten no value back")
}
}
}
func TestReplaceWithNumbers(t *testing.T) {
if replaceWithNumbers(GlobalFaker, "") != "" {
t.Error("You should have gotten an empty string")
}
}
func BenchmarkReplaceWithNumbers(b *testing.B) {
for i := 0; i < b.N; i++ {
Seed(11)
replaceWithNumbers(GlobalFaker, "###☺#☻##☹##")
}
}
func TestReplaceWithNumbersUnicode(t *testing.T) {
for _, test := range []struct{ in string }{
{"#界#世#"},
{"☺#☻☹#"},
{"\x80#¼#語"},
} {
Seed(11)
got := replaceWithNumbers(GlobalFaker, test.in)
if got == test.in {
t.Errorf("got %q, want something different", got)
}
// Check that # were replaced with numbers
for _, r := range got {
if r == '#' {
t.Errorf("got %q, want something different", got)
}
}
}
}
func TestReplaceWithLetters(t *testing.T) {
if replaceWithLetters(GlobalFaker, "") != "" {
t.Error("You should have gotten an empty string")
}
}
func TestReplaceWithHexLetters(t *testing.T) {
if replaceWithHexLetters(GlobalFaker, "") != "" {
t.Error("You should have gotten an empty string")
}
}
func TestToFixed(t *testing.T) {
floats := [][]float64{
{123.1234567489, 123.123456},
{987.987654321, 987.987654},
}
for _, f := range floats {
if toFixed(f[0], 6) != f[1] {
t.Fatalf("%g did not equal %g. Got: %g", f[0], f[1], toFixed(f[0], 6))
}
}
}
func TestEqualSlice(t *testing.T) {
// String Array
if equalSliceString([]string{"a", "b"}, []string{"a"}) {
t.Fatalf("Should have returned false because the string array are not the same")
}
if equalSliceString([]string{"a", "b"}, []string{"c", "d"}) {
t.Fatalf("Should have returned false because the string array are not the same")
}
if !equalSliceString([]string{"a", "b"}, []string{"a", "b"}) {
t.Fatalf("Should have returned true because the string array are the same")
}
// Int Array
if equalSliceInt([]int{1, 2}, []int{1}) {
t.Fatalf("Should have returned false because the int array are not the same")
}
if equalSliceInt([]int{1, 2}, []int{3, 4}) {
t.Fatalf("Should have returned false because the int array are not the same")
}
if !equalSliceInt([]int{1, 2}, []int{1, 2}) {
t.Fatalf("Should have returned true because the int array are the same")
}
// Interface Array
if equalSliceInterface([]any{1, "b"}, []any{1}) {
t.Fatalf("Should have returned false because the interface array are not the same")
}
if equalSliceInterface([]any{1, "b"}, []any{3, "d"}) {
t.Fatalf("Should have returned false because the interface array are not the same")
}
if !equalSliceInterface([]any{1, "b", []int{1, 2}, []string{"a", "b"}}, []any{1, "b", []int{1, 2}, []string{"a", "b"}}) {
t.Fatalf("Should have returned true because the ints array are the same")
}
}
func TestStringInSlice(t *testing.T) {
if stringInSlice("c", []string{"a", "b"}) {
t.Fatalf("Should have returned true because the string is in the slice")
}
if !stringInSlice("a", []string{"a", "b", "c"}) {
t.Fatalf("Should have returned true because the string is in the slice")
}
}
func TestAnyToString(t *testing.T) {
tests := []struct {
input any
want string
}{
{input: 42, want: "42"},
{input: "Hello, Go!", want: "Hello, Go!"},
{input: 3.14, want: "3.14"},
{input: true, want: "true"},
{input: struct{ Name string }{Name: "John"}, want: `{"Name":"John"}`},
{input: []int{1, 2, 3}, want: "[1,2,3]"},
{input: map[string]int{"a": 1, "b": 2}, want: `{"a":1,"b":2}`},
{input: []byte("hello world"), want: "hello world"},
{input: nil, want: ""},
}
for _, test := range tests {
t.Run(fmt.Sprintf("Input: %v", test.input), func(t *testing.T) {
got := anyToString(test.input)
if got != test.want {
t.Errorf("got %v, want %v", got, test.want)
}
})
}
// Additional tests for edge cases
t.Run("Edge case: empty byte slice", func(t *testing.T) {
emptyByteSlice := []byte{}
got := anyToString(emptyByteSlice)
want := ""
if got != want {
t.Errorf("got %v, want %v", got, want)
}
})
}
func TestAnyToStringEdgeCases(t *testing.T) {
// Additional tests for edge cases
// Test with a struct containing an unexported field
type unexportedFieldStruct struct {
unexportedField int
ExportedField string
}
unexportedStruct := unexportedFieldStruct{unexportedField: 42, ExportedField: "Hello"}
want := `{"ExportedField":"Hello"}`
got := anyToString(unexportedStruct)
if got != want {
t.Errorf("got %v, want %v", got, want)
}
// Test with a struct containing a nil interface field
type nilInterfaceFieldStruct struct {
Data any
}
nilInterfaceStruct := nilInterfaceFieldStruct{}
want = `{"Data":null}`
got = anyToString(nilInterfaceStruct)
if got != want {
t.Errorf("got %v, want %v", got, want)
}
}
func TestTitle(t *testing.T) {
test := map[string]string{
"": "",
"i have a best friend": "I Have A Best Friend",
"this is a test": "This Is A Test",
"I am 36 years old": "I Am 36 Years Old",
"whats_up": "Whats_up",
}
for in, out := range test {
if title(in) != out {
t.Fatalf("%s did not equal %s. Got: %s", in, out, title(in))
}
}
}
func TestFuncLookupSplit(t *testing.T) {
tests := map[string][]string{
"": {},
"a": {"a"},
"a,b,c": {"a", "b", "c"},
"a, b, c": {"a", "b", "c"},
"[a,b,c]": {"[a,b,c]"},
"a,[1,2,3],b": {"a", "[1,2,3]", "b"},
"[1,2,3],a,[1,2,3]": {"[1,2,3]", "a", "[1,2,3]"},
}
for input, expected := range tests {
values, err := funcLookupSplit(input)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(values) != len(expected) {
t.Fatalf("%s was not %s", values, expected)
}
for i := 0; i < len(values); i++ {
if values[i] != expected[i] {
t.Fatalf("expected %s got %s", expected[i], values[i])
}
}
}
}