encode_test.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. // Unexported field
  94. {"a: 1\n",
  95. &struct {
  96. u int
  97. A int
  98. }{0, 1}},
  99. }
  100. func (s *S) TestMarshal(c *C) {
  101. for _, item := range marshalTests {
  102. data, err := goyaml.Marshal(item.value)
  103. c.Assert(err, IsNil)
  104. c.Assert(string(data), Equals, item.data)
  105. }
  106. }
  107. //var unmarshalErrorTests = []struct{data, error string}{
  108. // {"v: !!float 'error'", "Can't decode !!str 'error' as a !!float"},
  109. //}
  110. //
  111. //func (s *S) TestUnmarshalErrors(c *C) {
  112. // for _, item := range unmarshalErrorTests {
  113. // var value interface{}
  114. // err := goyaml.Unmarshal([]byte(item.data), &value)
  115. // c.Assert(err, Matches, item.error)
  116. // }
  117. //}
  118. var marshalTaggedIfaceTest interface{} = &struct{ A string }{"B"}
  119. var getterTests = []struct {
  120. data, tag string
  121. value interface{}
  122. }{
  123. {"_:\n hi: there\n", "", map[interface{}]interface{}{"hi": "there"}},
  124. {"_:\n- 1\n- A\n", "", []interface{}{1, "A"}},
  125. {"_: 10\n", "", 10},
  126. {"_: null\n", "", nil},
  127. {"_: !foo BAR!\n", "!foo", "BAR!"},
  128. {"_: !foo 1\n", "!foo", "1"},
  129. {"_: !foo '\"1\"'\n", "!foo", "\"1\""},
  130. {"_: !foo 1.1\n", "!foo", 1.1},
  131. {"_: !foo 1\n", "!foo", 1},
  132. {"_: !foo 1\n", "!foo", uint(1)},
  133. {"_: !foo true\n", "!foo", true},
  134. {"_: !foo\n- A\n- B\n", "!foo", []string{"A", "B"}},
  135. {"_: !foo\n A: B\n", "!foo", map[string]string{"A": "B"}},
  136. {"_: !foo\n a: B\n", "!foo", &marshalTaggedIfaceTest},
  137. }
  138. func (s *S) TestMarshalTypeCache(c *C) {
  139. var data []byte
  140. var err error
  141. func() {
  142. type T struct{ A int }
  143. data, err = goyaml.Marshal(&T{})
  144. c.Assert(err, IsNil)
  145. }()
  146. func() {
  147. type T struct{ B int }
  148. data, err = goyaml.Marshal(&T{})
  149. c.Assert(err, IsNil)
  150. }()
  151. c.Assert(string(data), Equals, "b: 0\n")
  152. }
  153. type typeWithGetter struct {
  154. tag string
  155. value interface{}
  156. }
  157. func (o typeWithGetter) GetYAML() (tag string, value interface{}) {
  158. return o.tag, o.value
  159. }
  160. type typeWithGetterField struct {
  161. Field typeWithGetter "_"
  162. }
  163. func (s *S) TestMashalWithGetter(c *C) {
  164. for _, item := range getterTests {
  165. obj := &typeWithGetterField{}
  166. obj.Field.tag = item.tag
  167. obj.Field.value = item.value
  168. data, err := goyaml.Marshal(obj)
  169. c.Assert(err, IsNil)
  170. c.Assert(string(data), Equals, string(item.data))
  171. }
  172. }
  173. func (s *S) TestUnmarshalWholeDocumentWithGetter(c *C) {
  174. obj := &typeWithGetter{}
  175. obj.tag = ""
  176. obj.value = map[string]string{"hello": "world!"}
  177. data, err := goyaml.Marshal(obj)
  178. c.Assert(err, IsNil)
  179. c.Assert(string(data), Equals, "hello: world!\n")
  180. }
  181. func (s *S) TestSortedOutput(c *C) {
  182. order := []interface{}{
  183. false,
  184. true,
  185. 1,
  186. uint(1),
  187. 1.0,
  188. 1.1,
  189. 1.2,
  190. 2,
  191. uint(2),
  192. 2.0,
  193. 2.1,
  194. "",
  195. ".1",
  196. ".2",
  197. ".a",
  198. "1",
  199. "2",
  200. "a!10",
  201. "a/2",
  202. "a/10",
  203. "a~10",
  204. "ab/1",
  205. "b/1",
  206. "b/01",
  207. "b/2",
  208. "b/02",
  209. "b/3",
  210. "b/03",
  211. "b1",
  212. "b01",
  213. "b3",
  214. "c2.10",
  215. "c10.2",
  216. "d1",
  217. "d12",
  218. "d12a",
  219. }
  220. m := make(map[interface{}]int)
  221. for _, k := range order {
  222. m[k] = 1
  223. }
  224. data, err := goyaml.Marshal(m)
  225. c.Assert(err, IsNil)
  226. out := "\n" + string(data)
  227. last := 0
  228. for i, k := range order {
  229. repr := fmt.Sprint(k)
  230. if s, ok := k.(string); ok {
  231. if _, err = strconv.ParseFloat(repr, 32); s == "" || err == nil {
  232. repr = `"` + repr + `"`
  233. }
  234. }
  235. index := strings.Index(out, "\n"+repr+":")
  236. if index == -1 {
  237. c.Fatalf("%#v is not in the output: %#v", k, out)
  238. }
  239. if index < last {
  240. c.Fatalf("%#v was generated before %#v: %q", k, order[i-1], out)
  241. }
  242. last = index
  243. }
  244. }