-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator_test.go
73 lines (65 loc) · 1.47 KB
/
generator_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
package humantoken
import (
"github.com/stretchr/testify/assert"
"math/rand"
"testing"
)
func TestGenerator(t *testing.T) {
tests := []struct {
name string
generator TokenGenerator // if nil, NewGeneratorRandom() is used
size int
expectedLength int
}{
{
name: "zero size",
size: 0,
expectedLength: 0,
},
{
name: "ten size",
size: 10,
expectedLength: 10,
},
{
name: "negative size",
size: -10,
expectedLength: 0,
},
{
name: "zero seed generator",
generator: NewGenerator(rand.New(rand.NewSource(0))),
size: 10,
expectedLength: 10,
},
{
name: "nil generator",
generator: NewGenerator(nil),
size: 12,
expectedLength: 12,
},
}
for _, tt := range tests {
test := tt
t.Run(test.name, func(t *testing.T) {
g := test.generator
if g == nil {
g = NewGeneratorRandom()
}
got := g.Generate(test.size)
assert.Len(t, got, test.expectedLength)
})
}
}
func TestNewGeneratorWithRandSameAsGenerateWithRand(t *testing.T) {
sources := make([]int64, 10)
for i := range sources {
sources[i] = rand.Int63()
}
for _, sourceSeed := range sources {
g := NewGenerator(rand.New(rand.NewSource(0)))
want := Generate(10, rand.New(rand.NewSource(0)))
got := g.Generate(10)
assert.Equalf(t, want, got, "for Generate with %d seed to have same result", sourceSeed)
}
}