decode_test.go 29 KB

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