-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvalidator_test.go
More file actions
166 lines (150 loc) · 3.33 KB
/
validator_test.go
File metadata and controls
166 lines (150 loc) · 3.33 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package forms
import (
"errors"
"net/http"
"net/url"
"testing"
)
var (
validator_error = errors.New("validator_error")
error_validator ValidatorFunc = func(in string) (out string, err error) {
err = validator_error
return
}
pass_validator ValidatorFunc = func(in string) (out string, err error) {
out = in
return
}
)
func fatal_validator(t *testing.T) ValidatorFunc {
return func(in string) (out string, err error) {
t.Fail()
out = in
return
}
}
func change_to(out string) ValidatorFunc {
return func(in string) (o string, err error) {
o = out
return
}
}
func create_req(values url.Values) (req *http.Request) {
req, err := http.NewRequest("GET", "/", nil)
if err != nil {
panic(err)
}
req.Form = values
return
}
func TestValidatorFails(t *testing.T) {
f := &Form{
Fields: []Field{
{
Name: "foo",
Validators: []Validator{error_validator},
},
},
}
res := f.Load(create_req(url.Values{
"foo": {"bar"},
}))
if ex, ok := res.Errors["foo"]; !ok || ex != validator_error {
t.Fatalf("Expected %v. Got %v", validator_error, ex)
}
if ex, ok := res.Values["foo"]; !ok || ex != "bar" {
t.Fatalf("Expected %v. Got %v", "bar", ex)
}
}
func TestValidatorFailsChain(t *testing.T) {
f := &Form{
Fields: []Field{
{
Name: "foo",
Validators: []Validator{pass_validator, pass_validator, error_validator},
},
},
}
res := f.Load(create_req(url.Values{
"foo": {"bar"},
}))
if ex, ok := res.Errors["foo"]; !ok || ex != validator_error {
t.Fatalf("Expected %v. Got %v", validator_error, ex)
}
if ex, ok := res.Values["foo"]; !ok || ex != "bar" {
t.Fatalf("Expected %v. Got %v", "bar", ex)
}
}
func TestValidatorChanges(t *testing.T) {
f := &Form{
Fields: []Field{
{
Name: "foo",
Validators: []Validator{change_to("baz")},
},
},
}
res := f.Load(create_req(url.Values{
"foo": {"bar"},
}))
if ex, ok := res.Errors["foo"]; ok || ex != nil {
t.Fatalf("Expected %v. Got %v", nil, ex)
}
rval := res.Value.(map[string]interface{})
if ex, ok := rval["foo"]; !ok || ex != "baz" {
t.Fatalf("Expected %v. Got %v", "baz", ex)
}
}
func TestValidatorEarlyExit(t *testing.T) {
f := &Form{
Fields: []Field{
{
Name: "foo",
Validators: []Validator{error_validator, fatal_validator(t)},
},
},
}
f.Load(create_req(url.Values{
"foo": {"bar"},
}))
}
func TestValidatorEmpty(t *testing.T) {
f := &Form{
Fields: []Field{
{Name: "foo"},
},
}
res := f.Load(create_req(url.Values{
"foo": {"bar"},
}))
if ex, ok := res.Errors["foo"]; ok || ex != nil {
t.Fatalf("Expected %v. Got %v", nil, ex)
}
rval := res.Value.(map[string]interface{})
if ex, ok := rval["foo"]; !ok || ex != "bar" {
t.Fatalf("Expected %v. Got %v", "bar", ex)
}
}
func TestValidatorsNonemptyValidator(t *testing.T) {
f := &Form{
Fields: []Field{
{Name: "foo", Validators: []Validator{NonemptyValidator}},
},
}
res := f.Load(create_req(url.Values{
"foo": {"bar"},
}))
if ex, ok := res.Errors["foo"]; ok || ex != nil {
t.Fatalf("Expected %v. Got %v", nil, ex)
}
res = f.Load(create_req(url.Values{
"foo": {""},
}))
if ex, ok := res.Errors["foo"]; !ok || ex == nil {
t.Fatalf("Expected %v. Got %v", "not nil", ex)
}
res = f.Load(create_req(nil))
if ex, ok := res.Errors["foo"]; !ok || ex == nil {
t.Fatalf("Expected %v. Got %v", "not nil", ex)
}
}