-
Notifications
You must be signed in to change notification settings - Fork 0
/
strings_test.go
89 lines (79 loc) · 2.33 KB
/
strings_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
package validator
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestLength(t *testing.T) {
cases := []struct {
name string
length int
message string
exp error
}{
{name: "zero", length: 0, message: "", exp: nil},
{name: "empty", length: 2, message: "", exp: ErrWrongLength},
{name: "less", length: 2, message: "1", exp: ErrWrongLength},
{name: "equal", length: 2, message: "12", exp: nil},
{name: "more", length: 2, message: "123", exp: ErrWrongLength},
{name: "much more", length: 2, message: "1234", exp: ErrWrongLength},
}
for _, tc := range cases {
err := Length(tc.length)(tc.message)
assert.ErrorIs(t, err, tc.exp, tc.name)
}
}
func TestShorter(t *testing.T) {
cases := []struct {
name string
length int
message string
exp error
}{
{name: "zero", length: 0, message: "", exp: nil},
{name: "much shorter", length: 2, message: "", exp: nil},
{name: "shorter", length: 2, message: "1", exp: nil},
{name: "equal", length: 2, message: "12", exp: nil},
{name: "longer", length: 2, message: "123", exp: ErrTooLong},
{name: "much longer", length: 2, message: "1234", exp: ErrTooLong},
}
for _, tc := range cases {
err := Shorter(tc.length)(tc.message)
assert.ErrorIs(t, err, tc.exp, tc.name)
}
}
func TestLonger(t *testing.T) {
cases := []struct {
name string
length int
message string
exp error
}{
{name: "zero", length: 0, message: "", exp: nil},
{name: "much shorter", length: 2, message: "", exp: ErrTooShort},
{name: "shorter", length: 2, message: "1", exp: ErrTooShort},
{name: "equal", length: 2, message: "12", exp: nil},
{name: "longer", length: 2, message: "123", exp: nil},
{name: "much longer", length: 2, message: "1234", exp: nil},
}
for _, tc := range cases {
err := Longer(tc.length)(tc.message)
assert.ErrorIs(t, err, tc.exp, tc.name)
}
}
func TestEmail(t *testing.T) {
cases := []struct {
name string
email string
exp error
}{
{name: "empty", email: "", exp: ErrEmailInvalid},
{name: "one word", email: "local", exp: ErrEmailInvalid},
{name: "without domain", email: "local@local", exp: nil},
{name: "with domain", email: "[email protected]", exp: nil},
{name: "numbers", email: "[email protected]", exp: nil},
}
for _, tc := range cases {
err := Email()(tc.email)
assert.ErrorIs(t, err, tc.exp, tc.name)
}
}