OLD | NEW |
1 // Copyright 2011, 2012, 2013 Canonical Ltd. | 1 // Copyright 2011, 2012, 2013 Canonical Ltd. |
2 // Licensed under the AGPLv3, see LICENCE file for details. | 2 // Licensed under the AGPLv3, see LICENCE file for details. |
3 | 3 |
4 package charm_test | 4 package charm_test |
5 | 5 |
6 import ( | 6 import ( |
7 "bytes" | 7 "bytes" |
8 "fmt" | 8 "fmt" |
9 | 9 |
10 gc "launchpad.net/gocheck" | 10 gc "launchpad.net/gocheck" |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 "username": nil, | 105 "username": nil, |
106 "unknown": "whatever", | 106 "unknown": "whatever", |
107 "outlook": "", | 107 "outlook": "", |
108 "skill-level": 5.5, | 108 "skill-level": 5.5, |
109 "agility-ratio": true, | 109 "agility-ratio": true, |
110 "reticulate-splines": "hullo", | 110 "reticulate-splines": "hullo", |
111 }) | 111 }) |
112 c.Assert(settings, gc.DeepEquals, charm.Settings{ | 112 c.Assert(settings, gc.DeepEquals, charm.Settings{ |
113 "title": "something valid", | 113 "title": "something valid", |
114 "username": nil, | 114 "username": nil, |
115 » » "outlook": nil, | 115 » » "outlook": "", |
116 }) | 116 }) |
117 } | 117 } |
118 | 118 |
119 func (s *ConfigSuite) TestValidateSettings(c *gc.C) { | 119 func (s *ConfigSuite) TestValidateSettings(c *gc.C) { |
120 for i, test := range []struct { | 120 for i, test := range []struct { |
121 info string | 121 info string |
122 input charm.Settings | 122 input charm.Settings |
123 expect charm.Settings | 123 expect charm.Settings |
124 err string | 124 err string |
125 }{{ | 125 }{{ |
(...skipping 16 matching lines...) Expand all Loading... |
142 }, | 142 }, |
143 }, { | 143 }, { |
144 info: "correctly-typed values are valid", | 144 info: "correctly-typed values are valid", |
145 input: charm.Settings{ | 145 input: charm.Settings{ |
146 "outlook": "stormy", | 146 "outlook": "stormy", |
147 "skill-level": int64(123), | 147 "skill-level": int64(123), |
148 "agility-ratio": 0.5, | 148 "agility-ratio": 0.5, |
149 "reticulate-splines": true, | 149 "reticulate-splines": true, |
150 }, | 150 }, |
151 }, { | 151 }, { |
152 » » info: "empty string-typed values become nil", | 152 » » info: "empty string-typed values stay empty", |
153 input: charm.Settings{"outlook": ""}, | 153 input: charm.Settings{"outlook": ""}, |
154 » » expect: charm.Settings{"outlook": nil}, | 154 » » expect: charm.Settings{"outlook": ""}, |
155 }, { | 155 }, { |
156 info: "almost-correctly-typed values are valid", | 156 info: "almost-correctly-typed values are valid", |
157 input: charm.Settings{ | 157 input: charm.Settings{ |
158 "skill-level": 123, | 158 "skill-level": 123, |
159 "agility-ratio": float32(0.5), | 159 "agility-ratio": float32(0.5), |
160 }, | 160 }, |
161 expect: charm.Settings{ | 161 expect: charm.Settings{ |
162 "skill-level": int64(123), | 162 "skill-level": int64(123), |
163 "agility-ratio": 0.5, | 163 "agility-ratio": 0.5, |
164 }, | 164 }, |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
272 }, { | 272 }, { |
273 info: "nil values are valid", | 273 info: "nil values are valid", |
274 yaml: `blah: | 274 yaml: `blah: |
275 outlook: null | 275 outlook: null |
276 skill-level: null | 276 skill-level: null |
277 agility-ratio: null | 277 agility-ratio: null |
278 reticulate-splines: null`, | 278 reticulate-splines: null`, |
279 key: "blah", | 279 key: "blah", |
280 expect: settingsWithNils, | 280 expect: settingsWithNils, |
281 }, { | 281 }, { |
282 » » info: "empty strings are considered nil", | 282 » » info: "empty strings for non-string options are not accepted", |
283 yaml: `blah: | 283 yaml: `blah: |
284 outlook: "" | 284 outlook: "" |
285 skill-level: "" | 285 skill-level: "" |
286 agility-ratio: "" | 286 agility-ratio: "" |
287 reticulate-splines: ""`, | 287 reticulate-splines: ""`, |
288 » » key: "blah", | 288 » » key: "blah", |
289 » » expect: settingsWithNils, | 289 » » err: `option "skill-level" expected int, got ""`, |
290 }, { | 290 }, { |
291 info: "appropriate strings are valid", | 291 info: "appropriate strings are valid", |
292 yaml: `blah: | 292 yaml: `blah: |
293 outlook: whatever | 293 outlook: whatever |
294 skill-level: "123" | 294 skill-level: "123" |
295 agility-ratio: "2.22" | 295 agility-ratio: "2.22" |
296 reticulate-splines: "true"`, | 296 reticulate-splines: "true"`, |
297 key: "blah", | 297 key: "blah", |
298 expect: settingsWithValues, | 298 expect: settingsWithValues, |
299 }, { | 299 }, { |
(...skipping 24 matching lines...) Expand all Loading... |
324 expect charm.Settings | 324 expect charm.Settings |
325 err string | 325 err string |
326 }{{ | 326 }{{ |
327 info: "nil map is valid", | 327 info: "nil map is valid", |
328 expect: charm.Settings{}, | 328 expect: charm.Settings{}, |
329 }, { | 329 }, { |
330 info: "empty map is valid", | 330 info: "empty map is valid", |
331 input: map[string]string{}, | 331 input: map[string]string{}, |
332 expect: charm.Settings{}, | 332 expect: charm.Settings{}, |
333 }, { | 333 }, { |
334 » » info: "empty strings are nil values", | 334 » » info: "empty strings for string options are valid", |
335 » » input: map[string]string{ | 335 » » input: map[string]string{"outlook": ""}, |
336 » » » "outlook": "", | 336 » » expect: charm.Settings{"outlook": ""}, |
337 » » » "skill-level": "", | 337 » }, { |
338 » » » "agility-ratio": "", | 338 » » info: "empty strings for non-string options are invalid", |
339 » » » "reticulate-splines": "", | 339 » » input: map[string]string{"skill-level": ""}, |
340 » » }, | 340 » » err: `option "skill-level" expected int, got ""`, |
341 » » expect: settingsWithNils, | |
342 }, { | 341 }, { |
343 info: "strings are converted", | 342 info: "strings are converted", |
344 input: map[string]string{ | 343 input: map[string]string{ |
345 "outlook": "whatever", | 344 "outlook": "whatever", |
346 "skill-level": "123", | 345 "skill-level": "123", |
347 "agility-ratio": "2.22", | 346 "agility-ratio": "2.22", |
348 "reticulate-splines": "true", | 347 "reticulate-splines": "true", |
349 }, | 348 }, |
350 expect: settingsWithValues, | 349 expect: settingsWithValues, |
351 }, { | 350 }, { |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
404 assertTypeError("int", "true", "true") | 403 assertTypeError("int", "true", "true") |
405 } | 404 } |
406 | 405 |
407 // When an empty config is supplied an error should be returned | 406 // When an empty config is supplied an error should be returned |
408 func (s *ConfigSuite) TestEmptyConfigReturnsError(c *gc.C) { | 407 func (s *ConfigSuite) TestEmptyConfigReturnsError(c *gc.C) { |
409 config := "" | 408 config := "" |
410 result, err := charm.ReadConfig(bytes.NewBuffer([]byte(config))) | 409 result, err := charm.ReadConfig(bytes.NewBuffer([]byte(config))) |
411 c.Assert(result, gc.IsNil) | 410 c.Assert(result, gc.IsNil) |
412 c.Assert(err, gc.ErrorMatches, "invalid config: empty configuration") | 411 c.Assert(err, gc.ErrorMatches, "invalid config: empty configuration") |
413 } | 412 } |
OLD | NEW |