OLD | NEW |
1 package goyaml_test | 1 package goyaml_test |
2 | 2 |
3 import ( | 3 import ( |
4 . "launchpad.net/gocheck" | 4 . "launchpad.net/gocheck" |
5 "launchpad.net/goyaml" | 5 "launchpad.net/goyaml" |
6 "math" | 6 "math" |
7 "reflect" | 7 "reflect" |
8 ) | 8 ) |
9 | 9 |
10 var unmarshalIntTest = 123 | 10 var unmarshalIntTest = 123 |
(...skipping 23 matching lines...) Expand all Loading... |
34 | 34 |
35 // Simple values. | 35 // Simple values. |
36 {"123", &unmarshalIntTest}, | 36 {"123", &unmarshalIntTest}, |
37 | 37 |
38 // Floats from spec | 38 // Floats from spec |
39 {"canonical: 6.8523e+5", map[string]interface{}{"canonical": 6.8523e+5}}
, | 39 {"canonical: 6.8523e+5", map[string]interface{}{"canonical": 6.8523e+5}}
, |
40 {"expo: 685.230_15e+03", map[string]interface{}{"expo": 685.23015e+03}}, | 40 {"expo: 685.230_15e+03", map[string]interface{}{"expo": 685.23015e+03}}, |
41 {"fixed: 685_230.15", map[string]interface{}{"fixed": 685230.15}}, | 41 {"fixed: 685_230.15", map[string]interface{}{"fixed": 685230.15}}, |
42 //{"sexa: 190:20:30.15", map[string]interface{}{"sexa": 0}}, // Unsuppor
ted | 42 //{"sexa: 190:20:30.15", map[string]interface{}{"sexa": 0}}, // Unsuppor
ted |
43 {"neginf: -.inf", map[string]interface{}{"neginf": math.Inf(-1)}}, | 43 {"neginf: -.inf", map[string]interface{}{"neginf": math.Inf(-1)}}, |
44 » {"notanum: .NaN", map[string]interface{}{"notanum": math.NaN()}}, | 44 » //{"notanum: .NaN", map[string]interface{}{"notanum": math.NaN()}}, // E
quality of NaN fails. |
45 {"fixed: 685_230.15", map[string]float64{"fixed": 685230.15}}, | 45 {"fixed: 685_230.15", map[string]float64{"fixed": 685230.15}}, |
46 | 46 |
47 // Bools from spec | 47 // Bools from spec |
48 {"canonical: y", map[string]interface{}{"canonical": true}}, | 48 {"canonical: y", map[string]interface{}{"canonical": true}}, |
49 {"answer: NO", map[string]interface{}{"answer": false}}, | 49 {"answer: NO", map[string]interface{}{"answer": false}}, |
50 {"logical: True", map[string]interface{}{"logical": true}}, | 50 {"logical: True", map[string]interface{}{"logical": true}}, |
51 {"option: on", map[string]interface{}{"option": true}}, | 51 {"option: on", map[string]interface{}{"option": true}}, |
52 {"option: on", map[string]bool{"option": true}}, | 52 {"option: on", map[string]bool{"option": true}}, |
53 | 53 |
54 // Ints from spec | 54 // Ints from spec |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
129 t := reflect.ValueOf(item.value).Type() | 129 t := reflect.ValueOf(item.value).Type() |
130 var value interface{} | 130 var value interface{} |
131 if t.Kind() == reflect.Map { | 131 if t.Kind() == reflect.Map { |
132 value = reflect.MakeMap(t).Interface() | 132 value = reflect.MakeMap(t).Interface() |
133 } else { | 133 } else { |
134 pt := reflect.ValueOf(item.value).Type() | 134 pt := reflect.ValueOf(item.value).Type() |
135 pv := reflect.New(pt.Elem()) | 135 pv := reflect.New(pt.Elem()) |
136 value = pv.Interface() | 136 value = pv.Interface() |
137 } | 137 } |
138 err := goyaml.Unmarshal([]byte(item.data), value) | 138 err := goyaml.Unmarshal([]byte(item.data), value) |
139 » » c.Assert(err, IsNil, Bug("Item #%d", i)) | 139 » » c.Assert(err, IsNil, Commentf("Item #%d", i)) |
140 » » c.Assert(value, Equals, item.value) | 140 » » c.Assert(value, DeepEquals, item.value) |
141 } | 141 } |
142 } | 142 } |
143 | 143 |
| 144 func (s *S) TestUnmarshalNaN(c *C) { |
| 145 value := map[string]interface{}{} |
| 146 err := goyaml.Unmarshal([]byte("notanum: .NaN"), &value) |
| 147 c.Assert(err, IsNil) |
| 148 c.Assert(math.IsNaN(value["notanum"].(float64)), Equals, true) |
| 149 } |
| 150 |
144 var unmarshalErrorTests = []struct { | 151 var unmarshalErrorTests = []struct { |
145 data, error string | 152 data, error string |
146 }{ | 153 }{ |
147 {"v: !!float 'error'", "YAML error: Can't decode !!str 'error' as a !!fl
oat"}, | 154 {"v: !!float 'error'", "YAML error: Can't decode !!str 'error' as a !!fl
oat"}, |
148 {"v: [A,", "YAML error: line 1: did not find expected node content"}, | 155 {"v: [A,", "YAML error: line 1: did not find expected node content"}, |
149 {"v:\n- [A,", "YAML error: line 2: did not find expected node content"}, | 156 {"v:\n- [A,", "YAML error: line 2: did not find expected node content"}, |
150 {"a: *b\n", "YAML error: Unknown anchor 'b' referenced"}, | 157 {"a: *b\n", "YAML error: Unknown anchor 'b' referenced"}, |
151 {"a: &a\n b: *a\n", "YAML error: Anchor 'a' value contains itself"}, | 158 {"a: &a\n b: *a\n", "YAML error: Anchor 'a' value contains itself"}, |
152 } | 159 } |
153 | 160 |
154 func (s *S) TestUnmarshalErrors(c *C) { | 161 func (s *S) TestUnmarshalErrors(c *C) { |
155 for _, item := range unmarshalErrorTests { | 162 for _, item := range unmarshalErrorTests { |
156 var value interface{} | 163 var value interface{} |
157 err := goyaml.Unmarshal([]byte(item.data), &value) | 164 err := goyaml.Unmarshal([]byte(item.data), &value) |
158 » » c.Assert(err, ErrorMatches, item.error, Bug("Partial unmarshal:
%#v", value)) | 165 » » c.Assert(err, ErrorMatches, item.error, Commentf("Partial unmars
hal: %#v", value)) |
159 } | 166 } |
160 } | 167 } |
161 | 168 |
162 var setterTests = []struct { | 169 var setterTests = []struct { |
163 data, tag string | 170 data, tag string |
164 value interface{} | 171 value interface{} |
165 }{ | 172 }{ |
166 {"_: {hi: there}", "!!map", map[interface{}]interface{}{"hi": "there"}}, | 173 {"_: {hi: there}", "!!map", map[interface{}]interface{}{"hi": "there"}}, |
167 {"_: [1,A]", "!!seq", []interface{}{1, "A"}}, | 174 {"_: [1,A]", "!!seq", []interface{}{1, "A"}}, |
168 {"_: 10", "!!int", 10}, | 175 {"_: 10", "!!int", 10}, |
(...skipping 22 matching lines...) Expand all Loading... |
191 type typeWithSetterField struct { | 198 type typeWithSetterField struct { |
192 Field *typeWithSetter "_" | 199 Field *typeWithSetter "_" |
193 } | 200 } |
194 | 201 |
195 func (s *S) TestUnmarshalWithSetter(c *C) { | 202 func (s *S) TestUnmarshalWithSetter(c *C) { |
196 for _, item := range setterTests { | 203 for _, item := range setterTests { |
197 obj := &typeWithSetterField{} | 204 obj := &typeWithSetterField{} |
198 err := goyaml.Unmarshal([]byte(item.data), obj) | 205 err := goyaml.Unmarshal([]byte(item.data), obj) |
199 c.Assert(err, IsNil) | 206 c.Assert(err, IsNil) |
200 c.Assert(obj.Field, NotNil, | 207 c.Assert(obj.Field, NotNil, |
201 » » » Bug("Pointer not initialized (%#v)", item.value)) | 208 » » » Commentf("Pointer not initialized (%#v)", item.value)) |
202 c.Assert(obj.Field.tag, Equals, item.tag) | 209 c.Assert(obj.Field.tag, Equals, item.tag) |
203 » » c.Assert(obj.Field.value, Equals, item.value) | 210 » » c.Assert(obj.Field.value, DeepEquals, item.value) |
204 } | 211 } |
205 } | 212 } |
206 | 213 |
207 func (s *S) TestUnmarshalWholeDocumentWithSetter(c *C) { | 214 func (s *S) TestUnmarshalWholeDocumentWithSetter(c *C) { |
208 obj := &typeWithSetter{} | 215 obj := &typeWithSetter{} |
209 err := goyaml.Unmarshal([]byte(setterTests[0].data), obj) | 216 err := goyaml.Unmarshal([]byte(setterTests[0].data), obj) |
210 c.Assert(err, IsNil) | 217 c.Assert(err, IsNil) |
211 c.Assert(obj.tag, Equals, setterTests[0].tag) | 218 c.Assert(obj.tag, Equals, setterTests[0].tag) |
212 value, ok := obj.value.(map[interface{}]interface{}) | 219 value, ok := obj.value.(map[interface{}]interface{}) |
213 c.Assert(ok, Equals, true) | 220 c.Assert(ok, Equals, true) |
214 » c.Assert(value["_"], Equals, setterTests[0].value) | 221 » c.Assert(value["_"], DeepEquals, setterTests[0].value) |
215 } | 222 } |
216 | 223 |
217 func (s *S) TestUnmarshalWithFalseSetterIgnoresValue(c *C) { | 224 func (s *S) TestUnmarshalWithFalseSetterIgnoresValue(c *C) { |
218 setterResult[2] = false | 225 setterResult[2] = false |
219 setterResult[4] = false | 226 setterResult[4] = false |
220 defer func() { | 227 defer func() { |
221 delete(setterResult, 2) | 228 delete(setterResult, 2) |
222 delete(setterResult, 4) | 229 delete(setterResult, 4) |
223 }() | 230 }() |
224 | 231 |
225 m := map[string]*typeWithSetter{} | 232 m := map[string]*typeWithSetter{} |
226 data := "{abc: 1, def: 2, ghi: 3, jkl: 4}" | 233 data := "{abc: 1, def: 2, ghi: 3, jkl: 4}" |
227 err := goyaml.Unmarshal([]byte(data), m) | 234 err := goyaml.Unmarshal([]byte(data), m) |
228 c.Assert(err, IsNil) | 235 c.Assert(err, IsNil) |
229 c.Assert(m["abc"], NotNil) | 236 c.Assert(m["abc"], NotNil) |
230 c.Assert(m["def"], IsNil) | 237 c.Assert(m["def"], IsNil) |
231 c.Assert(m["ghi"], NotNil) | 238 c.Assert(m["ghi"], NotNil) |
232 c.Assert(m["jkl"], IsNil) | 239 c.Assert(m["jkl"], IsNil) |
233 | 240 |
234 c.Assert(m["abc"].value, Equals, 1) | 241 c.Assert(m["abc"].value, Equals, 1) |
235 c.Assert(m["ghi"].value, Equals, 3) | 242 c.Assert(m["ghi"].value, Equals, 3) |
236 } | 243 } |
OLD | NEW |