-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmapper_test.go
108 lines (89 loc) · 2.03 KB
/
mapper_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
package cookoo
import (
"testing"
)
func TestMap(t *testing.T) {
cxt := NewContext()
params := NewParamsWithValues(map[string]interface{}{
"funty": 1,
"FloatField": float32(2.3),
"StrField": "hello",
"BoolField": true,
"SliceField": []int{1, 2, 3},
"MapField": map[string]bool{"true": true},
"StructField": basic{true},
"StructPtrField": &basic{false},
})
s := &mystruct{}
def, err := Map(cxt, params, s)
if err != nil {
t.Errorf("Failed: %s", err)
}
res := def.(*mystruct)
if res.IntField != 1 {
t.Errorf("Expected 1, got %d", res.IntField)
}
if res.FloatField != 2.3 {
t.Errorf("Expected 2.3, got %f", res.FloatField)
}
if res.StrField != "hello" {
t.Errorf("Expected hello, got %s", res.StrField)
}
if !res.BoolField {
t.Errorf("BoolField is false")
}
if len(res.SliceField) != 3 {
t.Errorf("Expected slice of 2.")
}
if !res.MapField["true"] {
t.Errorf("Expected true:true.")
}
if !res.StructField.hai {
t.Errorf("expected basic to have true")
}
if res.StructPtrField.hai {
t.Errorf("expected *basic to have false.")
}
}
func TestMappedRoute(t *testing.T) {
reg, router, cxt := Cookoo()
reg.AddRoute(Route{
Name: "test",
Does: Tasks{
CmdDef{
Name: "cmdDef",
Def: &mystruct{},
Using: []Param{
{Name: "funty", DefaultValue: 5},
{Name: "StrField", DefaultValue: "Batty"},
},
},
},
})
if err := router.HandleRequest("test", cxt, true); err != nil {
t.Error(err)
}
cmdDef := cxt.Get("cmdDef", nil).(*mystruct)
if cmdDef.IntField != 5 {
t.Errorf("Expected 5, got %d", cmdDef.IntField)
}
if cmdDef.StrField != "Batty" {
t.Errorf("Expected Batty, got %s", cmdDef.StrField)
}
}
type mystruct struct {
IntField int `coo:"funty"`
FloatField float32
StrField string
BoolField bool
SliceField []int
MapField map[string]bool
StructField basic
StructPtrField *basic
}
func (m *mystruct) Run(c Context) (interface{}, Interrupt) {
return m, nil
}
type basic struct {
hai bool
}