decode_test.go 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325
  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": time.Date(2015, 1, 1, 0, 0, 0, 0, time.UTC)},
  616. },
  617. {
  618. // implicit timestamp tag into interface.
  619. "a: 2015-01-01",
  620. map[string]interface{}{"a": time.Date(2015, 1, 1, 0, 0, 0, 0, time.UTC)},
  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. "First occurrence": "Foo",
  660. "Second occurrence": "Foo",
  661. "Override anchor": "Bar",
  662. "Reuse anchor": "Bar",
  663. },
  664. },
  665. // Single document with garbage following it.
  666. {
  667. "---\nhello\n...\n}not yaml",
  668. "hello",
  669. },
  670. }
  671. type M map[interface{}]interface{}
  672. type inlineB struct {
  673. B int
  674. inlineC `yaml:",inline"`
  675. }
  676. type inlineC struct {
  677. C int
  678. }
  679. func (s *S) TestUnmarshal(c *C) {
  680. for i, item := range unmarshalTests {
  681. c.Logf("test %d: %q", i, item.data)
  682. t := reflect.ValueOf(item.value).Type()
  683. value := reflect.New(t)
  684. err := yaml.Unmarshal([]byte(item.data), value.Interface())
  685. if _, ok := err.(*yaml.TypeError); !ok {
  686. c.Assert(err, IsNil)
  687. }
  688. c.Assert(value.Elem().Interface(), DeepEquals, item.value, Commentf("error: %v", err))
  689. }
  690. }
  691. func (s *S) TestUnmarshalFullTimestamp(c *C) {
  692. // Full timestamp in same format as encoded. This is confirmed to be
  693. // properly decoded by Python as a timestamp as well.
  694. var str = "2015-02-24T18:19:39.123456789-03:00"
  695. var t interface{}
  696. err := yaml.Unmarshal([]byte(str), &t)
  697. c.Assert(err, IsNil)
  698. c.Assert(t, Equals, time.Date(2015, 2, 24, 18, 19, 39, 123456789, t.(time.Time).Location()))
  699. c.Assert(t.(time.Time).In(time.UTC), Equals, time.Date(2015, 2, 24, 21, 19, 39, 123456789, time.UTC))
  700. }
  701. func (s *S) TestDecoderSingleDocument(c *C) {
  702. // Test that Decoder.Decode works as expected on
  703. // all the unmarshal tests.
  704. for i, item := range unmarshalTests {
  705. c.Logf("test %d: %q", i, item.data)
  706. if item.data == "" {
  707. // Behaviour differs when there's no YAML.
  708. continue
  709. }
  710. t := reflect.ValueOf(item.value).Type()
  711. value := reflect.New(t)
  712. err := yaml.NewDecoder(strings.NewReader(item.data)).Decode(value.Interface())
  713. if _, ok := err.(*yaml.TypeError); !ok {
  714. c.Assert(err, IsNil)
  715. }
  716. c.Assert(value.Elem().Interface(), DeepEquals, item.value)
  717. }
  718. }
  719. var decoderTests = []struct {
  720. data string
  721. values []interface{}
  722. }{{
  723. "",
  724. nil,
  725. }, {
  726. "a: b",
  727. []interface{}{
  728. map[interface{}]interface{}{"a": "b"},
  729. },
  730. }, {
  731. "---\na: b\n...\n",
  732. []interface{}{
  733. map[interface{}]interface{}{"a": "b"},
  734. },
  735. }, {
  736. "---\n'hello'\n...\n---\ngoodbye\n...\n",
  737. []interface{}{
  738. "hello",
  739. "goodbye",
  740. },
  741. }}
  742. func (s *S) TestDecoder(c *C) {
  743. for i, item := range decoderTests {
  744. c.Logf("test %d: %q", i, item.data)
  745. var values []interface{}
  746. dec := yaml.NewDecoder(strings.NewReader(item.data))
  747. for {
  748. var value interface{}
  749. err := dec.Decode(&value)
  750. if err == io.EOF {
  751. break
  752. }
  753. c.Assert(err, IsNil)
  754. values = append(values, value)
  755. }
  756. c.Assert(values, DeepEquals, item.values)
  757. }
  758. }
  759. type errReader struct{}
  760. func (errReader) Read([]byte) (int, error) {
  761. return 0, errors.New("some read error")
  762. }
  763. func (s *S) TestDecoderReadError(c *C) {
  764. err := yaml.NewDecoder(errReader{}).Decode(&struct{}{})
  765. c.Assert(err, ErrorMatches, `yaml: input error: some read error`)
  766. }
  767. func (s *S) TestUnmarshalNaN(c *C) {
  768. value := map[string]interface{}{}
  769. err := yaml.Unmarshal([]byte("notanum: .NaN"), &value)
  770. c.Assert(err, IsNil)
  771. c.Assert(math.IsNaN(value["notanum"].(float64)), Equals, true)
  772. }
  773. var unmarshalErrorTests = []struct {
  774. data, error string
  775. }{
  776. {"v: !!float 'error'", "yaml: cannot decode !!str `error` as a !!float"},
  777. {"v: [A,", "yaml: line 1: did not find expected node content"},
  778. {"v:\n- [A,", "yaml: line 2: did not find expected node content"},
  779. {"a:\n- b: *,", "yaml: line 2: did not find expected alphabetic or numeric character"},
  780. {"a: *b\n", "yaml: unknown anchor 'b' referenced"},
  781. {"a: &a\n b: *a\n", "yaml: anchor 'a' value contains itself"},
  782. {"value: -", "yaml: block sequence entries are not allowed in this context"},
  783. {"a: !!binary ==", "yaml: !!binary value contains invalid base64 data"},
  784. {"{[.]}", `yaml: invalid map key: \[\]interface \{\}\{"\."\}`},
  785. {"{{.}}", `yaml: invalid map key: map\[interface\ \{\}\]interface \{\}\{".":interface \{\}\(nil\)\}`},
  786. {"b: *a\na: &a {c: 1}", `yaml: unknown anchor 'a' referenced`},
  787. {"%TAG !%79! tag:yaml.org,2002:\n---\nv: !%79!int '1'", "yaml: did not find expected whitespace"},
  788. }
  789. func (s *S) TestUnmarshalErrors(c *C) {
  790. for i, item := range unmarshalErrorTests {
  791. c.Logf("test %d: %q", i, item.data)
  792. var value interface{}
  793. err := yaml.Unmarshal([]byte(item.data), &value)
  794. c.Assert(err, ErrorMatches, item.error, Commentf("Partial unmarshal: %#v", value))
  795. }
  796. }
  797. func (s *S) TestDecoderErrors(c *C) {
  798. for _, item := range unmarshalErrorTests {
  799. var value interface{}
  800. err := yaml.NewDecoder(strings.NewReader(item.data)).Decode(&value)
  801. c.Assert(err, ErrorMatches, item.error, Commentf("Partial unmarshal: %#v", value))
  802. }
  803. }
  804. var unmarshalerTests = []struct {
  805. data, tag string
  806. value interface{}
  807. }{
  808. {"_: {hi: there}", "!!map", map[interface{}]interface{}{"hi": "there"}},
  809. {"_: [1,A]", "!!seq", []interface{}{1, "A"}},
  810. {"_: 10", "!!int", 10},
  811. {"_: null", "!!null", nil},
  812. {`_: BAR!`, "!!str", "BAR!"},
  813. {`_: "BAR!"`, "!!str", "BAR!"},
  814. {"_: !!foo 'BAR!'", "!!foo", "BAR!"},
  815. {`_: ""`, "!!str", ""},
  816. }
  817. var unmarshalerResult = map[int]error{}
  818. type unmarshalerType struct {
  819. value interface{}
  820. }
  821. func (o *unmarshalerType) 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 unmarshalerPointer struct {
  833. Field *unmarshalerType "_"
  834. }
  835. type unmarshalerValue struct {
  836. Field unmarshalerType "_"
  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. }
  851. func (s *S) TestUnmarshalerValueField(c *C) {
  852. for _, item := range unmarshalerTests {
  853. obj := &unmarshalerValue{}
  854. err := yaml.Unmarshal([]byte(item.data), obj)
  855. c.Assert(err, IsNil)
  856. c.Assert(obj.Field, NotNil, Commentf("Pointer not initialized (%#v)", item.value))
  857. c.Assert(obj.Field.value, DeepEquals, item.value)
  858. }
  859. }
  860. func (s *S) TestUnmarshalerWholeDocument(c *C) {
  861. obj := &unmarshalerType{}
  862. err := yaml.Unmarshal([]byte(unmarshalerTests[0].data), obj)
  863. c.Assert(err, IsNil)
  864. value, ok := obj.value.(map[interface{}]interface{})
  865. c.Assert(ok, Equals, true, Commentf("value: %#v", obj.value))
  866. c.Assert(value["_"], DeepEquals, unmarshalerTests[0].value)
  867. }
  868. func (s *S) TestUnmarshalerTypeError(c *C) {
  869. unmarshalerResult[2] = &yaml.TypeError{[]string{"foo"}}
  870. unmarshalerResult[4] = &yaml.TypeError{[]string{"bar"}}
  871. defer func() {
  872. delete(unmarshalerResult, 2)
  873. delete(unmarshalerResult, 4)
  874. }()
  875. type T struct {
  876. Before int
  877. After int
  878. M map[string]*unmarshalerType
  879. }
  880. var v T
  881. data := `{before: A, m: {abc: 1, def: 2, ghi: 3, jkl: 4}, after: B}`
  882. err := yaml.Unmarshal([]byte(data), &v)
  883. c.Assert(err, ErrorMatches, ""+
  884. "yaml: unmarshal errors:\n"+
  885. " line 1: cannot unmarshal !!str `A` into int\n"+
  886. " foo\n"+
  887. " bar\n"+
  888. " line 1: cannot unmarshal !!str `B` into int")
  889. c.Assert(v.M["abc"], NotNil)
  890. c.Assert(v.M["def"], IsNil)
  891. c.Assert(v.M["ghi"], NotNil)
  892. c.Assert(v.M["jkl"], IsNil)
  893. c.Assert(v.M["abc"].value, Equals, 1)
  894. c.Assert(v.M["ghi"].value, Equals, 3)
  895. }
  896. type proxyTypeError struct{}
  897. func (v *proxyTypeError) UnmarshalYAML(unmarshal func(interface{}) error) error {
  898. var s string
  899. var a int32
  900. var b int64
  901. if err := unmarshal(&s); err != nil {
  902. panic(err)
  903. }
  904. if s == "a" {
  905. if err := unmarshal(&b); err == nil {
  906. panic("should have failed")
  907. }
  908. return unmarshal(&a)
  909. }
  910. if err := unmarshal(&a); err == nil {
  911. panic("should have failed")
  912. }
  913. return unmarshal(&b)
  914. }
  915. func (s *S) TestUnmarshalerTypeErrorProxying(c *C) {
  916. type T struct {
  917. Before int
  918. After int
  919. M map[string]*proxyTypeError
  920. }
  921. var v T
  922. data := `{before: A, m: {abc: a, def: b}, after: B}`
  923. err := yaml.Unmarshal([]byte(data), &v)
  924. c.Assert(err, ErrorMatches, ""+
  925. "yaml: unmarshal errors:\n"+
  926. " line 1: cannot unmarshal !!str `A` into int\n"+
  927. " line 1: cannot unmarshal !!str `a` into int32\n"+
  928. " line 1: cannot unmarshal !!str `b` into int64\n"+
  929. " line 1: cannot unmarshal !!str `B` into int")
  930. }
  931. type failingUnmarshaler struct{}
  932. var failingErr = errors.New("failingErr")
  933. func (ft *failingUnmarshaler) UnmarshalYAML(unmarshal func(interface{}) error) error {
  934. return failingErr
  935. }
  936. func (s *S) TestUnmarshalerError(c *C) {
  937. err := yaml.Unmarshal([]byte("a: b"), &failingUnmarshaler{})
  938. c.Assert(err, Equals, failingErr)
  939. }
  940. type sliceUnmarshaler []int
  941. func (su *sliceUnmarshaler) UnmarshalYAML(unmarshal func(interface{}) error) error {
  942. var slice []int
  943. err := unmarshal(&slice)
  944. if err == nil {
  945. *su = slice
  946. return nil
  947. }
  948. var intVal int
  949. err = unmarshal(&intVal)
  950. if err == nil {
  951. *su = []int{intVal}
  952. return nil
  953. }
  954. return err
  955. }
  956. func (s *S) TestUnmarshalerRetry(c *C) {
  957. var su sliceUnmarshaler
  958. err := yaml.Unmarshal([]byte("[1, 2, 3]"), &su)
  959. c.Assert(err, IsNil)
  960. c.Assert(su, DeepEquals, sliceUnmarshaler([]int{1, 2, 3}))
  961. err = yaml.Unmarshal([]byte("1"), &su)
  962. c.Assert(err, IsNil)
  963. c.Assert(su, DeepEquals, sliceUnmarshaler([]int{1}))
  964. }
  965. // From http://yaml.org/type/merge.html
  966. var mergeTests = `
  967. anchors:
  968. list:
  969. - &CENTER { "x": 1, "y": 2 }
  970. - &LEFT { "x": 0, "y": 2 }
  971. - &BIG { "r": 10 }
  972. - &SMALL { "r": 1 }
  973. # All the following maps are equal:
  974. plain:
  975. # Explicit keys
  976. "x": 1
  977. "y": 2
  978. "r": 10
  979. label: center/big
  980. mergeOne:
  981. # Merge one map
  982. << : *CENTER
  983. "r": 10
  984. label: center/big
  985. mergeMultiple:
  986. # Merge multiple maps
  987. << : [ *CENTER, *BIG ]
  988. label: center/big
  989. override:
  990. # Override
  991. << : [ *BIG, *LEFT, *SMALL ]
  992. "x": 1
  993. label: center/big
  994. shortTag:
  995. # Explicit short merge tag
  996. !!merge "<<" : [ *CENTER, *BIG ]
  997. label: center/big
  998. longTag:
  999. # Explicit merge long tag
  1000. !<tag:yaml.org,2002:merge> "<<" : [ *CENTER, *BIG ]
  1001. label: center/big
  1002. inlineMap:
  1003. # Inlined map
  1004. << : {"x": 1, "y": 2, "r": 10}
  1005. label: center/big
  1006. inlineSequenceMap:
  1007. # Inlined map in sequence
  1008. << : [ *CENTER, {"r": 10} ]
  1009. label: center/big
  1010. `
  1011. func (s *S) TestMerge(c *C) {
  1012. var want = map[interface{}]interface{}{
  1013. "x": 1,
  1014. "y": 2,
  1015. "r": 10,
  1016. "label": "center/big",
  1017. }
  1018. var m map[interface{}]interface{}
  1019. err := yaml.Unmarshal([]byte(mergeTests), &m)
  1020. c.Assert(err, IsNil)
  1021. for name, test := range m {
  1022. if name == "anchors" {
  1023. continue
  1024. }
  1025. c.Assert(test, DeepEquals, want, Commentf("test %q failed", name))
  1026. }
  1027. }
  1028. func (s *S) TestMergeStruct(c *C) {
  1029. type Data struct {
  1030. X, Y, R int
  1031. Label string
  1032. }
  1033. want := Data{1, 2, 10, "center/big"}
  1034. var m map[string]Data
  1035. err := yaml.Unmarshal([]byte(mergeTests), &m)
  1036. c.Assert(err, IsNil)
  1037. for name, test := range m {
  1038. if name == "anchors" {
  1039. continue
  1040. }
  1041. c.Assert(test, Equals, want, Commentf("test %q failed", name))
  1042. }
  1043. }
  1044. var unmarshalNullTests = []func() interface{}{
  1045. func() interface{} { var v interface{}; v = "v"; return &v },
  1046. func() interface{} { var s = "s"; return &s },
  1047. func() interface{} { var s = "s"; sptr := &s; return &sptr },
  1048. func() interface{} { var i = 1; return &i },
  1049. func() interface{} { var i = 1; iptr := &i; return &iptr },
  1050. func() interface{} { m := map[string]int{"s": 1}; return &m },
  1051. func() interface{} { m := map[string]int{"s": 1}; return m },
  1052. }
  1053. func (s *S) TestUnmarshalNull(c *C) {
  1054. for _, test := range unmarshalNullTests {
  1055. item := test()
  1056. zero := reflect.Zero(reflect.TypeOf(item).Elem()).Interface()
  1057. err := yaml.Unmarshal([]byte("null"), item)
  1058. c.Assert(err, IsNil)
  1059. if reflect.TypeOf(item).Kind() == reflect.Map {
  1060. c.Assert(reflect.ValueOf(item).Interface(), DeepEquals, reflect.MakeMap(reflect.TypeOf(item)).Interface())
  1061. } else {
  1062. c.Assert(reflect.ValueOf(item).Elem().Interface(), DeepEquals, zero)
  1063. }
  1064. }
  1065. }
  1066. func (s *S) TestUnmarshalSliceOnPreset(c *C) {
  1067. // Issue #48.
  1068. v := struct{ A []int }{[]int{1}}
  1069. yaml.Unmarshal([]byte("a: [2]"), &v)
  1070. c.Assert(v.A, DeepEquals, []int{2})
  1071. }
  1072. var unmarshalStrictTests = []struct {
  1073. data string
  1074. value interface{}
  1075. error string
  1076. }{{
  1077. data: "a: 1\nc: 2\n",
  1078. value: struct{ A, B int }{A: 1},
  1079. error: `yaml: unmarshal errors:\n line 2: field c not found in type struct { A int; B int }`,
  1080. }, {
  1081. data: "a: 1\nb: 2\na: 3\n",
  1082. value: struct{ A, B int }{A: 3, B: 2},
  1083. error: `yaml: unmarshal errors:\n line 3: field a already set in type struct { A int; B int }`,
  1084. }, {
  1085. data: "c: 3\na: 1\nb: 2\nc: 4\n",
  1086. value: struct {
  1087. A int
  1088. inlineB `yaml:",inline"`
  1089. }{
  1090. A: 1,
  1091. inlineB: inlineB{
  1092. B: 2,
  1093. inlineC: inlineC{
  1094. C: 4,
  1095. },
  1096. },
  1097. },
  1098. error: `yaml: unmarshal errors:\n line 4: field c already set in type struct { A int; yaml_test.inlineB "yaml:\\",inline\\"" }`,
  1099. }, {
  1100. data: "c: 0\na: 1\nb: 2\nc: 1\n",
  1101. value: struct {
  1102. A int
  1103. inlineB `yaml:",inline"`
  1104. }{
  1105. A: 1,
  1106. inlineB: inlineB{
  1107. B: 2,
  1108. inlineC: inlineC{
  1109. C: 1,
  1110. },
  1111. },
  1112. },
  1113. error: `yaml: unmarshal errors:\n line 4: field c already set in type struct { A int; yaml_test.inlineB "yaml:\\",inline\\"" }`,
  1114. }, {
  1115. data: "c: 1\na: 1\nb: 2\nc: 3\n",
  1116. value: struct {
  1117. A int
  1118. M map[string]interface{} `yaml:",inline"`
  1119. }{
  1120. A: 1,
  1121. M: map[string]interface{}{
  1122. "b": 2,
  1123. "c": 3,
  1124. },
  1125. },
  1126. error: `yaml: unmarshal errors:\n line 4: key "c" already set in map`,
  1127. }, {
  1128. data: "a: 1\n9: 2\nnull: 3\n9: 4",
  1129. value: map[interface{}]interface{}{
  1130. "a": 1,
  1131. nil: 3,
  1132. 9: 4,
  1133. },
  1134. error: `yaml: unmarshal errors:\n line 4: key 9 already set in map`,
  1135. }}
  1136. func (s *S) TestUnmarshalStrict(c *C) {
  1137. for i, item := range unmarshalStrictTests {
  1138. c.Logf("test %d: %q", i, item.data)
  1139. // First test that normal Unmarshal unmarshals to the expected value.
  1140. t := reflect.ValueOf(item.value).Type()
  1141. value := reflect.New(t)
  1142. err := yaml.Unmarshal([]byte(item.data), value.Interface())
  1143. c.Assert(err, Equals, nil)
  1144. c.Assert(value.Elem().Interface(), DeepEquals, item.value)
  1145. // Then test that UnmarshalStrict fails on the same thing.
  1146. t = reflect.ValueOf(item.value).Type()
  1147. value = reflect.New(t)
  1148. err = yaml.UnmarshalStrict([]byte(item.data), value.Interface())
  1149. c.Assert(err, ErrorMatches, item.error)
  1150. }
  1151. }
  1152. type textUnmarshaler struct {
  1153. S string
  1154. }
  1155. func (t *textUnmarshaler) UnmarshalText(s []byte) error {
  1156. t.S = string(s)
  1157. return nil
  1158. }
  1159. func (s *S) TestFuzzCrashers(c *C) {
  1160. cases := []string{
  1161. // runtime error: index out of range
  1162. "\"\\0\\\r\n",
  1163. // should not happen
  1164. " 0: [\n] 0",
  1165. "? ? \"\n\" 0",
  1166. " - {\n000}0",
  1167. "0:\n 0: [0\n] 0",
  1168. " - \"\n000\"0",
  1169. " - \"\n000\"\"",
  1170. "0:\n - {\n000}0",
  1171. "0:\n - \"\n000\"0",
  1172. "0:\n - \"\n000\"\"",
  1173. // runtime error: index out of range
  1174. " \ufeff\n",
  1175. "? \ufeff\n",
  1176. "? \ufeff:\n",
  1177. "0: \ufeff\n",
  1178. "? \ufeff: \ufeff\n",
  1179. }
  1180. for _, data := range cases {
  1181. var v interface{}
  1182. _ = yaml.Unmarshal([]byte(data), &v)
  1183. }
  1184. }
  1185. //var data []byte
  1186. //func init() {
  1187. // var err error
  1188. // data, err = ioutil.ReadFile("/tmp/file.yaml")
  1189. // if err != nil {
  1190. // panic(err)
  1191. // }
  1192. //}
  1193. //
  1194. //func (s *S) BenchmarkUnmarshal(c *C) {
  1195. // var err error
  1196. // for i := 0; i < c.N; i++ {
  1197. // var v map[string]interface{}
  1198. // err = yaml.Unmarshal(data, &v)
  1199. // }
  1200. // if err != nil {
  1201. // panic(err)
  1202. // }
  1203. //}
  1204. //
  1205. //func (s *S) BenchmarkMarshal(c *C) {
  1206. // var v map[string]interface{}
  1207. // yaml.Unmarshal(data, &v)
  1208. // c.ResetTimer()
  1209. // for i := 0; i < c.N; i++ {
  1210. // yaml.Marshal(&v)
  1211. // }
  1212. //}