-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator_test.go
151 lines (137 loc) · 4.01 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
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
package uniquenamegenerator
import (
"regexp"
"strings"
"testing"
"github.com/dillonstreator/go-unique-name-generator/dictionaries"
)
func includes(items []string, str string, transform func(item string) string) bool {
for _, item := range items {
if transform(item) == str {
return true
}
}
return false
}
func TestNewUniqueNameGenerator(t *testing.T) {
t.Run("should use correct default options", func(t *testing.T) {
g := NewUniqueNameGenerator(
WithTransformer(strings.ToLower),
)
if len(g.dictionaries) != 3 {
t.Error("default generator should use 3 dictionaries")
}
word := g.Generate()
if strings.Count(word, "_") != 2 {
t.Error("should output 3 words separated by 2 underscores")
}
words := strings.Split(word, "_")
if !includes(dictionaries.Adjectives, words[0], strings.ToLower) {
t.Error("first word should be adjective")
}
if !includes(dictionaries.Colors, words[1], strings.ToLower) {
t.Error("second word should be color")
}
if !includes(dictionaries.Names, words[2], strings.ToLower) {
t.Error("third word should be name")
}
})
t.Run("should respect provided config", func(t *testing.T) {
g := NewUniqueNameGenerator(
WithDictionaries([][]string{
dictionaries.Colors,
dictionaries.Colors,
dictionaries.Adjectives,
dictionaries.Names,
}),
WithSeparator("-"),
WithTransformer(strings.ToUpper),
)
if len(g.dictionaries) != 4 {
t.Error("should have 4 dictionaries")
}
word := g.Generate()
if strings.Count(word, "-") != 3 {
t.Error("should have 4 words separated by 3 dashes")
}
words := strings.Split(word, "-")
if !includes(dictionaries.Colors, words[0], strings.ToUpper) {
t.Error("first word should be color")
}
if !includes(dictionaries.Colors, words[1], strings.ToUpper) {
t.Error("second word should be color")
}
if !includes(dictionaries.Adjectives, words[2], strings.ToUpper) {
t.Error("third word should be adjective")
}
if !includes(dictionaries.Names, words[3], strings.ToUpper) {
t.Error("fourth word should be name")
}
g2 := NewUniqueNameGenerator(WithDictionaries([][]string{
{"dillon"},
{"streator"},
}), WithTransformer(func(s string) string { return strings.ToUpper(s) }))
word = g2.Generate()
if word != "DILLON_STREATOR" {
t.Error("it should transform to upper")
}
})
t.Run("UniquenessCount calculations", func(t *testing.T) {
dicts := [][]string{
{"1", "2", "3"},
{"1", "2", "3"},
}
g := NewUniqueNameGenerator(WithDictionaries(dicts))
actual := g.UniquenessCount()
expected := uint64(9)
if actual != expected {
t.Errorf("expected %d combinations with %v but got %d", expected, dicts, actual)
}
g = NewUniqueNameGenerator(WithDictionaries([][]string{}))
actual = g.UniquenessCount()
expected = uint64(0)
if actual != expected {
t.Errorf("expected %d combinations with %v but got %d", expected, dicts, actual)
}
g = NewUniqueNameGenerator(WithDictionaries([][]string{{}}))
actual = g.UniquenessCount()
expected = uint64(0)
if actual != expected {
t.Errorf("expected %d combinations with %v but got %d", expected, dicts, actual)
}
})
t.Run("Sanitizes dictionaries", func(t *testing.T) {
re := regexp.MustCompile("[. ]")
g := NewUniqueNameGenerator(
WithDictionaries([][]string{
{"St. John"},
{"t t"},
}),
WithTransformer(func(s string) string {
return strings.ToLower(re.ReplaceAllString(s, ""))
}),
)
actual := g.Generate()
expected := "stjohn_tt"
if actual != expected {
t.Errorf("expected %s but got %s", expected, actual)
}
g = NewUniqueNameGenerator(WithDictionaries([][]string{
{"St. John"},
{"t t"},
}), WithTransformer(func(s string) string {
return strings.ToLower(strings.ReplaceAll(s, " ", ""))
}))
actual = g.Generate()
expected = "st.john_tt"
if actual != expected {
t.Errorf("expected %s but got %s", expected, actual)
}
})
}
func BenchmarkUniqueNameGenerator_Generate(b *testing.B) {
ung := NewUniqueNameGenerator()
for i := 0; i < b.N; i++ {
ung.Generate()
}
}