decode_test.go 39 KB

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