encode_test.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. value interface{}
  13. data string
  14. }{
  15. {
  16. &struct{}{},
  17. "{}\n",
  18. }, {
  19. map[string]string{"v": "hi"},
  20. "v: hi\n",
  21. }, {
  22. map[string]interface{}{"v": "hi"},
  23. "v: hi\n",
  24. }, {
  25. map[string]string{"v": "true"},
  26. "v: \"true\"\n",
  27. }, {
  28. map[string]string{"v": "false"},
  29. "v: \"false\"\n",
  30. }, {
  31. map[string]interface{}{"v": true},
  32. "v: true\n",
  33. }, {
  34. map[string]interface{}{"v": false},
  35. "v: false\n",
  36. }, {
  37. map[string]interface{}{"v": 10},
  38. "v: 10\n",
  39. }, {
  40. map[string]interface{}{"v": -10},
  41. "v: -10\n",
  42. }, {
  43. map[string]uint{"v": 42},
  44. "v: 42\n",
  45. }, {
  46. map[string]interface{}{"v": int64(4294967296)},
  47. "v: 4294967296\n",
  48. }, {
  49. map[string]int64{"v": int64(4294967296)},
  50. "v: 4294967296\n",
  51. }, {
  52. map[string]uint64{"v": 4294967296},
  53. "v: 4294967296\n",
  54. }, {
  55. map[string]interface{}{"v": "10"},
  56. "v: \"10\"\n",
  57. }, {
  58. map[string]interface{}{"v": 0.1},
  59. "v: 0.1\n",
  60. }, {
  61. map[string]interface{}{"v": float64(0.1)},
  62. "v: 0.1\n",
  63. }, {
  64. map[string]interface{}{"v": -0.1},
  65. "v: -0.1\n",
  66. }, {
  67. map[string]interface{}{"v": math.Inf(+1)},
  68. "v: .inf\n",
  69. }, {
  70. map[string]interface{}{"v": math.Inf(-1)},
  71. "v: -.inf\n",
  72. }, {
  73. map[string]interface{}{"v": math.NaN()},
  74. "v: .nan\n",
  75. }, {
  76. map[string]interface{}{"v": nil},
  77. "v: null\n",
  78. }, {
  79. map[string]interface{}{"v": ""},
  80. "v: \"\"\n",
  81. }, {
  82. map[string][]string{"v": []string{"A", "B"}},
  83. "v:\n- A\n- B\n",
  84. }, {
  85. map[string][]string{"v": []string{"A", "B\nC"}},
  86. "v:\n- A\n- 'B\n\n C'\n",
  87. }, {
  88. map[string][]interface{}{"v": []interface{}{"A", 1, map[string][]int{"B": []int{2, 3}}}},
  89. "v:\n- A\n- 1\n- B:\n - 2\n - 3\n",
  90. }, {
  91. map[string]interface{}{"a": map[interface{}]interface{}{"b": "c"}},
  92. "a:\n b: c\n",
  93. },
  94. // Simple values.
  95. {
  96. &marshalIntTest,
  97. "123\n",
  98. },
  99. // Structures
  100. {
  101. &struct{ Hello string }{"world"},
  102. "hello: world\n",
  103. }, {
  104. &struct {
  105. A struct {
  106. B string
  107. }
  108. }{struct{ B string }{"c"}},
  109. "a:\n b: c\n",
  110. }, {
  111. &struct {
  112. A *struct {
  113. B string
  114. }
  115. }{&struct{ B string }{"c"}},
  116. "a:\n b: c\n",
  117. }, {
  118. &struct {
  119. A *struct {
  120. B string
  121. }
  122. }{},
  123. "a: null\n",
  124. }, {
  125. &struct{ A int }{1},
  126. "a: 1\n",
  127. }, {
  128. &struct{ A []int }{[]int{1, 2}},
  129. "a:\n- 1\n- 2\n",
  130. }, {
  131. &struct {
  132. B int "a"
  133. }{1},
  134. "a: 1\n",
  135. }, {
  136. &struct{ A bool }{true},
  137. "a: true\n",
  138. },
  139. // Conditional flag
  140. {
  141. &struct {
  142. A int "a,omitempty"
  143. B int "b,omitempty"
  144. }{1, 0},
  145. "a: 1\n",
  146. }, {
  147. &struct {
  148. A int "a,omitempty"
  149. B int "b,omitempty"
  150. }{0, 0},
  151. "{}\n",
  152. }, {
  153. &struct {
  154. A *struct{ X int } "a,omitempty"
  155. B int "b,omitempty"
  156. }{nil, 0},
  157. "{}\n",
  158. },
  159. // Flow flag
  160. {
  161. &struct {
  162. A []int "a,flow"
  163. }{[]int{1, 2}},
  164. "a: [1, 2]\n",
  165. }, {
  166. &struct {
  167. A map[string]string "a,flow"
  168. }{map[string]string{"b": "c", "d": "e"}},
  169. "a: {b: c, d: e}\n",
  170. }, {
  171. &struct {
  172. A struct {
  173. B, D string
  174. } "a,flow"
  175. }{struct{ B, D string }{"c", "e"}},
  176. "a: {b: c, d: e}\n",
  177. },
  178. // Unexported field
  179. {
  180. &struct {
  181. u int
  182. A int
  183. }{0, 1},
  184. "a: 1\n",
  185. },
  186. // Ignored field
  187. {
  188. &struct {
  189. A int
  190. B int "-"
  191. }{1, 2},
  192. "a: 1\n",
  193. },
  194. }
  195. func (s *S) TestMarshal(c *C) {
  196. for _, item := range marshalTests {
  197. data, err := goyaml.Marshal(item.value)
  198. c.Assert(err, IsNil)
  199. c.Assert(string(data), Equals, item.data)
  200. }
  201. }
  202. //var unmarshalErrorTests = []struct{data, error string}{
  203. // {"v: !!float 'error'", "Can't decode !!str 'error' as a !!float"},
  204. //}
  205. //
  206. //func (s *S) TestUnmarshalErrors(c *C) {
  207. // for _, item := range unmarshalErrorTests {
  208. // var value interface{}
  209. // err := goyaml.Unmarshal([]byte(item.data), &value)
  210. // c.Assert(err, Matches, item.error)
  211. // }
  212. //}
  213. var marshalTaggedIfaceTest interface{} = &struct{ A string }{"B"}
  214. var getterTests = []struct {
  215. data, tag string
  216. value interface{}
  217. }{
  218. {"_:\n hi: there\n", "", map[interface{}]interface{}{"hi": "there"}},
  219. {"_:\n- 1\n- A\n", "", []interface{}{1, "A"}},
  220. {"_: 10\n", "", 10},
  221. {"_: null\n", "", nil},
  222. {"_: !foo BAR!\n", "!foo", "BAR!"},
  223. {"_: !foo 1\n", "!foo", "1"},
  224. {"_: !foo '\"1\"'\n", "!foo", "\"1\""},
  225. {"_: !foo 1.1\n", "!foo", 1.1},
  226. {"_: !foo 1\n", "!foo", 1},
  227. {"_: !foo 1\n", "!foo", uint(1)},
  228. {"_: !foo true\n", "!foo", true},
  229. {"_: !foo\n- A\n- B\n", "!foo", []string{"A", "B"}},
  230. {"_: !foo\n A: B\n", "!foo", map[string]string{"A": "B"}},
  231. {"_: !foo\n a: B\n", "!foo", &marshalTaggedIfaceTest},
  232. }
  233. func (s *S) TestMarshalTypeCache(c *C) {
  234. var data []byte
  235. var err error
  236. func() {
  237. type T struct{ A int }
  238. data, err = goyaml.Marshal(&T{})
  239. c.Assert(err, IsNil)
  240. }()
  241. func() {
  242. type T struct{ B int }
  243. data, err = goyaml.Marshal(&T{})
  244. c.Assert(err, IsNil)
  245. }()
  246. c.Assert(string(data), Equals, "b: 0\n")
  247. }
  248. type typeWithGetter struct {
  249. tag string
  250. value interface{}
  251. }
  252. func (o typeWithGetter) GetYAML() (tag string, value interface{}) {
  253. return o.tag, o.value
  254. }
  255. type typeWithGetterField struct {
  256. Field typeWithGetter "_"
  257. }
  258. func (s *S) TestMashalWithGetter(c *C) {
  259. for _, item := range getterTests {
  260. obj := &typeWithGetterField{}
  261. obj.Field.tag = item.tag
  262. obj.Field.value = item.value
  263. data, err := goyaml.Marshal(obj)
  264. c.Assert(err, IsNil)
  265. c.Assert(string(data), Equals, string(item.data))
  266. }
  267. }
  268. func (s *S) TestUnmarshalWholeDocumentWithGetter(c *C) {
  269. obj := &typeWithGetter{}
  270. obj.tag = ""
  271. obj.value = map[string]string{"hello": "world!"}
  272. data, err := goyaml.Marshal(obj)
  273. c.Assert(err, IsNil)
  274. c.Assert(string(data), Equals, "hello: world!\n")
  275. }
  276. func (s *S) TestSortedOutput(c *C) {
  277. order := []interface{}{
  278. false,
  279. true,
  280. 1,
  281. uint(1),
  282. 1.0,
  283. 1.1,
  284. 1.2,
  285. 2,
  286. uint(2),
  287. 2.0,
  288. 2.1,
  289. "",
  290. ".1",
  291. ".2",
  292. ".a",
  293. "1",
  294. "2",
  295. "a!10",
  296. "a/2",
  297. "a/10",
  298. "a~10",
  299. "ab/1",
  300. "b/1",
  301. "b/01",
  302. "b/2",
  303. "b/02",
  304. "b/3",
  305. "b/03",
  306. "b1",
  307. "b01",
  308. "b3",
  309. "c2.10",
  310. "c10.2",
  311. "d1",
  312. "d12",
  313. "d12a",
  314. }
  315. m := make(map[interface{}]int)
  316. for _, k := range order {
  317. m[k] = 1
  318. }
  319. data, err := goyaml.Marshal(m)
  320. c.Assert(err, IsNil)
  321. out := "\n" + string(data)
  322. last := 0
  323. for i, k := range order {
  324. repr := fmt.Sprint(k)
  325. if s, ok := k.(string); ok {
  326. if _, err = strconv.ParseFloat(repr, 32); s == "" || err == nil {
  327. repr = `"` + repr + `"`
  328. }
  329. }
  330. index := strings.Index(out, "\n"+repr+":")
  331. if index == -1 {
  332. c.Fatalf("%#v is not in the output: %#v", k, out)
  333. }
  334. if index < last {
  335. c.Fatalf("%#v was generated before %#v: %q", k, order[i-1], out)
  336. }
  337. last = index
  338. }
  339. }