-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathconverter_test.go
More file actions
135 lines (125 loc) · 2.88 KB
/
converter_test.go
File metadata and controls
135 lines (125 loc) · 2.88 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
package forms
import (
"errors"
"fmt"
"net/url"
"testing"
)
var (
converter_error = errors.New("converter_error")
test_int_converter ConverterFunc = func(in string) (out interface{}, err error) {
out = 2
return
}
error_converter ConverterFunc = func(in string) (out interface{}, err error) {
out = in
err = converter_error
return
}
)
func fatal_converter(t *testing.T) ConverterFunc {
return func(in string) (out interface{}, err error) {
t.Fail()
out = in
return
}
}
func TestConverterInt(t *testing.T) {
f := &Form{
Fields: []Field{
{Name: "foo", Converter: test_int_converter},
},
}
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.(int) != 2 {
t.Fatalf("Expected %v. Got %v", 2, ex)
}
}
func TestConverterError(t *testing.T) {
f := &Form{
Fields: []Field{
{Name: "foo", Converter: error_converter},
},
}
res := f.Load(create_req(url.Values{
"foo": {"bar"},
}))
if ex, ok := res.Errors["foo"]; !ok || ex != converter_error {
t.Fatalf("Expected %v. Got %v", converter_error, ex)
}
}
func TestConverterNotCalledOnValidatorError(t *testing.T) {
f := &Form{
Fields: []Field{
{
Name: "foo",
Validators: []Validator{error_validator},
Converter: fatal_converter(t),
},
},
}
f.Load(create_req(url.Values{
"foo": {"bar"},
}))
}
//test specific converters
func TestConvertersIntConverter(t *testing.T) {
f := &Form{
Fields: []Field{
{Name: "foo", Converter: IntConverter},
},
}
for i := -10; i <= 10; i++ {
res := f.Load(create_req(url.Values{
"foo": {fmt.Sprint(i)},
}))
if ex, ok := res.Errors["foo"]; ok || ex != nil {
t.Errorf("Expected %v. Got %v", nil, ex)
continue
}
rval := res.Value.(map[string]interface{})
if ex, ok := rval["foo"].(int); !ok || ex != i {
t.Errorf("Expected %v. Got %v", i, ex)
}
}
}
func TestConvertersFloat32Converter(t *testing.T) {
f := &Form{
Fields: []Field{
{Name: "foo", Converter: Float32Converter},
},
}
res := f.Load(create_req(url.Values{
"foo": {"3.14159"},
}))
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"].(float32); !ok || ex != 3.14159 {
t.Fatalf("Expected %v. Got %v", 3.14159, ex)
}
}
func TestConvertersFloat64Converter(t *testing.T) {
f := &Form{
Fields: []Field{
{Name: "foo", Converter: Float64Converter},
},
}
res := f.Load(create_req(url.Values{
"foo": {"3.14159"},
}))
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"].(float64); !ok || ex != 3.14159 {
t.Fatalf("Expected %v. Got %v", 3.14159, ex)
}
}