encode_test.go 6.8 KB

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