-
Notifications
You must be signed in to change notification settings - Fork 1
/
walk_test.go
111 lines (87 loc) · 2.9 KB
/
walk_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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package validator_test
import (
. "github.com/typerandom/validator"
"reflect"
"testing"
)
func testThatValidatorCanWalkItems(t *testing.T, items interface{}, numItems int) {
errs := Validate(items)
if len(errs) != numItems {
t.Fatalf("Expected %d errors, got %d.", numItems, len(errs))
}
for _, err := range errs {
if err.Error() != "Value cannot be empty." {
t.Fatalf("Expected validation error, got %s.", err)
}
}
}
type walkDummy struct {
Value string `validate:"not_empty"`
}
func TestThatValidatorCanWalkSlice(t *testing.T) {
dummies := []*walkDummy{&walkDummy{}, &walkDummy{}, &walkDummy{}, &walkDummy{}}
testThatValidatorCanWalkItems(t, dummies, len(dummies))
}
func TestThatValidatorCanWalkArray(t *testing.T) {
dummies := [...]*walkDummy{&walkDummy{}, &walkDummy{}, &walkDummy{}, &walkDummy{}}
testThatValidatorCanWalkItems(t, dummies, len(dummies))
}
func TestThatValidatorCanWalkMap(t *testing.T) {
dummies := map[string]*walkDummy{"a": &walkDummy{}, "b": &walkDummy{}, "c": &walkDummy{}, "d": &walkDummy{}}
testThatValidatorCanWalkItems(t, dummies, len(dummies))
}
func TestThatValidatorCanWalkStruct(t *testing.T) {
type Dummy struct {
StringValue string `validate:"not_empty"`
IntValue int `validate:"not_empty"`
FloatValue float32 `validate:"not_empty"`
UIntValue uint `validate:"not_empty"`
PtrValue *bool `validate:"not_empty"`
}
dummy := Dummy{}
errs := Validate(dummy)
totalFields := reflect.TypeOf(dummy).NumField()
if len(errs) != totalFields {
t.Fatalf("Expected %d errors, got %d.", totalFields, len(errs))
}
expectedErrors := []string{
"StringValue cannot be empty.",
"IntValue cannot be empty.",
"FloatValue cannot be empty.",
"UIntValue cannot be empty.",
"PtrValue cannot be empty.",
}
for i, err := range expectedErrors {
if errs[i].Error() != err {
t.Fatalf("Expected error '%s', but got '%s'.", err, errs[i].Error())
}
}
}
func testThatValidatorCannotWalkValue(t *testing.T, dummy interface{}, typeName string) {
errs := Validate(dummy)
if !errs.Any() {
t.Fatalf("Expected error, but didn't get any.")
}
if len(errs) != 1 {
t.Fatalf("Expected 1 error, but got %d.", len(errs))
}
expectedErr := "Unable to directly validate type '" + typeName + "'."
if errs.First().Error() != expectedErr {
t.Fatalf("Expected error '%s', but got '%s'.", expectedErr, errs.First())
}
}
func TestThatValidatorCannotWalkString(t *testing.T) {
testThatValidatorCannotWalkValue(t, "test", "string")
}
func TestThatValidatorCannotWalkInt(t *testing.T) {
testThatValidatorCannotWalkValue(t, int64(123), "int64")
}
func TestThatValidatorCannotWalkFloat(t *testing.T) {
testThatValidatorCannotWalkValue(t, 123.456, "float64")
}
func TestThatValidatorCannotWalkBool(t *testing.T) {
testThatValidatorCannotWalkValue(t, false, "bool")
}
func TestThatValidatorCannotWalkInvalid(t *testing.T) {
testThatValidatorCannotWalkValue(t, nil, "invalid")
}