decode_test.go 7.9 KB

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