encode_test.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. // Struct inlining
  195. {
  196. &struct {
  197. A int
  198. C inlineB `yaml:",inline"`
  199. }{1, inlineB{2, inlineC{3}}},
  200. "a: 1\nb: 2\nc: 3\n",
  201. },
  202. }
  203. func (s *S) TestMarshal(c *C) {
  204. for _, item := range marshalTests {
  205. data, err := goyaml.Marshal(item.value)
  206. c.Assert(err, IsNil)
  207. c.Assert(string(data), Equals, item.data)
  208. }
  209. }
  210. var marshalErrorTests = []struct {
  211. value interface{}
  212. error string
  213. }{
  214. {
  215. &struct {
  216. B int
  217. inlineB ",inline"
  218. }{1, inlineB{2, inlineC{3}}},
  219. `Duplicated key 'b' in struct struct \{ B int; .*`,
  220. },
  221. }
  222. func (s *S) TestMarshalErrors(c *C) {
  223. for _, item := range marshalErrorTests {
  224. _, err := goyaml.Marshal(item.value)
  225. c.Assert(err, ErrorMatches, item.error)
  226. }
  227. }
  228. var marshalTaggedIfaceTest interface{} = &struct{ A string }{"B"}
  229. var getterTests = []struct {
  230. data, tag string
  231. value interface{}
  232. }{
  233. {"_:\n hi: there\n", "", map[interface{}]interface{}{"hi": "there"}},
  234. {"_:\n- 1\n- A\n", "", []interface{}{1, "A"}},
  235. {"_: 10\n", "", 10},
  236. {"_: null\n", "", nil},
  237. {"_: !foo BAR!\n", "!foo", "BAR!"},
  238. {"_: !foo 1\n", "!foo", "1"},
  239. {"_: !foo '\"1\"'\n", "!foo", "\"1\""},
  240. {"_: !foo 1.1\n", "!foo", 1.1},
  241. {"_: !foo 1\n", "!foo", 1},
  242. {"_: !foo 1\n", "!foo", uint(1)},
  243. {"_: !foo true\n", "!foo", true},
  244. {"_: !foo\n- A\n- B\n", "!foo", []string{"A", "B"}},
  245. {"_: !foo\n A: B\n", "!foo", map[string]string{"A": "B"}},
  246. {"_: !foo\n a: B\n", "!foo", &marshalTaggedIfaceTest},
  247. }
  248. func (s *S) TestMarshalTypeCache(c *C) {
  249. var data []byte
  250. var err error
  251. func() {
  252. type T struct{ A int }
  253. data, err = goyaml.Marshal(&T{})
  254. c.Assert(err, IsNil)
  255. }()
  256. func() {
  257. type T struct{ B int }
  258. data, err = goyaml.Marshal(&T{})
  259. c.Assert(err, IsNil)
  260. }()
  261. c.Assert(string(data), Equals, "b: 0\n")
  262. }
  263. type typeWithGetter struct {
  264. tag string
  265. value interface{}
  266. }
  267. func (o typeWithGetter) GetYAML() (tag string, value interface{}) {
  268. return o.tag, o.value
  269. }
  270. type typeWithGetterField struct {
  271. Field typeWithGetter "_"
  272. }
  273. func (s *S) TestMashalWithGetter(c *C) {
  274. for _, item := range getterTests {
  275. obj := &typeWithGetterField{}
  276. obj.Field.tag = item.tag
  277. obj.Field.value = item.value
  278. data, err := goyaml.Marshal(obj)
  279. c.Assert(err, IsNil)
  280. c.Assert(string(data), Equals, string(item.data))
  281. }
  282. }
  283. func (s *S) TestUnmarshalWholeDocumentWithGetter(c *C) {
  284. obj := &typeWithGetter{}
  285. obj.tag = ""
  286. obj.value = map[string]string{"hello": "world!"}
  287. data, err := goyaml.Marshal(obj)
  288. c.Assert(err, IsNil)
  289. c.Assert(string(data), Equals, "hello: world!\n")
  290. }
  291. func (s *S) TestSortedOutput(c *C) {
  292. order := []interface{}{
  293. false,
  294. true,
  295. 1,
  296. uint(1),
  297. 1.0,
  298. 1.1,
  299. 1.2,
  300. 2,
  301. uint(2),
  302. 2.0,
  303. 2.1,
  304. "",
  305. ".1",
  306. ".2",
  307. ".a",
  308. "1",
  309. "2",
  310. "a!10",
  311. "a/2",
  312. "a/10",
  313. "a~10",
  314. "ab/1",
  315. "b/1",
  316. "b/01",
  317. "b/2",
  318. "b/02",
  319. "b/3",
  320. "b/03",
  321. "b1",
  322. "b01",
  323. "b3",
  324. "c2.10",
  325. "c10.2",
  326. "d1",
  327. "d12",
  328. "d12a",
  329. }
  330. m := make(map[interface{}]int)
  331. for _, k := range order {
  332. m[k] = 1
  333. }
  334. data, err := goyaml.Marshal(m)
  335. c.Assert(err, IsNil)
  336. out := "\n" + string(data)
  337. last := 0
  338. for i, k := range order {
  339. repr := fmt.Sprint(k)
  340. if s, ok := k.(string); ok {
  341. if _, err = strconv.ParseFloat(repr, 32); s == "" || err == nil {
  342. repr = `"` + repr + `"`
  343. }
  344. }
  345. index := strings.Index(out, "\n"+repr+":")
  346. if index == -1 {
  347. c.Fatalf("%#v is not in the output: %#v", k, out)
  348. }
  349. if index < last {
  350. c.Fatalf("%#v was generated before %#v: %q", k, order[i-1], out)
  351. }
  352. last = index
  353. }
  354. }