decode_test.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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()}},
  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: 1", &struct{ A int }{1}},
  77. {"a: [1, 2]", &struct{ A []int }{[]int{1, 2}}},
  78. {"a: 1", &struct{ B int }{0}},
  79. {"a: 1", &struct {
  80. B int "a"
  81. }{1}},
  82. {"a: y", &struct{ A bool }{true}},
  83. // Some cross type conversions
  84. {"v: 42", map[string]uint{"v": 42}},
  85. {"v: -42", map[string]uint{}},
  86. {"v: 4294967296", map[string]uint64{"v": 4294967296}},
  87. {"v: -4294967296", map[string]uint64{}},
  88. // Overflow cases.
  89. {"v: 4294967297", map[string]int32{}},
  90. {"v: 128", map[string]int8{}},
  91. // Quoted values.
  92. {"'1': '\"2\"'", map[interface{}]interface{}{"1": "\"2\""}},
  93. // Explicit tags.
  94. {"v: !!float '1.1'", map[string]interface{}{"v": 1.1}},
  95. {"v: !!null ''", map[string]interface{}{"v": nil}},
  96. {"%TAG !y! tag:yaml.org,2002:\n---\nv: !y!int '1'",
  97. map[string]interface{}{"v": 1}},
  98. // Anchors and aliases.
  99. {"a: &x 1\nb: &y 2\nc: *x\nd: *y\n", &struct{ A, B, C, D int }{1, 2, 1, 2}},
  100. {"a: &a {c: 1}\nb: *a",
  101. &struct {
  102. A, B struct {
  103. C int
  104. }
  105. }{struct{ C int }{1}, struct{ C int }{1}}},
  106. {"a: &a [1, 2]\nb: *a", &struct{ B []int }{[]int{1, 2}}},
  107. }
  108. func (s *S) TestUnmarshal(c *C) {
  109. for i, item := range unmarshalTests {
  110. t := reflect.ValueOf(item.value).Type()
  111. var value interface{}
  112. if t.Kind() == reflect.Map {
  113. value = reflect.MakeMap(t).Interface()
  114. } else {
  115. pt := reflect.ValueOf(item.value).Type()
  116. pv := reflect.New(pt.Elem())
  117. value = pv.Interface()
  118. }
  119. err := goyaml.Unmarshal([]byte(item.data), value)
  120. c.Assert(err, IsNil, Bug("Item #%d", i))
  121. c.Assert(value, Equals, item.value)
  122. }
  123. }
  124. var unmarshalErrorTests = []struct {
  125. data, error string
  126. }{
  127. {"v: !!float 'error'", "YAML error: Can't decode !!str 'error' as a !!float"},
  128. {"v: [A,", "YAML error: line 1: did not find expected node content"},
  129. {"v:\n- [A,", "YAML error: line 2: did not find expected node content"},
  130. {"a: *b\n", "YAML error: Unknown anchor 'b' referenced"},
  131. {"a: &a\n b: *a\n", "YAML error: Anchor 'a' value contains itself"},
  132. }
  133. func (s *S) TestUnmarshalErrors(c *C) {
  134. for _, item := range unmarshalErrorTests {
  135. var value interface{}
  136. err := goyaml.Unmarshal([]byte(item.data), &value)
  137. c.Assert(err, ErrorMatches, item.error, Bug("Partial unmarshal: %#v", value))
  138. }
  139. }
  140. var setterTests = []struct {
  141. data, tag string
  142. value interface{}
  143. }{
  144. {"_: {hi: there}", "!!map", map[interface{}]interface{}{"hi": "there"}},
  145. {"_: [1,A]", "!!seq", []interface{}{1, "A"}},
  146. {"_: 10", "!!int", 10},
  147. {"_: null", "!!null", nil},
  148. {"_: !!foo 'BAR!'", "!!foo", "BAR!"},
  149. }
  150. var setterResult = map[int]bool{}
  151. type typeWithSetter struct {
  152. tag string
  153. value interface{}
  154. }
  155. func (o *typeWithSetter) SetYAML(tag string, value interface{}) (ok bool) {
  156. o.tag = tag
  157. o.value = value
  158. if i, ok := value.(int); ok {
  159. if result, ok := setterResult[i]; ok {
  160. return result
  161. }
  162. }
  163. return true
  164. }
  165. type typeWithSetterField struct {
  166. Field *typeWithSetter "_"
  167. }
  168. func (s *S) TestUnmarshalWithSetter(c *C) {
  169. for _, item := range setterTests {
  170. obj := &typeWithSetterField{}
  171. err := goyaml.Unmarshal([]byte(item.data), obj)
  172. c.Assert(err, IsNil)
  173. c.Assert(obj.Field, NotNil,
  174. Bug("Pointer not initialized (%#v)", item.value))
  175. c.Assert(obj.Field.tag, Equals, item.tag)
  176. c.Assert(obj.Field.value, Equals, item.value)
  177. }
  178. }
  179. func (s *S) TestUnmarshalWholeDocumentWithSetter(c *C) {
  180. obj := &typeWithSetter{}
  181. err := goyaml.Unmarshal([]byte(setterTests[0].data), obj)
  182. c.Assert(err, IsNil)
  183. c.Assert(obj.tag, Equals, setterTests[0].tag)
  184. value, ok := obj.value.(map[interface{}]interface{})
  185. c.Assert(ok, Equals, true)
  186. c.Assert(value["_"], Equals, setterTests[0].value)
  187. }
  188. func (s *S) TestUnmarshalWithFalseSetterIgnoresValue(c *C) {
  189. setterResult[2] = false
  190. setterResult[4] = false
  191. defer func() {
  192. delete(setterResult, 2)
  193. delete(setterResult, 4)
  194. }()
  195. m := map[string]*typeWithSetter{}
  196. data := "{abc: 1, def: 2, ghi: 3, jkl: 4}"
  197. err := goyaml.Unmarshal([]byte(data), m)
  198. c.Assert(err, IsNil)
  199. c.Assert(m["abc"], NotNil)
  200. c.Assert(m["def"], IsNil)
  201. c.Assert(m["ghi"], NotNil)
  202. c.Assert(m["jkl"], IsNil)
  203. c.Assert(m["abc"].value, Equals, 1)
  204. c.Assert(m["ghi"].value, Equals, 3)
  205. }