decode_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. package goyaml_test
  2. import (
  3. . "launchpad.net/gocheck"
  4. "launchpad.net/goyaml"
  5. "math"
  6. "reflect"
  7. )
  8. var unmarshalIntTest = 123
  9. var unmarshalTests = []struct {
  10. data string
  11. value interface{}
  12. }{
  13. {
  14. "",
  15. &struct{}{},
  16. }, {
  17. "{}", &struct{}{},
  18. }, {
  19. "v: hi",
  20. map[string]string{"v": "hi"},
  21. }, {
  22. "v: hi", map[string]interface{}{"v": "hi"},
  23. }, {
  24. "v: true",
  25. map[string]string{"v": "true"},
  26. }, {
  27. "v: true",
  28. map[string]interface{}{"v": true},
  29. }, {
  30. "v: 10",
  31. map[string]interface{}{"v": 10},
  32. }, {
  33. "v: 0b10",
  34. map[string]interface{}{"v": 2},
  35. }, {
  36. "v: 0xA",
  37. map[string]interface{}{"v": 10},
  38. }, {
  39. "v: 4294967296",
  40. map[string]int64{"v": 4294967296},
  41. }, {
  42. "v: 0.1",
  43. map[string]interface{}{"v": 0.1},
  44. }, {
  45. "v: .1",
  46. map[string]interface{}{"v": 0.1},
  47. }, {
  48. "v: .Inf",
  49. map[string]interface{}{"v": math.Inf(+1)},
  50. }, {
  51. "v: -.Inf",
  52. map[string]interface{}{"v": math.Inf(-1)},
  53. }, {
  54. "v: -10",
  55. map[string]interface{}{"v": -10},
  56. }, {
  57. "v: -.1",
  58. map[string]interface{}{"v": -0.1},
  59. },
  60. // Simple values.
  61. {
  62. "123",
  63. &unmarshalIntTest,
  64. },
  65. // Floats from spec
  66. {
  67. "canonical: 6.8523e+5",
  68. map[string]interface{}{"canonical": 6.8523e+5},
  69. }, {
  70. "expo: 685.230_15e+03",
  71. map[string]interface{}{"expo": 685.23015e+03},
  72. }, {
  73. "fixed: 685_230.15",
  74. map[string]interface{}{"fixed": 685230.15},
  75. }, {
  76. "neginf: -.inf",
  77. map[string]interface{}{"neginf": math.Inf(-1)},
  78. }, {
  79. "fixed: 685_230.15",
  80. map[string]float64{"fixed": 685230.15},
  81. },
  82. //{"sexa: 190:20:30.15", map[string]interface{}{"sexa": 0}}, // Unsupported
  83. //{"notanum: .NaN", map[string]interface{}{"notanum": math.NaN()}}, // Equality of NaN fails.
  84. // Bools from spec
  85. {
  86. "canonical: y",
  87. map[string]interface{}{"canonical": true},
  88. }, {
  89. "answer: NO",
  90. map[string]interface{}{"answer": false},
  91. }, {
  92. "logical: True",
  93. map[string]interface{}{"logical": true},
  94. }, {
  95. "option: on",
  96. map[string]interface{}{"option": true},
  97. }, {
  98. "option: on",
  99. map[string]bool{"option": true},
  100. },
  101. // Ints from spec
  102. {
  103. "canonical: 685230",
  104. map[string]interface{}{"canonical": 685230},
  105. }, {
  106. "decimal: +685_230",
  107. map[string]interface{}{"decimal": 685230},
  108. }, {
  109. "octal: 02472256",
  110. map[string]interface{}{"octal": 685230},
  111. }, {
  112. "hexa: 0x_0A_74_AE",
  113. map[string]interface{}{"hexa": 685230},
  114. }, {
  115. "bin: 0b1010_0111_0100_1010_1110",
  116. map[string]interface{}{"bin": 685230},
  117. }, {
  118. "bin: -0b101010",
  119. map[string]interface{}{"bin": -42},
  120. }, {
  121. "decimal: +685_230",
  122. map[string]int{"decimal": 685230},
  123. },
  124. //{"sexa: 190:20:30", map[string]interface{}{"sexa": 0}}, // Unsupported
  125. // Nulls from spec
  126. {
  127. "empty:",
  128. map[string]interface{}{"empty": nil},
  129. }, {
  130. "canonical: ~",
  131. map[string]interface{}{"canonical": nil},
  132. }, {
  133. "english: null",
  134. map[string]interface{}{"english": nil},
  135. }, {
  136. "~: null key",
  137. map[interface{}]string{nil: "null key"},
  138. }, {
  139. "empty:",
  140. map[string]*bool{"empty": nil},
  141. },
  142. // Flow sequence
  143. {
  144. "seq: [A,B]",
  145. map[string]interface{}{"seq": []interface{}{"A", "B"}},
  146. }, {
  147. "seq: [A,B,C,]",
  148. map[string][]string{"seq": []string{"A", "B", "C"}},
  149. }, {
  150. "seq: [A,1,C]",
  151. map[string][]string{"seq": []string{"A", "1", "C"}},
  152. }, {
  153. "seq: [A,1,C]",
  154. map[string][]int{"seq": []int{1}},
  155. }, {
  156. "seq: [A,1,C]",
  157. map[string]interface{}{"seq": []interface{}{"A", 1, "C"}},
  158. },
  159. // Block sequence
  160. {
  161. "seq:\n - A\n - B",
  162. map[string]interface{}{"seq": []interface{}{"A", "B"}},
  163. }, {
  164. "seq:\n - A\n - B\n - C",
  165. map[string][]string{"seq": []string{"A", "B", "C"}},
  166. }, {
  167. "seq:\n - A\n - 1\n - C",
  168. map[string][]string{"seq": []string{"A", "1", "C"}},
  169. }, {
  170. "seq:\n - A\n - 1\n - C",
  171. map[string][]int{"seq": []int{1}},
  172. }, {
  173. "seq:\n - A\n - 1\n - C",
  174. map[string]interface{}{"seq": []interface{}{"A", 1, "C"}},
  175. },
  176. // Literal block scalar
  177. {
  178. "scalar: | # Comment\n\n literal\n\n \ttext\n\n",
  179. map[string]string{"scalar": "\nliteral\n\n\ttext\n"},
  180. },
  181. // Folded block scalar
  182. {
  183. "scalar: > # Comment\n\n folded\n line\n \n next\n line\n * one\n * two\n\n last\n line\n\n",
  184. map[string]string{"scalar": "\nfolded line\nnext line\n * one\n * two\n\nlast line\n"},
  185. },
  186. // Map inside interface with no type hints.
  187. {
  188. "a: {b: c}",
  189. map[string]interface{}{"a": map[interface{}]interface{}{"b": "c"}},
  190. },
  191. // Structs and type conversions.
  192. {
  193. "hello: world",
  194. &struct{ Hello string }{"world"},
  195. }, {
  196. "a: {b: c}",
  197. &struct{ A struct{ B string } }{struct{ B string }{"c"}},
  198. }, {
  199. "a: {b: c}",
  200. &struct{ A *struct{ B string } }{&struct{ B string }{"c"}},
  201. }, {
  202. "a: {b: c}",
  203. &struct{ A map[string]string }{map[string]string{"b": "c"}},
  204. }, {
  205. "a: {b: c}",
  206. &struct{ A *map[string]string }{&map[string]string{"b": "c"}},
  207. }, {
  208. "a:",
  209. &struct{ A map[string]string }{},
  210. }, {
  211. "a: 1",
  212. &struct{ A int }{1},
  213. }, {
  214. "a: [1, 2]",
  215. &struct{ A []int }{[]int{1, 2}},
  216. }, {
  217. "a: 1",
  218. &struct{ B int }{0},
  219. }, {
  220. "a: 1",
  221. &struct {
  222. B int "a"
  223. }{1},
  224. }, {
  225. "a: y",
  226. &struct{ A bool }{true},
  227. },
  228. // Some cross type conversions
  229. {
  230. "v: 42",
  231. map[string]uint{"v": 42},
  232. }, {
  233. "v: -42",
  234. map[string]uint{},
  235. }, {
  236. "v: 4294967296",
  237. map[string]uint64{"v": 4294967296},
  238. }, {
  239. "v: -4294967296",
  240. map[string]uint64{},
  241. },
  242. // Overflow cases.
  243. {
  244. "v: 4294967297",
  245. map[string]int32{},
  246. }, {
  247. "v: 128",
  248. map[string]int8{},
  249. },
  250. // Quoted values.
  251. {
  252. "'1': '\"2\"'",
  253. map[interface{}]interface{}{"1": "\"2\""},
  254. }, {
  255. "v:\n- A\n- 'B\n\n C'\n",
  256. map[string][]string{"v": []string{"A", "B\nC"}},
  257. },
  258. // Explicit tags.
  259. {
  260. "v: !!float '1.1'",
  261. map[string]interface{}{"v": 1.1},
  262. }, {
  263. "v: !!null ''",
  264. map[string]interface{}{"v": nil},
  265. }, {
  266. "%TAG !y! tag:yaml.org,2002:\n---\nv: !y!int '1'",
  267. map[string]interface{}{"v": 1},
  268. },
  269. // Anchors and aliases.
  270. {
  271. "a: &x 1\nb: &y 2\nc: *x\nd: *y\n",
  272. &struct{ A, B, C, D int }{1, 2, 1, 2},
  273. }, {
  274. "a: &a {c: 1}\nb: *a",
  275. &struct {
  276. A, B struct {
  277. C int
  278. }
  279. }{struct{ C int }{1}, struct{ C int }{1}},
  280. }, {
  281. "a: &a [1, 2]\nb: *a",
  282. &struct{ B []int }{[]int{1, 2}},
  283. },
  284. // Bug #1133337
  285. {
  286. "foo: ''",
  287. map[string]*string{"foo": new(string)},
  288. }, {
  289. "foo: null",
  290. map[string]string{},
  291. },
  292. // Ignored field
  293. {
  294. "a: 1\nb: 2\n",
  295. &struct {
  296. A int
  297. B int "-"
  298. }{1, 0},
  299. },
  300. // Bug #1191981
  301. {
  302. "" +
  303. "%YAML 1.1\n" +
  304. "--- !!str\n" +
  305. `"Generic line break (no glyph)\n\` + "\n" +
  306. ` Generic line break (glyphed)\n\` + "\n" +
  307. ` Line separator\u2028\` + "\n" +
  308. ` Paragraph separator\u2029"` + "\n",
  309. "" +
  310. "Generic line break (no glyph)\n" +
  311. "Generic line break (glyphed)\n" +
  312. "Line separator\u2028Paragraph separator\u2029",
  313. },
  314. // Struct inlining
  315. {
  316. "a: 1\nb: 2\nc: 3\n",
  317. &struct {
  318. A int
  319. C inlineB `yaml:",inline"`
  320. }{1, inlineB{2, inlineC{3}}},
  321. },
  322. }
  323. type inlineB struct {
  324. B int
  325. inlineC `yaml:",inline"`
  326. }
  327. type inlineC struct {
  328. C int
  329. }
  330. func (s *S) TestUnmarshal(c *C) {
  331. for i, item := range unmarshalTests {
  332. t := reflect.ValueOf(item.value).Type()
  333. var value interface{}
  334. switch t.Kind() {
  335. case reflect.Map:
  336. value = reflect.MakeMap(t).Interface()
  337. case reflect.String:
  338. t := reflect.ValueOf(item.value).Type()
  339. v := reflect.New(t)
  340. value = v.Interface()
  341. default:
  342. pt := reflect.ValueOf(item.value).Type()
  343. pv := reflect.New(pt.Elem())
  344. value = pv.Interface()
  345. }
  346. err := goyaml.Unmarshal([]byte(item.data), value)
  347. c.Assert(err, IsNil, Commentf("Item #%d", i))
  348. if t.Kind() == reflect.String {
  349. c.Assert(*value.(*string), Equals, item.value, Commentf("Item #%d", i))
  350. } else {
  351. c.Assert(value, DeepEquals, item.value, Commentf("Item #%d", i))
  352. }
  353. }
  354. }
  355. func (s *S) TestUnmarshalNaN(c *C) {
  356. value := map[string]interface{}{}
  357. err := goyaml.Unmarshal([]byte("notanum: .NaN"), &value)
  358. c.Assert(err, IsNil)
  359. c.Assert(math.IsNaN(value["notanum"].(float64)), Equals, true)
  360. }
  361. var unmarshalErrorTests = []struct {
  362. data, error string
  363. }{
  364. {"v: !!float 'error'", "YAML error: Can't decode !!str 'error' as a !!float"},
  365. {"v: [A,", "YAML error: line 1: did not find expected node content"},
  366. {"v:\n- [A,", "YAML error: line 2: did not find expected node content"},
  367. {"a: *b\n", "YAML error: Unknown anchor 'b' referenced"},
  368. {"a: &a\n b: *a\n", "YAML error: Anchor 'a' value contains itself"},
  369. }
  370. func (s *S) TestUnmarshalErrors(c *C) {
  371. for _, item := range unmarshalErrorTests {
  372. var value interface{}
  373. err := goyaml.Unmarshal([]byte(item.data), &value)
  374. c.Assert(err, ErrorMatches, item.error, Commentf("Partial unmarshal: %#v", value))
  375. }
  376. }
  377. var setterTests = []struct {
  378. data, tag string
  379. value interface{}
  380. }{
  381. {"_: {hi: there}", "!!map", map[interface{}]interface{}{"hi": "there"}},
  382. {"_: [1,A]", "!!seq", []interface{}{1, "A"}},
  383. {"_: 10", "!!int", 10},
  384. {"_: null", "!!null", nil},
  385. {"_: !!foo 'BAR!'", "!!foo", "BAR!"},
  386. }
  387. var setterResult = map[int]bool{}
  388. type typeWithSetter struct {
  389. tag string
  390. value interface{}
  391. }
  392. func (o *typeWithSetter) SetYAML(tag string, value interface{}) (ok bool) {
  393. o.tag = tag
  394. o.value = value
  395. if i, ok := value.(int); ok {
  396. if result, ok := setterResult[i]; ok {
  397. return result
  398. }
  399. }
  400. return true
  401. }
  402. type typeWithSetterField struct {
  403. Field *typeWithSetter "_"
  404. }
  405. func (s *S) TestUnmarshalWithSetter(c *C) {
  406. for _, item := range setterTests {
  407. obj := &typeWithSetterField{}
  408. err := goyaml.Unmarshal([]byte(item.data), obj)
  409. c.Assert(err, IsNil)
  410. c.Assert(obj.Field, NotNil,
  411. Commentf("Pointer not initialized (%#v)", item.value))
  412. c.Assert(obj.Field.tag, Equals, item.tag)
  413. c.Assert(obj.Field.value, DeepEquals, item.value)
  414. }
  415. }
  416. func (s *S) TestUnmarshalWholeDocumentWithSetter(c *C) {
  417. obj := &typeWithSetter{}
  418. err := goyaml.Unmarshal([]byte(setterTests[0].data), obj)
  419. c.Assert(err, IsNil)
  420. c.Assert(obj.tag, Equals, setterTests[0].tag)
  421. value, ok := obj.value.(map[interface{}]interface{})
  422. c.Assert(ok, Equals, true)
  423. c.Assert(value["_"], DeepEquals, setterTests[0].value)
  424. }
  425. func (s *S) TestUnmarshalWithFalseSetterIgnoresValue(c *C) {
  426. setterResult[2] = false
  427. setterResult[4] = false
  428. defer func() {
  429. delete(setterResult, 2)
  430. delete(setterResult, 4)
  431. }()
  432. m := map[string]*typeWithSetter{}
  433. data := "{abc: 1, def: 2, ghi: 3, jkl: 4}"
  434. err := goyaml.Unmarshal([]byte(data), m)
  435. c.Assert(err, IsNil)
  436. c.Assert(m["abc"], NotNil)
  437. c.Assert(m["def"], IsNil)
  438. c.Assert(m["ghi"], NotNil)
  439. c.Assert(m["jkl"], IsNil)
  440. c.Assert(m["abc"].value, Equals, 1)
  441. c.Assert(m["ghi"].value, Equals, 3)
  442. }
  443. //var data []byte
  444. //func init() {
  445. // var err error
  446. // data, err = ioutil.ReadFile("/tmp/file.yaml")
  447. // if err != nil {
  448. // panic(err)
  449. // }
  450. //}
  451. //
  452. //func (s *S) BenchmarkUnmarshal(c *C) {
  453. // var err error
  454. // for i := 0; i < c.N; i++ {
  455. // var v map[string]interface{}
  456. // err = goyaml.Unmarshal(data, &v)
  457. // }
  458. // if err != nil {
  459. // panic(err)
  460. // }
  461. //}
  462. //
  463. //func (s *S) BenchmarkMarshal(c *C) {
  464. // var v map[string]interface{}
  465. // goyaml.Unmarshal(data, &v)
  466. // c.ResetTimer()
  467. // for i := 0; i < c.N; i++ {
  468. // goyaml.Marshal(&v)
  469. // }
  470. //}