decode_test.go 31 KB

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