-
Notifications
You must be signed in to change notification settings - Fork 0
/
checktype_test.go
163 lines (129 loc) · 3.55 KB
/
checktype_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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package toml
import (
gs "github.com/rafrombrc/gospec/src/gospec"
"time"
)
func CheckTypeSpec(c gs.Context) {
var err error
var tomlBlob = `
ranking = ["Springsteen", "J Geils"]
[bands.Springsteen]
type = "ignore_this"
started = 1973
albums = ["Greetings", "WIESS", "Born to Run", "Darkness"]
not_albums = ["Greetings", "WIESS", "Born to Run", "Darkness"]
[bands.J Geils]
started = 1970
albums = ["The J. Geils Band", "Full House", "Blow Your Face Out"]
`
type classics struct {
Ranking []string
Bands map[string]Primitive
}
c.Specify("check mapping", func() {
// Do the initial decode. Reflection is delayed on Primitive values.
var music classics
var md MetaData
md, err = Decode(tomlBlob, &music)
c.Assume(err, gs.IsNil)
empty_ignore := map[string]interface{}{}
err = CheckType(md.mapping, music, empty_ignore)
c.Assume(err, gs.IsNil)
})
}
func DecodeStrictSpec(c gs.Context) {
// This blob when used with an empty_ignore blob
var testSimple = `
age = 250
andrew = "gallant"
kait = "brady"
now = 1987-07-05T05:45:00Z
yesOrNo = true
pi = 3.14
colors = [
["red", "green", "blue"],
["cyan", "magenta", "yellow", "black"],
]
[Annoying.Cats]
plato = "smelly"
cauchy = "stupido"
`
var tomlBlob = `
# Some comments.
[alpha]
ip = "10.0.0.1"
[alpha.config]
Ports = [8001, 8002]
Location = "Toronto"
Created = 1987-07-05T05:45:00Z
`
type serverConfig struct {
Ports []int
Location string
Created time.Time
}
type server struct {
IPAddress string `toml:"ip"`
Config serverConfig `toml:"config"`
}
type kitties struct {
Plato string
Cauchy string
}
type simple struct {
Age int
Colors [][]string
Pi float64
YesOrNo bool
Now time.Time
Andrew string
Kait string
Annoying map[string]kitties
}
type servers map[string]server
var config servers
var val simple
var err error
empty_ignore := map[string]interface{}{}
_, err = DecodeStrict(tomlBlob, &config, empty_ignore)
c.Assume(err, gs.IsNil)
_, err = DecodeStrict(testSimple, &val, empty_ignore)
c.Assume(err, gs.IsNil)
_, err = DecodeStrict(testBadArg, &val, empty_ignore)
c.Assume(err.Error(), gs.Equals, "Configuration contains key [not_andrew] which doesn't exist in struct")
}
func DecodeStrictInterfaceSpec(c gs.Context) {
// Check that we can safely decode into an empty interface
// properly
var tomlBlob = `
[MyMultiDecoder]
type = "MultiDecoder"
order = ["MyJsonDecoder", "MyProtobufDecoder"]
[MyMultiDecoder.delegates.MyJsonDecoder]
type = "JsonDecoder"
encoding_name = "JSON"
[MyMultiDecoder.delegates.MyProtobufDecoder]
type = "ProtobufDecoder"
encoding_name = "PROTOCOL_BUFFER"
`
var err error
var obj interface{}
empty_ignore := map[string]interface{}{}
_, err = DecodeStrict(tomlBlob, &obj, empty_ignore)
c.Assume(err, gs.IsNil)
actualObj := obj.(map[string]interface{})
multidecoder := actualObj["MyMultiDecoder"].(map[string]interface{})
c.Expect(multidecoder["type"], gs.Equals, "MultiDecoder")
order := multidecoder["order"].([]interface{})
d1 := order[0].(string)
d2 := order[1].(string)
c.Expect(d1, gs.Equals, "MyJsonDecoder")
c.Expect(d2, gs.Equals, "MyProtobufDecoder")
delegates := multidecoder["delegates"].(map[string]interface{})
myjson := delegates["MyJsonDecoder"].(map[string]interface{})
myproto := delegates["MyProtobufDecoder"].(map[string]interface{})
c.Expect(myjson["type"], gs.Equals, "JsonDecoder")
c.Expect(myjson["encoding_name"], gs.Equals, "JSON")
c.Expect(myproto["type"], gs.Equals, "ProtobufDecoder")
c.Expect(myproto["encoding_name"], gs.Equals, "PROTOCOL_BUFFER")
}