-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnamegen_test.go
77 lines (68 loc) · 2.62 KB
/
namegen_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
package namegen_test
import (
"strings"
"testing"
"github.com/forscht/namegen"
)
// TestGenerate tests the Generate method of the Generator.
func TestGenerate(t *testing.T) {
ng := namegen.New()
// Test that generated name contains expected number of words.
numWords := 3
name := ng.WithNumberOfWords(numWords).Generate()
if len(strings.Split(name, " ")) != numWords {
t.Errorf("Generate() did not return name with %d words", numWords)
}
// Test that generated name contains expected separator.
separator := "-"
name = ng.WithWordSeparator(separator).Generate()
if !strings.Contains(name, separator) {
t.Errorf("Generate() did not return name with separator '%s'", separator)
}
// Test that generated name is capitalized as expected.
name = ng.WithStyle(namegen.Title).Generate()
if !strings.EqualFold(name, strings.Title(strings.ToLower(name))) {
t.Errorf("Generate() did not capitalize name as expected with style '%s'", namegen.Title)
}
name = ng.WithStyle(namegen.Uppercase).Generate()
if !strings.EqualFold(name, strings.ToUpper(name)) {
t.Errorf("Generate() did not capitalize name as expected with style '%s'", namegen.Uppercase)
}
name = ng.WithStyle(namegen.Lowercase).Generate()
if !strings.EqualFold(name, strings.ToLower(name)) {
t.Errorf("Generate() did not capitalize name as expected with style '%s'", namegen.Lowercase)
}
// Test that generated name contains at least one word from each dictionary.
adjectives := []string{"big", "small", "red", "blue", "happy"}
colors := []string{"red", "green", "blue", "yellow", "purple"}
animals := []string{"dog", "cat", "bird", "fish", "lion"}
name = ng.WithDictionaries(adjectives, colors, animals).Generate()
for _, dict := range [][]string{adjectives, colors, animals} {
found := false
for _, word := range strings.Split(name, "-") {
if contains(word, dict) {
found = true
break
}
}
if !found {
t.Errorf("Generate() did not contain word from dictionary %v", dict)
}
}
}
// TestGenerateName tests the GenerateName function.
func TestGenerateName(t *testing.T) {
name := namegen.GenerateName()
if len(strings.Split(name, " ")) != 3 {
t.Error("GenerateName() did not return name with 3 words")
}
}
// contains returns true if the given slice contains the given string.
func contains(s string, slice []string) bool {
for _, elem := range slice {
if s == elem {
return true
}
}
return false
}