How to use isNil method of got Package

Best Got code snippet using got.isNil

Run Got automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

schema_test.go

Source: schema_test.go Github

copy
1// Copyright 2011, 2012, 2013 Canonical Ltd.
2// Licensed under the AGPLv3, see LICENCE file for details.
3
4package schema_test
5
6import (
7	"math"
8	"testing"
9
10	. "launchpad.net/gocheck"
11
12	"launchpad.net/juju-core/schema"
13)
14
15func Test(t *testing.T) {
16	TestingT(t)
17}
18
19type S struct{}
20
21var _ = Suite(&S{})
22
23type Dummy struct{}
24
25func (d *Dummy) Coerce(value interface{}, path []string) (coerced interface{}, err error) {
26	return "i-am-dummy", nil
27}
28
29var aPath = []string{"<pa", "th>"}
30
31func (s *S) TestConst(c *C) {
32	sch := schema.Const("foo")
33
34	out, err := sch.Coerce("foo", aPath)
35	c.Assert(err, IsNil)
36	c.Assert(out, Equals, "foo")
37
38	out, err = sch.Coerce(42, aPath)
39	c.Assert(out, IsNil)
40	c.Assert(err, ErrorMatches, `<path>: expected "foo", got 42`)
41
42	out, err = sch.Coerce(nil, aPath)
43	c.Assert(out, IsNil)
44	c.Assert(err, ErrorMatches, `<path>: expected "foo", got nothing`)
45}
46
47func (s *S) TestAny(c *C) {
48	sch := schema.Any()
49
50	out, err := sch.Coerce("foo", aPath)
51	c.Assert(err, IsNil)
52	c.Assert(out, Equals, "foo")
53
54	out, err = sch.Coerce(nil, aPath)
55	c.Assert(err, IsNil)
56	c.Assert(out, Equals, nil)
57}
58
59func (s *S) TestOneOf(c *C) {
60	sch := schema.OneOf(schema.Const("foo"), schema.Const(42))
61
62	out, err := sch.Coerce("foo", aPath)
63	c.Assert(err, IsNil)
64	c.Assert(out, Equals, "foo")
65
66	out, err = sch.Coerce(42, aPath)
67	c.Assert(err, IsNil)
68	c.Assert(out, Equals, 42)
69
70	out, err = sch.Coerce("bar", aPath)
71	c.Assert(out, IsNil)
72	c.Assert(err, ErrorMatches, `<path>: unexpected value "bar"`)
73}
74
75func (s *S) TestBool(c *C) {
76	sch := schema.Bool()
77
78	out, err := sch.Coerce(true, aPath)
79	c.Assert(err, IsNil)
80	c.Assert(out, Equals, true)
81
82	out, err = sch.Coerce(false, aPath)
83	c.Assert(err, IsNil)
84	c.Assert(out, Equals, false)
85
86	out, err = sch.Coerce(1, aPath)
87	c.Assert(out, IsNil)
88	c.Assert(err, ErrorMatches, "<path>: expected bool, got 1")
89
90	out, err = sch.Coerce(nil, aPath)
91	c.Assert(out, IsNil)
92	c.Assert(err, ErrorMatches, "<path>: expected bool, got nothing")
93}
94
95func (s *S) TestInt(c *C) {
96	sch := schema.Int()
97
98	out, err := sch.Coerce(42, aPath)
99	c.Assert(err, IsNil)
100	c.Assert(out, Equals, int64(42))
101
102	out, err = sch.Coerce(int8(42), aPath)
103	c.Assert(err, IsNil)
104	c.Assert(out, Equals, int64(42))
105
106	out, err = sch.Coerce(true, aPath)
107	c.Assert(out, IsNil)
108	c.Assert(err, ErrorMatches, "<path>: expected int, got true")
109
110	out, err = sch.Coerce(nil, aPath)
111	c.Assert(out, IsNil)
112	c.Assert(err, ErrorMatches, "<path>: expected int, got nothing")
113}
114
115func (s *S) TestForceInt(c *C) {
116	sch := schema.ForceInt()
117
118	out, err := sch.Coerce(42, aPath)
119	c.Assert(err, IsNil)
120	c.Assert(out, Equals, int(42))
121
122	out, err = sch.Coerce(int8(42), aPath)
123	c.Assert(err, IsNil)
124	c.Assert(out, Equals, int(42))
125
126	out, err = sch.Coerce(float32(42), aPath)
127	c.Assert(err, IsNil)
128	c.Assert(out, Equals, int(42))
129
130	out, err = sch.Coerce(float64(42), aPath)
131	c.Assert(err, IsNil)
132	c.Assert(out, Equals, int(42))
133
134	out, err = sch.Coerce(42.66, aPath)
135	c.Assert(err, IsNil)
136	c.Assert(out, Equals, int(42))
137
138	// If an out of range value is provided, that value is truncated,
139	// generating unexpected results, but no error is raised.
140	out, err = sch.Coerce(float64(math.MaxInt64+1), aPath)
141	c.Assert(err, IsNil)
142
143	out, err = sch.Coerce(true, aPath)
144	c.Assert(out, IsNil)
145	c.Assert(err, ErrorMatches, "<path>: expected number, got true")
146
147	out, err = sch.Coerce(nil, aPath)
148	c.Assert(out, IsNil)
149	c.Assert(err, ErrorMatches, "<path>: expected number, got nothing")
150}
151
152func (s *S) TestFloat(c *C) {
153	sch := schema.Float()
154
155	out, err := sch.Coerce(float32(1.0), aPath)
156	c.Assert(err, IsNil)
157	c.Assert(out, Equals, float64(1.0))
158
159	out, err = sch.Coerce(float64(1.0), aPath)
160	c.Assert(err, IsNil)
161	c.Assert(out, Equals, float64(1.0))
162
163	out, err = sch.Coerce(true, aPath)
164	c.Assert(out, IsNil)
165	c.Assert(err, ErrorMatches, "<path>: expected float, got true")
166
167	out, err = sch.Coerce(nil, aPath)
168	c.Assert(out, IsNil)
169	c.Assert(err, ErrorMatches, "<path>: expected float, got nothing")
170}
171
172func (s *S) TestString(c *C) {
173	sch := schema.String()
174
175	out, err := sch.Coerce("foo", aPath)
176	c.Assert(err, IsNil)
177	c.Assert(out, Equals, "foo")
178
179	out, err = sch.Coerce(true, aPath)
180	c.Assert(out, IsNil)
181	c.Assert(err, ErrorMatches, "<path>: expected string, got true")
182
183	out, err = sch.Coerce(nil, aPath)
184	c.Assert(out, IsNil)
185	c.Assert(err, ErrorMatches, "<path>: expected string, got nothing")
186}
187
188func (s *S) TestSimpleRegexp(c *C) {
189	sch := schema.SimpleRegexp()
190	out, err := sch.Coerce("[0-9]+", aPath)
191	c.Assert(err, IsNil)
192	c.Assert(out, Equals, "[0-9]+")
193
194	out, err = sch.Coerce(1, aPath)
195	c.Assert(out, IsNil)
196	c.Assert(err, ErrorMatches, "<path>: expected regexp string, got 1")
197
198	out, err = sch.Coerce("[", aPath)
199	c.Assert(out, IsNil)
200	c.Assert(err, ErrorMatches, `<path>: expected valid regexp, got "\["`)
201
202	out, err = sch.Coerce(nil, aPath)
203	c.Assert(out, IsNil)
204	c.Assert(err, ErrorMatches, `<path>: expected regexp string, got nothing`)
205}
206
207func (s *S) TestList(c *C) {
208	sch := schema.List(schema.Int())
209	out, err := sch.Coerce([]int8{1, 2}, aPath)
210	c.Assert(err, IsNil)
211	c.Assert(out, DeepEquals, []interface{}{int64(1), int64(2)})
212
213	out, err = sch.Coerce(42, aPath)
214	c.Assert(out, IsNil)
215	c.Assert(err, ErrorMatches, "<path>: expected list, got 42")
216
217	out, err = sch.Coerce(nil, aPath)
218	c.Assert(out, IsNil)
219	c.Assert(err, ErrorMatches, "<path>: expected list, got nothing")
220
221	out, err = sch.Coerce([]interface{}{1, true}, aPath)
222	c.Assert(out, IsNil)
223	c.Assert(err, ErrorMatches, `<path>\[1\]: expected int, got true`)
224}
225
226func (s *S) TestMap(c *C) {
227	sch := schema.Map(schema.String(), schema.Int())
228	out, err := sch.Coerce(map[string]interface{}{"a": 1, "b": int8(2)}, aPath)
229	c.Assert(err, IsNil)
230	c.Assert(out, DeepEquals, map[interface{}]interface{}{"a": int64(1), "b": int64(2)})
231
232	out, err = sch.Coerce(42, aPath)
233	c.Assert(out, IsNil)
234	c.Assert(err, ErrorMatches, "<path>: expected map, got 42")
235
236	out, err = sch.Coerce(nil, aPath)
237	c.Assert(out, IsNil)
238	c.Assert(err, ErrorMatches, "<path>: expected map, got nothing")
239
240	out, err = sch.Coerce(map[int]int{1: 1}, aPath)
241	c.Assert(out, IsNil)
242	c.Assert(err, ErrorMatches, "<path>: expected string, got 1")
243
244	out, err = sch.Coerce(map[string]bool{"a": true}, aPath)
245	c.Assert(out, IsNil)
246	c.Assert(err, ErrorMatches, `<path>\.a: expected int, got true`)
247
248	// First path entry shouldn't have dots in an error message.
249	out, err = sch.Coerce(map[string]bool{"a": true}, nil)
250	c.Assert(out, IsNil)
251	c.Assert(err, ErrorMatches, `a: expected int, got true`)
252}
253
254func (s *S) TestStringMap(c *C) {
255	sch := schema.StringMap(schema.Int())
256	out, err := sch.Coerce(map[string]interface{}{"a": 1, "b": int8(2)}, aPath)
257	c.Assert(err, IsNil)
258	c.Assert(out, DeepEquals, map[string]interface{}{"a": int64(1), "b": int64(2)})
259
260	out, err = sch.Coerce(42, aPath)
261	c.Assert(out, IsNil)
262	c.Assert(err, ErrorMatches, "<path>: expected map, got 42")
263
264	out, err = sch.Coerce(nil, aPath)
265	c.Assert(out, IsNil)
266	c.Assert(err, ErrorMatches, "<path>: expected map, got nothing")
267
268	out, err = sch.Coerce(map[int]int{1: 1}, aPath)
269	c.Assert(out, IsNil)
270	c.Assert(err, ErrorMatches, "<path>: expected string, got 1")
271
272	out, err = sch.Coerce(map[string]bool{"a": true}, aPath)
273	c.Assert(out, IsNil)
274	c.Assert(err, ErrorMatches, `<path>\.a: expected int, got true`)
275
276	// First path entry shouldn't have dots in an error message.
277	out, err = sch.Coerce(map[string]bool{"a": true}, nil)
278	c.Assert(out, IsNil)
279	c.Assert(err, ErrorMatches, `a: expected int, got true`)
280}
281
282func assertFieldMap(c *C, sch schema.Checker) {
283	out, err := sch.Coerce(map[string]interface{}{"a": "A", "b": "B"}, aPath)
284
285	c.Assert(err, IsNil)
286	c.Assert(out, DeepEquals, map[string]interface{}{"a": "A", "b": "B", "c": "C"})
287
288	out, err = sch.Coerce(42, aPath)
289	c.Assert(out, IsNil)
290	c.Assert(err, ErrorMatches, "<path>: expected map, got 42")
291
292	out, err = sch.Coerce(nil, aPath)
293	c.Assert(out, IsNil)
294	c.Assert(err, ErrorMatches, "<path>: expected map, got nothing")
295
296	out, err = sch.Coerce(map[string]interface{}{"a": "A", "b": "C"}, aPath)
297	c.Assert(out, IsNil)
298	c.Assert(err, ErrorMatches, `<path>\.b: expected "B", got "C"`)
299
300	out, err = sch.Coerce(map[string]interface{}{"b": "B"}, aPath)
301	c.Assert(out, IsNil)
302	c.Assert(err, ErrorMatches, `<path>\.a: expected "A", got nothing`)
303
304	// b is optional
305	out, err = sch.Coerce(map[string]interface{}{"a": "A"}, aPath)
306	c.Assert(err, IsNil)
307	c.Assert(out, DeepEquals, map[string]interface{}{"a": "A", "c": "C"})
308
309	// First path entry shouldn't have dots in an error message.
310	out, err = sch.Coerce(map[string]bool{"a": true}, nil)
311	c.Assert(out, IsNil)
312	c.Assert(err, ErrorMatches, `a: expected "A", got true`)
313}
314
315func (s *S) TestFieldMap(c *C) {
316	fields := schema.Fields{
317		"a": schema.Const("A"),
318		"b": schema.Const("B"),
319		"c": schema.Const("C"),
320	}
321	defaults := schema.Defaults{
322		"b": schema.Omit,
323		"c": "C",
324	}
325	sch := schema.FieldMap(fields, defaults)
326	assertFieldMap(c, sch)
327
328	out, err := sch.Coerce(map[string]interface{}{"a": "A", "b": "B", "d": "D"}, aPath)
329	c.Assert(err, IsNil)
330	c.Assert(out, DeepEquals, map[string]interface{}{"a": "A", "b": "B", "c": "C"})
331
332	out, err = sch.Coerce(map[string]interface{}{"a": "A", "d": "D"}, aPath)
333	c.Assert(err, IsNil)
334	c.Assert(out, DeepEquals, map[string]interface{}{"a": "A", "c": "C"})
335}
336
337func (s *S) TestFieldMapDefaultInvalid(c *C) {
338	fields := schema.Fields{
339		"a": schema.Const("A"),
340	}
341	defaults := schema.Defaults{
342		"a": "B",
343	}
344	sch := schema.FieldMap(fields, defaults)
345	_, err := sch.Coerce(map[string]interface{}{}, aPath)
346	c.Assert(err, ErrorMatches, `<path>.a: expected "A", got "B"`)
347}
348
349func (s *S) TestStrictFieldMap(c *C) {
350	fields := schema.Fields{
351		"a": schema.Const("A"),
352		"b": schema.Const("B"),
353		"c": schema.Const("C"),
354	}
355	defaults := schema.Defaults{
356		"b": schema.Omit,
357		"c": "C",
358	}
359	sch := schema.StrictFieldMap(fields, defaults)
360	assertFieldMap(c, sch)
361
362	out, err := sch.Coerce(map[string]interface{}{"a": "A", "b": "B", "d": "D"}, aPath)
363	c.Assert(out, IsNil)
364	c.Assert(err, ErrorMatches, `<path>.d: expected nothing, got "D"`)
365}
366
367func (s *S) TestSchemaMap(c *C) {
368	fields1 := schema.FieldMap(schema.Fields{
369		"type": schema.Const(1),
370		"a":    schema.Const(2),
371	}, nil)
372	fields2 := schema.FieldMap(schema.Fields{
373		"type": schema.Const(3),
374		"b":    schema.Const(4),
375	}, nil)
376	sch := schema.FieldMapSet("type", []schema.Checker{fields1, fields2})
377
378	out, err := sch.Coerce(map[string]int{"type": 1, "a": 2}, aPath)
379	c.Assert(err, IsNil)
380	c.Assert(out, DeepEquals, map[string]interface{}{"type": 1, "a": 2})
381
382	out, err = sch.Coerce(map[string]int{"type": 3, "b": 4}, aPath)
383	c.Assert(err, IsNil)
384	c.Assert(out, DeepEquals, map[string]interface{}{"type": 3, "b": 4})
385
386	out, err = sch.Coerce(map[string]int{}, aPath)
387	c.Assert(out, IsNil)
388	c.Assert(err, ErrorMatches, `<path>\.type: expected supported selector, got nothing`)
389
390	out, err = sch.Coerce(map[string]int{"type": 2}, aPath)
391	c.Assert(out, IsNil)
392	c.Assert(err, ErrorMatches, `<path>\.type: expected supported selector, got 2`)
393
394	out, err = sch.Coerce(map[string]int{"type": 3, "b": 5}, aPath)
395	c.Assert(out, IsNil)
396	c.Assert(err, ErrorMatches, `<path>\.b: expected 4, got 5`)
397
398	out, err = sch.Coerce(42, aPath)
399	c.Assert(out, IsNil)
400	c.Assert(err, ErrorMatches, `<path>: expected map, got 42`)
401
402	out, err = sch.Coerce(nil, aPath)
403	c.Assert(out, IsNil)
404	c.Assert(err, ErrorMatches, `<path>: expected map, got nothing`)
405
406	// First path entry shouldn't have dots in an error message.
407	out, err = sch.Coerce(map[string]int{"a": 1}, nil)
408	c.Assert(out, IsNil)
409	c.Assert(err, ErrorMatches, `type: expected supported selector, got nothing`)
410}
411
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)