decode_test.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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": int(4294967296)}},
  23. {"v: 4294967296", map[string]int64{"v": 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. // BUG #1133337
  109. {"foo: ''", map[string]*string{"foo": new(string)}},
  110. {"foo: null", map[string]string{}},
  111. }
  112. func (s *S) TestUnmarshal(c *C) {
  113. for i, item := range unmarshalTests {
  114. t := reflect.ValueOf(item.value).Type()
  115. var value interface{}
  116. if t.Kind() == reflect.Map {
  117. value = reflect.MakeMap(t).Interface()
  118. } else {
  119. pt := reflect.ValueOf(item.value).Type()
  120. pv := reflect.New(pt.Elem())
  121. value = pv.Interface()
  122. }
  123. err := goyaml.Unmarshal([]byte(item.data), value)
  124. c.Assert(err, IsNil, Commentf("Item #%d", i))
  125. c.Assert(value, DeepEquals, item.value)
  126. }
  127. }
  128. func (s *S) TestUnmarshalNaN(c *C) {
  129. value := map[string]interface{}{}
  130. err := goyaml.Unmarshal([]byte("notanum: .NaN"), &value)
  131. c.Assert(err, IsNil)
  132. c.Assert(math.IsNaN(value["notanum"].(float64)), Equals, true)
  133. }
  134. var unmarshalErrorTests = []struct {
  135. data, error string
  136. }{
  137. {"v: !!float 'error'", "YAML error: Can't decode !!str 'error' as a !!float"},
  138. {"v: [A,", "YAML error: line 1: did not find expected node content"},
  139. {"v:\n- [A,", "YAML error: line 2: did not find expected node content"},
  140. {"a: *b\n", "YAML error: Unknown anchor 'b' referenced"},
  141. {"a: &a\n b: *a\n", "YAML error: Anchor 'a' value contains itself"},
  142. }
  143. func (s *S) TestUnmarshalErrors(c *C) {
  144. for _, item := range unmarshalErrorTests {
  145. var value interface{}
  146. err := goyaml.Unmarshal([]byte(item.data), &value)
  147. c.Assert(err, ErrorMatches, item.error, Commentf("Partial unmarshal: %#v", value))
  148. }
  149. }
  150. var setterTests = []struct {
  151. data, tag string
  152. value interface{}
  153. }{
  154. {"_: {hi: there}", "!!map", map[interface{}]interface{}{"hi": "there"}},
  155. {"_: [1,A]", "!!seq", []interface{}{1, "A"}},
  156. {"_: 10", "!!int", 10},
  157. {"_: null", "!!null", nil},
  158. {"_: !!foo 'BAR!'", "!!foo", "BAR!"},
  159. }
  160. var setterResult = map[int]bool{}
  161. type typeWithSetter struct {
  162. tag string
  163. value interface{}
  164. }
  165. func (o *typeWithSetter) SetYAML(tag string, value interface{}) (ok bool) {
  166. o.tag = tag
  167. o.value = value
  168. if i, ok := value.(int); ok {
  169. if result, ok := setterResult[i]; ok {
  170. return result
  171. }
  172. }
  173. return true
  174. }
  175. type typeWithSetterField struct {
  176. Field *typeWithSetter "_"
  177. }
  178. func (s *S) TestUnmarshalWithSetter(c *C) {
  179. for _, item := range setterTests {
  180. obj := &typeWithSetterField{}
  181. err := goyaml.Unmarshal([]byte(item.data), obj)
  182. c.Assert(err, IsNil)
  183. c.Assert(obj.Field, NotNil,
  184. Commentf("Pointer not initialized (%#v)", item.value))
  185. c.Assert(obj.Field.tag, Equals, item.tag)
  186. c.Assert(obj.Field.value, DeepEquals, item.value)
  187. }
  188. }
  189. func (s *S) TestUnmarshalWholeDocumentWithSetter(c *C) {
  190. obj := &typeWithSetter{}
  191. err := goyaml.Unmarshal([]byte(setterTests[0].data), obj)
  192. c.Assert(err, IsNil)
  193. c.Assert(obj.tag, Equals, setterTests[0].tag)
  194. value, ok := obj.value.(map[interface{}]interface{})
  195. c.Assert(ok, Equals, true)
  196. c.Assert(value["_"], DeepEquals, setterTests[0].value)
  197. }
  198. func (s *S) TestUnmarshalWithFalseSetterIgnoresValue(c *C) {
  199. setterResult[2] = false
  200. setterResult[4] = false
  201. defer func() {
  202. delete(setterResult, 2)
  203. delete(setterResult, 4)
  204. }()
  205. m := map[string]*typeWithSetter{}
  206. data := "{abc: 1, def: 2, ghi: 3, jkl: 4}"
  207. err := goyaml.Unmarshal([]byte(data), m)
  208. c.Assert(err, IsNil)
  209. c.Assert(m["abc"], NotNil)
  210. c.Assert(m["def"], IsNil)
  211. c.Assert(m["ghi"], NotNil)
  212. c.Assert(m["jkl"], IsNil)
  213. c.Assert(m["abc"].value, Equals, 1)
  214. c.Assert(m["ghi"].value, Equals, 3)
  215. }