-
Notifications
You must be signed in to change notification settings - Fork 10
/
codename_test.go
51 lines (39 loc) · 918 Bytes
/
codename_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
package codename
import (
"math/rand"
"strings"
"testing"
"time"
)
// Make sure the generated names exist
func TestGenerator(t *testing.T) {
seed := time.Now().UnixNano()
rng := rand.New(rand.NewSource(seed))
for i := 0; i < 10; i++ {
name := Generate(rng, 3)
t.Logf("%v", name)
}
}
func TestReturnsSameForSameSeed(t *testing.T) {
var seed int64 = 1001
rng := rand.New(rand.NewSource(seed))
n1 := Generate(rng, 4)
rng = rand.New(rand.NewSource(seed))
n2 := Generate(rng, 4)
t.Logf("%v -- %v", n1, n2)
if n1 != n2 {
t.Error("Should return same")
}
}
func TestCryptoSeed(t *testing.T) {
seed, err := NewCryptoSeed()
if err != nil {
t.Fatal(err)
}
rng := rand.New(rand.NewSource(seed))
id := Generate(rng, 4)
parts := strings.Split(id, "-")
if got, want := len(parts[len(parts)-1]), 4; got != want {
t.Fatalf("token length does not match got [%d], want [%d]", got, want)
}
}