decode_test.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. package goyaml_test
  2. import (
  3. . "launchpad.net/gocheck"
  4. "launchpad.net/goyaml"
  5. "math"
  6. "reflect"
  7. )
  8. var unmarshalIntTest = 123
  9. var unmarshalTests = []struct {
  10. data string
  11. value interface{}
  12. }{
  13. {"", &struct{}{}},
  14. {"{}", &struct{}{}},
  15. {"v: hi", map[string]string{"v": "hi"}},
  16. {"v: hi", map[string]interface{}{"v": "hi"}},
  17. {"v: true", map[string]string{"v": "true"}},
  18. {"v: true", map[string]interface{}{"v": true}},
  19. {"v: 10", map[string]interface{}{"v": 10}},
  20. {"v: 0b10", map[string]interface{}{"v": 2}},
  21. {"v: 0xA", map[string]interface{}{"v": 10}},
  22. {"v: 4294967296", map[string]interface{}{"v": int64(4294967296)}},
  23. {"v: 4294967296", map[string]int64{"v": int64(4294967296)}},
  24. {"v: 0.1", map[string]interface{}{"v": 0.1}},
  25. {"v: .1", map[string]interface{}{"v": 0.1}},
  26. {"v: .Inf", map[string]interface{}{"v": math.Inf(+1)}},
  27. {"v: -.Inf", map[string]interface{}{"v": math.Inf(-1)}},
  28. {"v: -10", map[string]interface{}{"v": -10}},
  29. {"v: -.1", map[string]interface{}{"v": -0.1}},
  30. // Simple values.
  31. {"123", &unmarshalIntTest},
  32. // Floats from spec
  33. {"canonical: 6.8523e+5", map[string]interface{}{"canonical": 6.8523e+5}},
  34. {"expo: 685.230_15e+03", map[string]interface{}{"expo": 685.23015e+03}},
  35. {"fixed: 685_230.15", map[string]interface{}{"fixed": 685230.15}},
  36. //{"sexa: 190:20:30.15", map[string]interface{}{"sexa": 0}}, // Unsupported
  37. {"neginf: -.inf", map[string]interface{}{"neginf": math.Inf(-1)}},
  38. //{"notanum: .NaN", map[string]interface{}{"notanum": math.NaN()}}, // Equality of NaN fails.
  39. {"fixed: 685_230.15", map[string]float64{"fixed": 685230.15}},
  40. // Bools from spec
  41. {"canonical: y", map[string]interface{}{"canonical": true}},
  42. {"answer: NO", map[string]interface{}{"answer": false}},
  43. {"logical: True", map[string]interface{}{"logical": true}},
  44. {"option: on", map[string]interface{}{"option": true}},
  45. {"option: on", map[string]bool{"option": true}},
  46. // Ints from spec
  47. {"canonical: 685230", map[string]interface{}{"canonical": 685230}},
  48. {"decimal: +685_230", map[string]interface{}{"decimal": 685230}},
  49. {"octal: 02472256", map[string]interface{}{"octal": 685230}},
  50. {"hexa: 0x_0A_74_AE", map[string]interface{}{"hexa": 685230}},
  51. {"bin: 0b1010_0111_0100_1010_1110", map[string]interface{}{"bin": 685230}},
  52. {"bin: -0b101010", map[string]interface{}{"bin": -42}},
  53. //{"sexa: 190:20:30", map[string]interface{}{"sexa": 0}}, // Unsupported
  54. {"decimal: +685_230", map[string]int{"decimal": 685230}},
  55. // Nulls from spec
  56. {"empty:", map[string]interface{}{"empty": nil}},
  57. {"canonical: ~", map[string]interface{}{"canonical": nil}},
  58. {"english: null", map[string]interface{}{"english": nil}},
  59. {"~: null key", map[interface{}]string{nil: "null key"}},
  60. {"empty:", map[string]*bool{"empty": nil}},
  61. // Sequence
  62. {"seq: [A,B]", map[string]interface{}{"seq": []interface{}{"A", "B"}}},
  63. {"seq: [A,B,C]", map[string][]string{"seq": []string{"A", "B", "C"}}},
  64. {"seq: [A,1,C]", map[string][]string{"seq": []string{"A", "1", "C"}}},
  65. {"seq: [A,1,C]", map[string][]int{"seq": []int{1}}},
  66. {"seq: [A,1,C]", map[string]interface{}{"seq": []interface{}{"A", 1, "C"}}},
  67. // Map inside interface with no type hints.
  68. {"a: {b: c}",
  69. map[string]interface{}{"a": map[interface{}]interface{}{"b": "c"}}},
  70. // Structs and type conversions.
  71. {"hello: world", &struct{ Hello string }{"world"}},
  72. {"a: {b: c}", &struct{ A struct{ B string } }{struct{ B string }{"c"}}},
  73. {"a: {b: c}", &struct{ A *struct{ B string } }{&struct{ B string }{"c"}}},
  74. {"a: {b: c}", &struct{ A map[string]string }{map[string]string{"b": "c"}}},
  75. {"a: {b: c}", &struct{ A *map[string]string }{&map[string]string{"b": "c"}}},
  76. {"a:", &struct{ A map[string]string }{}},
  77. {"a: 1", &struct{ A int }{1}},
  78. {"a: [1, 2]", &struct{ A []int }{[]int{1, 2}}},
  79. {"a: 1", &struct{ B int }{0}},
  80. {"a: 1", &struct {
  81. B int "a"
  82. }{1}},
  83. {"a: y", &struct{ A bool }{true}},
  84. // Some cross type conversions
  85. {"v: 42", map[string]uint{"v": 42}},
  86. {"v: -42", map[string]uint{}},
  87. {"v: 4294967296", map[string]uint64{"v": 4294967296}},
  88. {"v: -4294967296", map[string]uint64{}},
  89. // Overflow cases.
  90. {"v: 4294967297", map[string]int32{}},
  91. {"v: 128", map[string]int8{}},
  92. // Quoted values.
  93. {"'1': '\"2\"'", map[interface{}]interface{}{"1": "\"2\""}},
  94. // Explicit tags.
  95. {"v: !!float '1.1'", map[string]interface{}{"v": 1.1}},
  96. {"v: !!null ''", map[string]interface{}{"v": nil}},
  97. {"%TAG !y! tag:yaml.org,2002:\n---\nv: !y!int '1'",
  98. map[string]interface{}{"v": 1}},
  99. // Anchors and aliases.
  100. {"a: &x 1\nb: &y 2\nc: *x\nd: *y\n", &struct{ A, B, C, D int }{1, 2, 1, 2}},
  101. {"a: &a {c: 1}\nb: *a",
  102. &struct {
  103. A, B struct {
  104. C int
  105. }
  106. }{struct{ C int }{1}, struct{ C int }{1}}},
  107. {"a: &a [1, 2]\nb: *a", &struct{ B []int }{[]int{1, 2}}},
  108. }
  109. func (s *S) TestUnmarshal(c *C) {
  110. for i, item := range unmarshalTests {
  111. t := reflect.ValueOf(item.value).Type()
  112. var value interface{}
  113. if t.Kind() == reflect.Map {
  114. value = reflect.MakeMap(t).Interface()
  115. } else {
  116. pt := reflect.ValueOf(item.value).Type()
  117. pv := reflect.New(pt.Elem())
  118. value = pv.Interface()
  119. }
  120. err := goyaml.Unmarshal([]byte(item.data), value)
  121. c.Assert(err, IsNil, Commentf("Item #%d", i))
  122. c.Assert(value, DeepEquals, item.value)
  123. }
  124. }
  125. func (s *S) TestUnmarshalNaN(c *C) {
  126. value := map[string]interface{}{}
  127. err := goyaml.Unmarshal([]byte("notanum: .NaN"), &value)
  128. c.Assert(err, IsNil)
  129. c.Assert(math.IsNaN(value["notanum"].(float64)), Equals, true)
  130. }
  131. var unmarshalErrorTests = []struct {
  132. data, error string
  133. }{
  134. {"v: !!float 'error'", "YAML error: Can't decode !!str 'error' as a !!float"},
  135. {"v: [A,", "YAML error: line 1: did not find expected node content"},
  136. {"v:\n- [A,", "YAML error: line 2: did not find expected node content"},
  137. {"a: *b\n", "YAML error: Unknown anchor 'b' referenced"},
  138. {"a: &a\n b: *a\n", "YAML error: Anchor 'a' value contains itself"},
  139. }
  140. func (s *S) TestUnmarshalErrors(c *C) {
  141. for _, item := range unmarshalErrorTests {
  142. var value interface{}
  143. err := goyaml.Unmarshal([]byte(item.data), &value)
  144. c.Assert(err, ErrorMatches, item.error, Commentf("Partial unmarshal: %#v", value))
  145. }
  146. }
  147. var setterTests = []struct {
  148. data, tag string
  149. value interface{}
  150. }{
  151. {"_: {hi: there}", "!!map", map[interface{}]interface{}{"hi": "there"}},
  152. {"_: [1,A]", "!!seq", []interface{}{1, "A"}},
  153. {"_: 10", "!!int", 10},
  154. {"_: null", "!!null", nil},
  155. {"_: !!foo 'BAR!'", "!!foo", "BAR!"},
  156. }
  157. var setterResult = map[int]bool{}
  158. type typeWithSetter struct {
  159. tag string
  160. value interface{}
  161. }
  162. func (o *typeWithSetter) SetYAML(tag string, value interface{}) (ok bool) {
  163. o.tag = tag
  164. o.value = value
  165. if i, ok := value.(int); ok {
  166. if result, ok := setterResult[i]; ok {
  167. return result
  168. }
  169. }
  170. return true
  171. }
  172. type typeWithSetterField struct {
  173. Field *typeWithSetter "_"
  174. }
  175. func (s *S) TestUnmarshalWithSetter(c *C) {
  176. for _, item := range setterTests {
  177. obj := &typeWithSetterField{}
  178. err := goyaml.Unmarshal([]byte(item.data), obj)
  179. c.Assert(err, IsNil)
  180. c.Assert(obj.Field, NotNil,
  181. Commentf("Pointer not initialized (%#v)", item.value))
  182. c.Assert(obj.Field.tag, Equals, item.tag)
  183. c.Assert(obj.Field.value, DeepEquals, item.value)
  184. }
  185. }
  186. func (s *S) TestUnmarshalWholeDocumentWithSetter(c *C) {
  187. obj := &typeWithSetter{}
  188. err := goyaml.Unmarshal([]byte(setterTests[0].data), obj)
  189. c.Assert(err, IsNil)
  190. c.Assert(obj.tag, Equals, setterTests[0].tag)
  191. value, ok := obj.value.(map[interface{}]interface{})
  192. c.Assert(ok, Equals, true)
  193. c.Assert(value["_"], DeepEquals, setterTests[0].value)
  194. }
  195. func (s *S) TestUnmarshalWithFalseSetterIgnoresValue(c *C) {
  196. setterResult[2] = false
  197. setterResult[4] = false
  198. defer func() {
  199. delete(setterResult, 2)
  200. delete(setterResult, 4)
  201. }()
  202. m := map[string]*typeWithSetter{}
  203. data := "{abc: 1, def: 2, ghi: 3, jkl: 4}"
  204. err := goyaml.Unmarshal([]byte(data), m)
  205. c.Assert(err, IsNil)
  206. c.Assert(m["abc"], NotNil)
  207. c.Assert(m["def"], IsNil)
  208. c.Assert(m["ghi"], NotNil)
  209. c.Assert(m["jkl"], IsNil)
  210. c.Assert(m["abc"].value, Equals, 1)
  211. c.Assert(m["ghi"].value, Equals, 3)
  212. }