-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgrand_test.go
52 lines (41 loc) · 938 Bytes
/
grand_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
package grand
import (
"math/rand"
"regexp"
"testing"
"time"
)
func init() {
rand.Seed(time.Now().UTC().UnixNano())
}
func TestGeneratesDifferentStrings(t *testing.T) {
randStr1 := GenerateRandomString(20)
if len(randStr1) != 20 {
t.Fatal()
}
randStr2 := GenerateRandomString(20)
if randStr1 == randStr2 {
t.Fatal()
}
randStr3 := GenerateRandomString(20)
if randStr2 == randStr3 {
t.Fatal()
}
}
func TestSetDefaultCharSet(t *testing.T) {
randStr1 := GenerateRandomString(20)
// There should not be any numbers in the string
res := regexp.MustCompile("\\d").FindAllString(randStr1, -1)
if len(res) != 0 {
t.Fatal()
}
}
func TestSetDifferentCharSet(t *testing.T) {
gen := NewGenerator(CharSetBase62)
randStr1 := gen.GenerateRandomString(20)
// There should be at least one number in generated string
res := regexp.MustCompile("\\d").FindAllString(randStr1, -1)
if len(res) == 0 {
t.Fatal()
}
}