~wallyworld/juju-core/fast-lxc-everywhere

« back to all changes in this revision

Viewing changes to schema/schema_test.go

  • Committer: Gustavo Niemeyer
  • Date: 2011-09-05 20:35:00 UTC
  • mfrom: (2.1.4 go-formulas)
  • Revision ID: gustavo@niemeyer.net-20110905203500-20rf3w8b16m4zrfl
Merged go-iface-schemas branch [r=fwereade,hazmat]

This kicks off the formula schema support in the Go port.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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`)
 
36
 
 
37
        out, err = sch.Coerce(nil, aPath)
 
38
        c.Assert(out, IsNil)
 
39
        c.Assert(err, Matches, `<path>: expected "foo", got nothing`)
36
40
}
37
41
 
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")
 
48
 
 
49
        out, err = sch.Coerce(nil, aPath)
 
50
        c.Assert(err, IsNil)
 
51
        c.Assert(out, Equals, nil)
44
52
}
45
53
 
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")
 
84
 
 
85
        out, err = sch.Coerce(nil, aPath)
 
86
        c.Assert(out, IsNil)
 
87
        c.Assert(err, Matches, "<path>: expected bool, got nothing")
76
88
}
77
89
 
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")
 
104
 
 
105
        out, err = sch.Coerce(nil, aPath)
 
106
        c.Assert(out, IsNil)
 
107
        c.Assert(err, Matches, "<path>: expected int, got nothing")
92
108
}
93
109
 
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")
 
124
 
 
125
        out, err = sch.Coerce(nil, aPath)
 
126
        c.Assert(out, IsNil)
 
127
        c.Assert(err, Matches, "<path>: expected float, got nothing")
108
128
}
109
129
 
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")
 
140
 
 
141
        out, err = sch.Coerce(nil, aPath)
 
142
        c.Assert(out, IsNil)
 
143
        c.Assert(err, Matches, "<path>: expected string, got nothing")
120
144
}
121
145
 
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 "\["`)
 
159
 
 
160
        out, err = sch.Coerce(nil, aPath)
 
161
        c.Assert(out, IsNil)
 
162
        c.Assert(err, Matches, `<path>: expected regexp string, got nothing`)
135
163
}
136
164
 
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)})
142
170
 
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")
146
174
 
 
175
        out, err = sch.Coerce(nil, aPath)
 
176
        c.Assert(out, IsNil)
 
177
        c.Assert(err, Matches, "<path>: expected list, got nothing")
 
178
 
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)})
157
189
 
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")
161
193
 
 
194
        out, err = sch.Coerce(nil, aPath)
 
195
        c.Assert(out, IsNil)
 
196
        c.Assert(err, Matches, "<path>: expected map, got nothing")
 
197
 
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")
182
218
 
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"})
186
222
 
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")
190
226
 
 
227
        out, err = sch.Coerce(nil, aPath)
 
228
        c.Assert(out, IsNil)
 
229
        c.Assert(err, Matches, "<path>: expected map, got nothing")
 
230
 
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"`)
199
239
        // b is optional
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"})
203
243
 
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)
220
260
 
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})
224
264
 
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})
228
268
 
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`)
240
280
 
 
281
        out, err = sch.Coerce(42, aPath)
 
282
        c.Assert(out, IsNil)
 
283
        c.Assert(err, Matches, `<path>: expected map, got 42`)
 
284
 
 
285
        out, err = sch.Coerce(nil, aPath)
 
286
        c.Assert(out, IsNil)
 
287
        c.Assert(err, Matches, `<path>: expected map, got nothing`)
 
288
 
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)