-
Notifications
You must be signed in to change notification settings - Fork 1
/
humid_test.go
120 lines (99 loc) · 2.42 KB
/
humid_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
package humid_test
import (
"fmt"
"strings"
"testing"
"unicode"
"github.com/kscarlett/humid"
"github.com/kscarlett/humid/wordlist"
)
// Examples
func ExampleGenerate() {
fmt.Println(humid.Generate())
// Example output: blue-hummingbird
}
func ExampleGenerateWithOptions() {
fmt.Println(humid.GenerateWithOptions(&humid.Options{
List: wordlist.Animals,
AdjectiveCount: 2,
Separator: "_",
Capitalize: true,
}))
// Example output: Bumpy_Brown_Cat
}
// Tests
func TestGenerateWithOptions(t *testing.T) {
const expectedSeparator = "-"
const expectedWordCount = 2
result := humid.GenerateWithOptions(&humid.Options{
List: wordlist.Adjectives,
AdjectiveCount: 1,
Separator: "-",
Capitalize: false,
})
t.Logf("returned id: %s\n", result)
// Test separator
// Test word count
words := strings.Split(result, expectedSeparator)
if len(words) != expectedWordCount {
t.Fatalf("expected %d words separated by \"%s\", got %d words instead", expectedWordCount, expectedSeparator, expectedWordCount)
}
// Test capitalization
if !isLower(words[0]) {
t.Errorf("expected word \"%s\" to be lowercase", words[0])
}
if !isLower(words[1]) {
t.Errorf("expected word \"%s\" to be lowercase", words[1])
}
// Test wordlist
adjectives := wordlist.Adjectives
if !find(adjectives, words[0]) {
t.Errorf("expected to find \"%s\" in wordlist it was not there", words[0])
}
if !find(adjectives, words[1]) {
t.Errorf("expected to find \"%s\" in wordlist it was not there", words[1])
}
}
func TestRandomQuality(t *testing.T) {
id1 := humid.Generate()
id2 := humid.Generate()
if id1 == id2 {
t.Errorf("random quality not as expected - %s and %s generated sequentially", id1, id2)
}
}
// Benchmarks
func BenchmarkGenerateSimple(b *testing.B) {
for i := 0; i < b.N; i++ {
humid.Generate()
}
}
func BenchmarkGenerateWithOptions(b *testing.B) {
for i := 0; i < b.N; i++ {
humid.GenerateWithOptions(&humid.Options{
AdjectiveCount: 1,
Separator: "_",
Capitalize: false,
})
}
}
// Utility
func find(slice []string, val string) bool {
for _, item := range slice {
if item == val {
return true
}
}
return false
}
func isTitle(s string) bool {
if unicode.IsUpper(rune(s[0])) && unicode.IsLower(rune(s[1])) {
return true
}
return false
}
func isLower(s string) bool {
if unicode.IsLower(rune(s[0])) && unicode.IsLower(rune(s[1])) {
return true
}
return false
}