decode_test.go 8.7 KB

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