decode_test.go 29 KB

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