encode_test.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. package yaml_test
  2. import (
  3. "fmt"
  4. . "gopkg.in/check.v1"
  5. "gopkg.in/yaml.v1"
  6. "math"
  7. "strconv"
  8. "strings"
  9. "time"
  10. )
  11. var marshalIntTest = 123
  12. var marshalTests = []struct {
  13. value interface{}
  14. data string
  15. }{
  16. {
  17. &struct{}{},
  18. "{}\n",
  19. }, {
  20. map[string]string{"v": "hi"},
  21. "v: hi\n",
  22. }, {
  23. map[string]interface{}{"v": "hi"},
  24. "v: hi\n",
  25. }, {
  26. map[string]string{"v": "true"},
  27. "v: \"true\"\n",
  28. }, {
  29. map[string]string{"v": "false"},
  30. "v: \"false\"\n",
  31. }, {
  32. map[string]interface{}{"v": true},
  33. "v: true\n",
  34. }, {
  35. map[string]interface{}{"v": false},
  36. "v: false\n",
  37. }, {
  38. map[string]interface{}{"v": 10},
  39. "v: 10\n",
  40. }, {
  41. map[string]interface{}{"v": -10},
  42. "v: -10\n",
  43. }, {
  44. map[string]uint{"v": 42},
  45. "v: 42\n",
  46. }, {
  47. map[string]interface{}{"v": int64(4294967296)},
  48. "v: 4294967296\n",
  49. }, {
  50. map[string]int64{"v": int64(4294967296)},
  51. "v: 4294967296\n",
  52. }, {
  53. map[string]uint64{"v": 4294967296},
  54. "v: 4294967296\n",
  55. }, {
  56. map[string]interface{}{"v": "10"},
  57. "v: \"10\"\n",
  58. }, {
  59. map[string]interface{}{"v": 0.1},
  60. "v: 0.1\n",
  61. }, {
  62. map[string]interface{}{"v": float64(0.1)},
  63. "v: 0.1\n",
  64. }, {
  65. map[string]interface{}{"v": -0.1},
  66. "v: -0.1\n",
  67. }, {
  68. map[string]interface{}{"v": math.Inf(+1)},
  69. "v: .inf\n",
  70. }, {
  71. map[string]interface{}{"v": math.Inf(-1)},
  72. "v: -.inf\n",
  73. }, {
  74. map[string]interface{}{"v": math.NaN()},
  75. "v: .nan\n",
  76. }, {
  77. map[string]interface{}{"v": nil},
  78. "v: null\n",
  79. }, {
  80. map[string]interface{}{"v": ""},
  81. "v: \"\"\n",
  82. }, {
  83. map[string][]string{"v": []string{"A", "B"}},
  84. "v:\n- A\n- B\n",
  85. }, {
  86. map[string][]string{"v": []string{"A", "B\nC"}},
  87. "v:\n- A\n- |-\n B\n C\n",
  88. }, {
  89. map[string][]interface{}{"v": []interface{}{"A", 1, map[string][]int{"B": []int{2, 3}}}},
  90. "v:\n- A\n- 1\n- B:\n - 2\n - 3\n",
  91. }, {
  92. map[string]interface{}{"a": map[interface{}]interface{}{"b": "c"}},
  93. "a:\n b: c\n",
  94. }, {
  95. map[string]interface{}{"a": "-"},
  96. "a: '-'\n",
  97. },
  98. // Simple values.
  99. {
  100. &marshalIntTest,
  101. "123\n",
  102. },
  103. // Structures
  104. {
  105. &struct{ Hello string }{"world"},
  106. "hello: world\n",
  107. }, {
  108. &struct {
  109. A struct {
  110. B string
  111. }
  112. }{struct{ B string }{"c"}},
  113. "a:\n b: c\n",
  114. }, {
  115. &struct {
  116. A *struct {
  117. B string
  118. }
  119. }{&struct{ B string }{"c"}},
  120. "a:\n b: c\n",
  121. }, {
  122. &struct {
  123. A *struct {
  124. B string
  125. }
  126. }{},
  127. "a: null\n",
  128. }, {
  129. &struct{ A int }{1},
  130. "a: 1\n",
  131. }, {
  132. &struct{ A []int }{[]int{1, 2}},
  133. "a:\n- 1\n- 2\n",
  134. }, {
  135. &struct {
  136. B int "a"
  137. }{1},
  138. "a: 1\n",
  139. }, {
  140. &struct{ A bool }{true},
  141. "a: true\n",
  142. },
  143. // Conditional flag
  144. {
  145. &struct {
  146. A int "a,omitempty"
  147. B int "b,omitempty"
  148. }{1, 0},
  149. "a: 1\n",
  150. }, {
  151. &struct {
  152. A int "a,omitempty"
  153. B int "b,omitempty"
  154. }{0, 0},
  155. "{}\n",
  156. }, {
  157. &struct {
  158. A *struct{ X int } "a,omitempty"
  159. B int "b,omitempty"
  160. }{nil, 0},
  161. "{}\n",
  162. },
  163. // Flow flag
  164. {
  165. &struct {
  166. A []int "a,flow"
  167. }{[]int{1, 2}},
  168. "a: [1, 2]\n",
  169. }, {
  170. &struct {
  171. A map[string]string "a,flow"
  172. }{map[string]string{"b": "c", "d": "e"}},
  173. "a: {b: c, d: e}\n",
  174. }, {
  175. &struct {
  176. A struct {
  177. B, D string
  178. } "a,flow"
  179. }{struct{ B, D string }{"c", "e"}},
  180. "a: {b: c, d: e}\n",
  181. },
  182. // Unexported field
  183. {
  184. &struct {
  185. u int
  186. A int
  187. }{0, 1},
  188. "a: 1\n",
  189. },
  190. // Ignored field
  191. {
  192. &struct {
  193. A int
  194. B int "-"
  195. }{1, 2},
  196. "a: 1\n",
  197. },
  198. // Struct inlining
  199. {
  200. &struct {
  201. A int
  202. C inlineB `yaml:",inline"`
  203. }{1, inlineB{2, inlineC{3}}},
  204. "a: 1\nb: 2\nc: 3\n",
  205. },
  206. // Duration
  207. {
  208. map[string]time.Duration{"a": 3 * time.Second},
  209. "a: 3s\n",
  210. },
  211. // Issue #24: bug in map merging logic.
  212. {
  213. map[string]string{"a": "<foo>"},
  214. "a: <foo>\n",
  215. },
  216. // Issue #34: marshal unsupported base 60 floats quoted for compatibility
  217. // with old YAML 1.1 parsers.
  218. {
  219. map[string]string{"a": "1:1"},
  220. "a: \"1:1\"\n",
  221. },
  222. // Binary data.
  223. {
  224. map[string]string{"a": "\x00"},
  225. "a: \"\\0\"\n",
  226. }, {
  227. map[string]string{"a": "\x80\x81\x82"},
  228. "a: !!binary gIGC\n",
  229. }, {
  230. map[string]string{"a": strings.Repeat("\x90", 54)},
  231. "a: !!binary |\n " + strings.Repeat("kJCQ", 17) + "kJ\n CQ\n",
  232. }, {
  233. map[string]interface{}{"a": typeWithGetter{"!!str", "\x80\x81\x82"}},
  234. "a: !!binary gIGC\n",
  235. },
  236. // Escaping of tags.
  237. {
  238. map[string]interface{}{"a": typeWithGetter{"foo!bar", 1}},
  239. "a: !<foo%21bar> 1\n",
  240. },
  241. }
  242. func (s *S) TestMarshal(c *C) {
  243. for _, item := range marshalTests {
  244. data, err := yaml.Marshal(item.value)
  245. c.Assert(err, IsNil)
  246. c.Assert(string(data), Equals, item.data)
  247. }
  248. }
  249. var marshalErrorTests = []struct {
  250. value interface{}
  251. error string
  252. panic string
  253. }{{
  254. value: &struct {
  255. B int
  256. inlineB ",inline"
  257. }{1, inlineB{2, inlineC{3}}},
  258. panic: `Duplicated key 'b' in struct struct \{ B int; .*`,
  259. }, {
  260. value: typeWithGetter{"!!binary", "\x80"},
  261. error: "YAML error: explicitly tagged !!binary data must be base64-encoded",
  262. }, {
  263. value: typeWithGetter{"!!float", "\x80"},
  264. error: `YAML error: cannot marshal invalid UTF-8 data as !!float`,
  265. }}
  266. func (s *S) TestMarshalErrors(c *C) {
  267. for _, item := range marshalErrorTests {
  268. if item.panic != "" {
  269. c.Assert(func() { yaml.Marshal(item.value) }, PanicMatches, item.panic)
  270. } else {
  271. _, err := yaml.Marshal(item.value)
  272. c.Assert(err, ErrorMatches, item.error)
  273. }
  274. }
  275. }
  276. var marshalTaggedIfaceTest interface{} = &struct{ A string }{"B"}
  277. var getterTests = []struct {
  278. data, tag string
  279. value interface{}
  280. }{
  281. {"_:\n hi: there\n", "", map[interface{}]interface{}{"hi": "there"}},
  282. {"_:\n- 1\n- A\n", "", []interface{}{1, "A"}},
  283. {"_: 10\n", "", 10},
  284. {"_: null\n", "", nil},
  285. {"_: !foo BAR!\n", "!foo", "BAR!"},
  286. {"_: !foo 1\n", "!foo", "1"},
  287. {"_: !foo '\"1\"'\n", "!foo", "\"1\""},
  288. {"_: !foo 1.1\n", "!foo", 1.1},
  289. {"_: !foo 1\n", "!foo", 1},
  290. {"_: !foo 1\n", "!foo", uint(1)},
  291. {"_: !foo true\n", "!foo", true},
  292. {"_: !foo\n- A\n- B\n", "!foo", []string{"A", "B"}},
  293. {"_: !foo\n A: B\n", "!foo", map[string]string{"A": "B"}},
  294. {"_: !foo\n a: B\n", "!foo", &marshalTaggedIfaceTest},
  295. }
  296. func (s *S) TestMarshalTypeCache(c *C) {
  297. var data []byte
  298. var err error
  299. func() {
  300. type T struct{ A int }
  301. data, err = yaml.Marshal(&T{})
  302. c.Assert(err, IsNil)
  303. }()
  304. func() {
  305. type T struct{ B int }
  306. data, err = yaml.Marshal(&T{})
  307. c.Assert(err, IsNil)
  308. }()
  309. c.Assert(string(data), Equals, "b: 0\n")
  310. }
  311. type typeWithGetter struct {
  312. tag string
  313. value interface{}
  314. }
  315. func (o typeWithGetter) GetYAML() (tag string, value interface{}) {
  316. return o.tag, o.value
  317. }
  318. type typeWithGetterField struct {
  319. Field typeWithGetter "_"
  320. }
  321. func (s *S) TestMashalWithGetter(c *C) {
  322. for _, item := range getterTests {
  323. obj := &typeWithGetterField{}
  324. obj.Field.tag = item.tag
  325. obj.Field.value = item.value
  326. data, err := yaml.Marshal(obj)
  327. c.Assert(err, IsNil)
  328. c.Assert(string(data), Equals, string(item.data))
  329. }
  330. }
  331. func (s *S) TestUnmarshalWholeDocumentWithGetter(c *C) {
  332. obj := &typeWithGetter{}
  333. obj.tag = ""
  334. obj.value = map[string]string{"hello": "world!"}
  335. data, err := yaml.Marshal(obj)
  336. c.Assert(err, IsNil)
  337. c.Assert(string(data), Equals, "hello: world!\n")
  338. }
  339. func (s *S) TestSortedOutput(c *C) {
  340. order := []interface{}{
  341. false,
  342. true,
  343. 1,
  344. uint(1),
  345. 1.0,
  346. 1.1,
  347. 1.2,
  348. 2,
  349. uint(2),
  350. 2.0,
  351. 2.1,
  352. "",
  353. ".1",
  354. ".2",
  355. ".a",
  356. "1",
  357. "2",
  358. "a!10",
  359. "a/2",
  360. "a/10",
  361. "a~10",
  362. "ab/1",
  363. "b/1",
  364. "b/01",
  365. "b/2",
  366. "b/02",
  367. "b/3",
  368. "b/03",
  369. "b1",
  370. "b01",
  371. "b3",
  372. "c2.10",
  373. "c10.2",
  374. "d1",
  375. "d12",
  376. "d12a",
  377. }
  378. m := make(map[interface{}]int)
  379. for _, k := range order {
  380. m[k] = 1
  381. }
  382. data, err := yaml.Marshal(m)
  383. c.Assert(err, IsNil)
  384. out := "\n" + string(data)
  385. last := 0
  386. for i, k := range order {
  387. repr := fmt.Sprint(k)
  388. if s, ok := k.(string); ok {
  389. if _, err = strconv.ParseFloat(repr, 32); s == "" || err == nil {
  390. repr = `"` + repr + `"`
  391. }
  392. }
  393. index := strings.Index(out, "\n"+repr+":")
  394. if index == -1 {
  395. c.Fatalf("%#v is not in the output: %#v", k, out)
  396. }
  397. if index < last {
  398. c.Fatalf("%#v was generated before %#v: %q", k, order[i-1], out)
  399. }
  400. last = index
  401. }
  402. }