| 1 |
package xkcdpwgen |
| 2 |
|
| 3 |
import ( |
| 4 |
"strings" |
| 5 |
"testing" |
| 6 |
|
| 7 |
"github.com/stretchr/testify/assert" |
| 8 |
) |
| 9 |
|
| 10 |
func TestGenerator_GeneratePassword(t *testing.T) { |
| 11 |
g := NewGenerator() |
| 12 |
password := g.GeneratePasswordString() |
| 13 |
assert.Equal(t, 4, len(strings.Split(password, " ")), "not the right number of words for default settings") |
| 14 |
} |
| 15 |
|
| 16 |
func TestGenerator_GeneratePasswordWithCustomDelimiter(t *testing.T) { |
| 17 |
g := NewGenerator() |
| 18 |
g.SetDelimiter("%") |
| 19 |
password := g.GeneratePasswordString() |
| 20 |
assert.Equal(t, 4, len(strings.Split(password, "%")), "not the right number of words with custom delimiter") |
| 21 |
} |
| 22 |
|
| 23 |
func TestGenerator_GeneratePasswordWithCustomWordcount(t *testing.T) { |
| 24 |
g := NewGenerator() |
| 25 |
g.SetNumWords(5) |
| 26 |
password := g.GeneratePasswordString() |
| 27 |
assert.Equal(t, 5, len(strings.Split(password, " ")), "not the right number of words with custom wordcount") |
| 28 |
} |
| 29 |
|
| 30 |
func TestGenerator_GeneratePasswordWithCustomWordlist(t *testing.T) { |
| 31 |
g := NewGenerator() |
| 32 |
g.UseCustomWordlist([]string{"muh", "muh"}) |
| 33 |
password := g.GeneratePasswordString() |
| 34 |
assert.Equal(t, "muh muh muh muh", password, "not the expected password for custom wordlist") |
| 35 |
} |
| 36 |
|
| 37 |
func TestGenerator_GeneratePasswordWithCapitalization(t *testing.T) { |
| 38 |
g := NewGenerator() |
| 39 |
g.UseCustomWordlist([]string{"muh", "muh"}) |
| 40 |
g.SetCapitalize(true) |
| 41 |
password := g.GeneratePasswordString() |
| 42 |
assert.Equal(t, "Muh Muh Muh Muh", password, "not the correct capitalization") |
| 43 |
} |
| 44 |
|
| 45 |
func TestGenerator_SetLanguage(t *testing.T) { |
| 46 |
g := NewGenerator() |
| 47 |
err := g.UseLangWordlist("en") |
| 48 |
assert.Equal(t, nil, err, "Setting language to \"en\" should not raise an error") |
| 49 |
password := g.GeneratePasswordString() |
| 50 |
assert.Equal(t, 4, len(strings.Split(password, " ")), "not the right number of words for default settings") |
| 51 |
} |
| 52 |
|
| 53 |
func TestGenerator_SetInvalidLanguageRaisesError(t *testing.T) { |
| 54 |
g := NewGenerator() |
| 55 |
err := g.UseLangWordlist("muh") |
| 56 |
assert.EqualError(t, err, "language \"muh\" has no matching wordlist", "not the correct error message") |
| 57 |
} |