func (s *S) TestUnmarshalErrors(c *C) { for _, item := range unmarshalErrorTests { var value interface{} err := goyaml.Unmarshal([]byte(item.data), &value) c.Assert(err, ErrorMatches, item.error, Commentf("Partial unmarshal: %#v", value)) } }
func (s *S) TestUnmarshal(c *C) { for i, item := range unmarshalTests { t := reflect.ValueOf(item.value).Type() var value interface{} switch t.Kind() { case reflect.Map: value = reflect.MakeMap(t).Interface() case reflect.String: t := reflect.ValueOf(item.value).Type() v := reflect.New(t) value = v.Interface() default: pt := reflect.ValueOf(item.value).Type() pv := reflect.New(pt.Elem()) value = pv.Interface() } err := goyaml.Unmarshal([]byte(item.data), value) c.Assert(err, IsNil, Commentf("Item #%d", i)) if t.Kind() == reflect.String { c.Assert(*value.(*string), Equals, item.value, Commentf("Item #%d", i)) } else { c.Assert(value, DeepEquals, item.value, Commentf("Item #%d", i)) } } }
func (s *S) TestUnmarshalWholeDocumentWithSetter(c *C) { obj := &typeWithSetter{} err := goyaml.Unmarshal([]byte(setterTests[0].data), obj) c.Assert(err, IsNil) c.Assert(obj.tag, Equals, setterTests[0].tag) value, ok := obj.value.(map[interface{}]interface{}) c.Assert(ok, Equals, true) c.Assert(value["_"], DeepEquals, setterTests[0].value) }
func (s *S) TestUnmarshalWithSetter(c *C) { for _, item := range setterTests { obj := &typeWithSetterField{} err := goyaml.Unmarshal([]byte(item.data), obj) c.Assert(err, IsNil) c.Assert(obj.Field, NotNil, Commentf("Pointer not initialized (%#v)", item.value)) c.Assert(obj.Field.tag, Equals, item.tag) c.Assert(obj.Field.value, DeepEquals, item.value) } }
func (s *S) TestUnmarshalWithFalseSetterIgnoresValue(c *C) { setterResult[2] = false setterResult[4] = false defer func() { delete(setterResult, 2) delete(setterResult, 4) }() m := map[string]*typeWithSetter{} data := "{abc: 1, def: 2, ghi: 3, jkl: 4}" err := goyaml.Unmarshal([]byte(data), m) c.Assert(err, IsNil) c.Assert(m["abc"], NotNil) c.Assert(m["def"], IsNil) c.Assert(m["ghi"], NotNil) c.Assert(m["jkl"], IsNil) c.Assert(m["abc"].value, Equals, 1) c.Assert(m["ghi"].value, Equals, 3) }
func (s *S) TestUnmarshalNaN(c *C) { value := map[string]interface{}{} err := goyaml.Unmarshal([]byte("notanum: .NaN"), &value) c.Assert(err, IsNil) c.Assert(math.IsNaN(value["notanum"].(float64)), Equals, true) }
func NewCloudConfig(contents []byte) (*CloudConfig, error) { var cfg CloudConfig err := goyaml.Unmarshal(contents, &cfg) return &cfg, err }