decode_test.go 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207
  1. package yaml_test
  2. import (
  3. "errors"
  4. "io"
  5. "math"
  6. "net"
  7. "reflect"
  8. "strings"
  9. "time"
  10. . "gopkg.in/check.v1"
  11. "gopkg.in/yaml.v2"
  12. )
  13. var unmarshalIntTest = 123
  14. var unmarshalTests = []struct {
  15. data string
  16. value interface{}
  17. }{
  18. {
  19. "",
  20. (*struct{})(nil),
  21. },
  22. {
  23. "{}", &struct{}{},
  24. }, {
  25. "v: hi",
  26. map[string]string{"v": "hi"},
  27. }, {
  28. "v: hi", map[string]interface{}{"v": "hi"},
  29. }, {
  30. "v: true",
  31. map[string]string{"v": "true"},
  32. }, {
  33. "v: true",
  34. map[string]interface{}{"v": true},
  35. }, {
  36. "v: 10",
  37. map[string]interface{}{"v": 10},
  38. }, {
  39. "v: 0b10",
  40. map[string]interface{}{"v": 2},
  41. }, {
  42. "v: 0xA",
  43. map[string]interface{}{"v": 10},
  44. }, {
  45. "v: 4294967296",
  46. map[string]int64{"v": 4294967296},
  47. }, {
  48. "v: 0.1",
  49. map[string]interface{}{"v": 0.1},
  50. }, {
  51. "v: .1",
  52. map[string]interface{}{"v": 0.1},
  53. }, {
  54. "v: .Inf",
  55. map[string]interface{}{"v": math.Inf(+1)},
  56. }, {
  57. "v: -.Inf",
  58. map[string]interface{}{"v": math.Inf(-1)},
  59. }, {
  60. "v: -10",
  61. map[string]interface{}{"v": -10},
  62. }, {
  63. "v: -.1",
  64. map[string]interface{}{"v": -0.1},
  65. },
  66. // Simple values.
  67. {
  68. "123",
  69. &unmarshalIntTest,
  70. },
  71. // Floats from spec
  72. {
  73. "canonical: 6.8523e+5",
  74. map[string]interface{}{"canonical": 6.8523e+5},
  75. }, {
  76. "expo: 685.230_15e+03",
  77. map[string]interface{}{"expo": 685.23015e+03},
  78. }, {
  79. "fixed: 685_230.15",
  80. map[string]interface{}{"fixed": 685230.15},
  81. }, {
  82. "neginf: -.inf",
  83. map[string]interface{}{"neginf": math.Inf(-1)},
  84. }, {
  85. "fixed: 685_230.15",
  86. map[string]float64{"fixed": 685230.15},
  87. },
  88. //{"sexa: 190:20:30.15", map[string]interface{}{"sexa": 0}}, // Unsupported
  89. //{"notanum: .NaN", map[string]interface{}{"notanum": math.NaN()}}, // Equality of NaN fails.
  90. // Bools from spec
  91. {
  92. "canonical: y",
  93. map[string]interface{}{"canonical": true},
  94. }, {
  95. "answer: NO",
  96. map[string]interface{}{"answer": false},
  97. }, {
  98. "logical: True",
  99. map[string]interface{}{"logical": true},
  100. }, {
  101. "option: on",
  102. map[string]interface{}{"option": true},
  103. }, {
  104. "option: on",
  105. map[string]bool{"option": true},
  106. },
  107. // Ints from spec
  108. {
  109. "canonical: 685230",
  110. map[string]interface{}{"canonical": 685230},
  111. }, {
  112. "decimal: +685_230",
  113. map[string]interface{}{"decimal": 685230},
  114. }, {
  115. "octal: 02472256",
  116. map[string]interface{}{"octal": 685230},
  117. }, {
  118. "hexa: 0x_0A_74_AE",
  119. map[string]interface{}{"hexa": 685230},
  120. }, {
  121. "bin: 0b1010_0111_0100_1010_1110",
  122. map[string]interface{}{"bin": 685230},
  123. }, {
  124. "bin: -0b101010",
  125. map[string]interface{}{"bin": -42},
  126. }, {
  127. "decimal: +685_230",
  128. map[string]int{"decimal": 685230},
  129. },
  130. //{"sexa: 190:20:30", map[string]interface{}{"sexa": 0}}, // Unsupported
  131. // Nulls from spec
  132. {
  133. "empty:",
  134. map[string]interface{}{"empty": nil},
  135. }, {
  136. "canonical: ~",
  137. map[string]interface{}{"canonical": nil},
  138. }, {
  139. "english: null",
  140. map[string]interface{}{"english": nil},
  141. }, {
  142. "~: null key",
  143. map[interface{}]string{nil: "null key"},
  144. }, {
  145. "empty:",
  146. map[string]*bool{"empty": nil},
  147. },
  148. // Flow sequence
  149. {
  150. "seq: [A,B]",
  151. map[string]interface{}{"seq": []interface{}{"A", "B"}},
  152. }, {
  153. "seq: [A,B,C,]",
  154. map[string][]string{"seq": []string{"A", "B", "C"}},
  155. }, {
  156. "seq: [A,1,C]",
  157. map[string][]string{"seq": []string{"A", "1", "C"}},
  158. }, {
  159. "seq: [A,1,C]",
  160. map[string][]int{"seq": []int{1}},
  161. }, {
  162. "seq: [A,1,C]",
  163. map[string]interface{}{"seq": []interface{}{"A", 1, "C"}},
  164. },
  165. // Block sequence
  166. {
  167. "seq:\n - A\n - B",
  168. map[string]interface{}{"seq": []interface{}{"A", "B"}},
  169. }, {
  170. "seq:\n - A\n - B\n - C",
  171. map[string][]string{"seq": []string{"A", "B", "C"}},
  172. }, {
  173. "seq:\n - A\n - 1\n - C",
  174. map[string][]string{"seq": []string{"A", "1", "C"}},
  175. }, {
  176. "seq:\n - A\n - 1\n - C",
  177. map[string][]int{"seq": []int{1}},
  178. }, {
  179. "seq:\n - A\n - 1\n - C",
  180. map[string]interface{}{"seq": []interface{}{"A", 1, "C"}},
  181. },
  182. // Literal block scalar
  183. {
  184. "scalar: | # Comment\n\n literal\n\n \ttext\n\n",
  185. map[string]string{"scalar": "\nliteral\n\n\ttext\n"},
  186. },
  187. // Folded block scalar
  188. {
  189. "scalar: > # Comment\n\n folded\n line\n \n next\n line\n * one\n * two\n\n last\n line\n\n",
  190. map[string]string{"scalar": "\nfolded line\nnext line\n * one\n * two\n\nlast line\n"},
  191. },
  192. // Map inside interface with no type hints.
  193. {
  194. "a: {b: c}",
  195. map[interface{}]interface{}{"a": map[interface{}]interface{}{"b": "c"}},
  196. },
  197. // Structs and type conversions.
  198. {
  199. "hello: world",
  200. &struct{ Hello string }{"world"},
  201. }, {
  202. "a: {b: c}",
  203. &struct{ A struct{ B string } }{struct{ B string }{"c"}},
  204. }, {
  205. "a: {b: c}",
  206. &struct{ A *struct{ B string } }{&struct{ B string }{"c"}},
  207. }, {
  208. "a: {b: c}",
  209. &struct{ A map[string]string }{map[string]string{"b": "c"}},
  210. }, {
  211. "a: {b: c}",
  212. &struct{ A *map[string]string }{&map[string]string{"b": "c"}},
  213. }, {
  214. "a:",
  215. &struct{ A map[string]string }{},
  216. }, {
  217. "a: 1",
  218. &struct{ A int }{1},
  219. }, {
  220. "a: 1",
  221. &struct{ A float64 }{1},
  222. }, {
  223. "a: 1.0",
  224. &struct{ A int }{1},
  225. }, {
  226. "a: 1.0",
  227. &struct{ A uint }{1},
  228. }, {
  229. "a: [1, 2]",
  230. &struct{ A []int }{[]int{1, 2}},
  231. }, {
  232. "a: 1",
  233. &struct{ B int }{0},
  234. }, {
  235. "a: 1",
  236. &struct {
  237. B int "a"
  238. }{1},
  239. }, {
  240. "a: y",
  241. &struct{ A bool }{true},
  242. },
  243. // Some cross type conversions
  244. {
  245. "v: 42",
  246. map[string]uint{"v": 42},
  247. }, {
  248. "v: -42",
  249. map[string]uint{},
  250. }, {
  251. "v: 4294967296",
  252. map[string]uint64{"v": 4294967296},
  253. }, {
  254. "v: -4294967296",
  255. map[string]uint64{},
  256. },
  257. // int
  258. {
  259. "int_max: 2147483647",
  260. map[string]int{"int_max": math.MaxInt32},
  261. },
  262. {
  263. "int_min: -2147483648",
  264. map[string]int{"int_min": math.MinInt32},
  265. },
  266. {
  267. "int_overflow: 9223372036854775808", // math.MaxInt64 + 1
  268. map[string]int{},
  269. },
  270. // int64
  271. {
  272. "int64_max: 9223372036854775807",
  273. map[string]int64{"int64_max": math.MaxInt64},
  274. },
  275. {
  276. "int64_max_base2: 0b111111111111111111111111111111111111111111111111111111111111111",
  277. map[string]int64{"int64_max_base2": math.MaxInt64},
  278. },
  279. {
  280. "int64_min: -9223372036854775808",
  281. map[string]int64{"int64_min": math.MinInt64},
  282. },
  283. {
  284. "int64_neg_base2: -0b111111111111111111111111111111111111111111111111111111111111111",
  285. map[string]int64{"int64_neg_base2": -math.MaxInt64},
  286. },
  287. {
  288. "int64_overflow: 9223372036854775808", // math.MaxInt64 + 1
  289. map[string]int64{},
  290. },
  291. // uint
  292. {
  293. "uint_min: 0",
  294. map[string]uint{"uint_min": 0},
  295. },
  296. {
  297. "uint_max: 4294967295",
  298. map[string]uint{"uint_max": math.MaxUint32},
  299. },
  300. {
  301. "uint_underflow: -1",
  302. map[string]uint{},
  303. },
  304. // uint64
  305. {
  306. "uint64_min: 0",
  307. map[string]uint{"uint64_min": 0},
  308. },
  309. {
  310. "uint64_max: 18446744073709551615",
  311. map[string]uint64{"uint64_max": math.MaxUint64},
  312. },
  313. {
  314. "uint64_max_base2: 0b1111111111111111111111111111111111111111111111111111111111111111",
  315. map[string]uint64{"uint64_max_base2": math.MaxUint64},
  316. },
  317. {
  318. "uint64_maxint64: 9223372036854775807",
  319. map[string]uint64{"uint64_maxint64": math.MaxInt64},
  320. },
  321. {
  322. "uint64_underflow: -1",
  323. map[string]uint64{},
  324. },
  325. // float32
  326. {
  327. "float32_max: 3.40282346638528859811704183484516925440e+38",
  328. map[string]float32{"float32_max": math.MaxFloat32},
  329. },
  330. {
  331. "float32_nonzero: 1.401298464324817070923729583289916131280e-45",
  332. map[string]float32{"float32_nonzero": math.SmallestNonzeroFloat32},
  333. },
  334. {
  335. "float32_maxuint64: 18446744073709551615",
  336. map[string]float32{"float32_maxuint64": float32(math.MaxUint64)},
  337. },
  338. {
  339. "float32_maxuint64+1: 18446744073709551616",
  340. map[string]float32{"float32_maxuint64+1": float32(math.MaxUint64 + 1)},
  341. },
  342. // float64
  343. {
  344. "float64_max: 1.797693134862315708145274237317043567981e+308",
  345. map[string]float64{"float64_max": math.MaxFloat64},
  346. },
  347. {
  348. "float64_nonzero: 4.940656458412465441765687928682213723651e-324",
  349. map[string]float64{"float64_nonzero": math.SmallestNonzeroFloat64},
  350. },
  351. {
  352. "float64_maxuint64: 18446744073709551615",
  353. map[string]float64{"float64_maxuint64": float64(math.MaxUint64)},
  354. },
  355. {
  356. "float64_maxuint64+1: 18446744073709551616",
  357. map[string]float64{"float64_maxuint64+1": float64(math.MaxUint64 + 1)},
  358. },
  359. // Overflow cases.
  360. {
  361. "v: 4294967297",
  362. map[string]int32{},
  363. }, {
  364. "v: 128",
  365. map[string]int8{},
  366. },
  367. // Quoted values.
  368. {
  369. "'1': '\"2\"'",
  370. map[interface{}]interface{}{"1": "\"2\""},
  371. }, {
  372. "v:\n- A\n- 'B\n\n C'\n",
  373. map[string][]string{"v": []string{"A", "B\nC"}},
  374. },
  375. // Explicit tags.
  376. {
  377. "v: !!float '1.1'",
  378. map[string]interface{}{"v": 1.1},
  379. }, {
  380. "v: !!null ''",
  381. map[string]interface{}{"v": nil},
  382. }, {
  383. "%TAG !y! tag:yaml.org,2002:\n---\nv: !y!int '1'",
  384. map[string]interface{}{"v": 1},
  385. },
  386. // Non-specific tag (Issue #75)
  387. {
  388. "v: ! test",
  389. map[string]interface{}{"v": "test"},
  390. },
  391. // Anchors and aliases.
  392. {
  393. "a: &x 1\nb: &y 2\nc: *x\nd: *y\n",
  394. &struct{ A, B, C, D int }{1, 2, 1, 2},
  395. }, {
  396. "a: &a {c: 1}\nb: *a",
  397. &struct {
  398. A, B struct {
  399. C int
  400. }
  401. }{struct{ C int }{1}, struct{ C int }{1}},
  402. }, {
  403. "a: &a [1, 2]\nb: *a",
  404. &struct{ B []int }{[]int{1, 2}},
  405. },
  406. // Bug #1133337
  407. {
  408. "foo: ''",
  409. map[string]*string{"foo": new(string)},
  410. }, {
  411. "foo: null",
  412. map[string]*string{"foo": nil},
  413. }, {
  414. "foo: null",
  415. map[string]string{"foo": ""},
  416. }, {
  417. "foo: null",
  418. map[string]interface{}{"foo": nil},
  419. },
  420. // Support for ~
  421. {
  422. "foo: ~",
  423. map[string]*string{"foo": nil},
  424. }, {
  425. "foo: ~",
  426. map[string]string{"foo": ""},
  427. }, {
  428. "foo: ~",
  429. map[string]interface{}{"foo": nil},
  430. },
  431. // Ignored field
  432. {
  433. "a: 1\nb: 2\n",
  434. &struct {
  435. A int
  436. B int "-"
  437. }{1, 0},
  438. },
  439. // Bug #1191981
  440. {
  441. "" +
  442. "%YAML 1.1\n" +
  443. "--- !!str\n" +
  444. `"Generic line break (no glyph)\n\` + "\n" +
  445. ` Generic line break (glyphed)\n\` + "\n" +
  446. ` Line separator\u2028\` + "\n" +
  447. ` Paragraph separator\u2029"` + "\n",
  448. "" +
  449. "Generic line break (no glyph)\n" +
  450. "Generic line break (glyphed)\n" +
  451. "Line separator\u2028Paragraph separator\u2029",
  452. },
  453. // Struct inlining
  454. {
  455. "a: 1\nb: 2\nc: 3\n",
  456. &struct {
  457. A int
  458. C inlineB `yaml:",inline"`
  459. }{1, inlineB{2, inlineC{3}}},
  460. },
  461. // Map inlining
  462. {
  463. "a: 1\nb: 2\nc: 3\n",
  464. &struct {
  465. A int
  466. C map[string]int `yaml:",inline"`
  467. }{1, map[string]int{"b": 2, "c": 3}},
  468. },
  469. // bug 1243827
  470. {
  471. "a: -b_c",
  472. map[string]interface{}{"a": "-b_c"},
  473. },
  474. {
  475. "a: +b_c",
  476. map[string]interface{}{"a": "+b_c"},
  477. },
  478. {
  479. "a: 50cent_of_dollar",
  480. map[string]interface{}{"a": "50cent_of_dollar"},
  481. },
  482. // issue #295 (allow scalars with colons in flow mappings and sequences)
  483. {
  484. "a: {b: https://github.com/go-yaml/yaml}",
  485. map[string]interface{}{"a": map[interface{}]interface{}{
  486. "b": "https://github.com/go-yaml/yaml",
  487. }},
  488. },
  489. {
  490. "a: [https://github.com/go-yaml/yaml]",
  491. map[string]interface{}{"a": []interface{}{"https://github.com/go-yaml/yaml"}},
  492. },
  493. // Duration
  494. {
  495. "a: 3s",
  496. map[string]time.Duration{"a": 3 * time.Second},
  497. },
  498. // Issue #24.
  499. {
  500. "a: <foo>",
  501. map[string]string{"a": "<foo>"},
  502. },
  503. // Base 60 floats are obsolete and unsupported.
  504. {
  505. "a: 1:1\n",
  506. map[string]string{"a": "1:1"},
  507. },
  508. // Binary data.
  509. {
  510. "a: !!binary gIGC\n",
  511. map[string]string{"a": "\x80\x81\x82"},
  512. }, {
  513. "a: !!binary |\n " + strings.Repeat("kJCQ", 17) + "kJ\n CQ\n",
  514. map[string]string{"a": strings.Repeat("\x90", 54)},
  515. }, {
  516. "a: !!binary |\n " + strings.Repeat("A", 70) + "\n ==\n",
  517. map[string]string{"a": strings.Repeat("\x00", 52)},
  518. },
  519. // Ordered maps.
  520. {
  521. "{b: 2, a: 1, d: 4, c: 3, sub: {e: 5}}",
  522. &yaml.MapSlice{{"b", 2}, {"a", 1}, {"d", 4}, {"c", 3}, {"sub", yaml.MapSlice{{"e", 5}}}},
  523. },
  524. // Issue #39.
  525. {
  526. "a:\n b:\n c: d\n",
  527. map[string]struct{ B interface{} }{"a": {map[interface{}]interface{}{"c": "d"}}},
  528. },
  529. // Custom map type.
  530. {
  531. "a: {b: c}",
  532. M{"a": M{"b": "c"}},
  533. },
  534. // Support encoding.TextUnmarshaler.
  535. {
  536. "a: 1.2.3.4\n",
  537. map[string]net.IP{"a": net.IPv4(1, 2, 3, 4)},
  538. },
  539. {
  540. "a: 2015-02-24T18:19:39Z\n",
  541. map[string]time.Time{"a": time.Unix(1424801979, 0).In(time.UTC)},
  542. },
  543. {
  544. "a: 2015-01-01",
  545. map[string]time.Time{"a": time.Unix(1420070400, 0)},
  546. },
  547. {
  548. "a: !!str 2015-01-01",
  549. map[string]string{"a": "2015-01-01"},
  550. },
  551. {
  552. "a: \"2015-01-01\"",
  553. map[string]interface{}{"a": "2015-01-01"},
  554. },
  555. // Encode empty lists as zero-length slices.
  556. {
  557. "a: []",
  558. &struct{ A []int }{[]int{}},
  559. },
  560. // UTF-16-LE
  561. {
  562. "\xff\xfe\xf1\x00o\x00\xf1\x00o\x00:\x00 \x00v\x00e\x00r\x00y\x00 \x00y\x00e\x00s\x00\n\x00",
  563. M{"ñoño": "very yes"},
  564. },
  565. // UTF-16-LE with surrogate.
  566. {
  567. "\xff\xfe\xf1\x00o\x00\xf1\x00o\x00:\x00 \x00v\x00e\x00r\x00y\x00 \x00y\x00e\x00s\x00 \x00=\xd8\xd4\xdf\n\x00",
  568. M{"ñoño": "very yes 🟔"},
  569. },
  570. // UTF-16-BE
  571. {
  572. "\xfe\xff\x00\xf1\x00o\x00\xf1\x00o\x00:\x00 \x00v\x00e\x00r\x00y\x00 \x00y\x00e\x00s\x00\n",
  573. M{"ñoño": "very yes"},
  574. },
  575. // UTF-16-BE with surrogate.
  576. {
  577. "\xfe\xff\x00\xf1\x00o\x00\xf1\x00o\x00:\x00 \x00v\x00e\x00r\x00y\x00 \x00y\x00e\x00s\x00 \xd8=\xdf\xd4\x00\n",
  578. M{"ñoño": "very yes 🟔"},
  579. },
  580. // YAML Float regex shouldn't match this
  581. {
  582. "a: 123456e1\n",
  583. M{"a": "123456e1"},
  584. }, {
  585. "a: 123456E1\n",
  586. M{"a": "123456E1"},
  587. },
  588. // yaml-test-suite 3GZX: Spec Example 7.1. Alias Nodes
  589. {
  590. "First occurrence: &anchor Foo\nSecond occurrence: *anchor\nOverride anchor: &anchor Bar\nReuse anchor: *anchor\n",
  591. map[interface{}]interface{}{
  592. "Reuse anchor": "Bar",
  593. "First occurrence": "Foo",
  594. "Second occurrence": "Foo",
  595. "Override anchor": "Bar",
  596. },
  597. },
  598. // Single document with garbage following it.
  599. {
  600. "---\nhello\n...\n}not yaml",
  601. "hello",
  602. },
  603. }
  604. type M map[interface{}]interface{}
  605. type inlineB struct {
  606. B int
  607. inlineC `yaml:",inline"`
  608. }
  609. type inlineC struct {
  610. C int
  611. }
  612. func (s *S) TestUnmarshal(c *C) {
  613. for i, item := range unmarshalTests {
  614. c.Logf("test %d: %q", i, item.data)
  615. t := reflect.ValueOf(item.value).Type()
  616. value := reflect.New(t)
  617. err := yaml.Unmarshal([]byte(item.data), value.Interface())
  618. if _, ok := err.(*yaml.TypeError); !ok {
  619. c.Assert(err, IsNil)
  620. }
  621. c.Assert(value.Elem().Interface(), DeepEquals, item.value)
  622. }
  623. }
  624. func (s *S) TestDecoderSingleDocument(c *C) {
  625. // Test that Decoder.Decode works as expected on
  626. // all the unmarshal tests.
  627. for i, item := range unmarshalTests {
  628. c.Logf("test %d: %q", i, item.data)
  629. if item.data == "" {
  630. // Behaviour differs when there's no YAML.
  631. continue
  632. }
  633. t := reflect.ValueOf(item.value).Type()
  634. value := reflect.New(t)
  635. err := yaml.NewDecoder(strings.NewReader(item.data)).Decode(value.Interface())
  636. if _, ok := err.(*yaml.TypeError); !ok {
  637. c.Assert(err, IsNil)
  638. }
  639. c.Assert(value.Elem().Interface(), DeepEquals, item.value)
  640. }
  641. }
  642. var decoderTests = []struct {
  643. data string
  644. values []interface{}
  645. }{{
  646. "",
  647. nil,
  648. }, {
  649. "a: b",
  650. []interface{}{
  651. map[interface{}]interface{}{"a": "b"},
  652. },
  653. }, {
  654. "---\na: b\n...\n",
  655. []interface{}{
  656. map[interface{}]interface{}{"a": "b"},
  657. },
  658. }, {
  659. "---\n'hello'\n...\n---\ngoodbye\n...\n",
  660. []interface{}{
  661. "hello",
  662. "goodbye",
  663. },
  664. }}
  665. func (s *S) TestDecoder(c *C) {
  666. for i, item := range decoderTests {
  667. c.Logf("test %d: %q", i, item.data)
  668. var values []interface{}
  669. dec := yaml.NewDecoder(strings.NewReader(item.data))
  670. for {
  671. var value interface{}
  672. err := dec.Decode(&value)
  673. if err == io.EOF {
  674. break
  675. }
  676. c.Assert(err, IsNil)
  677. values = append(values, value)
  678. }
  679. c.Assert(values, DeepEquals, item.values)
  680. }
  681. }
  682. type errReader struct{}
  683. func (errReader) Read([]byte) (int, error) {
  684. return 0, errors.New("some read error")
  685. }
  686. func (s *S) TestDecoderReadError(c *C) {
  687. err := yaml.NewDecoder(errReader{}).Decode(&struct{}{})
  688. c.Assert(err, ErrorMatches, `yaml: input error: some read error`)
  689. }
  690. func (s *S) TestUnmarshalNaN(c *C) {
  691. value := map[string]interface{}{}
  692. err := yaml.Unmarshal([]byte("notanum: .NaN"), &value)
  693. c.Assert(err, IsNil)
  694. c.Assert(math.IsNaN(value["notanum"].(float64)), Equals, true)
  695. }
  696. var unmarshalErrorTests = []struct {
  697. data, error string
  698. }{
  699. {"v: !!float 'error'", "yaml: cannot decode !!str `error` as a !!float"},
  700. {"v: [A,", "yaml: line 1: did not find expected node content"},
  701. {"v:\n- [A,", "yaml: line 2: did not find expected node content"},
  702. {"a: *b\n", "yaml: unknown anchor 'b' referenced"},
  703. {"a: &a\n b: *a\n", "yaml: anchor 'a' value contains itself"},
  704. {"value: -", "yaml: block sequence entries are not allowed in this context"},
  705. {"a: !!binary ==", "yaml: !!binary value contains invalid base64 data"},
  706. {"{[.]}", `yaml: invalid map key: \[\]interface \{\}\{"\."\}`},
  707. {"{{.}}", `yaml: invalid map key: map\[interface\ \{\}\]interface \{\}\{".":interface \{\}\(nil\)\}`},
  708. {"b: *a\na: &a {c: 1}", `yaml: unknown anchor 'a' referenced`},
  709. {"%TAG !%79! tag:yaml.org,2002:\n---\nv: !%79!int '1'", "yaml: did not find expected whitespace"},
  710. }
  711. func (s *S) TestUnmarshalErrors(c *C) {
  712. for i, item := range unmarshalErrorTests {
  713. c.Logf("test %d: %q", i, item.data)
  714. var value interface{}
  715. err := yaml.Unmarshal([]byte(item.data), &value)
  716. c.Assert(err, ErrorMatches, item.error, Commentf("Partial unmarshal: %#v", value))
  717. }
  718. }
  719. func (s *S) TestDecoderErrors(c *C) {
  720. for _, item := range unmarshalErrorTests {
  721. var value interface{}
  722. err := yaml.NewDecoder(strings.NewReader(item.data)).Decode(&value)
  723. c.Assert(err, ErrorMatches, item.error, Commentf("Partial unmarshal: %#v", value))
  724. }
  725. }
  726. var unmarshalerTests = []struct {
  727. data, tag string
  728. value interface{}
  729. }{
  730. {"_: {hi: there}", "!!map", map[interface{}]interface{}{"hi": "there"}},
  731. {"_: [1,A]", "!!seq", []interface{}{1, "A"}},
  732. {"_: 10", "!!int", 10},
  733. {"_: null", "!!null", nil},
  734. {`_: BAR!`, "!!str", "BAR!"},
  735. {`_: "BAR!"`, "!!str", "BAR!"},
  736. {"_: !!foo 'BAR!'", "!!foo", "BAR!"},
  737. {`_: ""`, "!!str", ""},
  738. }
  739. var unmarshalerResult = map[int]error{}
  740. type unmarshalerType struct {
  741. value interface{}
  742. }
  743. func (o *unmarshalerType) UnmarshalYAML(unmarshal func(v interface{}) error) error {
  744. if err := unmarshal(&o.value); err != nil {
  745. return err
  746. }
  747. if i, ok := o.value.(int); ok {
  748. if result, ok := unmarshalerResult[i]; ok {
  749. return result
  750. }
  751. }
  752. return nil
  753. }
  754. type unmarshalerPointer struct {
  755. Field *unmarshalerType "_"
  756. }
  757. type unmarshalerValue struct {
  758. Field unmarshalerType "_"
  759. }
  760. func (s *S) TestUnmarshalerPointerField(c *C) {
  761. for _, item := range unmarshalerTests {
  762. obj := &unmarshalerPointer{}
  763. err := yaml.Unmarshal([]byte(item.data), obj)
  764. c.Assert(err, IsNil)
  765. if item.value == nil {
  766. c.Assert(obj.Field, IsNil)
  767. } else {
  768. c.Assert(obj.Field, NotNil, Commentf("Pointer not initialized (%#v)", item.value))
  769. c.Assert(obj.Field.value, DeepEquals, item.value)
  770. }
  771. }
  772. }
  773. func (s *S) TestUnmarshalerValueField(c *C) {
  774. for _, item := range unmarshalerTests {
  775. obj := &unmarshalerValue{}
  776. err := yaml.Unmarshal([]byte(item.data), obj)
  777. c.Assert(err, IsNil)
  778. c.Assert(obj.Field, NotNil, Commentf("Pointer not initialized (%#v)", item.value))
  779. c.Assert(obj.Field.value, DeepEquals, item.value)
  780. }
  781. }
  782. func (s *S) TestUnmarshalerWholeDocument(c *C) {
  783. obj := &unmarshalerType{}
  784. err := yaml.Unmarshal([]byte(unmarshalerTests[0].data), obj)
  785. c.Assert(err, IsNil)
  786. value, ok := obj.value.(map[interface{}]interface{})
  787. c.Assert(ok, Equals, true, Commentf("value: %#v", obj.value))
  788. c.Assert(value["_"], DeepEquals, unmarshalerTests[0].value)
  789. }
  790. func (s *S) TestUnmarshalerTypeError(c *C) {
  791. unmarshalerResult[2] = &yaml.TypeError{[]string{"foo"}}
  792. unmarshalerResult[4] = &yaml.TypeError{[]string{"bar"}}
  793. defer func() {
  794. delete(unmarshalerResult, 2)
  795. delete(unmarshalerResult, 4)
  796. }()
  797. type T struct {
  798. Before int
  799. After int
  800. M map[string]*unmarshalerType
  801. }
  802. var v T
  803. data := `{before: A, m: {abc: 1, def: 2, ghi: 3, jkl: 4}, after: B}`
  804. err := yaml.Unmarshal([]byte(data), &v)
  805. c.Assert(err, ErrorMatches, ""+
  806. "yaml: unmarshal errors:\n"+
  807. " line 1: cannot unmarshal !!str `A` into int\n"+
  808. " foo\n"+
  809. " bar\n"+
  810. " line 1: cannot unmarshal !!str `B` into int")
  811. c.Assert(v.M["abc"], NotNil)
  812. c.Assert(v.M["def"], IsNil)
  813. c.Assert(v.M["ghi"], NotNil)
  814. c.Assert(v.M["jkl"], IsNil)
  815. c.Assert(v.M["abc"].value, Equals, 1)
  816. c.Assert(v.M["ghi"].value, Equals, 3)
  817. }
  818. type proxyTypeError struct{}
  819. func (v *proxyTypeError) UnmarshalYAML(unmarshal func(interface{}) error) error {
  820. var s string
  821. var a int32
  822. var b int64
  823. if err := unmarshal(&s); err != nil {
  824. panic(err)
  825. }
  826. if s == "a" {
  827. if err := unmarshal(&b); err == nil {
  828. panic("should have failed")
  829. }
  830. return unmarshal(&a)
  831. }
  832. if err := unmarshal(&a); err == nil {
  833. panic("should have failed")
  834. }
  835. return unmarshal(&b)
  836. }
  837. func (s *S) TestUnmarshalerTypeErrorProxying(c *C) {
  838. type T struct {
  839. Before int
  840. After int
  841. M map[string]*proxyTypeError
  842. }
  843. var v T
  844. data := `{before: A, m: {abc: a, def: b}, after: B}`
  845. err := yaml.Unmarshal([]byte(data), &v)
  846. c.Assert(err, ErrorMatches, ""+
  847. "yaml: unmarshal errors:\n"+
  848. " line 1: cannot unmarshal !!str `A` into int\n"+
  849. " line 1: cannot unmarshal !!str `a` into int32\n"+
  850. " line 1: cannot unmarshal !!str `b` into int64\n"+
  851. " line 1: cannot unmarshal !!str `B` into int")
  852. }
  853. type failingUnmarshaler struct{}
  854. var failingErr = errors.New("failingErr")
  855. func (ft *failingUnmarshaler) UnmarshalYAML(unmarshal func(interface{}) error) error {
  856. return failingErr
  857. }
  858. func (s *S) TestUnmarshalerError(c *C) {
  859. err := yaml.Unmarshal([]byte("a: b"), &failingUnmarshaler{})
  860. c.Assert(err, Equals, failingErr)
  861. }
  862. type sliceUnmarshaler []int
  863. func (su *sliceUnmarshaler) UnmarshalYAML(unmarshal func(interface{}) error) error {
  864. var slice []int
  865. err := unmarshal(&slice)
  866. if err == nil {
  867. *su = slice
  868. return nil
  869. }
  870. var intVal int
  871. err = unmarshal(&intVal)
  872. if err == nil {
  873. *su = []int{intVal}
  874. return nil
  875. }
  876. return err
  877. }
  878. func (s *S) TestUnmarshalerRetry(c *C) {
  879. var su sliceUnmarshaler
  880. err := yaml.Unmarshal([]byte("[1, 2, 3]"), &su)
  881. c.Assert(err, IsNil)
  882. c.Assert(su, DeepEquals, sliceUnmarshaler([]int{1, 2, 3}))
  883. err = yaml.Unmarshal([]byte("1"), &su)
  884. c.Assert(err, IsNil)
  885. c.Assert(su, DeepEquals, sliceUnmarshaler([]int{1}))
  886. }
  887. // From http://yaml.org/type/merge.html
  888. var mergeTests = `
  889. anchors:
  890. list:
  891. - &CENTER { "x": 1, "y": 2 }
  892. - &LEFT { "x": 0, "y": 2 }
  893. - &BIG { "r": 10 }
  894. - &SMALL { "r": 1 }
  895. # All the following maps are equal:
  896. plain:
  897. # Explicit keys
  898. "x": 1
  899. "y": 2
  900. "r": 10
  901. label: center/big
  902. mergeOne:
  903. # Merge one map
  904. << : *CENTER
  905. "r": 10
  906. label: center/big
  907. mergeMultiple:
  908. # Merge multiple maps
  909. << : [ *CENTER, *BIG ]
  910. label: center/big
  911. override:
  912. # Override
  913. << : [ *BIG, *LEFT, *SMALL ]
  914. "x": 1
  915. label: center/big
  916. shortTag:
  917. # Explicit short merge tag
  918. !!merge "<<" : [ *CENTER, *BIG ]
  919. label: center/big
  920. longTag:
  921. # Explicit merge long tag
  922. !<tag:yaml.org,2002:merge> "<<" : [ *CENTER, *BIG ]
  923. label: center/big
  924. inlineMap:
  925. # Inlined map
  926. << : {"x": 1, "y": 2, "r": 10}
  927. label: center/big
  928. inlineSequenceMap:
  929. # Inlined map in sequence
  930. << : [ *CENTER, {"r": 10} ]
  931. label: center/big
  932. `
  933. func (s *S) TestMerge(c *C) {
  934. var want = map[interface{}]interface{}{
  935. "x": 1,
  936. "y": 2,
  937. "r": 10,
  938. "label": "center/big",
  939. }
  940. var m map[interface{}]interface{}
  941. err := yaml.Unmarshal([]byte(mergeTests), &m)
  942. c.Assert(err, IsNil)
  943. for name, test := range m {
  944. if name == "anchors" {
  945. continue
  946. }
  947. c.Assert(test, DeepEquals, want, Commentf("test %q failed", name))
  948. }
  949. }
  950. func (s *S) TestMergeStruct(c *C) {
  951. type Data struct {
  952. X, Y, R int
  953. Label string
  954. }
  955. want := Data{1, 2, 10, "center/big"}
  956. var m map[string]Data
  957. err := yaml.Unmarshal([]byte(mergeTests), &m)
  958. c.Assert(err, IsNil)
  959. for name, test := range m {
  960. if name == "anchors" {
  961. continue
  962. }
  963. c.Assert(test, Equals, want, Commentf("test %q failed", name))
  964. }
  965. }
  966. var unmarshalNullTests = []func() interface{}{
  967. func() interface{} { var v interface{}; v = "v"; return &v },
  968. func() interface{} { var s = "s"; return &s },
  969. func() interface{} { var s = "s"; sptr := &s; return &sptr },
  970. func() interface{} { var i = 1; return &i },
  971. func() interface{} { var i = 1; iptr := &i; return &iptr },
  972. func() interface{} { m := map[string]int{"s": 1}; return &m },
  973. func() interface{} { m := map[string]int{"s": 1}; return m },
  974. }
  975. func (s *S) TestUnmarshalNull(c *C) {
  976. for _, test := range unmarshalNullTests {
  977. item := test()
  978. zero := reflect.Zero(reflect.TypeOf(item).Elem()).Interface()
  979. err := yaml.Unmarshal([]byte("null"), item)
  980. c.Assert(err, IsNil)
  981. if reflect.TypeOf(item).Kind() == reflect.Map {
  982. c.Assert(reflect.ValueOf(item).Interface(), DeepEquals, reflect.MakeMap(reflect.TypeOf(item)).Interface())
  983. } else {
  984. c.Assert(reflect.ValueOf(item).Elem().Interface(), DeepEquals, zero)
  985. }
  986. }
  987. }
  988. func (s *S) TestUnmarshalSliceOnPreset(c *C) {
  989. // Issue #48.
  990. v := struct{ A []int }{[]int{1}}
  991. yaml.Unmarshal([]byte("a: [2]"), &v)
  992. c.Assert(v.A, DeepEquals, []int{2})
  993. }
  994. var unmarshalStrictTests = []struct {
  995. data string
  996. value interface{}
  997. error string
  998. }{{
  999. data: "a: 1\nc: 2\n",
  1000. value: struct{ A, B int }{A: 1},
  1001. error: `yaml: unmarshal errors:\n line 2: field c not found in type struct { A int; B int }`,
  1002. }, {
  1003. data: "a: 1\nb: 2\na: 3\n",
  1004. value: struct{ A, B int }{A: 3, B: 2},
  1005. error: `yaml: unmarshal errors:\n line 3: field a already set in type struct { A int; B int }`,
  1006. }, {
  1007. data: "c: 3\na: 1\nb: 2\nc: 4\n",
  1008. value: struct {
  1009. A int
  1010. inlineB `yaml:",inline"`
  1011. }{
  1012. A: 1,
  1013. inlineB: inlineB{
  1014. B: 2,
  1015. inlineC: inlineC{
  1016. C: 4,
  1017. },
  1018. },
  1019. },
  1020. error: `yaml: unmarshal errors:\n line 4: field c already set in type struct { A int; yaml_test.inlineB "yaml:\\",inline\\"" }`,
  1021. }, {
  1022. data: "c: 0\na: 1\nb: 2\nc: 1\n",
  1023. value: struct {
  1024. A int
  1025. inlineB `yaml:",inline"`
  1026. }{
  1027. A: 1,
  1028. inlineB: inlineB{
  1029. B: 2,
  1030. inlineC: inlineC{
  1031. C: 1,
  1032. },
  1033. },
  1034. },
  1035. error: `yaml: unmarshal errors:\n line 4: field c already set in type struct { A int; yaml_test.inlineB "yaml:\\",inline\\"" }`,
  1036. }, {
  1037. data: "c: 1\na: 1\nb: 2\nc: 3\n",
  1038. value: struct {
  1039. A int
  1040. M map[string]interface{} `yaml:",inline"`
  1041. }{
  1042. A: 1,
  1043. M: map[string]interface{}{
  1044. "b": 2,
  1045. "c": 3,
  1046. },
  1047. },
  1048. error: `yaml: unmarshal errors:\n line 4: key "c" already set in map`,
  1049. }, {
  1050. data: "a: 1\n9: 2\nnull: 3\n9: 4",
  1051. value: map[interface{}]interface{}{
  1052. "a": 1,
  1053. nil: 3,
  1054. 9: 4,
  1055. },
  1056. error: `yaml: unmarshal errors:\n line 4: key 9 already set in map`,
  1057. }}
  1058. func (s *S) TestUnmarshalStrict(c *C) {
  1059. for i, item := range unmarshalStrictTests {
  1060. c.Logf("test %d: %q", i, item.data)
  1061. // First test that normal Unmarshal unmarshals to the expected value.
  1062. t := reflect.ValueOf(item.value).Type()
  1063. value := reflect.New(t)
  1064. err := yaml.Unmarshal([]byte(item.data), value.Interface())
  1065. c.Assert(err, Equals, nil)
  1066. c.Assert(value.Elem().Interface(), DeepEquals, item.value)
  1067. // Then test that UnmarshalStrict fails on the same thing.
  1068. t = reflect.ValueOf(item.value).Type()
  1069. value = reflect.New(t)
  1070. err = yaml.UnmarshalStrict([]byte(item.data), value.Interface())
  1071. c.Assert(err, ErrorMatches, item.error)
  1072. }
  1073. }
  1074. //var data []byte
  1075. //func init() {
  1076. // var err error
  1077. // data, err = ioutil.ReadFile("/tmp/file.yaml")
  1078. // if err != nil {
  1079. // panic(err)
  1080. // }
  1081. //}
  1082. //
  1083. //func (s *S) BenchmarkUnmarshal(c *C) {
  1084. // var err error
  1085. // for i := 0; i < c.N; i++ {
  1086. // var v map[string]interface{}
  1087. // err = yaml.Unmarshal(data, &v)
  1088. // }
  1089. // if err != nil {
  1090. // panic(err)
  1091. // }
  1092. //}
  1093. //
  1094. //func (s *S) BenchmarkMarshal(c *C) {
  1095. // var v map[string]interface{}
  1096. // yaml.Unmarshal(data, &v)
  1097. // c.ResetTimer()
  1098. // for i := 0; i < c.N; i++ {
  1099. // yaml.Marshal(&v)
  1100. // }
  1101. //}