decode_test.go 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195
  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. // Encode empty lists as zero-length slices.
  544. {
  545. "a: []",
  546. &struct{ A []int }{[]int{}},
  547. },
  548. // UTF-16-LE
  549. {
  550. "\xff\xfe\xf1\x00o\x00\xf1\x00o\x00:\x00 \x00v\x00e\x00r\x00y\x00 \x00y\x00e\x00s\x00\n\x00",
  551. M{"ñoño": "very yes"},
  552. },
  553. // UTF-16-LE with surrogate.
  554. {
  555. "\xff\xfe\xf1\x00o\x00\xf1\x00o\x00:\x00 \x00v\x00e\x00r\x00y\x00 \x00y\x00e\x00s\x00 \x00=\xd8\xd4\xdf\n\x00",
  556. M{"ñoño": "very yes 🟔"},
  557. },
  558. // UTF-16-BE
  559. {
  560. "\xfe\xff\x00\xf1\x00o\x00\xf1\x00o\x00:\x00 \x00v\x00e\x00r\x00y\x00 \x00y\x00e\x00s\x00\n",
  561. M{"ñoño": "very yes"},
  562. },
  563. // UTF-16-BE with surrogate.
  564. {
  565. "\xfe\xff\x00\xf1\x00o\x00\xf1\x00o\x00:\x00 \x00v\x00e\x00r\x00y\x00 \x00y\x00e\x00s\x00 \xd8=\xdf\xd4\x00\n",
  566. M{"ñoño": "very yes 🟔"},
  567. },
  568. // YAML Float regex shouldn't match this
  569. {
  570. "a: 123456e1\n",
  571. M{"a": "123456e1"},
  572. }, {
  573. "a: 123456E1\n",
  574. M{"a": "123456E1"},
  575. },
  576. // yaml-test-suite 3GZX: Spec Example 7.1. Alias Nodes
  577. {
  578. "First occurrence: &anchor Foo\nSecond occurrence: *anchor\nOverride anchor: &anchor Bar\nReuse anchor: *anchor\n",
  579. map[interface{}]interface{}{
  580. "Reuse anchor": "Bar",
  581. "First occurrence": "Foo",
  582. "Second occurrence": "Foo",
  583. "Override anchor": "Bar",
  584. },
  585. },
  586. // Single document with garbage following it.
  587. {
  588. "---\nhello\n...\n}not yaml",
  589. "hello",
  590. },
  591. }
  592. type M map[interface{}]interface{}
  593. type inlineB struct {
  594. B int
  595. inlineC `yaml:",inline"`
  596. }
  597. type inlineC struct {
  598. C int
  599. }
  600. func (s *S) TestUnmarshal(c *C) {
  601. for i, item := range unmarshalTests {
  602. c.Logf("test %d: %q", i, item.data)
  603. t := reflect.ValueOf(item.value).Type()
  604. value := reflect.New(t)
  605. err := yaml.Unmarshal([]byte(item.data), value.Interface())
  606. if _, ok := err.(*yaml.TypeError); !ok {
  607. c.Assert(err, IsNil)
  608. }
  609. c.Assert(value.Elem().Interface(), DeepEquals, item.value)
  610. }
  611. }
  612. func (s *S) TestDecoderSingleDocument(c *C) {
  613. // Test that Decoder.Decode works as expected on
  614. // all the unmarshal tests.
  615. for i, item := range unmarshalTests {
  616. c.Logf("test %d: %q", i, item.data)
  617. if item.data == "" {
  618. // Behaviour differs when there's no YAML.
  619. continue
  620. }
  621. t := reflect.ValueOf(item.value).Type()
  622. value := reflect.New(t)
  623. err := yaml.NewDecoder(strings.NewReader(item.data)).Decode(value.Interface())
  624. if _, ok := err.(*yaml.TypeError); !ok {
  625. c.Assert(err, IsNil)
  626. }
  627. c.Assert(value.Elem().Interface(), DeepEquals, item.value)
  628. }
  629. }
  630. var decoderTests = []struct {
  631. data string
  632. values []interface{}
  633. }{{
  634. "",
  635. nil,
  636. }, {
  637. "a: b",
  638. []interface{}{
  639. map[interface{}]interface{}{"a": "b"},
  640. },
  641. }, {
  642. "---\na: b\n...\n",
  643. []interface{}{
  644. map[interface{}]interface{}{"a": "b"},
  645. },
  646. }, {
  647. "---\n'hello'\n...\n---\ngoodbye\n...\n",
  648. []interface{}{
  649. "hello",
  650. "goodbye",
  651. },
  652. }}
  653. func (s *S) TestDecoder(c *C) {
  654. for i, item := range decoderTests {
  655. c.Logf("test %d: %q", i, item.data)
  656. var values []interface{}
  657. dec := yaml.NewDecoder(strings.NewReader(item.data))
  658. for {
  659. var value interface{}
  660. err := dec.Decode(&value)
  661. if err == io.EOF {
  662. break
  663. }
  664. c.Assert(err, IsNil)
  665. values = append(values, value)
  666. }
  667. c.Assert(values, DeepEquals, item.values)
  668. }
  669. }
  670. type errReader struct{}
  671. func (errReader) Read([]byte) (int, error) {
  672. return 0, errors.New("some read error")
  673. }
  674. func (s *S) TestDecoderReadError(c *C) {
  675. err := yaml.NewDecoder(errReader{}).Decode(&struct{}{})
  676. c.Assert(err, ErrorMatches, `yaml: input error: some read error`)
  677. }
  678. func (s *S) TestUnmarshalNaN(c *C) {
  679. value := map[string]interface{}{}
  680. err := yaml.Unmarshal([]byte("notanum: .NaN"), &value)
  681. c.Assert(err, IsNil)
  682. c.Assert(math.IsNaN(value["notanum"].(float64)), Equals, true)
  683. }
  684. var unmarshalErrorTests = []struct {
  685. data, error string
  686. }{
  687. {"v: !!float 'error'", "yaml: cannot decode !!str `error` as a !!float"},
  688. {"v: [A,", "yaml: line 1: did not find expected node content"},
  689. {"v:\n- [A,", "yaml: line 2: did not find expected node content"},
  690. {"a: *b\n", "yaml: unknown anchor 'b' referenced"},
  691. {"a: &a\n b: *a\n", "yaml: anchor 'a' value contains itself"},
  692. {"value: -", "yaml: block sequence entries are not allowed in this context"},
  693. {"a: !!binary ==", "yaml: !!binary value contains invalid base64 data"},
  694. {"{[.]}", `yaml: invalid map key: \[\]interface \{\}\{"\."\}`},
  695. {"{{.}}", `yaml: invalid map key: map\[interface\ \{\}\]interface \{\}\{".":interface \{\}\(nil\)\}`},
  696. {"b: *a\na: &a {c: 1}", `yaml: unknown anchor 'a' referenced`},
  697. {"%TAG !%79! tag:yaml.org,2002:\n---\nv: !%79!int '1'", "yaml: did not find expected whitespace"},
  698. }
  699. func (s *S) TestUnmarshalErrors(c *C) {
  700. for i, item := range unmarshalErrorTests {
  701. c.Logf("test %d: %q", i, item.data)
  702. var value interface{}
  703. err := yaml.Unmarshal([]byte(item.data), &value)
  704. c.Assert(err, ErrorMatches, item.error, Commentf("Partial unmarshal: %#v", value))
  705. }
  706. }
  707. func (s *S) TestDecoderErrors(c *C) {
  708. for _, item := range unmarshalErrorTests {
  709. var value interface{}
  710. err := yaml.NewDecoder(strings.NewReader(item.data)).Decode(&value)
  711. c.Assert(err, ErrorMatches, item.error, Commentf("Partial unmarshal: %#v", value))
  712. }
  713. }
  714. var unmarshalerTests = []struct {
  715. data, tag string
  716. value interface{}
  717. }{
  718. {"_: {hi: there}", "!!map", map[interface{}]interface{}{"hi": "there"}},
  719. {"_: [1,A]", "!!seq", []interface{}{1, "A"}},
  720. {"_: 10", "!!int", 10},
  721. {"_: null", "!!null", nil},
  722. {`_: BAR!`, "!!str", "BAR!"},
  723. {`_: "BAR!"`, "!!str", "BAR!"},
  724. {"_: !!foo 'BAR!'", "!!foo", "BAR!"},
  725. {`_: ""`, "!!str", ""},
  726. }
  727. var unmarshalerResult = map[int]error{}
  728. type unmarshalerType struct {
  729. value interface{}
  730. }
  731. func (o *unmarshalerType) UnmarshalYAML(unmarshal func(v interface{}) error) error {
  732. if err := unmarshal(&o.value); err != nil {
  733. return err
  734. }
  735. if i, ok := o.value.(int); ok {
  736. if result, ok := unmarshalerResult[i]; ok {
  737. return result
  738. }
  739. }
  740. return nil
  741. }
  742. type unmarshalerPointer struct {
  743. Field *unmarshalerType "_"
  744. }
  745. type unmarshalerValue struct {
  746. Field unmarshalerType "_"
  747. }
  748. func (s *S) TestUnmarshalerPointerField(c *C) {
  749. for _, item := range unmarshalerTests {
  750. obj := &unmarshalerPointer{}
  751. err := yaml.Unmarshal([]byte(item.data), obj)
  752. c.Assert(err, IsNil)
  753. if item.value == nil {
  754. c.Assert(obj.Field, IsNil)
  755. } else {
  756. c.Assert(obj.Field, NotNil, Commentf("Pointer not initialized (%#v)", item.value))
  757. c.Assert(obj.Field.value, DeepEquals, item.value)
  758. }
  759. }
  760. }
  761. func (s *S) TestUnmarshalerValueField(c *C) {
  762. for _, item := range unmarshalerTests {
  763. obj := &unmarshalerValue{}
  764. err := yaml.Unmarshal([]byte(item.data), obj)
  765. c.Assert(err, IsNil)
  766. c.Assert(obj.Field, NotNil, Commentf("Pointer not initialized (%#v)", item.value))
  767. c.Assert(obj.Field.value, DeepEquals, item.value)
  768. }
  769. }
  770. func (s *S) TestUnmarshalerWholeDocument(c *C) {
  771. obj := &unmarshalerType{}
  772. err := yaml.Unmarshal([]byte(unmarshalerTests[0].data), obj)
  773. c.Assert(err, IsNil)
  774. value, ok := obj.value.(map[interface{}]interface{})
  775. c.Assert(ok, Equals, true, Commentf("value: %#v", obj.value))
  776. c.Assert(value["_"], DeepEquals, unmarshalerTests[0].value)
  777. }
  778. func (s *S) TestUnmarshalerTypeError(c *C) {
  779. unmarshalerResult[2] = &yaml.TypeError{[]string{"foo"}}
  780. unmarshalerResult[4] = &yaml.TypeError{[]string{"bar"}}
  781. defer func() {
  782. delete(unmarshalerResult, 2)
  783. delete(unmarshalerResult, 4)
  784. }()
  785. type T struct {
  786. Before int
  787. After int
  788. M map[string]*unmarshalerType
  789. }
  790. var v T
  791. data := `{before: A, m: {abc: 1, def: 2, ghi: 3, jkl: 4}, after: B}`
  792. err := yaml.Unmarshal([]byte(data), &v)
  793. c.Assert(err, ErrorMatches, ""+
  794. "yaml: unmarshal errors:\n"+
  795. " line 1: cannot unmarshal !!str `A` into int\n"+
  796. " foo\n"+
  797. " bar\n"+
  798. " line 1: cannot unmarshal !!str `B` into int")
  799. c.Assert(v.M["abc"], NotNil)
  800. c.Assert(v.M["def"], IsNil)
  801. c.Assert(v.M["ghi"], NotNil)
  802. c.Assert(v.M["jkl"], IsNil)
  803. c.Assert(v.M["abc"].value, Equals, 1)
  804. c.Assert(v.M["ghi"].value, Equals, 3)
  805. }
  806. type proxyTypeError struct{}
  807. func (v *proxyTypeError) UnmarshalYAML(unmarshal func(interface{}) error) error {
  808. var s string
  809. var a int32
  810. var b int64
  811. if err := unmarshal(&s); err != nil {
  812. panic(err)
  813. }
  814. if s == "a" {
  815. if err := unmarshal(&b); err == nil {
  816. panic("should have failed")
  817. }
  818. return unmarshal(&a)
  819. }
  820. if err := unmarshal(&a); err == nil {
  821. panic("should have failed")
  822. }
  823. return unmarshal(&b)
  824. }
  825. func (s *S) TestUnmarshalerTypeErrorProxying(c *C) {
  826. type T struct {
  827. Before int
  828. After int
  829. M map[string]*proxyTypeError
  830. }
  831. var v T
  832. data := `{before: A, m: {abc: a, def: b}, after: B}`
  833. err := yaml.Unmarshal([]byte(data), &v)
  834. c.Assert(err, ErrorMatches, ""+
  835. "yaml: unmarshal errors:\n"+
  836. " line 1: cannot unmarshal !!str `A` into int\n"+
  837. " line 1: cannot unmarshal !!str `a` into int32\n"+
  838. " line 1: cannot unmarshal !!str `b` into int64\n"+
  839. " line 1: cannot unmarshal !!str `B` into int")
  840. }
  841. type failingUnmarshaler struct{}
  842. var failingErr = errors.New("failingErr")
  843. func (ft *failingUnmarshaler) UnmarshalYAML(unmarshal func(interface{}) error) error {
  844. return failingErr
  845. }
  846. func (s *S) TestUnmarshalerError(c *C) {
  847. err := yaml.Unmarshal([]byte("a: b"), &failingUnmarshaler{})
  848. c.Assert(err, Equals, failingErr)
  849. }
  850. type sliceUnmarshaler []int
  851. func (su *sliceUnmarshaler) UnmarshalYAML(unmarshal func(interface{}) error) error {
  852. var slice []int
  853. err := unmarshal(&slice)
  854. if err == nil {
  855. *su = slice
  856. return nil
  857. }
  858. var intVal int
  859. err = unmarshal(&intVal)
  860. if err == nil {
  861. *su = []int{intVal}
  862. return nil
  863. }
  864. return err
  865. }
  866. func (s *S) TestUnmarshalerRetry(c *C) {
  867. var su sliceUnmarshaler
  868. err := yaml.Unmarshal([]byte("[1, 2, 3]"), &su)
  869. c.Assert(err, IsNil)
  870. c.Assert(su, DeepEquals, sliceUnmarshaler([]int{1, 2, 3}))
  871. err = yaml.Unmarshal([]byte("1"), &su)
  872. c.Assert(err, IsNil)
  873. c.Assert(su, DeepEquals, sliceUnmarshaler([]int{1}))
  874. }
  875. // From http://yaml.org/type/merge.html
  876. var mergeTests = `
  877. anchors:
  878. list:
  879. - &CENTER { "x": 1, "y": 2 }
  880. - &LEFT { "x": 0, "y": 2 }
  881. - &BIG { "r": 10 }
  882. - &SMALL { "r": 1 }
  883. # All the following maps are equal:
  884. plain:
  885. # Explicit keys
  886. "x": 1
  887. "y": 2
  888. "r": 10
  889. label: center/big
  890. mergeOne:
  891. # Merge one map
  892. << : *CENTER
  893. "r": 10
  894. label: center/big
  895. mergeMultiple:
  896. # Merge multiple maps
  897. << : [ *CENTER, *BIG ]
  898. label: center/big
  899. override:
  900. # Override
  901. << : [ *BIG, *LEFT, *SMALL ]
  902. "x": 1
  903. label: center/big
  904. shortTag:
  905. # Explicit short merge tag
  906. !!merge "<<" : [ *CENTER, *BIG ]
  907. label: center/big
  908. longTag:
  909. # Explicit merge long tag
  910. !<tag:yaml.org,2002:merge> "<<" : [ *CENTER, *BIG ]
  911. label: center/big
  912. inlineMap:
  913. # Inlined map
  914. << : {"x": 1, "y": 2, "r": 10}
  915. label: center/big
  916. inlineSequenceMap:
  917. # Inlined map in sequence
  918. << : [ *CENTER, {"r": 10} ]
  919. label: center/big
  920. `
  921. func (s *S) TestMerge(c *C) {
  922. var want = map[interface{}]interface{}{
  923. "x": 1,
  924. "y": 2,
  925. "r": 10,
  926. "label": "center/big",
  927. }
  928. var m map[interface{}]interface{}
  929. err := yaml.Unmarshal([]byte(mergeTests), &m)
  930. c.Assert(err, IsNil)
  931. for name, test := range m {
  932. if name == "anchors" {
  933. continue
  934. }
  935. c.Assert(test, DeepEquals, want, Commentf("test %q failed", name))
  936. }
  937. }
  938. func (s *S) TestMergeStruct(c *C) {
  939. type Data struct {
  940. X, Y, R int
  941. Label string
  942. }
  943. want := Data{1, 2, 10, "center/big"}
  944. var m map[string]Data
  945. err := yaml.Unmarshal([]byte(mergeTests), &m)
  946. c.Assert(err, IsNil)
  947. for name, test := range m {
  948. if name == "anchors" {
  949. continue
  950. }
  951. c.Assert(test, Equals, want, Commentf("test %q failed", name))
  952. }
  953. }
  954. var unmarshalNullTests = []func() interface{}{
  955. func() interface{} { var v interface{}; v = "v"; return &v },
  956. func() interface{} { var s = "s"; return &s },
  957. func() interface{} { var s = "s"; sptr := &s; return &sptr },
  958. func() interface{} { var i = 1; return &i },
  959. func() interface{} { var i = 1; iptr := &i; return &iptr },
  960. func() interface{} { m := map[string]int{"s": 1}; return &m },
  961. func() interface{} { m := map[string]int{"s": 1}; return m },
  962. }
  963. func (s *S) TestUnmarshalNull(c *C) {
  964. for _, test := range unmarshalNullTests {
  965. item := test()
  966. zero := reflect.Zero(reflect.TypeOf(item).Elem()).Interface()
  967. err := yaml.Unmarshal([]byte("null"), item)
  968. c.Assert(err, IsNil)
  969. if reflect.TypeOf(item).Kind() == reflect.Map {
  970. c.Assert(reflect.ValueOf(item).Interface(), DeepEquals, reflect.MakeMap(reflect.TypeOf(item)).Interface())
  971. } else {
  972. c.Assert(reflect.ValueOf(item).Elem().Interface(), DeepEquals, zero)
  973. }
  974. }
  975. }
  976. func (s *S) TestUnmarshalSliceOnPreset(c *C) {
  977. // Issue #48.
  978. v := struct{ A []int }{[]int{1}}
  979. yaml.Unmarshal([]byte("a: [2]"), &v)
  980. c.Assert(v.A, DeepEquals, []int{2})
  981. }
  982. var unmarshalStrictTests = []struct {
  983. data string
  984. value interface{}
  985. error string
  986. }{{
  987. data: "a: 1\nc: 2\n",
  988. value: struct{ A, B int }{A: 1},
  989. error: `yaml: unmarshal errors:\n line 2: field c not found in type struct { A int; B int }`,
  990. }, {
  991. data: "a: 1\nb: 2\na: 3\n",
  992. value: struct{ A, B int }{A: 3, B: 2},
  993. error: `yaml: unmarshal errors:\n line 3: field a already set in type struct { A int; B int }`,
  994. }, {
  995. data: "c: 3\na: 1\nb: 2\nc: 4\n",
  996. value: struct {
  997. A int
  998. inlineB `yaml:",inline"`
  999. }{
  1000. A: 1,
  1001. inlineB: inlineB{
  1002. B: 2,
  1003. inlineC: inlineC{
  1004. C: 4,
  1005. },
  1006. },
  1007. },
  1008. error: `yaml: unmarshal errors:\n line 4: field c already set in type struct { A int; yaml_test.inlineB "yaml:\\",inline\\"" }`,
  1009. }, {
  1010. data: "c: 0\na: 1\nb: 2\nc: 1\n",
  1011. value: struct {
  1012. A int
  1013. inlineB `yaml:",inline"`
  1014. }{
  1015. A: 1,
  1016. inlineB: inlineB{
  1017. B: 2,
  1018. inlineC: inlineC{
  1019. C: 1,
  1020. },
  1021. },
  1022. },
  1023. error: `yaml: unmarshal errors:\n line 4: field c already set in type struct { A int; yaml_test.inlineB "yaml:\\",inline\\"" }`,
  1024. }, {
  1025. data: "c: 1\na: 1\nb: 2\nc: 3\n",
  1026. value: struct {
  1027. A int
  1028. M map[string]interface{} `yaml:",inline"`
  1029. }{
  1030. A: 1,
  1031. M: map[string]interface{}{
  1032. "b": 2,
  1033. "c": 3,
  1034. },
  1035. },
  1036. error: `yaml: unmarshal errors:\n line 4: key "c" already set in map`,
  1037. }, {
  1038. data: "a: 1\n9: 2\nnull: 3\n9: 4",
  1039. value: map[interface{}]interface{}{
  1040. "a": 1,
  1041. nil: 3,
  1042. 9: 4,
  1043. },
  1044. error: `yaml: unmarshal errors:\n line 4: key 9 already set in map`,
  1045. }}
  1046. func (s *S) TestUnmarshalStrict(c *C) {
  1047. for i, item := range unmarshalStrictTests {
  1048. c.Logf("test %d: %q", i, item.data)
  1049. // First test that normal Unmarshal unmarshals to the expected value.
  1050. t := reflect.ValueOf(item.value).Type()
  1051. value := reflect.New(t)
  1052. err := yaml.Unmarshal([]byte(item.data), value.Interface())
  1053. c.Assert(err, Equals, nil)
  1054. c.Assert(value.Elem().Interface(), DeepEquals, item.value)
  1055. // Then test that UnmarshalStrict fails on the same thing.
  1056. t = reflect.ValueOf(item.value).Type()
  1057. value = reflect.New(t)
  1058. err = yaml.UnmarshalStrict([]byte(item.data), value.Interface())
  1059. c.Assert(err, ErrorMatches, item.error)
  1060. }
  1061. }
  1062. //var data []byte
  1063. //func init() {
  1064. // var err error
  1065. // data, err = ioutil.ReadFile("/tmp/file.yaml")
  1066. // if err != nil {
  1067. // panic(err)
  1068. // }
  1069. //}
  1070. //
  1071. //func (s *S) BenchmarkUnmarshal(c *C) {
  1072. // var err error
  1073. // for i := 0; i < c.N; i++ {
  1074. // var v map[string]interface{}
  1075. // err = yaml.Unmarshal(data, &v)
  1076. // }
  1077. // if err != nil {
  1078. // panic(err)
  1079. // }
  1080. //}
  1081. //
  1082. //func (s *S) BenchmarkMarshal(c *C) {
  1083. // var v map[string]interface{}
  1084. // yaml.Unmarshal(data, &v)
  1085. // c.ResetTimer()
  1086. // for i := 0; i < c.N; i++ {
  1087. // yaml.Marshal(&v)
  1088. // }
  1089. //}