33
33
out, err = sch.Coerce(42, aPath)
34
34
c.Assert(out, IsNil)
35
35
c.Assert(err, Matches, `<path>: expected "foo", got 42`)
37
out, err = sch.Coerce(nil, aPath)
39
c.Assert(err, Matches, `<path>: expected "foo", got nothing`)
38
42
func (s *S) TestAny(c *C) {
41
45
out, err := sch.Coerce("foo", aPath)
42
46
c.Assert(err, IsNil)
43
47
c.Assert(out, Equals, "foo")
49
out, err = sch.Coerce(nil, aPath)
51
c.Assert(out, Equals, nil)
46
54
func (s *S) TestOneOf(c *C) {
73
81
out, err = sch.Coerce(1, aPath)
74
82
c.Assert(out, IsNil)
75
83
c.Assert(err, Matches, "<path>: expected bool, got 1")
85
out, err = sch.Coerce(nil, aPath)
87
c.Assert(err, Matches, "<path>: expected bool, got nothing")
78
90
func (s *S) TestInt(c *C) {
89
101
out, err = sch.Coerce(true, aPath)
90
102
c.Assert(out, IsNil)
91
103
c.Assert(err, Matches, "<path>: expected int, got true")
105
out, err = sch.Coerce(nil, aPath)
107
c.Assert(err, Matches, "<path>: expected int, got nothing")
94
110
func (s *S) TestFloat(c *C) {
105
121
out, err = sch.Coerce(true, aPath)
106
122
c.Assert(out, IsNil)
107
123
c.Assert(err, Matches, "<path>: expected float, got true")
125
out, err = sch.Coerce(nil, aPath)
127
c.Assert(err, Matches, "<path>: expected float, got nothing")
110
130
func (s *S) TestString(c *C) {
117
137
out, err = sch.Coerce(true, aPath)
118
138
c.Assert(out, IsNil)
119
139
c.Assert(err, Matches, "<path>: expected string, got true")
141
out, err = sch.Coerce(nil, aPath)
143
c.Assert(err, Matches, "<path>: expected string, got nothing")
122
146
func (s *S) TestSimpleRegexp(c *C) {
132
156
out, err = sch.Coerce("[", aPath)
133
157
c.Assert(out, IsNil)
134
158
c.Assert(err, Matches, `<path>: expected valid regexp, got "\["`)
160
out, err = sch.Coerce(nil, aPath)
162
c.Assert(err, Matches, `<path>: expected regexp string, got nothing`)
137
165
func (s *S) TestList(c *C) {
138
166
sch := schema.List(schema.Int())
139
167
out, err := sch.Coerce([]int8{1, 2}, aPath)
140
168
c.Assert(err, IsNil)
141
c.Assert(out, Equals, []interface{}{int64(1), int64(2)})
169
c.Assert(out, Equals, schema.L{int64(1), int64(2)})
143
171
out, err = sch.Coerce(42, aPath)
144
172
c.Assert(out, IsNil)
145
173
c.Assert(err, Matches, "<path>: expected list, got 42")
175
out, err = sch.Coerce(nil, aPath)
177
c.Assert(err, Matches, "<path>: expected list, got nothing")
147
179
out, err = sch.Coerce([]interface{}{1, true}, aPath)
148
180
c.Assert(out, IsNil)
149
181
c.Assert(err, Matches, `<path>\[1\]: expected int, got true`)
153
185
sch := schema.Map(schema.String(), schema.Int())
154
186
out, err := sch.Coerce(map[string]interface{}{"a": 1, "b": int8(2)}, aPath)
155
187
c.Assert(err, IsNil)
156
c.Assert(out, Equals, map[interface{}]interface{}{"a": int64(1), "b": int64(2)})
188
c.Assert(out, Equals, schema.M{"a": int64(1), "b": int64(2)})
158
190
out, err = sch.Coerce(42, aPath)
159
191
c.Assert(out, IsNil)
160
192
c.Assert(err, Matches, "<path>: expected map, got 42")
194
out, err = sch.Coerce(nil, aPath)
196
c.Assert(err, Matches, "<path>: expected map, got nothing")
162
198
out, err = sch.Coerce(map[int]int{1: 1}, aPath)
163
199
c.Assert(out, IsNil)
164
200
c.Assert(err, Matches, "<path>: expected string, got 1")
183
219
out, err := sch.Coerce(map[string]interface{}{"a": "A", "b": "B"}, aPath)
184
220
c.Assert(err, IsNil)
185
c.Assert(out, Equals, map[string]interface{}{"a": "A", "b": "B"})
221
c.Assert(out, Equals, schema.M{"a": "A", "b": "B"})
187
223
out, err = sch.Coerce(42, aPath)
188
224
c.Assert(out, IsNil)
189
225
c.Assert(err, Matches, "<path>: expected map, got 42")
227
out, err = sch.Coerce(nil, aPath)
229
c.Assert(err, Matches, "<path>: expected map, got nothing")
191
231
out, err = sch.Coerce(map[string]interface{}{"a": "A", "b": "C"}, aPath)
192
232
c.Assert(out, IsNil)
193
233
c.Assert(err, Matches, `<path>\.b: expected "B", got "C"`)
200
240
out, err = sch.Coerce(map[string]interface{}{"a": "A"}, aPath)
201
241
c.Assert(err, IsNil)
202
c.Assert(out, Equals, map[string]interface{}{"a": "A"})
242
c.Assert(out, Equals, schema.M{"a": "A"})
204
244
// First path entry shouldn't have dots in an error message.
205
245
out, err = sch.Coerce(map[string]bool{"a": true}, nil)
221
261
out, err := sch.Coerce(map[string]int{"type": 1, "a": 2}, aPath)
222
262
c.Assert(err, IsNil)
223
c.Assert(out, Equals, map[string]interface{}{"type": 1, "a": 2})
263
c.Assert(out, Equals, schema.M{"type": 1, "a": 2})
225
265
out, err = sch.Coerce(map[string]int{"type": 3, "b": 4}, aPath)
226
266
c.Assert(err, IsNil)
227
c.Assert(out, Equals, map[string]interface{}{"type": 3, "b": 4})
267
c.Assert(out, Equals, schema.M{"type": 3, "b": 4})
229
269
out, err = sch.Coerce(map[string]int{}, aPath)
230
270
c.Assert(out, IsNil)
238
278
c.Assert(out, IsNil)
239
279
c.Assert(err, Matches, `<path>\.b: expected 4, got 5`)
281
out, err = sch.Coerce(42, aPath)
283
c.Assert(err, Matches, `<path>: expected map, got 42`)
285
out, err = sch.Coerce(nil, aPath)
287
c.Assert(err, Matches, `<path>: expected map, got nothing`)
241
289
// First path entry shouldn't have dots in an error message.
242
290
out, err = sch.Coerce(map[string]int{"a": 1}, nil)
243
291
c.Assert(out, IsNil)