-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathidgen_test.go
94 lines (77 loc) · 2.27 KB
/
idgen_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
// Copyright Garrett Sparks.
// All Rights Reserved
package idgen
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestNew(t *testing.T) {
t.Run("it should properly calculate maxCharIndex", func(t *testing.T) {
builder := New()
assert.Equal(t, 61, builder.maxCharIndex)
})
}
func TestWithCharset(t *testing.T) {
t.Run("it should build an ID when only one character is supplied", func(t *testing.T) {
idLength := 5
builder := New().WithCharset("a").WithLength(idLength)
id := builder.BuildID()
assert.Equal(t, "aaaaa", id)
})
t.Run("it should return an empty ID when no characters are supplied", func(t *testing.T) {
idLength := 5
builder := New().WithCharset("").WithLength(idLength)
id := builder.BuildID()
assert.Equal(t, "", id)
})
}
func TestWithLength(t *testing.T) {
t.Run("it should build an ID of size 10", func(t *testing.T) {
idLength := 10
builder := New().WithLength(idLength)
id := builder.BuildID()
assert.Len(t, id, idLength)
})
t.Run("it should build an ID of size 0", func(t *testing.T) {
idLength := 0
builder := New().WithLength(idLength)
id := builder.BuildID()
assert.Len(t, id, idLength)
})
t.Run("it should build an ID of size 1", func(t *testing.T) {
idLength := 1
builder := New().WithLength(idLength)
id := builder.BuildID()
assert.Len(t, id, idLength)
})
}
func TestChooseChar(t *testing.T) {
builder := New()
t.Run("it should choose a 0 byte character", func(t *testing.T) {
char := string([]byte{builder.chooseChar(0)})
assert.Equal(t, "a", char)
})
t.Run("it should choose a max byte character", func(t *testing.T) {
char := string([]byte{builder.chooseChar(255)})
assert.Equal(t, "9", char)
})
t.Run("it should choose a middle byte character", func(t *testing.T) {
char := string([]byte{builder.chooseChar(128)})
assert.Equal(t, "E", char)
})
}
func TestBucketByte(t *testing.T) {
builder := New()
t.Run("it should bucket a 0 byte", func(t *testing.T) {
bucket := builder.bucketByte(0)
assert.Zero(t, bucket)
})
t.Run("it should bucket a max byte", func(t *testing.T) {
bucket := builder.bucketByte(255)
assert.Equal(t, 61, bucket)
})
t.Run("it should bucket a middle byte", func(t *testing.T) {
bucket := builder.bucketByte(128)
assert.Equal(t, 30, bucket)
})
}