forked from bluele/gforms
-
Notifications
You must be signed in to change notification settings - Fork 1
/
booleanfield_test.go
68 lines (64 loc) · 1.44 KB
/
booleanfield_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 gforms
import (
"net/http"
"net/url"
"strings"
"testing"
)
type testBooleanObject struct {
Check bool `gforms:"check"`
}
func TestTrueBooleanField(t *testing.T) {
Form := DefineForm(NewFields(
NewBooleanField("check", nil),
))
req, _ := http.NewRequest("POST", "/", strings.NewReader(url.Values{"check": {""}}.Encode()))
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
form := Form(req)
if form.IsValid() {
v, ok := form.CleanedData["check"]
if !ok {
t.Error(`"check" is required.`)
return
}
_, ok = v.(bool)
if !ok {
t.Error(`"check" should be boolean type.`)
return
}
obj := new(testBooleanObject)
form.MapTo(obj)
if obj.Check == false {
t.Error(`"obj.Check" should not be false.`)
}
} else {
t.Error("validation error.")
}
}
func TestFalseBooleanField(t *testing.T) {
Form := DefineForm(NewFields(
NewBooleanField("check", nil),
))
req, _ := http.NewRequest("POST", "/", strings.NewReader(url.Values{}.Encode()))
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
form := Form(req)
if form.IsValid() {
v, ok := form.CleanedData["check"]
if !ok {
t.Error(`"check" is required.`)
return
}
_, ok = v.(bool)
if !ok {
t.Error(`"check" should be boolean type.`)
return
}
obj := new(testBooleanObject)
form.MapTo(obj)
if obj.Check == true {
t.Error(`"obj.Check" should not be true.`)
}
} else {
t.Error("validation error.")
}
}