encode_test.go 6.4 KB

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