Index: charm/config_test.go |
=== modified file 'charm/config_test.go' |
--- charm/config_test.go 2013-02-11 05:51:12 +0000 |
+++ charm/config_test.go 2013-02-27 08:18:52 +0000 |
@@ -189,3 +189,68 @@ |
c.Assert(output, IsNil) |
c.Assert(err, ErrorMatches, `Unknown configuration option: "bad"`) |
} |
+ |
+var convertTests = []struct { |
+ summary string |
+ input map[string]interface{} |
+ expect map[string]interface{} |
+ err string |
+}{{ |
+ // Schema defaults are ignored. |
+ summary: "valid strings", |
+ input: map[string]interface{}{ |
+ "title": "Helpful Title", |
+ "outlook": "Peachy", |
+ }, |
+ expect: map[string]interface{}{ |
+ "title": "Helpful Title", |
+ "outlook": "Peachy", |
+ }, |
+}, { |
+ // Integers are always int64 in YAML, where the input is usually coming from. |
+ summary: "valid integers and floats", |
+ input: map[string]interface{}{ |
+ "agility-ratio": 0.5, |
+ "skill-level": int64(7), |
+ }, |
+ expect: map[string]interface{}{ |
+ "agility-ratio": 0.5, |
+ "skill-level": int64(7), |
+ }, |
+}, { |
+ summary: "valid booleans", |
+ input: map[string]interface{}{"reticulate-splines": true}, |
+ expect: map[string]interface{}{"reticulate-splines": true}, |
+}, { |
+ summary: "invalid type error with floats", |
+ input: map[string]interface{}{"agility-ratio": "bar"}, |
+ err: `unexpected type in service configuration "agility-ratio"="bar"; expected float`, |
+}, { |
+ summary: "invalid type error with integers", |
+ input: map[string]interface{}{"skill-level": "foo"}, |
+ err: `unexpected type in service configuration "skill-level"="foo"; expected int`, |
+}, { |
+ summary: "invalid type error with booleans", |
+ input: map[string]interface{}{"reticulate-splines": "maybe"}, |
+ err: `unexpected type in service configuration "reticulate-splines"="maybe"; expected boolean`, |
+}, { |
+ summary: "with value not in the schema (ignored)", |
+ input: map[string]interface{}{"bad": "value"}, |
+ expect: map[string]interface{}{}, |
+}} |
+ |
+func (s *ConfigSuite) TestConvert(c *C) { |
+ config, err := charm.ReadConfig(bytes.NewBuffer([]byte(sampleConfig))) |
+ c.Assert(err, IsNil) |
+ |
+ for i, t := range convertTests { |
+ c.Logf("test %d: %s", i, t.summary) |
+ output, err := config.Convert(t.input) |
+ if t.err != "" { |
+ c.Assert(err, ErrorMatches, t.err) |
+ } else { |
+ c.Assert(err, IsNil) |
+ c.Assert(output, DeepEquals, t.expect) |
+ } |
+ } |
+} |