shiori/cmd/account_test.go
2018-03-06 15:33:29 +07:00

50 lines
1.1 KiB
Go

package cmd
import (
"bytes"
"strings"
"testing"
)
func TestAddAccount(t *testing.T) {
tests := []struct {
username string
password string
want string
}{
{"", "", "Username must not be empty"},
{"abc", "abc", "Password must be at least"},
{"abc", "fooBar123", ""},
}
for _, tt := range tests {
err := addAccount(tt.username, tt.password)
if err != nil {
if tt.want == "" {
t.Errorf("got unexpected error: %v", err)
}
if !strings.Contains(err.Error(), tt.want) {
t.Errorf("expected error containing '%s', got error '%v'", err, tt.want)
}
continue
}
if tt.want != "" {
t.Errorf("expected error '%s', got no error", tt.want)
}
}
}
func TestPrintAccounts(t *testing.T) {
if err := addAccount("foo", "fooBar123"); err != nil {
t.Errorf("failed to add test account: %v", err)
return
}
b := bytes.NewBufferString("")
err := printAccounts("", b)
if err != nil {
t.Errorf("got unexpected error: %v", err)
}
got := b.String()
if !strings.Contains(got, "foo") {
t.Errorf("expected string containing 'foo', got '%s'", got)
}
}