encode_test.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. package goyaml_test
  2. import (
  3. "fmt"
  4. . "launchpad.net/gocheck"
  5. "launchpad.net/goyaml"
  6. "math"
  7. "strconv"
  8. "strings"
  9. )
  10. var marshalIntTest = 123
  11. var marshalTests = []struct {
  12. data string
  13. value interface{}
  14. }{
  15. {"{}\n", &struct{}{}},
  16. {"v: hi\n", map[string]string{"v": "hi"}},
  17. {"v: hi\n", map[string]interface{}{"v": "hi"}},
  18. {"v: \"true\"\n", map[string]string{"v": "true"}},
  19. {"v: \"false\"\n", map[string]string{"v": "false"}},
  20. {"v: true\n", map[string]interface{}{"v": true}},
  21. {"v: false\n", map[string]interface{}{"v": false}},
  22. {"v: 10\n", map[string]interface{}{"v": 10}},
  23. {"v: -10\n", map[string]interface{}{"v": -10}},
  24. {"v: 42\n", map[string]uint{"v": 42}},
  25. {"v: 4294967296\n", map[string]interface{}{"v": int64(4294967296)}},
  26. {"v: 4294967296\n", map[string]int64{"v": int64(4294967296)}},
  27. {"v: 4294967296\n", map[string]uint64{"v": 4294967296}},
  28. {"v: \"10\"\n", map[string]interface{}{"v": "10"}},
  29. {"v: 0.1\n", map[string]interface{}{"v": 0.1}},
  30. {"v: 0.1\n", map[string]interface{}{"v": float64(0.1)}},
  31. {"v: -0.1\n", map[string]interface{}{"v": -0.1}},
  32. {"v: .inf\n", map[string]interface{}{"v": math.Inf(+1)}},
  33. {"v: -.inf\n", map[string]interface{}{"v": math.Inf(-1)}},
  34. {"v: .nan\n", map[string]interface{}{"v": math.NaN()}},
  35. {"v: null\n", map[string]interface{}{"v": nil}},
  36. {"v: \"\"\n", map[string]interface{}{"v": ""}},
  37. {"v:\n- A\n- B\n", map[string][]string{"v": []string{"A", "B"}}},
  38. {"v:\n- A\n- 1\n", map[string][]interface{}{"v": []interface{}{"A", 1}}},
  39. {"a:\n b: c\n",
  40. map[string]interface{}{"a": map[interface{}]interface{}{"b": "c"}}},
  41. // Simple values.
  42. {"123\n", &marshalIntTest},
  43. // Structures
  44. {"hello: world\n", &struct{ Hello string }{"world"}},
  45. {"a:\n b: c\n", &struct {
  46. A struct {
  47. B string
  48. }
  49. }{struct{ B string }{"c"}}},
  50. {"a:\n b: c\n", &struct {
  51. A *struct {
  52. B string
  53. }
  54. }{&struct{ B string }{"c"}}},
  55. {"a: null\n", &struct {
  56. A *struct {
  57. B string
  58. }
  59. }{}},
  60. {"a: 1\n", &struct{ A int }{1}},
  61. {"a:\n- 1\n- 2\n", &struct{ A []int }{[]int{1, 2}}},
  62. {"a: 1\n", &struct {
  63. B int "a"
  64. }{1}},
  65. {"a: true\n", &struct{ A bool }{true}},
  66. // Conditional flag
  67. {"a: 1\n", &struct {
  68. A int "a,omitempty"
  69. B int "b,omitempty"
  70. }{1, 0}},
  71. {"{}\n", &struct {
  72. A int "a,omitempty"
  73. B int "b,omitempty"
  74. }{0, 0}},
  75. {"{}\n", &struct {
  76. A *struct{ X int } "a,omitempty"
  77. B int "b,omitempty"
  78. }{nil, 0}},
  79. // Flow flag
  80. {"a: [1, 2]\n", &struct {
  81. A []int "a,flow"
  82. }{[]int{1, 2}}},
  83. {"a: {b: c}\n",
  84. &struct {
  85. A map[string]string "a,flow"
  86. }{map[string]string{"b": "c"}}},
  87. {"a: {b: c}\n",
  88. &struct {
  89. A struct {
  90. B string
  91. } "a,flow"
  92. }{struct{ B string }{"c"}}},
  93. }
  94. func (s *S) TestMarshal(c *C) {
  95. for _, item := range marshalTests {
  96. data, err := goyaml.Marshal(item.value)
  97. c.Assert(err, IsNil)
  98. c.Assert(string(data), Equals, item.data)
  99. }
  100. }
  101. //var unmarshalErrorTests = []struct{data, error string}{
  102. // {"v: !!float 'error'", "Can't decode !!str 'error' as a !!float"},
  103. //}
  104. //
  105. //func (s *S) TestUnmarshalErrors(c *C) {
  106. // for _, item := range unmarshalErrorTests {
  107. // var value interface{}
  108. // err := goyaml.Unmarshal([]byte(item.data), &value)
  109. // c.Assert(err, Matches, item.error)
  110. // }
  111. //}
  112. var marshalTaggedIfaceTest interface{} = &struct{ A string }{"B"}
  113. var getterTests = []struct {
  114. data, tag string
  115. value interface{}
  116. }{
  117. {"_:\n hi: there\n", "", map[interface{}]interface{}{"hi": "there"}},
  118. {"_:\n- 1\n- A\n", "", []interface{}{1, "A"}},
  119. {"_: 10\n", "", 10},
  120. {"_: null\n", "", nil},
  121. {"_: !foo BAR!\n", "!foo", "BAR!"},
  122. {"_: !foo 1\n", "!foo", "1"},
  123. {"_: !foo '\"1\"'\n", "!foo", "\"1\""},
  124. {"_: !foo 1.1\n", "!foo", 1.1},
  125. {"_: !foo 1\n", "!foo", 1},
  126. {"_: !foo 1\n", "!foo", uint(1)},
  127. {"_: !foo true\n", "!foo", true},
  128. {"_: !foo\n- A\n- B\n", "!foo", []string{"A", "B"}},
  129. {"_: !foo\n A: B\n", "!foo", map[string]string{"A": "B"}},
  130. {"_: !foo\n a: B\n", "!foo", &marshalTaggedIfaceTest},
  131. }
  132. type typeWithGetter struct {
  133. tag string
  134. value interface{}
  135. }
  136. func (o typeWithGetter) GetYAML() (tag string, value interface{}) {
  137. return o.tag, o.value
  138. }
  139. type typeWithGetterField struct {
  140. Field typeWithGetter "_"
  141. }
  142. func (s *S) TestMashalWithGetter(c *C) {
  143. for _, item := range getterTests {
  144. obj := &typeWithGetterField{}
  145. obj.Field.tag = item.tag
  146. obj.Field.value = item.value
  147. data, err := goyaml.Marshal(obj)
  148. c.Assert(err, IsNil)
  149. c.Assert(string(data), Equals, string(item.data))
  150. }
  151. }
  152. func (s *S) TestUnmarshalWholeDocumentWithGetter(c *C) {
  153. obj := &typeWithGetter{}
  154. obj.tag = ""
  155. obj.value = map[string]string{"hello": "world!"}
  156. data, err := goyaml.Marshal(obj)
  157. c.Assert(err, IsNil)
  158. c.Assert(string(data), Equals, "hello: world!\n")
  159. }
  160. func (s *S) TestSortedOutput(c *C) {
  161. order := []interface{}{
  162. false,
  163. true,
  164. 1,
  165. uint(1),
  166. 1.0,
  167. 1.1,
  168. 1.2,
  169. 2,
  170. uint(2),
  171. 2.0,
  172. 2.1,
  173. ".1",
  174. ".2",
  175. ".a",
  176. "1",
  177. "2",
  178. "a!10",
  179. "a/2",
  180. "a/10",
  181. "a~10",
  182. "ab/1",
  183. "b/1",
  184. "b/01",
  185. "b/2",
  186. "b/02",
  187. "b/3",
  188. "b/03",
  189. "b1",
  190. "b01",
  191. "b3",
  192. "c2.10",
  193. "c10.2",
  194. "d1",
  195. "d12",
  196. "d12a",
  197. }
  198. m := make(map[interface{}]int)
  199. for _, k := range order {
  200. m[k] = 1
  201. }
  202. data, err := goyaml.Marshal(m)
  203. c.Assert(err, IsNil)
  204. out := "\n" + string(data)
  205. last := 0
  206. for i, k := range order {
  207. repr := fmt.Sprint(k)
  208. if _, ok := k.(string); ok {
  209. if _, err = strconv.ParseFloat(repr, 32); err == nil {
  210. repr = `"` + repr + `"`
  211. }
  212. }
  213. index := strings.Index(out, "\n"+repr+":")
  214. if index == -1 {
  215. c.Fatalf("%#v is not in the output: %#v", k, out)
  216. }
  217. if index < last {
  218. c.Fatalf("%#v was generated before %#v: %q", k, order[i-1], out)
  219. }
  220. last = index
  221. }
  222. }