-
Notifications
You must be signed in to change notification settings - Fork 10
/
validators_test.go
68 lines (64 loc) · 1.51 KB
/
validators_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
package iprepd
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestValidateType(t *testing.T) {
tests := []struct {
Name string
Type string
Object string
ExpectErr bool
ExpectedErr error
}{
{
Name: "test: validate good IP",
Type: TypeIP,
Object: "228.28.28.28",
ExpectErr: false,
},
{
Name: "test: validate good Email",
Type: TypeEmail,
Object: "[email protected]",
ExpectErr: false,
},
{
Name: "test: validate bad IP",
Type: TypeIP,
Object: "2assfa28.28",
ExpectErr: true,
ExpectedErr: fmt.Errorf("invalid ip format %v", "2assfa28.28"),
},
{
Name: "test: validate bad Email",
Type: TypeEmail,
Object: "not an email",
ExpectErr: true,
ExpectedErr: fmt.Errorf("invalid email format %v", "not an email"),
},
{
Name: "test: validate wrong type",
Type: TypeEmail,
Object: "228.28.28.28",
ExpectErr: true,
ExpectedErr: fmt.Errorf("invalid email format %v", "228.28.28.28"),
},
{
Name: "test: validate unknown type",
Type: "some undefined type",
Object: "228.28.28.28",
ExpectErr: true,
ExpectedErr: fmt.Errorf("unknown type for validation %v", "some undefined type"),
},
}
for _, tst := range tests {
err := validateType(tst.Type, tst.Object)
if tst.ExpectErr {
assert.Equal(t, tst.ExpectedErr, err, tst.Name)
} else {
assert.Nil(t, err, tst.Name)
}
}
}