decode_test.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  1. package yaml_test
  2. import (
  3. "errors"
  4. . "gopkg.in/check.v1"
  5. "gopkg.in/yaml.v2"
  6. "math"
  7. "net"
  8. "reflect"
  9. "strings"
  10. "time"
  11. )
  12. var unmarshalIntTest = 123
  13. var unmarshalTests = []struct {
  14. data string
  15. value interface{}
  16. }{
  17. {
  18. "",
  19. &struct{}{},
  20. }, {
  21. "{}", &struct{}{},
  22. }, {
  23. "v: hi",
  24. map[string]string{"v": "hi"},
  25. }, {
  26. "v: hi", map[string]interface{}{"v": "hi"},
  27. }, {
  28. "v: true",
  29. map[string]string{"v": "true"},
  30. }, {
  31. "v: true",
  32. map[string]interface{}{"v": true},
  33. }, {
  34. "v: 10",
  35. map[string]interface{}{"v": 10},
  36. }, {
  37. "v: 0b10",
  38. map[string]interface{}{"v": 2},
  39. }, {
  40. "v: 0xA",
  41. map[string]interface{}{"v": 10},
  42. }, {
  43. "v: 4294967296",
  44. map[string]int64{"v": 4294967296},
  45. }, {
  46. "v: 0.1",
  47. map[string]interface{}{"v": 0.1},
  48. }, {
  49. "v: .1",
  50. map[string]interface{}{"v": 0.1},
  51. }, {
  52. "v: .Inf",
  53. map[string]interface{}{"v": math.Inf(+1)},
  54. }, {
  55. "v: -.Inf",
  56. map[string]interface{}{"v": math.Inf(-1)},
  57. }, {
  58. "v: -10",
  59. map[string]interface{}{"v": -10},
  60. }, {
  61. "v: -.1",
  62. map[string]interface{}{"v": -0.1},
  63. },
  64. // Simple values.
  65. {
  66. "123",
  67. &unmarshalIntTest,
  68. },
  69. // Floats from spec
  70. {
  71. "canonical: 6.8523e+5",
  72. map[string]interface{}{"canonical": 6.8523e+5},
  73. }, {
  74. "expo: 685.230_15e+03",
  75. map[string]interface{}{"expo": 685.23015e+03},
  76. }, {
  77. "fixed: 685_230.15",
  78. map[string]interface{}{"fixed": 685230.15},
  79. }, {
  80. "neginf: -.inf",
  81. map[string]interface{}{"neginf": math.Inf(-1)},
  82. }, {
  83. "fixed: 685_230.15",
  84. map[string]float64{"fixed": 685230.15},
  85. },
  86. //{"sexa: 190:20:30.15", map[string]interface{}{"sexa": 0}}, // Unsupported
  87. //{"notanum: .NaN", map[string]interface{}{"notanum": math.NaN()}}, // Equality of NaN fails.
  88. // Bools from spec
  89. {
  90. "canonical: y",
  91. map[string]interface{}{"canonical": true},
  92. }, {
  93. "answer: NO",
  94. map[string]interface{}{"answer": false},
  95. }, {
  96. "logical: True",
  97. map[string]interface{}{"logical": true},
  98. }, {
  99. "option: on",
  100. map[string]interface{}{"option": true},
  101. }, {
  102. "option: on",
  103. map[string]bool{"option": true},
  104. },
  105. // Ints from spec
  106. {
  107. "canonical: 685230",
  108. map[string]interface{}{"canonical": 685230},
  109. }, {
  110. "decimal: +685_230",
  111. map[string]interface{}{"decimal": 685230},
  112. }, {
  113. "octal: 02472256",
  114. map[string]interface{}{"octal": 685230},
  115. }, {
  116. "hexa: 0x_0A_74_AE",
  117. map[string]interface{}{"hexa": 685230},
  118. }, {
  119. "bin: 0b1010_0111_0100_1010_1110",
  120. map[string]interface{}{"bin": 685230},
  121. }, {
  122. "bin: -0b101010",
  123. map[string]interface{}{"bin": -42},
  124. }, {
  125. "decimal: +685_230",
  126. map[string]int{"decimal": 685230},
  127. },
  128. //{"sexa: 190:20:30", map[string]interface{}{"sexa": 0}}, // Unsupported
  129. // Nulls from spec
  130. {
  131. "empty:",
  132. map[string]interface{}{"empty": nil},
  133. }, {
  134. "canonical: ~",
  135. map[string]interface{}{"canonical": nil},
  136. }, {
  137. "english: null",
  138. map[string]interface{}{"english": nil},
  139. }, {
  140. "~: null key",
  141. map[interface{}]string{nil: "null key"},
  142. }, {
  143. "empty:",
  144. map[string]*bool{"empty": nil},
  145. },
  146. // Flow sequence
  147. {
  148. "seq: [A,B]",
  149. map[string]interface{}{"seq": []interface{}{"A", "B"}},
  150. }, {
  151. "seq: [A,B,C,]",
  152. map[string][]string{"seq": []string{"A", "B", "C"}},
  153. }, {
  154. "seq: [A,1,C]",
  155. map[string][]string{"seq": []string{"A", "1", "C"}},
  156. }, {
  157. "seq: [A,1,C]",
  158. map[string][]int{"seq": []int{1}},
  159. }, {
  160. "seq: [A,1,C]",
  161. map[string]interface{}{"seq": []interface{}{"A", 1, "C"}},
  162. },
  163. // Block sequence
  164. {
  165. "seq:\n - A\n - B",
  166. map[string]interface{}{"seq": []interface{}{"A", "B"}},
  167. }, {
  168. "seq:\n - A\n - B\n - C",
  169. map[string][]string{"seq": []string{"A", "B", "C"}},
  170. }, {
  171. "seq:\n - A\n - 1\n - C",
  172. map[string][]string{"seq": []string{"A", "1", "C"}},
  173. }, {
  174. "seq:\n - A\n - 1\n - C",
  175. map[string][]int{"seq": []int{1}},
  176. }, {
  177. "seq:\n - A\n - 1\n - C",
  178. map[string]interface{}{"seq": []interface{}{"A", 1, "C"}},
  179. },
  180. // Literal block scalar
  181. {
  182. "scalar: | # Comment\n\n literal\n\n \ttext\n\n",
  183. map[string]string{"scalar": "\nliteral\n\n\ttext\n"},
  184. },
  185. // Folded block scalar
  186. {
  187. "scalar: > # Comment\n\n folded\n line\n \n next\n line\n * one\n * two\n\n last\n line\n\n",
  188. map[string]string{"scalar": "\nfolded line\nnext line\n * one\n * two\n\nlast line\n"},
  189. },
  190. // Map inside interface with no type hints.
  191. {
  192. "a: {b: c}",
  193. map[interface{}]interface{}{"a": map[interface{}]interface{}{"b": "c"}},
  194. },
  195. // Structs and type conversions.
  196. {
  197. "hello: world",
  198. &struct{ Hello string }{"world"},
  199. }, {
  200. "a: {b: c}",
  201. &struct{ A struct{ B string } }{struct{ B string }{"c"}},
  202. }, {
  203. "a: {b: c}",
  204. &struct{ A *struct{ B string } }{&struct{ B string }{"c"}},
  205. }, {
  206. "a: {b: c}",
  207. &struct{ A map[string]string }{map[string]string{"b": "c"}},
  208. }, {
  209. "a: {b: c}",
  210. &struct{ A *map[string]string }{&map[string]string{"b": "c"}},
  211. }, {
  212. "a:",
  213. &struct{ A map[string]string }{},
  214. }, {
  215. "a: 1",
  216. &struct{ A int }{1},
  217. }, {
  218. "a: 1",
  219. &struct{ A float64 }{1},
  220. }, {
  221. "a: 1.0",
  222. &struct{ A int }{1},
  223. }, {
  224. "a: 1.0",
  225. &struct{ A uint }{1},
  226. }, {
  227. "a: [1, 2]",
  228. &struct{ A []int }{[]int{1, 2}},
  229. }, {
  230. "a: 1",
  231. &struct{ B int }{0},
  232. }, {
  233. "a: 1",
  234. &struct {
  235. B int "a"
  236. }{1},
  237. }, {
  238. "a: y",
  239. &struct{ A bool }{true},
  240. },
  241. // Some cross type conversions
  242. {
  243. "v: 42",
  244. map[string]uint{"v": 42},
  245. }, {
  246. "v: -42",
  247. map[string]uint{},
  248. }, {
  249. "v: 4294967296",
  250. map[string]uint64{"v": 4294967296},
  251. }, {
  252. "v: -4294967296",
  253. map[string]uint64{},
  254. },
  255. // int
  256. {
  257. "int_max: 2147483647",
  258. map[string]int{"int_max": math.MaxInt32},
  259. },
  260. {
  261. "int_min: -2147483648",
  262. map[string]int{"int_min": math.MinInt32},
  263. },
  264. {
  265. "int_overflow: 9223372036854775808", // math.MaxInt64 + 1
  266. map[string]int{},
  267. },
  268. // int64
  269. {
  270. "int64_max: 9223372036854775807",
  271. map[string]int64{"int64_max": math.MaxInt64},
  272. },
  273. {
  274. "int64_max_base2: 0b111111111111111111111111111111111111111111111111111111111111111",
  275. map[string]int64{"int64_max_base2": math.MaxInt64},
  276. },
  277. {
  278. "int64_min: -9223372036854775808",
  279. map[string]int64{"int64_min": math.MinInt64},
  280. },
  281. {
  282. "int64_neg_base2: -0b111111111111111111111111111111111111111111111111111111111111111",
  283. map[string]int64{"int64_neg_base2": -math.MaxInt64},
  284. },
  285. {
  286. "int64_overflow: 9223372036854775808", // math.MaxInt64 + 1
  287. map[string]int64{},
  288. },
  289. // uint
  290. {
  291. "uint_min: 0",
  292. map[string]uint{"uint_min": 0},
  293. },
  294. {
  295. "uint_max: 4294967295",
  296. map[string]uint{"uint_max": math.MaxUint32},
  297. },
  298. {
  299. "uint_underflow: -1",
  300. map[string]uint{},
  301. },
  302. // uint64
  303. {
  304. "uint64_min: 0",
  305. map[string]uint{"uint64_min": 0},
  306. },
  307. {
  308. "uint64_max: 18446744073709551615",
  309. map[string]uint64{"uint64_max": math.MaxUint64},
  310. },
  311. {
  312. "uint64_max_base2: 0b1111111111111111111111111111111111111111111111111111111111111111",
  313. map[string]uint64{"uint64_max_base2": math.MaxUint64},
  314. },
  315. {
  316. "uint64_maxint64: 9223372036854775807",
  317. map[string]uint64{"uint64_maxint64": math.MaxInt64},
  318. },
  319. {
  320. "uint64_underflow: -1",
  321. map[string]uint64{},
  322. },
  323. // float32
  324. {
  325. "float32_max: 3.40282346638528859811704183484516925440e+38",
  326. map[string]float32{"float32_max": math.MaxFloat32},
  327. },
  328. {
  329. "float32_nonzero: 1.401298464324817070923729583289916131280e-45",
  330. map[string]float32{"float32_nonzero": math.SmallestNonzeroFloat32},
  331. },
  332. {
  333. "float32_maxuint64: 18446744073709551615",
  334. map[string]float32{"float32_maxuint64": float32(math.MaxUint64)},
  335. },
  336. {
  337. "float32_maxuint64+1: 18446744073709551616",
  338. map[string]float32{"float32_maxuint64+1": float32(math.MaxUint64 + 1)},
  339. },
  340. // float64
  341. {
  342. "float64_max: 1.797693134862315708145274237317043567981e+308",
  343. map[string]float64{"float64_max": math.MaxFloat64},
  344. },
  345. {
  346. "float64_nonzero: 4.940656458412465441765687928682213723651e-324",
  347. map[string]float64{"float64_nonzero": math.SmallestNonzeroFloat64},
  348. },
  349. {
  350. "float64_maxuint64: 18446744073709551615",
  351. map[string]float64{"float64_maxuint64": float64(math.MaxUint64)},
  352. },
  353. {
  354. "float64_maxuint64+1: 18446744073709551616",
  355. map[string]float64{"float64_maxuint64+1": float64(math.MaxUint64 + 1)},
  356. },
  357. // Overflow cases.
  358. {
  359. "v: 4294967297",
  360. map[string]int32{},
  361. }, {
  362. "v: 128",
  363. map[string]int8{},
  364. },
  365. // Quoted values.
  366. {
  367. "'1': '\"2\"'",
  368. map[interface{}]interface{}{"1": "\"2\""},
  369. }, {
  370. "v:\n- A\n- 'B\n\n C'\n",
  371. map[string][]string{"v": []string{"A", "B\nC"}},
  372. },
  373. // Explicit tags.
  374. {
  375. "v: !!float '1.1'",
  376. map[string]interface{}{"v": 1.1},
  377. }, {
  378. "v: !!null ''",
  379. map[string]interface{}{"v": nil},
  380. }, {
  381. "%TAG !y! tag:yaml.org,2002:\n---\nv: !y!int '1'",
  382. map[string]interface{}{"v": 1},
  383. },
  384. // Non-specific tag (Issue #75)
  385. {
  386. "v: ! test",
  387. map[string]interface{}{"v": "test"},
  388. },
  389. // Anchors and aliases.
  390. {
  391. "a: &x 1\nb: &y 2\nc: *x\nd: *y\n",
  392. &struct{ A, B, C, D int }{1, 2, 1, 2},
  393. }, {
  394. "a: &a {c: 1}\nb: *a",
  395. &struct {
  396. A, B struct {
  397. C int
  398. }
  399. }{struct{ C int }{1}, struct{ C int }{1}},
  400. }, {
  401. "a: &a [1, 2]\nb: *a",
  402. &struct{ B []int }{[]int{1, 2}},
  403. }, {
  404. "b: *a\na: &a {c: 1}",
  405. &struct {
  406. A, B struct {
  407. C int
  408. }
  409. }{struct{ C int }{1}, struct{ C int }{1}},
  410. },
  411. // Bug #1133337
  412. {
  413. "foo: ''",
  414. map[string]*string{"foo": new(string)},
  415. }, {
  416. "foo: null",
  417. map[string]*string{"foo": nil},
  418. }, {
  419. "foo: null",
  420. map[string]string{"foo": ""},
  421. }, {
  422. "foo: null",
  423. map[string]interface{}{"foo": nil},
  424. },
  425. // Support for ~
  426. {
  427. "foo: ~",
  428. map[string]*string{"foo": nil},
  429. }, {
  430. "foo: ~",
  431. map[string]string{"foo": ""},
  432. }, {
  433. "foo: ~",
  434. map[string]interface{}{"foo": nil},
  435. },
  436. // Ignored field
  437. {
  438. "a: 1\nb: 2\n",
  439. &struct {
  440. A int
  441. B int "-"
  442. }{1, 0},
  443. },
  444. // Bug #1191981
  445. {
  446. "" +
  447. "%YAML 1.1\n" +
  448. "--- !!str\n" +
  449. `"Generic line break (no glyph)\n\` + "\n" +
  450. ` Generic line break (glyphed)\n\` + "\n" +
  451. ` Line separator\u2028\` + "\n" +
  452. ` Paragraph separator\u2029"` + "\n",
  453. "" +
  454. "Generic line break (no glyph)\n" +
  455. "Generic line break (glyphed)\n" +
  456. "Line separator\u2028Paragraph separator\u2029",
  457. },
  458. // Struct inlining
  459. {
  460. "a: 1\nb: 2\nc: 3\n",
  461. &struct {
  462. A int
  463. C inlineB `yaml:",inline"`
  464. }{1, inlineB{2, inlineC{3}}},
  465. },
  466. // Map inlining
  467. {
  468. "a: 1\nb: 2\nc: 3\n",
  469. &struct {
  470. A int
  471. C map[string]int `yaml:",inline"`
  472. }{1, map[string]int{"b": 2, "c": 3}},
  473. },
  474. // bug 1243827
  475. {
  476. "a: -b_c",
  477. map[string]interface{}{"a": "-b_c"},
  478. },
  479. {
  480. "a: +b_c",
  481. map[string]interface{}{"a": "+b_c"},
  482. },
  483. {
  484. "a: 50cent_of_dollar",
  485. map[string]interface{}{"a": "50cent_of_dollar"},
  486. },
  487. // Duration
  488. {
  489. "a: 3s",
  490. map[string]time.Duration{"a": 3 * time.Second},
  491. },
  492. // Issue #24.
  493. {
  494. "a: <foo>",
  495. map[string]string{"a": "<foo>"},
  496. },
  497. // Base 60 floats are obsolete and unsupported.
  498. {
  499. "a: 1:1\n",
  500. map[string]string{"a": "1:1"},
  501. },
  502. // Binary data.
  503. {
  504. "a: !!binary gIGC\n",
  505. map[string]string{"a": "\x80\x81\x82"},
  506. }, {
  507. "a: !!binary |\n " + strings.Repeat("kJCQ", 17) + "kJ\n CQ\n",
  508. map[string]string{"a": strings.Repeat("\x90", 54)},
  509. }, {
  510. "a: !!binary |\n " + strings.Repeat("A", 70) + "\n ==\n",
  511. map[string]string{"a": strings.Repeat("\x00", 52)},
  512. },
  513. // Ordered maps.
  514. {
  515. "{b: 2, a: 1, d: 4, c: 3, sub: {e: 5}}",
  516. &yaml.MapSlice{{"b", 2}, {"a", 1}, {"d", 4}, {"c", 3}, {"sub", yaml.MapSlice{{"e", 5}}}},
  517. },
  518. // Issue #39.
  519. {
  520. "a:\n b:\n c: d\n",
  521. map[string]struct{ B interface{} }{"a": {map[interface{}]interface{}{"c": "d"}}},
  522. },
  523. // Custom map type.
  524. {
  525. "a: {b: c}",
  526. M{"a": M{"b": "c"}},
  527. },
  528. // Support encoding.TextUnmarshaler.
  529. {
  530. "a: 1.2.3.4\n",
  531. map[string]net.IP{"a": net.IPv4(1, 2, 3, 4)},
  532. },
  533. {
  534. "a: 2015-02-24T18:19:39Z\n",
  535. map[string]time.Time{"a": time.Unix(1424801979, 0).In(time.UTC)},
  536. },
  537. // Encode empty lists as zero-length slices.
  538. {
  539. "a: []",
  540. &struct{ A []int }{[]int{}},
  541. },
  542. // UTF-16-LE
  543. {
  544. "\xff\xfe\xf1\x00o\x00\xf1\x00o\x00:\x00 \x00v\x00e\x00r\x00y\x00 \x00y\x00e\x00s\x00\n\x00",
  545. M{"ñoño": "very yes"},
  546. },
  547. // UTF-16-LE with surrogate.
  548. {
  549. "\xff\xfe\xf1\x00o\x00\xf1\x00o\x00:\x00 \x00v\x00e\x00r\x00y\x00 \x00y\x00e\x00s\x00 \x00=\xd8\xd4\xdf\n\x00",
  550. M{"ñoño": "very yes 🟔"},
  551. },
  552. // UTF-16-BE
  553. {
  554. "\xfe\xff\x00\xf1\x00o\x00\xf1\x00o\x00:\x00 \x00v\x00e\x00r\x00y\x00 \x00y\x00e\x00s\x00\n",
  555. M{"ñoño": "very yes"},
  556. },
  557. // UTF-16-BE with surrogate.
  558. {
  559. "\xfe\xff\x00\xf1\x00o\x00\xf1\x00o\x00:\x00 \x00v\x00e\x00r\x00y\x00 \x00y\x00e\x00s\x00 \xd8=\xdf\xd4\x00\n",
  560. M{"ñoño": "very yes 🟔"},
  561. },
  562. // YAML Float regex shouldn't match this
  563. {
  564. "a: 123456e1\n",
  565. M{"a": "123456e1"},
  566. }, {
  567. "a: 123456E1\n",
  568. M{"a": "123456E1"},
  569. },
  570. }
  571. type M map[interface{}]interface{}
  572. type inlineB struct {
  573. B int
  574. inlineC `yaml:",inline"`
  575. }
  576. type inlineC struct {
  577. C int
  578. }
  579. func (s *S) TestUnmarshal(c *C) {
  580. for i, item := range unmarshalTests {
  581. c.Logf("test %d: %q", i, item.data)
  582. t := reflect.ValueOf(item.value).Type()
  583. var value interface{}
  584. switch t.Kind() {
  585. case reflect.Map:
  586. value = reflect.MakeMap(t).Interface()
  587. case reflect.String:
  588. value = reflect.New(t).Interface()
  589. case reflect.Ptr:
  590. value = reflect.New(t.Elem()).Interface()
  591. default:
  592. c.Fatalf("missing case for %s", t)
  593. }
  594. err := yaml.Unmarshal([]byte(item.data), value)
  595. if _, ok := err.(*yaml.TypeError); !ok {
  596. c.Assert(err, IsNil)
  597. }
  598. if t.Kind() == reflect.String {
  599. c.Assert(*value.(*string), Equals, item.value)
  600. } else {
  601. c.Assert(value, DeepEquals, item.value)
  602. }
  603. }
  604. }
  605. func (s *S) TestUnmarshalNaN(c *C) {
  606. value := map[string]interface{}{}
  607. err := yaml.Unmarshal([]byte("notanum: .NaN"), &value)
  608. c.Assert(err, IsNil)
  609. c.Assert(math.IsNaN(value["notanum"].(float64)), Equals, true)
  610. }
  611. var unmarshalErrorTests = []struct {
  612. data, error string
  613. }{
  614. {"v: !!float 'error'", "yaml: cannot decode !!str `error` as a !!float"},
  615. {"v: [A,", "yaml: line 1: did not find expected node content"},
  616. {"v:\n- [A,", "yaml: line 2: did not find expected node content"},
  617. {"a: *b\n", "yaml: unknown anchor 'b' referenced"},
  618. {"a: &a\n b: *a\n", "yaml: anchor 'a' value contains itself"},
  619. {"value: -", "yaml: block sequence entries are not allowed in this context"},
  620. {"a: !!binary ==", "yaml: !!binary value contains invalid base64 data"},
  621. {"{[.]}", `yaml: invalid map key: \[\]interface \{\}\{"\."\}`},
  622. {"{{.}}", `yaml: invalid map key: map\[interface\ \{\}\]interface \{\}\{".":interface \{\}\(nil\)\}`},
  623. {"%TAG !%79! tag:yaml.org,2002:\n---\nv: !%79!int '1'", "yaml: did not find expected whitespace"},
  624. }
  625. func (s *S) TestUnmarshalErrors(c *C) {
  626. for _, item := range unmarshalErrorTests {
  627. var value interface{}
  628. err := yaml.Unmarshal([]byte(item.data), &value)
  629. c.Assert(err, ErrorMatches, item.error, Commentf("Partial unmarshal: %#v", value))
  630. }
  631. }
  632. var unmarshalerTests = []struct {
  633. data, tag string
  634. value interface{}
  635. }{
  636. {"_: {hi: there}", "!!map", map[interface{}]interface{}{"hi": "there"}},
  637. {"_: [1,A]", "!!seq", []interface{}{1, "A"}},
  638. {"_: 10", "!!int", 10},
  639. {"_: null", "!!null", nil},
  640. {`_: BAR!`, "!!str", "BAR!"},
  641. {`_: "BAR!"`, "!!str", "BAR!"},
  642. {"_: !!foo 'BAR!'", "!!foo", "BAR!"},
  643. {`_: ""`, "!!str", ""},
  644. }
  645. var unmarshalerResult = map[int]error{}
  646. type unmarshalerType struct {
  647. value interface{}
  648. }
  649. func (o *unmarshalerType) UnmarshalYAML(unmarshal func(v interface{}) error) error {
  650. if err := unmarshal(&o.value); err != nil {
  651. return err
  652. }
  653. if i, ok := o.value.(int); ok {
  654. if result, ok := unmarshalerResult[i]; ok {
  655. return result
  656. }
  657. }
  658. return nil
  659. }
  660. type unmarshalerPointer struct {
  661. Field *unmarshalerType "_"
  662. }
  663. type unmarshalerValue struct {
  664. Field unmarshalerType "_"
  665. }
  666. func (s *S) TestUnmarshalerPointerField(c *C) {
  667. for _, item := range unmarshalerTests {
  668. obj := &unmarshalerPointer{}
  669. err := yaml.Unmarshal([]byte(item.data), obj)
  670. c.Assert(err, IsNil)
  671. if item.value == nil {
  672. c.Assert(obj.Field, IsNil)
  673. } else {
  674. c.Assert(obj.Field, NotNil, Commentf("Pointer not initialized (%#v)", item.value))
  675. c.Assert(obj.Field.value, DeepEquals, item.value)
  676. }
  677. }
  678. }
  679. func (s *S) TestUnmarshalerValueField(c *C) {
  680. for _, item := range unmarshalerTests {
  681. obj := &unmarshalerValue{}
  682. err := yaml.Unmarshal([]byte(item.data), obj)
  683. c.Assert(err, IsNil)
  684. c.Assert(obj.Field, NotNil, Commentf("Pointer not initialized (%#v)", item.value))
  685. c.Assert(obj.Field.value, DeepEquals, item.value)
  686. }
  687. }
  688. func (s *S) TestUnmarshalerWholeDocument(c *C) {
  689. obj := &unmarshalerType{}
  690. err := yaml.Unmarshal([]byte(unmarshalerTests[0].data), obj)
  691. c.Assert(err, IsNil)
  692. value, ok := obj.value.(map[interface{}]interface{})
  693. c.Assert(ok, Equals, true, Commentf("value: %#v", obj.value))
  694. c.Assert(value["_"], DeepEquals, unmarshalerTests[0].value)
  695. }
  696. func (s *S) TestUnmarshalerTypeError(c *C) {
  697. unmarshalerResult[2] = &yaml.TypeError{[]string{"foo"}}
  698. unmarshalerResult[4] = &yaml.TypeError{[]string{"bar"}}
  699. defer func() {
  700. delete(unmarshalerResult, 2)
  701. delete(unmarshalerResult, 4)
  702. }()
  703. type T struct {
  704. Before int
  705. After int
  706. M map[string]*unmarshalerType
  707. }
  708. var v T
  709. data := `{before: A, m: {abc: 1, def: 2, ghi: 3, jkl: 4}, after: B}`
  710. err := yaml.Unmarshal([]byte(data), &v)
  711. c.Assert(err, ErrorMatches, ""+
  712. "yaml: unmarshal errors:\n"+
  713. " line 1: cannot unmarshal !!str `A` into int\n"+
  714. " foo\n"+
  715. " bar\n"+
  716. " line 1: cannot unmarshal !!str `B` into int")
  717. c.Assert(v.M["abc"], NotNil)
  718. c.Assert(v.M["def"], IsNil)
  719. c.Assert(v.M["ghi"], NotNil)
  720. c.Assert(v.M["jkl"], IsNil)
  721. c.Assert(v.M["abc"].value, Equals, 1)
  722. c.Assert(v.M["ghi"].value, Equals, 3)
  723. }
  724. type proxyTypeError struct{}
  725. func (v *proxyTypeError) UnmarshalYAML(unmarshal func(interface{}) error) error {
  726. var s string
  727. var a int32
  728. var b int64
  729. if err := unmarshal(&s); err != nil {
  730. panic(err)
  731. }
  732. if s == "a" {
  733. if err := unmarshal(&b); err == nil {
  734. panic("should have failed")
  735. }
  736. return unmarshal(&a)
  737. }
  738. if err := unmarshal(&a); err == nil {
  739. panic("should have failed")
  740. }
  741. return unmarshal(&b)
  742. }
  743. func (s *S) TestUnmarshalerTypeErrorProxying(c *C) {
  744. type T struct {
  745. Before int
  746. After int
  747. M map[string]*proxyTypeError
  748. }
  749. var v T
  750. data := `{before: A, m: {abc: a, def: b}, after: B}`
  751. err := yaml.Unmarshal([]byte(data), &v)
  752. c.Assert(err, ErrorMatches, ""+
  753. "yaml: unmarshal errors:\n"+
  754. " line 1: cannot unmarshal !!str `A` into int\n"+
  755. " line 1: cannot unmarshal !!str `a` into int32\n"+
  756. " line 1: cannot unmarshal !!str `b` into int64\n"+
  757. " line 1: cannot unmarshal !!str `B` into int")
  758. }
  759. type failingUnmarshaler struct{}
  760. var failingErr = errors.New("failingErr")
  761. func (ft *failingUnmarshaler) UnmarshalYAML(unmarshal func(interface{}) error) error {
  762. return failingErr
  763. }
  764. func (s *S) TestUnmarshalerError(c *C) {
  765. err := yaml.Unmarshal([]byte("a: b"), &failingUnmarshaler{})
  766. c.Assert(err, Equals, failingErr)
  767. }
  768. type sliceUnmarshaler []int
  769. func (su *sliceUnmarshaler) UnmarshalYAML(unmarshal func(interface{}) error) error {
  770. var slice []int
  771. err := unmarshal(&slice)
  772. if err == nil {
  773. *su = slice
  774. return nil
  775. }
  776. var intVal int
  777. err = unmarshal(&intVal)
  778. if err == nil {
  779. *su = []int{intVal}
  780. return nil
  781. }
  782. return err
  783. }
  784. func (s *S) TestUnmarshalerRetry(c *C) {
  785. var su sliceUnmarshaler
  786. err := yaml.Unmarshal([]byte("[1, 2, 3]"), &su)
  787. c.Assert(err, IsNil)
  788. c.Assert(su, DeepEquals, sliceUnmarshaler([]int{1, 2, 3}))
  789. err = yaml.Unmarshal([]byte("1"), &su)
  790. c.Assert(err, IsNil)
  791. c.Assert(su, DeepEquals, sliceUnmarshaler([]int{1}))
  792. }
  793. // From http://yaml.org/type/merge.html
  794. var mergeTests = `
  795. anchors:
  796. list:
  797. - &CENTER { "x": 1, "y": 2 }
  798. - &LEFT { "x": 0, "y": 2 }
  799. - &BIG { "r": 10 }
  800. - &SMALL { "r": 1 }
  801. # All the following maps are equal:
  802. plain:
  803. # Explicit keys
  804. "x": 1
  805. "y": 2
  806. "r": 10
  807. label: center/big
  808. mergeOne:
  809. # Merge one map
  810. << : *CENTER
  811. "r": 10
  812. label: center/big
  813. mergeMultiple:
  814. # Merge multiple maps
  815. << : [ *CENTER, *BIG ]
  816. label: center/big
  817. override:
  818. # Override
  819. << : [ *BIG, *LEFT, *SMALL ]
  820. "x": 1
  821. label: center/big
  822. shortTag:
  823. # Explicit short merge tag
  824. !!merge "<<" : [ *CENTER, *BIG ]
  825. label: center/big
  826. longTag:
  827. # Explicit merge long tag
  828. !<tag:yaml.org,2002:merge> "<<" : [ *CENTER, *BIG ]
  829. label: center/big
  830. inlineMap:
  831. # Inlined map
  832. << : {"x": 1, "y": 2, "r": 10}
  833. label: center/big
  834. inlineSequenceMap:
  835. # Inlined map in sequence
  836. << : [ *CENTER, {"r": 10} ]
  837. label: center/big
  838. `
  839. func (s *S) TestMerge(c *C) {
  840. var want = map[interface{}]interface{}{
  841. "x": 1,
  842. "y": 2,
  843. "r": 10,
  844. "label": "center/big",
  845. }
  846. var m map[interface{}]interface{}
  847. err := yaml.Unmarshal([]byte(mergeTests), &m)
  848. c.Assert(err, IsNil)
  849. for name, test := range m {
  850. if name == "anchors" {
  851. continue
  852. }
  853. c.Assert(test, DeepEquals, want, Commentf("test %q failed", name))
  854. }
  855. }
  856. func (s *S) TestMergeStruct(c *C) {
  857. type Data struct {
  858. X, Y, R int
  859. Label string
  860. }
  861. want := Data{1, 2, 10, "center/big"}
  862. var m map[string]Data
  863. err := yaml.Unmarshal([]byte(mergeTests), &m)
  864. c.Assert(err, IsNil)
  865. for name, test := range m {
  866. if name == "anchors" {
  867. continue
  868. }
  869. c.Assert(test, Equals, want, Commentf("test %q failed", name))
  870. }
  871. }
  872. var unmarshalNullTests = []func() interface{}{
  873. func() interface{} { var v interface{}; v = "v"; return &v },
  874. func() interface{} { var s = "s"; return &s },
  875. func() interface{} { var s = "s"; sptr := &s; return &sptr },
  876. func() interface{} { var i = 1; return &i },
  877. func() interface{} { var i = 1; iptr := &i; return &iptr },
  878. func() interface{} { m := map[string]int{"s": 1}; return &m },
  879. func() interface{} { m := map[string]int{"s": 1}; return m },
  880. }
  881. func (s *S) TestUnmarshalNull(c *C) {
  882. for _, test := range unmarshalNullTests {
  883. item := test()
  884. zero := reflect.Zero(reflect.TypeOf(item).Elem()).Interface()
  885. err := yaml.Unmarshal([]byte("null"), item)
  886. c.Assert(err, IsNil)
  887. if reflect.TypeOf(item).Kind() == reflect.Map {
  888. c.Assert(reflect.ValueOf(item).Interface(), DeepEquals, reflect.MakeMap(reflect.TypeOf(item)).Interface())
  889. } else {
  890. c.Assert(reflect.ValueOf(item).Elem().Interface(), DeepEquals, zero)
  891. }
  892. }
  893. }
  894. func (s *S) TestUnmarshalSliceOnPreset(c *C) {
  895. // Issue #48.
  896. v := struct{ A []int }{[]int{1}}
  897. yaml.Unmarshal([]byte("a: [2]"), &v)
  898. c.Assert(v.A, DeepEquals, []int{2})
  899. }
  900. func (s *S) TestUnmarshalStrict(c *C) {
  901. v := struct{ A, B int }{}
  902. err := yaml.UnmarshalStrict([]byte("a: 1\nb: 2"), &v)
  903. c.Check(err, IsNil)
  904. err = yaml.Unmarshal([]byte("a: 1\nb: 2\nc: 3"), &v)
  905. c.Check(err, IsNil)
  906. err = yaml.UnmarshalStrict([]byte("a: 1\nb: 2\nc: 3"), &v)
  907. c.Check(err, ErrorMatches, "yaml: unmarshal errors:\n line 3: field c not found in struct struct { A int; B int }")
  908. }
  909. //var data []byte
  910. //func init() {
  911. // var err error
  912. // data, err = ioutil.ReadFile("/tmp/file.yaml")
  913. // if err != nil {
  914. // panic(err)
  915. // }
  916. //}
  917. //
  918. //func (s *S) BenchmarkUnmarshal(c *C) {
  919. // var err error
  920. // for i := 0; i < c.N; i++ {
  921. // var v map[string]interface{}
  922. // err = yaml.Unmarshal(data, &v)
  923. // }
  924. // if err != nil {
  925. // panic(err)
  926. // }
  927. //}
  928. //
  929. //func (s *S) BenchmarkMarshal(c *C) {
  930. // var v map[string]interface{}
  931. // yaml.Unmarshal(data, &v)
  932. // c.ResetTimer()
  933. // for i := 0; i < c.N; i++ {
  934. // yaml.Marshal(&v)
  935. // }
  936. //}