decode_test.go 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334
  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. // YAML Float regex shouldn't match this
  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. func (s *S) TestUnmarshalErrors(c *C) {
  799. for i, item := range unmarshalErrorTests {
  800. c.Logf("test %d: %q", i, item.data)
  801. var value interface{}
  802. err := yaml.Unmarshal([]byte(item.data), &value)
  803. c.Assert(err, ErrorMatches, item.error, Commentf("Partial unmarshal: %#v", value))
  804. }
  805. }
  806. func (s *S) TestDecoderErrors(c *C) {
  807. for _, item := range unmarshalErrorTests {
  808. var value interface{}
  809. err := yaml.NewDecoder(strings.NewReader(item.data)).Decode(&value)
  810. c.Assert(err, ErrorMatches, item.error, Commentf("Partial unmarshal: %#v", value))
  811. }
  812. }
  813. var unmarshalerTests = []struct {
  814. data, tag string
  815. value interface{}
  816. }{
  817. {"_: {hi: there}", "!!map", map[interface{}]interface{}{"hi": "there"}},
  818. {"_: [1,A]", "!!seq", []interface{}{1, "A"}},
  819. {"_: 10", "!!int", 10},
  820. {"_: null", "!!null", nil},
  821. {`_: BAR!`, "!!str", "BAR!"},
  822. {`_: "BAR!"`, "!!str", "BAR!"},
  823. {"_: !!foo 'BAR!'", "!!foo", "BAR!"},
  824. {`_: ""`, "!!str", ""},
  825. }
  826. var unmarshalerResult = map[int]error{}
  827. type unmarshalerType struct {
  828. value interface{}
  829. }
  830. func (o *unmarshalerType) UnmarshalYAML(unmarshal func(v interface{}) error) error {
  831. if err := unmarshal(&o.value); err != nil {
  832. return err
  833. }
  834. if i, ok := o.value.(int); ok {
  835. if result, ok := unmarshalerResult[i]; ok {
  836. return result
  837. }
  838. }
  839. return nil
  840. }
  841. type unmarshalerPointer struct {
  842. Field *unmarshalerType "_"
  843. }
  844. type unmarshalerValue struct {
  845. Field unmarshalerType "_"
  846. }
  847. func (s *S) TestUnmarshalerPointerField(c *C) {
  848. for _, item := range unmarshalerTests {
  849. obj := &unmarshalerPointer{}
  850. err := yaml.Unmarshal([]byte(item.data), obj)
  851. c.Assert(err, IsNil)
  852. if item.value == nil {
  853. c.Assert(obj.Field, IsNil)
  854. } else {
  855. c.Assert(obj.Field, NotNil, Commentf("Pointer not initialized (%#v)", item.value))
  856. c.Assert(obj.Field.value, DeepEquals, item.value)
  857. }
  858. }
  859. }
  860. func (s *S) TestUnmarshalerValueField(c *C) {
  861. for _, item := range unmarshalerTests {
  862. obj := &unmarshalerValue{}
  863. err := yaml.Unmarshal([]byte(item.data), obj)
  864. c.Assert(err, IsNil)
  865. c.Assert(obj.Field, NotNil, Commentf("Pointer not initialized (%#v)", item.value))
  866. c.Assert(obj.Field.value, DeepEquals, item.value)
  867. }
  868. }
  869. func (s *S) TestUnmarshalerWholeDocument(c *C) {
  870. obj := &unmarshalerType{}
  871. err := yaml.Unmarshal([]byte(unmarshalerTests[0].data), obj)
  872. c.Assert(err, IsNil)
  873. value, ok := obj.value.(map[interface{}]interface{})
  874. c.Assert(ok, Equals, true, Commentf("value: %#v", obj.value))
  875. c.Assert(value["_"], DeepEquals, unmarshalerTests[0].value)
  876. }
  877. func (s *S) TestUnmarshalerTypeError(c *C) {
  878. unmarshalerResult[2] = &yaml.TypeError{[]string{"foo"}}
  879. unmarshalerResult[4] = &yaml.TypeError{[]string{"bar"}}
  880. defer func() {
  881. delete(unmarshalerResult, 2)
  882. delete(unmarshalerResult, 4)
  883. }()
  884. type T struct {
  885. Before int
  886. After int
  887. M map[string]*unmarshalerType
  888. }
  889. var v T
  890. data := `{before: A, m: {abc: 1, def: 2, ghi: 3, jkl: 4}, after: B}`
  891. err := yaml.Unmarshal([]byte(data), &v)
  892. c.Assert(err, ErrorMatches, ""+
  893. "yaml: unmarshal errors:\n"+
  894. " line 1: cannot unmarshal !!str `A` into int\n"+
  895. " foo\n"+
  896. " bar\n"+
  897. " line 1: cannot unmarshal !!str `B` into int")
  898. c.Assert(v.M["abc"], NotNil)
  899. c.Assert(v.M["def"], IsNil)
  900. c.Assert(v.M["ghi"], NotNil)
  901. c.Assert(v.M["jkl"], IsNil)
  902. c.Assert(v.M["abc"].value, Equals, 1)
  903. c.Assert(v.M["ghi"].value, Equals, 3)
  904. }
  905. type proxyTypeError struct{}
  906. func (v *proxyTypeError) UnmarshalYAML(unmarshal func(interface{}) error) error {
  907. var s string
  908. var a int32
  909. var b int64
  910. if err := unmarshal(&s); err != nil {
  911. panic(err)
  912. }
  913. if s == "a" {
  914. if err := unmarshal(&b); err == nil {
  915. panic("should have failed")
  916. }
  917. return unmarshal(&a)
  918. }
  919. if err := unmarshal(&a); err == nil {
  920. panic("should have failed")
  921. }
  922. return unmarshal(&b)
  923. }
  924. func (s *S) TestUnmarshalerTypeErrorProxying(c *C) {
  925. type T struct {
  926. Before int
  927. After int
  928. M map[string]*proxyTypeError
  929. }
  930. var v T
  931. data := `{before: A, m: {abc: a, def: b}, after: B}`
  932. err := yaml.Unmarshal([]byte(data), &v)
  933. c.Assert(err, ErrorMatches, ""+
  934. "yaml: unmarshal errors:\n"+
  935. " line 1: cannot unmarshal !!str `A` into int\n"+
  936. " line 1: cannot unmarshal !!str `a` into int32\n"+
  937. " line 1: cannot unmarshal !!str `b` into int64\n"+
  938. " line 1: cannot unmarshal !!str `B` into int")
  939. }
  940. type failingUnmarshaler struct{}
  941. var failingErr = errors.New("failingErr")
  942. func (ft *failingUnmarshaler) UnmarshalYAML(unmarshal func(interface{}) error) error {
  943. return failingErr
  944. }
  945. func (s *S) TestUnmarshalerError(c *C) {
  946. err := yaml.Unmarshal([]byte("a: b"), &failingUnmarshaler{})
  947. c.Assert(err, Equals, failingErr)
  948. }
  949. type sliceUnmarshaler []int
  950. func (su *sliceUnmarshaler) UnmarshalYAML(unmarshal func(interface{}) error) error {
  951. var slice []int
  952. err := unmarshal(&slice)
  953. if err == nil {
  954. *su = slice
  955. return nil
  956. }
  957. var intVal int
  958. err = unmarshal(&intVal)
  959. if err == nil {
  960. *su = []int{intVal}
  961. return nil
  962. }
  963. return err
  964. }
  965. func (s *S) TestUnmarshalerRetry(c *C) {
  966. var su sliceUnmarshaler
  967. err := yaml.Unmarshal([]byte("[1, 2, 3]"), &su)
  968. c.Assert(err, IsNil)
  969. c.Assert(su, DeepEquals, sliceUnmarshaler([]int{1, 2, 3}))
  970. err = yaml.Unmarshal([]byte("1"), &su)
  971. c.Assert(err, IsNil)
  972. c.Assert(su, DeepEquals, sliceUnmarshaler([]int{1}))
  973. }
  974. // From http://yaml.org/type/merge.html
  975. var mergeTests = `
  976. anchors:
  977. list:
  978. - &CENTER { "x": 1, "y": 2 }
  979. - &LEFT { "x": 0, "y": 2 }
  980. - &BIG { "r": 10 }
  981. - &SMALL { "r": 1 }
  982. # All the following maps are equal:
  983. plain:
  984. # Explicit keys
  985. "x": 1
  986. "y": 2
  987. "r": 10
  988. label: center/big
  989. mergeOne:
  990. # Merge one map
  991. << : *CENTER
  992. "r": 10
  993. label: center/big
  994. mergeMultiple:
  995. # Merge multiple maps
  996. << : [ *CENTER, *BIG ]
  997. label: center/big
  998. override:
  999. # Override
  1000. << : [ *BIG, *LEFT, *SMALL ]
  1001. "x": 1
  1002. label: center/big
  1003. shortTag:
  1004. # Explicit short merge tag
  1005. !!merge "<<" : [ *CENTER, *BIG ]
  1006. label: center/big
  1007. longTag:
  1008. # Explicit merge long tag
  1009. !<tag:yaml.org,2002:merge> "<<" : [ *CENTER, *BIG ]
  1010. label: center/big
  1011. inlineMap:
  1012. # Inlined map
  1013. << : {"x": 1, "y": 2, "r": 10}
  1014. label: center/big
  1015. inlineSequenceMap:
  1016. # Inlined map in sequence
  1017. << : [ *CENTER, {"r": 10} ]
  1018. label: center/big
  1019. `
  1020. func (s *S) TestMerge(c *C) {
  1021. var want = map[interface{}]interface{}{
  1022. "x": 1,
  1023. "y": 2,
  1024. "r": 10,
  1025. "label": "center/big",
  1026. }
  1027. var m map[interface{}]interface{}
  1028. err := yaml.Unmarshal([]byte(mergeTests), &m)
  1029. c.Assert(err, IsNil)
  1030. for name, test := range m {
  1031. if name == "anchors" {
  1032. continue
  1033. }
  1034. c.Assert(test, DeepEquals, want, Commentf("test %q failed", name))
  1035. }
  1036. }
  1037. func (s *S) TestMergeStruct(c *C) {
  1038. type Data struct {
  1039. X, Y, R int
  1040. Label string
  1041. }
  1042. want := Data{1, 2, 10, "center/big"}
  1043. var m map[string]Data
  1044. err := yaml.Unmarshal([]byte(mergeTests), &m)
  1045. c.Assert(err, IsNil)
  1046. for name, test := range m {
  1047. if name == "anchors" {
  1048. continue
  1049. }
  1050. c.Assert(test, Equals, want, Commentf("test %q failed", name))
  1051. }
  1052. }
  1053. var unmarshalNullTests = []func() interface{}{
  1054. func() interface{} { var v interface{}; v = "v"; return &v },
  1055. func() interface{} { var s = "s"; return &s },
  1056. func() interface{} { var s = "s"; sptr := &s; return &sptr },
  1057. func() interface{} { var i = 1; return &i },
  1058. func() interface{} { var i = 1; iptr := &i; return &iptr },
  1059. func() interface{} { m := map[string]int{"s": 1}; return &m },
  1060. func() interface{} { m := map[string]int{"s": 1}; return m },
  1061. }
  1062. func (s *S) TestUnmarshalNull(c *C) {
  1063. for _, test := range unmarshalNullTests {
  1064. item := test()
  1065. zero := reflect.Zero(reflect.TypeOf(item).Elem()).Interface()
  1066. err := yaml.Unmarshal([]byte("null"), item)
  1067. c.Assert(err, IsNil)
  1068. if reflect.TypeOf(item).Kind() == reflect.Map {
  1069. c.Assert(reflect.ValueOf(item).Interface(), DeepEquals, reflect.MakeMap(reflect.TypeOf(item)).Interface())
  1070. } else {
  1071. c.Assert(reflect.ValueOf(item).Elem().Interface(), DeepEquals, zero)
  1072. }
  1073. }
  1074. }
  1075. func (s *S) TestUnmarshalSliceOnPreset(c *C) {
  1076. // Issue #48.
  1077. v := struct{ A []int }{[]int{1}}
  1078. yaml.Unmarshal([]byte("a: [2]"), &v)
  1079. c.Assert(v.A, DeepEquals, []int{2})
  1080. }
  1081. var unmarshalStrictTests = []struct {
  1082. data string
  1083. value interface{}
  1084. error string
  1085. }{{
  1086. data: "a: 1\nc: 2\n",
  1087. value: struct{ A, B int }{A: 1},
  1088. error: `yaml: unmarshal errors:\n line 2: field c not found in type struct { A int; B int }`,
  1089. }, {
  1090. data: "a: 1\nb: 2\na: 3\n",
  1091. value: struct{ A, B int }{A: 3, B: 2},
  1092. error: `yaml: unmarshal errors:\n line 3: field a already set in type struct { A int; B int }`,
  1093. }, {
  1094. data: "c: 3\na: 1\nb: 2\nc: 4\n",
  1095. value: struct {
  1096. A int
  1097. inlineB `yaml:",inline"`
  1098. }{
  1099. A: 1,
  1100. inlineB: inlineB{
  1101. B: 2,
  1102. inlineC: inlineC{
  1103. C: 4,
  1104. },
  1105. },
  1106. },
  1107. error: `yaml: unmarshal errors:\n line 4: field c already set in type struct { A int; yaml_test.inlineB "yaml:\\",inline\\"" }`,
  1108. }, {
  1109. data: "c: 0\na: 1\nb: 2\nc: 1\n",
  1110. value: struct {
  1111. A int
  1112. inlineB `yaml:",inline"`
  1113. }{
  1114. A: 1,
  1115. inlineB: inlineB{
  1116. B: 2,
  1117. inlineC: inlineC{
  1118. C: 1,
  1119. },
  1120. },
  1121. },
  1122. error: `yaml: unmarshal errors:\n line 4: field c already set in type struct { A int; yaml_test.inlineB "yaml:\\",inline\\"" }`,
  1123. }, {
  1124. data: "c: 1\na: 1\nb: 2\nc: 3\n",
  1125. value: struct {
  1126. A int
  1127. M map[string]interface{} `yaml:",inline"`
  1128. }{
  1129. A: 1,
  1130. M: map[string]interface{}{
  1131. "b": 2,
  1132. "c": 3,
  1133. },
  1134. },
  1135. error: `yaml: unmarshal errors:\n line 4: key "c" already set in map`,
  1136. }, {
  1137. data: "a: 1\n9: 2\nnull: 3\n9: 4",
  1138. value: map[interface{}]interface{}{
  1139. "a": 1,
  1140. nil: 3,
  1141. 9: 4,
  1142. },
  1143. error: `yaml: unmarshal errors:\n line 4: key 9 already set in map`,
  1144. }}
  1145. func (s *S) TestUnmarshalStrict(c *C) {
  1146. for i, item := range unmarshalStrictTests {
  1147. c.Logf("test %d: %q", i, item.data)
  1148. // First test that normal Unmarshal unmarshals to the expected value.
  1149. t := reflect.ValueOf(item.value).Type()
  1150. value := reflect.New(t)
  1151. err := yaml.Unmarshal([]byte(item.data), value.Interface())
  1152. c.Assert(err, Equals, nil)
  1153. c.Assert(value.Elem().Interface(), DeepEquals, item.value)
  1154. // Then test that UnmarshalStrict fails on the same thing.
  1155. t = reflect.ValueOf(item.value).Type()
  1156. value = reflect.New(t)
  1157. err = yaml.UnmarshalStrict([]byte(item.data), value.Interface())
  1158. c.Assert(err, ErrorMatches, item.error)
  1159. }
  1160. }
  1161. type textUnmarshaler struct {
  1162. S string
  1163. }
  1164. func (t *textUnmarshaler) UnmarshalText(s []byte) error {
  1165. t.S = string(s)
  1166. return nil
  1167. }
  1168. func (s *S) TestFuzzCrashers(c *C) {
  1169. cases := []string{
  1170. // runtime error: index out of range
  1171. "\"\\0\\\r\n",
  1172. // should not happen
  1173. " 0: [\n] 0",
  1174. "? ? \"\n\" 0",
  1175. " - {\n000}0",
  1176. "0:\n 0: [0\n] 0",
  1177. " - \"\n000\"0",
  1178. " - \"\n000\"\"",
  1179. "0:\n - {\n000}0",
  1180. "0:\n - \"\n000\"0",
  1181. "0:\n - \"\n000\"\"",
  1182. // runtime error: index out of range
  1183. " \ufeff\n",
  1184. "? \ufeff\n",
  1185. "? \ufeff:\n",
  1186. "0: \ufeff\n",
  1187. "? \ufeff: \ufeff\n",
  1188. }
  1189. for _, data := range cases {
  1190. var v interface{}
  1191. _ = yaml.Unmarshal([]byte(data), &v)
  1192. }
  1193. }
  1194. //var data []byte
  1195. //func init() {
  1196. // var err error
  1197. // data, err = ioutil.ReadFile("/tmp/file.yaml")
  1198. // if err != nil {
  1199. // panic(err)
  1200. // }
  1201. //}
  1202. //
  1203. //func (s *S) BenchmarkUnmarshal(c *C) {
  1204. // var err error
  1205. // for i := 0; i < c.N; i++ {
  1206. // var v map[string]interface{}
  1207. // err = yaml.Unmarshal(data, &v)
  1208. // }
  1209. // if err != nil {
  1210. // panic(err)
  1211. // }
  1212. //}
  1213. //
  1214. //func (s *S) BenchmarkMarshal(c *C) {
  1215. // var v map[string]interface{}
  1216. // yaml.Unmarshal(data, &v)
  1217. // c.ResetTimer()
  1218. // for i := 0; i < c.N; i++ {
  1219. // yaml.Marshal(&v)
  1220. // }
  1221. //}