encode_test.go 8.6 KB

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