decode_test.go 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272
  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. // Anchors and aliases.
  391. {
  392. "a: &x 1\nb: &y 2\nc: *x\nd: *y\n",
  393. &struct{ A, B, C, D int }{1, 2, 1, 2},
  394. }, {
  395. "a: &a {c: 1}\nb: *a",
  396. &struct {
  397. A, B struct {
  398. C int
  399. }
  400. }{struct{ C int }{1}, struct{ C int }{1}},
  401. }, {
  402. "a: &a [1, 2]\nb: *a",
  403. &struct{ B []int }{[]int{1, 2}},
  404. },
  405. // Bug #1133337
  406. {
  407. "foo: ''",
  408. map[string]*string{"foo": new(string)},
  409. }, {
  410. "foo: null",
  411. map[string]*string{"foo": nil},
  412. }, {
  413. "foo: null",
  414. map[string]string{"foo": ""},
  415. }, {
  416. "foo: null",
  417. map[string]interface{}{"foo": nil},
  418. },
  419. // Support for ~
  420. {
  421. "foo: ~",
  422. map[string]*string{"foo": nil},
  423. }, {
  424. "foo: ~",
  425. map[string]string{"foo": ""},
  426. }, {
  427. "foo: ~",
  428. map[string]interface{}{"foo": nil},
  429. },
  430. // Ignored field
  431. {
  432. "a: 1\nb: 2\n",
  433. &struct {
  434. A int
  435. B int "-"
  436. }{1, 0},
  437. },
  438. // Bug #1191981
  439. {
  440. "" +
  441. "%YAML 1.1\n" +
  442. "--- !!str\n" +
  443. `"Generic line break (no glyph)\n\` + "\n" +
  444. ` Generic line break (glyphed)\n\` + "\n" +
  445. ` Line separator\u2028\` + "\n" +
  446. ` Paragraph separator\u2029"` + "\n",
  447. "" +
  448. "Generic line break (no glyph)\n" +
  449. "Generic line break (glyphed)\n" +
  450. "Line separator\u2028Paragraph separator\u2029",
  451. },
  452. // Struct inlining
  453. {
  454. "a: 1\nb: 2\nc: 3\n",
  455. &struct {
  456. A int
  457. C inlineB `yaml:",inline"`
  458. }{1, inlineB{2, inlineC{3}}},
  459. },
  460. // Map inlining
  461. {
  462. "a: 1\nb: 2\nc: 3\n",
  463. &struct {
  464. A int
  465. C map[string]int `yaml:",inline"`
  466. }{1, map[string]int{"b": 2, "c": 3}},
  467. },
  468. // bug 1243827
  469. {
  470. "a: -b_c",
  471. map[string]interface{}{"a": "-b_c"},
  472. },
  473. {
  474. "a: +b_c",
  475. map[string]interface{}{"a": "+b_c"},
  476. },
  477. {
  478. "a: 50cent_of_dollar",
  479. map[string]interface{}{"a": "50cent_of_dollar"},
  480. },
  481. // issue #295 (allow scalars with colons in flow mappings and sequences)
  482. {
  483. "a: {b: https://github.com/go-yaml/yaml}",
  484. map[string]interface{}{"a": map[interface{}]interface{}{
  485. "b": "https://github.com/go-yaml/yaml",
  486. }},
  487. },
  488. {
  489. "a: [https://github.com/go-yaml/yaml]",
  490. map[string]interface{}{"a": []interface{}{"https://github.com/go-yaml/yaml"}},
  491. },
  492. // Duration
  493. {
  494. "a: 3s",
  495. map[string]time.Duration{"a": 3 * time.Second},
  496. },
  497. // Issue #24.
  498. {
  499. "a: <foo>",
  500. map[string]string{"a": "<foo>"},
  501. },
  502. // Base 60 floats are obsolete and unsupported.
  503. {
  504. "a: 1:1\n",
  505. map[string]string{"a": "1:1"},
  506. },
  507. // Binary data.
  508. {
  509. "a: !!binary gIGC\n",
  510. map[string]string{"a": "\x80\x81\x82"},
  511. }, {
  512. "a: !!binary |\n " + strings.Repeat("kJCQ", 17) + "kJ\n CQ\n",
  513. map[string]string{"a": strings.Repeat("\x90", 54)},
  514. }, {
  515. "a: !!binary |\n " + strings.Repeat("A", 70) + "\n ==\n",
  516. map[string]string{"a": strings.Repeat("\x00", 52)},
  517. },
  518. // Ordered maps.
  519. {
  520. "{b: 2, a: 1, d: 4, c: 3, sub: {e: 5}}",
  521. &yaml.MapSlice{{"b", 2}, {"a", 1}, {"d", 4}, {"c", 3}, {"sub", yaml.MapSlice{{"e", 5}}}},
  522. },
  523. // Issue #39.
  524. {
  525. "a:\n b:\n c: d\n",
  526. map[string]struct{ B interface{} }{"a": {map[interface{}]interface{}{"c": "d"}}},
  527. },
  528. // Custom map type.
  529. {
  530. "a: {b: c}",
  531. M{"a": M{"b": "c"}},
  532. },
  533. // Support encoding.TextUnmarshaler.
  534. {
  535. "a: 1.2.3.4\n",
  536. map[string]textUnmarshaler{"a": textUnmarshaler{S: "1.2.3.4"}},
  537. },
  538. {
  539. "a: 2015-02-24T18:19:39Z\n",
  540. map[string]textUnmarshaler{"a": textUnmarshaler{"2015-02-24T18:19:39Z"}},
  541. },
  542. // Timestamps
  543. {
  544. // Date only.
  545. "a: 2015-01-01\n",
  546. map[string]time.Time{"a": time.Date(2015, 1, 1, 0, 0, 0, 0, time.UTC)},
  547. },
  548. {
  549. // RFC3339
  550. "a: 2015-02-24T18:19:39.12Z\n",
  551. map[string]time.Time{"a": time.Date(2015, 2, 24, 18, 19, 39, .12e9, time.UTC)},
  552. },
  553. {
  554. // RFC3339 with short dates.
  555. "a: 2015-2-3T3:4:5Z",
  556. map[string]time.Time{"a": time.Date(2015, 2, 3, 3, 4, 5, 0, time.UTC)},
  557. },
  558. {
  559. // ISO8601 lower case t
  560. "a: 2015-02-24t18:19:39Z\n",
  561. map[string]time.Time{"a": time.Date(2015, 2, 24, 18, 19, 39, 0, time.UTC)},
  562. },
  563. {
  564. // space separate, no time zone
  565. "a: 2015-02-24 18:19:39\n",
  566. map[string]time.Time{"a": time.Date(2015, 2, 24, 18, 19, 39, 0, time.UTC)},
  567. },
  568. // Some cases not currently handled. Uncomment these when
  569. // the code is fixed.
  570. // {
  571. // // space separated with time zone
  572. // "a: 2001-12-14 21:59:43.10 -5",
  573. // map[string]interface{}{"a": time.Date(2001, 12, 14, 21, 59, 43, .1e9, time.UTC)},
  574. // },
  575. // {
  576. // // arbitrary whitespace between fields
  577. // "a: 2001-12-14 \t\t \t21:59:43.10 \t Z",
  578. // map[string]interface{}{"a": time.Date(2001, 12, 14, 21, 59, 43, .1e9, time.UTC)},
  579. // },
  580. {
  581. // explicit string tag
  582. "a: !!str 2015-01-01",
  583. map[string]interface{}{"a": "2015-01-01"},
  584. },
  585. {
  586. // explicit timestamp tag on quoted string
  587. "a: !!timestamp \"2015-01-01\"",
  588. map[string]time.Time{"a": time.Date(2015, 1, 1, 0, 0, 0, 0, time.UTC)},
  589. },
  590. {
  591. // explicit timestamp tag on unquoted 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. // quoted string that's a valid timestamp
  597. "a: \"2015-01-01\"",
  598. map[string]interface{}{"a": "2015-01-01"},
  599. },
  600. {
  601. // explicit timestamp tag into interface.
  602. "a: !!timestamp \"2015-01-01\"",
  603. map[string]interface{}{"a": "2015-01-01"},
  604. },
  605. {
  606. // implicit timestamp tag into interface.
  607. "a: 2015-01-01",
  608. map[string]interface{}{"a": "2015-01-01"},
  609. },
  610. // Encode empty lists as zero-length slices.
  611. {
  612. "a: []",
  613. &struct{ A []int }{[]int{}},
  614. },
  615. // UTF-16-LE
  616. {
  617. "\xff\xfe\xf1\x00o\x00\xf1\x00o\x00:\x00 \x00v\x00e\x00r\x00y\x00 \x00y\x00e\x00s\x00\n\x00",
  618. M{"ñoño": "very yes"},
  619. },
  620. // UTF-16-LE with surrogate.
  621. {
  622. "\xff\xfe\xf1\x00o\x00\xf1\x00o\x00:\x00 \x00v\x00e\x00r\x00y\x00 \x00y\x00e\x00s\x00 \x00=\xd8\xd4\xdf\n\x00",
  623. M{"ñoño": "very yes 🟔"},
  624. },
  625. // UTF-16-BE
  626. {
  627. "\xfe\xff\x00\xf1\x00o\x00\xf1\x00o\x00:\x00 \x00v\x00e\x00r\x00y\x00 \x00y\x00e\x00s\x00\n",
  628. M{"ñoño": "very yes"},
  629. },
  630. // UTF-16-BE with surrogate.
  631. {
  632. "\xfe\xff\x00\xf1\x00o\x00\xf1\x00o\x00:\x00 \x00v\x00e\x00r\x00y\x00 \x00y\x00e\x00s\x00 \xd8=\xdf\xd4\x00\n",
  633. M{"ñoño": "very yes 🟔"},
  634. },
  635. // YAML Float regex shouldn't match this
  636. {
  637. "a: 123456e1\n",
  638. M{"a": "123456e1"},
  639. }, {
  640. "a: 123456E1\n",
  641. M{"a": "123456E1"},
  642. },
  643. // yaml-test-suite 3GZX: Spec Example 7.1. Alias Nodes
  644. {
  645. "First occurrence: &anchor Foo\nSecond occurrence: *anchor\nOverride anchor: &anchor Bar\nReuse anchor: *anchor\n",
  646. map[interface{}]interface{}{
  647. "Reuse anchor": "Bar",
  648. "First occurrence": "Foo",
  649. "Second occurrence": "Foo",
  650. "Override anchor": "Bar",
  651. },
  652. },
  653. // Single document with garbage following it.
  654. {
  655. "---\nhello\n...\n}not yaml",
  656. "hello",
  657. },
  658. }
  659. type M map[interface{}]interface{}
  660. type inlineB struct {
  661. B int
  662. inlineC `yaml:",inline"`
  663. }
  664. type inlineC struct {
  665. C int
  666. }
  667. func (s *S) TestUnmarshal(c *C) {
  668. for i, item := range unmarshalTests {
  669. c.Logf("test %d: %q", i, item.data)
  670. t := reflect.ValueOf(item.value).Type()
  671. value := reflect.New(t)
  672. err := yaml.Unmarshal([]byte(item.data), value.Interface())
  673. if _, ok := err.(*yaml.TypeError); !ok {
  674. c.Assert(err, IsNil)
  675. }
  676. c.Assert(value.Elem().Interface(), DeepEquals, item.value, Commentf("error: %v", err))
  677. }
  678. }
  679. func (s *S) TestDecoderSingleDocument(c *C) {
  680. // Test that Decoder.Decode works as expected on
  681. // all the unmarshal tests.
  682. for i, item := range unmarshalTests {
  683. c.Logf("test %d: %q", i, item.data)
  684. if item.data == "" {
  685. // Behaviour differs when there's no YAML.
  686. continue
  687. }
  688. t := reflect.ValueOf(item.value).Type()
  689. value := reflect.New(t)
  690. err := yaml.NewDecoder(strings.NewReader(item.data)).Decode(value.Interface())
  691. if _, ok := err.(*yaml.TypeError); !ok {
  692. c.Assert(err, IsNil)
  693. }
  694. c.Assert(value.Elem().Interface(), DeepEquals, item.value)
  695. }
  696. }
  697. var decoderTests = []struct {
  698. data string
  699. values []interface{}
  700. }{{
  701. "",
  702. nil,
  703. }, {
  704. "a: b",
  705. []interface{}{
  706. map[interface{}]interface{}{"a": "b"},
  707. },
  708. }, {
  709. "---\na: b\n...\n",
  710. []interface{}{
  711. map[interface{}]interface{}{"a": "b"},
  712. },
  713. }, {
  714. "---\n'hello'\n...\n---\ngoodbye\n...\n",
  715. []interface{}{
  716. "hello",
  717. "goodbye",
  718. },
  719. }}
  720. func (s *S) TestDecoder(c *C) {
  721. for i, item := range decoderTests {
  722. c.Logf("test %d: %q", i, item.data)
  723. var values []interface{}
  724. dec := yaml.NewDecoder(strings.NewReader(item.data))
  725. for {
  726. var value interface{}
  727. err := dec.Decode(&value)
  728. if err == io.EOF {
  729. break
  730. }
  731. c.Assert(err, IsNil)
  732. values = append(values, value)
  733. }
  734. c.Assert(values, DeepEquals, item.values)
  735. }
  736. }
  737. type errReader struct{}
  738. func (errReader) Read([]byte) (int, error) {
  739. return 0, errors.New("some read error")
  740. }
  741. func (s *S) TestDecoderReadError(c *C) {
  742. err := yaml.NewDecoder(errReader{}).Decode(&struct{}{})
  743. c.Assert(err, ErrorMatches, `yaml: input error: some read error`)
  744. }
  745. func (s *S) TestUnmarshalNaN(c *C) {
  746. value := map[string]interface{}{}
  747. err := yaml.Unmarshal([]byte("notanum: .NaN"), &value)
  748. c.Assert(err, IsNil)
  749. c.Assert(math.IsNaN(value["notanum"].(float64)), Equals, true)
  750. }
  751. var unmarshalErrorTests = []struct {
  752. data, error string
  753. }{
  754. {"v: !!float 'error'", "yaml: cannot decode !!str `error` as a !!float"},
  755. {"v: [A,", "yaml: line 1: did not find expected node content"},
  756. {"v:\n- [A,", "yaml: line 2: did not find expected node content"},
  757. {"a: *b\n", "yaml: unknown anchor 'b' referenced"},
  758. {"a: &a\n b: *a\n", "yaml: anchor 'a' value contains itself"},
  759. {"value: -", "yaml: block sequence entries are not allowed in this context"},
  760. {"a: !!binary ==", "yaml: !!binary value contains invalid base64 data"},
  761. {"{[.]}", `yaml: invalid map key: \[\]interface \{\}\{"\."\}`},
  762. {"{{.}}", `yaml: invalid map key: map\[interface\ \{\}\]interface \{\}\{".":interface \{\}\(nil\)\}`},
  763. {"b: *a\na: &a {c: 1}", `yaml: unknown anchor 'a' referenced`},
  764. {"%TAG !%79! tag:yaml.org,2002:\n---\nv: !%79!int '1'", "yaml: did not find expected whitespace"},
  765. }
  766. func (s *S) TestUnmarshalErrors(c *C) {
  767. for i, item := range unmarshalErrorTests {
  768. c.Logf("test %d: %q", i, item.data)
  769. var value interface{}
  770. err := yaml.Unmarshal([]byte(item.data), &value)
  771. c.Assert(err, ErrorMatches, item.error, Commentf("Partial unmarshal: %#v", value))
  772. }
  773. }
  774. func (s *S) TestDecoderErrors(c *C) {
  775. for _, item := range unmarshalErrorTests {
  776. var value interface{}
  777. err := yaml.NewDecoder(strings.NewReader(item.data)).Decode(&value)
  778. c.Assert(err, ErrorMatches, item.error, Commentf("Partial unmarshal: %#v", value))
  779. }
  780. }
  781. var unmarshalerTests = []struct {
  782. data, tag string
  783. value interface{}
  784. }{
  785. {"_: {hi: there}", "!!map", map[interface{}]interface{}{"hi": "there"}},
  786. {"_: [1,A]", "!!seq", []interface{}{1, "A"}},
  787. {"_: 10", "!!int", 10},
  788. {"_: null", "!!null", nil},
  789. {`_: BAR!`, "!!str", "BAR!"},
  790. {`_: "BAR!"`, "!!str", "BAR!"},
  791. {"_: !!foo 'BAR!'", "!!foo", "BAR!"},
  792. {`_: ""`, "!!str", ""},
  793. }
  794. var unmarshalerResult = map[int]error{}
  795. type unmarshalerType struct {
  796. value interface{}
  797. }
  798. func (o *unmarshalerType) UnmarshalYAML(unmarshal func(v interface{}) error) error {
  799. if err := unmarshal(&o.value); err != nil {
  800. return err
  801. }
  802. if i, ok := o.value.(int); ok {
  803. if result, ok := unmarshalerResult[i]; ok {
  804. return result
  805. }
  806. }
  807. return nil
  808. }
  809. type unmarshalerPointer struct {
  810. Field *unmarshalerType "_"
  811. }
  812. type unmarshalerValue struct {
  813. Field unmarshalerType "_"
  814. }
  815. func (s *S) TestUnmarshalerPointerField(c *C) {
  816. for _, item := range unmarshalerTests {
  817. obj := &unmarshalerPointer{}
  818. err := yaml.Unmarshal([]byte(item.data), obj)
  819. c.Assert(err, IsNil)
  820. if item.value == nil {
  821. c.Assert(obj.Field, IsNil)
  822. } else {
  823. c.Assert(obj.Field, NotNil, Commentf("Pointer not initialized (%#v)", item.value))
  824. c.Assert(obj.Field.value, DeepEquals, item.value)
  825. }
  826. }
  827. }
  828. func (s *S) TestUnmarshalerValueField(c *C) {
  829. for _, item := range unmarshalerTests {
  830. obj := &unmarshalerValue{}
  831. err := yaml.Unmarshal([]byte(item.data), obj)
  832. c.Assert(err, IsNil)
  833. c.Assert(obj.Field, NotNil, Commentf("Pointer not initialized (%#v)", item.value))
  834. c.Assert(obj.Field.value, DeepEquals, item.value)
  835. }
  836. }
  837. func (s *S) TestUnmarshalerWholeDocument(c *C) {
  838. obj := &unmarshalerType{}
  839. err := yaml.Unmarshal([]byte(unmarshalerTests[0].data), obj)
  840. c.Assert(err, IsNil)
  841. value, ok := obj.value.(map[interface{}]interface{})
  842. c.Assert(ok, Equals, true, Commentf("value: %#v", obj.value))
  843. c.Assert(value["_"], DeepEquals, unmarshalerTests[0].value)
  844. }
  845. func (s *S) TestUnmarshalerTypeError(c *C) {
  846. unmarshalerResult[2] = &yaml.TypeError{[]string{"foo"}}
  847. unmarshalerResult[4] = &yaml.TypeError{[]string{"bar"}}
  848. defer func() {
  849. delete(unmarshalerResult, 2)
  850. delete(unmarshalerResult, 4)
  851. }()
  852. type T struct {
  853. Before int
  854. After int
  855. M map[string]*unmarshalerType
  856. }
  857. var v T
  858. data := `{before: A, m: {abc: 1, def: 2, ghi: 3, jkl: 4}, after: B}`
  859. err := yaml.Unmarshal([]byte(data), &v)
  860. c.Assert(err, ErrorMatches, ""+
  861. "yaml: unmarshal errors:\n"+
  862. " line 1: cannot unmarshal !!str `A` into int\n"+
  863. " foo\n"+
  864. " bar\n"+
  865. " line 1: cannot unmarshal !!str `B` into int")
  866. c.Assert(v.M["abc"], NotNil)
  867. c.Assert(v.M["def"], IsNil)
  868. c.Assert(v.M["ghi"], NotNil)
  869. c.Assert(v.M["jkl"], IsNil)
  870. c.Assert(v.M["abc"].value, Equals, 1)
  871. c.Assert(v.M["ghi"].value, Equals, 3)
  872. }
  873. type proxyTypeError struct{}
  874. func (v *proxyTypeError) UnmarshalYAML(unmarshal func(interface{}) error) error {
  875. var s string
  876. var a int32
  877. var b int64
  878. if err := unmarshal(&s); err != nil {
  879. panic(err)
  880. }
  881. if s == "a" {
  882. if err := unmarshal(&b); err == nil {
  883. panic("should have failed")
  884. }
  885. return unmarshal(&a)
  886. }
  887. if err := unmarshal(&a); err == nil {
  888. panic("should have failed")
  889. }
  890. return unmarshal(&b)
  891. }
  892. func (s *S) TestUnmarshalerTypeErrorProxying(c *C) {
  893. type T struct {
  894. Before int
  895. After int
  896. M map[string]*proxyTypeError
  897. }
  898. var v T
  899. data := `{before: A, m: {abc: a, def: b}, after: B}`
  900. err := yaml.Unmarshal([]byte(data), &v)
  901. c.Assert(err, ErrorMatches, ""+
  902. "yaml: unmarshal errors:\n"+
  903. " line 1: cannot unmarshal !!str `A` into int\n"+
  904. " line 1: cannot unmarshal !!str `a` into int32\n"+
  905. " line 1: cannot unmarshal !!str `b` into int64\n"+
  906. " line 1: cannot unmarshal !!str `B` into int")
  907. }
  908. type failingUnmarshaler struct{}
  909. var failingErr = errors.New("failingErr")
  910. func (ft *failingUnmarshaler) UnmarshalYAML(unmarshal func(interface{}) error) error {
  911. return failingErr
  912. }
  913. func (s *S) TestUnmarshalerError(c *C) {
  914. err := yaml.Unmarshal([]byte("a: b"), &failingUnmarshaler{})
  915. c.Assert(err, Equals, failingErr)
  916. }
  917. type sliceUnmarshaler []int
  918. func (su *sliceUnmarshaler) UnmarshalYAML(unmarshal func(interface{}) error) error {
  919. var slice []int
  920. err := unmarshal(&slice)
  921. if err == nil {
  922. *su = slice
  923. return nil
  924. }
  925. var intVal int
  926. err = unmarshal(&intVal)
  927. if err == nil {
  928. *su = []int{intVal}
  929. return nil
  930. }
  931. return err
  932. }
  933. func (s *S) TestUnmarshalerRetry(c *C) {
  934. var su sliceUnmarshaler
  935. err := yaml.Unmarshal([]byte("[1, 2, 3]"), &su)
  936. c.Assert(err, IsNil)
  937. c.Assert(su, DeepEquals, sliceUnmarshaler([]int{1, 2, 3}))
  938. err = yaml.Unmarshal([]byte("1"), &su)
  939. c.Assert(err, IsNil)
  940. c.Assert(su, DeepEquals, sliceUnmarshaler([]int{1}))
  941. }
  942. // From http://yaml.org/type/merge.html
  943. var mergeTests = `
  944. anchors:
  945. list:
  946. - &CENTER { "x": 1, "y": 2 }
  947. - &LEFT { "x": 0, "y": 2 }
  948. - &BIG { "r": 10 }
  949. - &SMALL { "r": 1 }
  950. # All the following maps are equal:
  951. plain:
  952. # Explicit keys
  953. "x": 1
  954. "y": 2
  955. "r": 10
  956. label: center/big
  957. mergeOne:
  958. # Merge one map
  959. << : *CENTER
  960. "r": 10
  961. label: center/big
  962. mergeMultiple:
  963. # Merge multiple maps
  964. << : [ *CENTER, *BIG ]
  965. label: center/big
  966. override:
  967. # Override
  968. << : [ *BIG, *LEFT, *SMALL ]
  969. "x": 1
  970. label: center/big
  971. shortTag:
  972. # Explicit short merge tag
  973. !!merge "<<" : [ *CENTER, *BIG ]
  974. label: center/big
  975. longTag:
  976. # Explicit merge long tag
  977. !<tag:yaml.org,2002:merge> "<<" : [ *CENTER, *BIG ]
  978. label: center/big
  979. inlineMap:
  980. # Inlined map
  981. << : {"x": 1, "y": 2, "r": 10}
  982. label: center/big
  983. inlineSequenceMap:
  984. # Inlined map in sequence
  985. << : [ *CENTER, {"r": 10} ]
  986. label: center/big
  987. `
  988. func (s *S) TestMerge(c *C) {
  989. var want = map[interface{}]interface{}{
  990. "x": 1,
  991. "y": 2,
  992. "r": 10,
  993. "label": "center/big",
  994. }
  995. var m map[interface{}]interface{}
  996. err := yaml.Unmarshal([]byte(mergeTests), &m)
  997. c.Assert(err, IsNil)
  998. for name, test := range m {
  999. if name == "anchors" {
  1000. continue
  1001. }
  1002. c.Assert(test, DeepEquals, want, Commentf("test %q failed", name))
  1003. }
  1004. }
  1005. func (s *S) TestMergeStruct(c *C) {
  1006. type Data struct {
  1007. X, Y, R int
  1008. Label string
  1009. }
  1010. want := Data{1, 2, 10, "center/big"}
  1011. var m map[string]Data
  1012. err := yaml.Unmarshal([]byte(mergeTests), &m)
  1013. c.Assert(err, IsNil)
  1014. for name, test := range m {
  1015. if name == "anchors" {
  1016. continue
  1017. }
  1018. c.Assert(test, Equals, want, Commentf("test %q failed", name))
  1019. }
  1020. }
  1021. var unmarshalNullTests = []func() interface{}{
  1022. func() interface{} { var v interface{}; v = "v"; return &v },
  1023. func() interface{} { var s = "s"; return &s },
  1024. func() interface{} { var s = "s"; sptr := &s; return &sptr },
  1025. func() interface{} { var i = 1; return &i },
  1026. func() interface{} { var i = 1; iptr := &i; return &iptr },
  1027. func() interface{} { m := map[string]int{"s": 1}; return &m },
  1028. func() interface{} { m := map[string]int{"s": 1}; return m },
  1029. }
  1030. func (s *S) TestUnmarshalNull(c *C) {
  1031. for _, test := range unmarshalNullTests {
  1032. item := test()
  1033. zero := reflect.Zero(reflect.TypeOf(item).Elem()).Interface()
  1034. err := yaml.Unmarshal([]byte("null"), item)
  1035. c.Assert(err, IsNil)
  1036. if reflect.TypeOf(item).Kind() == reflect.Map {
  1037. c.Assert(reflect.ValueOf(item).Interface(), DeepEquals, reflect.MakeMap(reflect.TypeOf(item)).Interface())
  1038. } else {
  1039. c.Assert(reflect.ValueOf(item).Elem().Interface(), DeepEquals, zero)
  1040. }
  1041. }
  1042. }
  1043. func (s *S) TestUnmarshalSliceOnPreset(c *C) {
  1044. // Issue #48.
  1045. v := struct{ A []int }{[]int{1}}
  1046. yaml.Unmarshal([]byte("a: [2]"), &v)
  1047. c.Assert(v.A, DeepEquals, []int{2})
  1048. }
  1049. var unmarshalStrictTests = []struct {
  1050. data string
  1051. value interface{}
  1052. error string
  1053. }{{
  1054. data: "a: 1\nc: 2\n",
  1055. value: struct{ A, B int }{A: 1},
  1056. error: `yaml: unmarshal errors:\n line 2: field c not found in type struct { A int; B int }`,
  1057. }, {
  1058. data: "a: 1\nb: 2\na: 3\n",
  1059. value: struct{ A, B int }{A: 3, B: 2},
  1060. error: `yaml: unmarshal errors:\n line 3: field a already set in type struct { A int; B int }`,
  1061. }, {
  1062. data: "c: 3\na: 1\nb: 2\nc: 4\n",
  1063. value: struct {
  1064. A int
  1065. inlineB `yaml:",inline"`
  1066. }{
  1067. A: 1,
  1068. inlineB: inlineB{
  1069. B: 2,
  1070. inlineC: inlineC{
  1071. C: 4,
  1072. },
  1073. },
  1074. },
  1075. error: `yaml: unmarshal errors:\n line 4: field c already set in type struct { A int; yaml_test.inlineB "yaml:\\",inline\\"" }`,
  1076. }, {
  1077. data: "c: 0\na: 1\nb: 2\nc: 1\n",
  1078. value: struct {
  1079. A int
  1080. inlineB `yaml:",inline"`
  1081. }{
  1082. A: 1,
  1083. inlineB: inlineB{
  1084. B: 2,
  1085. inlineC: inlineC{
  1086. C: 1,
  1087. },
  1088. },
  1089. },
  1090. error: `yaml: unmarshal errors:\n line 4: field c already set in type struct { A int; yaml_test.inlineB "yaml:\\",inline\\"" }`,
  1091. }, {
  1092. data: "c: 1\na: 1\nb: 2\nc: 3\n",
  1093. value: struct {
  1094. A int
  1095. M map[string]interface{} `yaml:",inline"`
  1096. }{
  1097. A: 1,
  1098. M: map[string]interface{}{
  1099. "b": 2,
  1100. "c": 3,
  1101. },
  1102. },
  1103. error: `yaml: unmarshal errors:\n line 4: key "c" already set in map`,
  1104. }, {
  1105. data: "a: 1\n9: 2\nnull: 3\n9: 4",
  1106. value: map[interface{}]interface{}{
  1107. "a": 1,
  1108. nil: 3,
  1109. 9: 4,
  1110. },
  1111. error: `yaml: unmarshal errors:\n line 4: key 9 already set in map`,
  1112. }}
  1113. func (s *S) TestUnmarshalStrict(c *C) {
  1114. for i, item := range unmarshalStrictTests {
  1115. c.Logf("test %d: %q", i, item.data)
  1116. // First test that normal Unmarshal unmarshals to the expected value.
  1117. t := reflect.ValueOf(item.value).Type()
  1118. value := reflect.New(t)
  1119. err := yaml.Unmarshal([]byte(item.data), value.Interface())
  1120. c.Assert(err, Equals, nil)
  1121. c.Assert(value.Elem().Interface(), DeepEquals, item.value)
  1122. // Then test that UnmarshalStrict fails on the same thing.
  1123. t = reflect.ValueOf(item.value).Type()
  1124. value = reflect.New(t)
  1125. err = yaml.UnmarshalStrict([]byte(item.data), value.Interface())
  1126. c.Assert(err, ErrorMatches, item.error)
  1127. }
  1128. }
  1129. type textUnmarshaler struct {
  1130. S string
  1131. }
  1132. func (t *textUnmarshaler) UnmarshalText(s []byte) error {
  1133. t.S = string(s)
  1134. return nil
  1135. }
  1136. //var data []byte
  1137. //func init() {
  1138. // var err error
  1139. // data, err = ioutil.ReadFile("/tmp/file.yaml")
  1140. // if err != nil {
  1141. // panic(err)
  1142. // }
  1143. //}
  1144. //
  1145. //func (s *S) BenchmarkUnmarshal(c *C) {
  1146. // var err error
  1147. // for i := 0; i < c.N; i++ {
  1148. // var v map[string]interface{}
  1149. // err = yaml.Unmarshal(data, &v)
  1150. // }
  1151. // if err != nil {
  1152. // panic(err)
  1153. // }
  1154. //}
  1155. //
  1156. //func (s *S) BenchmarkMarshal(c *C) {
  1157. // var v map[string]interface{}
  1158. // yaml.Unmarshal(data, &v)
  1159. // c.ResetTimer()
  1160. // for i := 0; i < c.N; i++ {
  1161. // yaml.Marshal(&v)
  1162. // }
  1163. //}