-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_test.go
56 lines (45 loc) · 1.07 KB
/
gen_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
package pronwords
import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestCharSetSameLengthAsWordLength(t *testing.T) {
assertGeneratedWords(t, "ab", "aa,ab,ba,bb", 2)
}
func TestCharSetLongerThanWordLength(t *testing.T) {
assertGeneratedWords(
t,
"abc", // charset
"aa,ab,ac,ba,bb,bc,ca,cb,cc",
2, // word length
)
}
func TestCharSetShorterThanWordLength(t *testing.T) {
assertGeneratedWords(
t,
"ab", // charset
"aaa,aab,aba,abb,baa,bab,bba,bbb",
3, // word length
)
}
func assertGeneratedWords(t *testing.T, characters, expectedCommaSeparated string, length int) {
var (
expectedList = strings.Split(expectedCommaSeparated, ",")
words = make(map[string]bool, len(expectedList))
)
for _, word := range expectedList {
words[word] = true
}
g := NewGenerator(length)
g.SetCharacters(characters)
count := g.Count()
assert.Equal(t, len(words), count)
for ; count > 0; count-- {
word := g.Next()
assert.Contains(t, words, word)
delete(words, word)
}
assert.Equal(t, "", g.Next())
assert.Equal(t, 0, len(words))
}