decode_test.go 33 KB

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