decode_test.go 30 KB

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