forked from bluele/gforms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintegerfield_test.go
More file actions
41 lines (38 loc) · 835 Bytes
/
integerfield_test.go
File metadata and controls
41 lines (38 loc) · 835 Bytes
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
package gforms
import (
"net/http"
"net/url"
"strings"
"testing"
)
type testIntegerObject struct {
Integer int `gforms:"integer"`
}
func TestIntegerField(t *testing.T) {
Form := DefineForm(NewFields(
NewIntegerField("integer", nil),
))
data := url.Values{"integer": {"100"}}
req, _ := http.NewRequest("POST", "/", strings.NewReader(data.Encode()))
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
form := Form(req)
if form.IsValid() {
v, ok := form.CleanedData["integer"]
if !ok {
t.Error(`"integer" is required.`)
return
}
_, ok = v.(int)
if !ok {
t.Error(`"integer" should be integer type.`)
return
}
obj := new(testIntegerObject)
form.MapTo(obj)
if obj.Integer == 0 {
t.Error(`"obj.Integer" should not be zero.`)
}
} else {
t.Error("validation error.")
}
}