decode_test.go 30 KB

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