forked from jfyne/live
-
Notifications
You must be signed in to change notification settings - Fork 0
/
params_test.go
114 lines (104 loc) · 2.31 KB
/
params_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
112
113
114
package live
import (
"net/http"
"net/url"
"testing"
)
func TestParamString(t *testing.T) {
p := Params{"test": "output"}
out := p.String("test")
if out != "output" {
t.Error("unexpected output of ParamString", out)
}
empty := p.String("nokey")
if empty != "" {
t.Error("unexpected output of ParamString", empty)
}
}
func TestParamCheckbox(t *testing.T) {
p := Params{"test": "on"}
state := p.Checkbox("test")
if state != true {
t.Error("unexpected output of ParamCheckbox", state)
}
p["test"] = "noton"
state = p.Checkbox("test")
if state != false {
t.Error("unexpected output of ParamCheckbox", state)
}
state = p.Checkbox("nottest")
if state != false {
t.Error("unexpected output of ParamCheckbox", state)
}
}
func TestParamInt(t *testing.T) {
var out int
p := Params{"test": 1}
out = p.Int("test")
if out != 1 {
t.Error("unexpected output of ParamInt", out)
}
p["test"] = "1"
out = p.Int("test")
if out != 1 {
t.Error("unexpected output of ParamInt", out)
}
p["test"] = "aaa"
out = p.Int("test")
if out != 0 {
t.Error("unexpected output of ParamInt", out)
}
p["test"] = 1
out = p.Int("nottest")
if out != 0 {
t.Error("unexpected output of ParamInt", out)
}
}
func TestParamFloat32(t *testing.T) {
var out float32
p := Params{"test": 1.0}
out = p.Float32("test")
if out != 1.0 {
t.Error("unexpected output of ParamFloat32", out)
}
p["test"] = "1.0"
out = p.Float32("test")
if out != 1.0 {
t.Error("unexpected output of ParamFloat32", out)
}
p["test"] = "aaa"
out = p.Float32("test")
if out != 0.0 {
t.Error("unexpected output of ParamFloat32", out)
}
p["test"] = 1.0
out = p.Float32("nottest")
if out != 0.0 {
t.Error("unexpected output of ParamFloat32", out)
}
}
func TestParamsFromRequest(t *testing.T) {
var err error
r := &http.Request{}
r.URL, err = url.Parse("http://example.com?one=1&two=2&three=3&three=4")
if err != nil {
t.Fatal(err)
}
params := NewParamsFromRequest(r)
var out int
out = params.Int("one")
if out != 1 {
t.Error("did not get expected params", params)
}
out = params.Int("two")
if out != 2 {
t.Error("did not get expected params", params)
}
sliceout, ok := params["three"].([]string)
if !ok {
t.Error("did not get expected params", params)
}
if len(sliceout) != 2 {
t.Error("did not get expected params", params)
}
}