yamlunmarshaler_test.go 20 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  1. package mapping
  2. import (
  3. "reflect"
  4. "strings"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. "k8s.io/utils/io"
  8. )
  9. func TestUnmarshalYamlBytes(t *testing.T) {
  10. var c struct {
  11. Name string
  12. }
  13. content := []byte(`Name: liao`)
  14. assert.Nil(t, UnmarshalYamlBytes(content, &c))
  15. assert.Equal(t, "liao", c.Name)
  16. }
  17. func TestUnmarshalYamlBytesErrorInput(t *testing.T) {
  18. var c struct {
  19. Name string
  20. }
  21. content := []byte(`liao`)
  22. assert.NotNil(t, UnmarshalYamlBytes(content, &c))
  23. }
  24. func TestUnmarshalYamlBytesEmptyInput(t *testing.T) {
  25. var c struct {
  26. Name string
  27. }
  28. content := []byte(``)
  29. assert.NotNil(t, UnmarshalYamlBytes(content, &c))
  30. }
  31. func TestUnmarshalYamlBytesOptional(t *testing.T) {
  32. var c struct {
  33. Name string
  34. Age int `json:",optional"`
  35. }
  36. content := []byte(`Name: liao`)
  37. assert.Nil(t, UnmarshalYamlBytes(content, &c))
  38. assert.Equal(t, "liao", c.Name)
  39. }
  40. func TestUnmarshalYamlBytesOptionalDefault(t *testing.T) {
  41. var c struct {
  42. Name string
  43. Age int `json:",optional,default=1"`
  44. }
  45. content := []byte(`Name: liao`)
  46. assert.Nil(t, UnmarshalYamlBytes(content, &c))
  47. assert.Equal(t, "liao", c.Name)
  48. assert.Equal(t, 1, c.Age)
  49. }
  50. func TestUnmarshalYamlBytesDefaultOptional(t *testing.T) {
  51. var c struct {
  52. Name string
  53. Age int `json:",default=1,optional"`
  54. }
  55. content := []byte(`Name: liao`)
  56. assert.Nil(t, UnmarshalYamlBytes(content, &c))
  57. assert.Equal(t, "liao", c.Name)
  58. assert.Equal(t, 1, c.Age)
  59. }
  60. func TestUnmarshalYamlBytesDefault(t *testing.T) {
  61. var c struct {
  62. Name string `json:",default=liao"`
  63. }
  64. content := []byte(`{}`)
  65. assert.Nil(t, UnmarshalYamlBytes(content, &c))
  66. assert.Equal(t, "liao", c.Name)
  67. }
  68. func TestUnmarshalYamlBytesBool(t *testing.T) {
  69. var c struct {
  70. Great bool
  71. }
  72. content := []byte(`Great: true`)
  73. assert.Nil(t, UnmarshalYamlBytes(content, &c))
  74. assert.True(t, c.Great)
  75. }
  76. func TestUnmarshalYamlBytesInt(t *testing.T) {
  77. var c struct {
  78. Age int
  79. }
  80. content := []byte(`Age: 1`)
  81. assert.Nil(t, UnmarshalYamlBytes(content, &c))
  82. assert.Equal(t, 1, c.Age)
  83. }
  84. func TestUnmarshalYamlBytesUint(t *testing.T) {
  85. var c struct {
  86. Age uint
  87. }
  88. content := []byte(`Age: 1`)
  89. assert.Nil(t, UnmarshalYamlBytes(content, &c))
  90. assert.Equal(t, uint(1), c.Age)
  91. }
  92. func TestUnmarshalYamlBytesFloat(t *testing.T) {
  93. var c struct {
  94. Age float32
  95. }
  96. content := []byte(`Age: 1.5`)
  97. assert.Nil(t, UnmarshalYamlBytes(content, &c))
  98. assert.Equal(t, float32(1.5), c.Age)
  99. }
  100. func TestUnmarshalYamlBytesMustInOptional(t *testing.T) {
  101. var c struct {
  102. Inner struct {
  103. There string
  104. Must string
  105. Optional string `json:",optional"`
  106. } `json:",optional"`
  107. }
  108. content := []byte(`{}`)
  109. assert.Nil(t, UnmarshalYamlBytes(content, &c))
  110. }
  111. func TestUnmarshalYamlBytesMustInOptionalMissedPart(t *testing.T) {
  112. var c struct {
  113. Inner struct {
  114. There string
  115. Must string
  116. Optional string `json:",optional"`
  117. } `json:",optional"`
  118. }
  119. content := []byte(`Inner:
  120. There: sure`)
  121. assert.NotNil(t, UnmarshalYamlBytes(content, &c))
  122. }
  123. func TestUnmarshalYamlBytesMustInOptionalOnlyOptionalFilled(t *testing.T) {
  124. var c struct {
  125. Inner struct {
  126. There string
  127. Must string
  128. Optional string `json:",optional"`
  129. } `json:",optional"`
  130. }
  131. content := []byte(`Inner:
  132. Optional: sure`)
  133. assert.NotNil(t, UnmarshalYamlBytes(content, &c))
  134. }
  135. func TestUnmarshalYamlBytesPartial(t *testing.T) {
  136. var c struct {
  137. Name string
  138. Age float32
  139. }
  140. content := []byte(`Age: 1.5`)
  141. assert.NotNil(t, UnmarshalYamlBytes(content, &c))
  142. }
  143. func TestUnmarshalYamlBytesStruct(t *testing.T) {
  144. var c struct {
  145. Inner struct {
  146. Name string
  147. }
  148. }
  149. content := []byte(`Inner:
  150. Name: liao`)
  151. assert.Nil(t, UnmarshalYamlBytes(content, &c))
  152. assert.Equal(t, "liao", c.Inner.Name)
  153. }
  154. func TestUnmarshalYamlBytesStructOptional(t *testing.T) {
  155. var c struct {
  156. Inner struct {
  157. Name string
  158. Age int `json:",optional"`
  159. }
  160. }
  161. content := []byte(`Inner:
  162. Name: liao`)
  163. assert.Nil(t, UnmarshalYamlBytes(content, &c))
  164. assert.Equal(t, "liao", c.Inner.Name)
  165. }
  166. func TestUnmarshalYamlBytesStructPtr(t *testing.T) {
  167. var c struct {
  168. Inner *struct {
  169. Name string
  170. }
  171. }
  172. content := []byte(`Inner:
  173. Name: liao`)
  174. assert.Nil(t, UnmarshalYamlBytes(content, &c))
  175. assert.Equal(t, "liao", c.Inner.Name)
  176. }
  177. func TestUnmarshalYamlBytesStructPtrOptional(t *testing.T) {
  178. var c struct {
  179. Inner *struct {
  180. Name string
  181. Age int `json:",optional"`
  182. }
  183. }
  184. content := []byte(`Inner:
  185. Name: liao`)
  186. assert.Nil(t, UnmarshalYamlBytes(content, &c))
  187. }
  188. func TestUnmarshalYamlBytesStructPtrDefault(t *testing.T) {
  189. var c struct {
  190. Inner *struct {
  191. Name string
  192. Age int `json:",default=4"`
  193. }
  194. }
  195. content := []byte(`Inner:
  196. Name: liao`)
  197. assert.Nil(t, UnmarshalYamlBytes(content, &c))
  198. assert.Equal(t, "liao", c.Inner.Name)
  199. assert.Equal(t, 4, c.Inner.Age)
  200. }
  201. func TestUnmarshalYamlBytesSliceString(t *testing.T) {
  202. var c struct {
  203. Names []string
  204. }
  205. content := []byte(`Names:
  206. - liao
  207. - chaoxin`)
  208. assert.Nil(t, UnmarshalYamlBytes(content, &c))
  209. want := []string{"liao", "chaoxin"}
  210. if !reflect.DeepEqual(c.Names, want) {
  211. t.Fatalf("want %q, got %q", c.Names, want)
  212. }
  213. }
  214. func TestUnmarshalYamlBytesSliceStringOptional(t *testing.T) {
  215. var c struct {
  216. Names []string
  217. Age []int `json:",optional"`
  218. }
  219. content := []byte(`Names:
  220. - liao
  221. - chaoxin`)
  222. assert.Nil(t, UnmarshalYamlBytes(content, &c))
  223. want := []string{"liao", "chaoxin"}
  224. if !reflect.DeepEqual(c.Names, want) {
  225. t.Fatalf("want %q, got %q", c.Names, want)
  226. }
  227. }
  228. func TestUnmarshalYamlBytesSliceStruct(t *testing.T) {
  229. var c struct {
  230. People []struct {
  231. Name string
  232. Age int
  233. }
  234. }
  235. content := []byte(`People:
  236. - Name: liao
  237. Age: 1
  238. - Name: chaoxin
  239. Age: 2`)
  240. assert.Nil(t, UnmarshalYamlBytes(content, &c))
  241. want := []struct {
  242. Name string
  243. Age int
  244. }{
  245. {"liao", 1},
  246. {"chaoxin", 2},
  247. }
  248. if !reflect.DeepEqual(c.People, want) {
  249. t.Fatalf("want %q, got %q", c.People, want)
  250. }
  251. }
  252. func TestUnmarshalYamlBytesSliceStructOptional(t *testing.T) {
  253. var c struct {
  254. People []struct {
  255. Name string
  256. Age int
  257. Emails []string `json:",optional"`
  258. }
  259. }
  260. content := []byte(`People:
  261. - Name: liao
  262. Age: 1
  263. - Name: chaoxin
  264. Age: 2`)
  265. assert.Nil(t, UnmarshalYamlBytes(content, &c))
  266. want := []struct {
  267. Name string
  268. Age int
  269. Emails []string `json:",optional"`
  270. }{
  271. {"liao", 1, nil},
  272. {"chaoxin", 2, nil},
  273. }
  274. if !reflect.DeepEqual(c.People, want) {
  275. t.Fatalf("want %q, got %q", c.People, want)
  276. }
  277. }
  278. func TestUnmarshalYamlBytesSliceStructPtr(t *testing.T) {
  279. var c struct {
  280. People []*struct {
  281. Name string
  282. Age int
  283. }
  284. }
  285. content := []byte(`People:
  286. - Name: liao
  287. Age: 1
  288. - Name: chaoxin
  289. Age: 2`)
  290. assert.Nil(t, UnmarshalYamlBytes(content, &c))
  291. want := []*struct {
  292. Name string
  293. Age int
  294. }{
  295. {"liao", 1},
  296. {"chaoxin", 2},
  297. }
  298. if !reflect.DeepEqual(c.People, want) {
  299. t.Fatalf("want %v, got %v", c.People, want)
  300. }
  301. }
  302. func TestUnmarshalYamlBytesSliceStructPtrOptional(t *testing.T) {
  303. var c struct {
  304. People []*struct {
  305. Name string
  306. Age int
  307. Emails []string `json:",optional"`
  308. }
  309. }
  310. content := []byte(`People:
  311. - Name: liao
  312. Age: 1
  313. - Name: chaoxin
  314. Age: 2`)
  315. assert.Nil(t, UnmarshalYamlBytes(content, &c))
  316. want := []*struct {
  317. Name string
  318. Age int
  319. Emails []string `json:",optional"`
  320. }{
  321. {"liao", 1, nil},
  322. {"chaoxin", 2, nil},
  323. }
  324. if !reflect.DeepEqual(c.People, want) {
  325. t.Fatalf("want %v, got %v", c.People, want)
  326. }
  327. }
  328. func TestUnmarshalYamlBytesSliceStructPtrPartial(t *testing.T) {
  329. var c struct {
  330. People []*struct {
  331. Name string
  332. Age int
  333. Email string
  334. }
  335. }
  336. content := []byte(`People:
  337. - Name: liao
  338. Age: 1
  339. - Name: chaoxin
  340. Age: 2`)
  341. assert.NotNil(t, UnmarshalYamlBytes(content, &c))
  342. }
  343. func TestUnmarshalYamlBytesSliceStructPtrDefault(t *testing.T) {
  344. var c struct {
  345. People []*struct {
  346. Name string
  347. Age int
  348. Email string `json:",default=chaoxin@liao.com"`
  349. }
  350. }
  351. content := []byte(`People:
  352. - Name: liao
  353. Age: 1
  354. - Name: chaoxin
  355. Age: 2`)
  356. assert.Nil(t, UnmarshalYamlBytes(content, &c))
  357. want := []*struct {
  358. Name string
  359. Age int
  360. Email string
  361. }{
  362. {"liao", 1, "chaoxin@liao.com"},
  363. {"chaoxin", 2, "chaoxin@liao.com"},
  364. }
  365. for i := range c.People {
  366. actual := c.People[i]
  367. expect := want[i]
  368. assert.Equal(t, expect.Age, actual.Age)
  369. assert.Equal(t, expect.Email, actual.Email)
  370. assert.Equal(t, expect.Name, actual.Name)
  371. }
  372. }
  373. func TestUnmarshalYamlBytesSliceStringPartial(t *testing.T) {
  374. var c struct {
  375. Names []string
  376. Age int
  377. }
  378. content := []byte(`Age: 1`)
  379. assert.NotNil(t, UnmarshalYamlBytes(content, &c))
  380. }
  381. func TestUnmarshalYamlBytesSliceStructPartial(t *testing.T) {
  382. var c struct {
  383. Group string
  384. People []struct {
  385. Name string
  386. Age int
  387. }
  388. }
  389. content := []byte(`Group: chaoxin`)
  390. assert.NotNil(t, UnmarshalYamlBytes(content, &c))
  391. }
  392. func TestUnmarshalYamlBytesInnerAnonymousPartial(t *testing.T) {
  393. type (
  394. Deep struct {
  395. A string
  396. B string `json:",optional"`
  397. }
  398. Inner struct {
  399. Deep
  400. InnerV string `json:",optional"`
  401. }
  402. )
  403. var c struct {
  404. Value Inner `json:",optional"`
  405. }
  406. content := []byte(`Value:
  407. InnerV: chaoxin`)
  408. assert.NotNil(t, UnmarshalYamlBytes(content, &c))
  409. }
  410. func TestUnmarshalYamlBytesStructPartial(t *testing.T) {
  411. var c struct {
  412. Group string
  413. Person struct {
  414. Name string
  415. Age int
  416. }
  417. }
  418. content := []byte(`Group: chaoxin`)
  419. assert.NotNil(t, UnmarshalYamlBytes(content, &c))
  420. }
  421. func TestUnmarshalYamlBytesEmptyMap(t *testing.T) {
  422. var c struct {
  423. Persons map[string]int `json:",optional"`
  424. }
  425. content := []byte(`{}`)
  426. assert.Nil(t, UnmarshalYamlBytes(content, &c))
  427. assert.Empty(t, c.Persons)
  428. }
  429. func TestUnmarshalYamlBytesMap(t *testing.T) {
  430. var c struct {
  431. Persons map[string]int
  432. }
  433. content := []byte(`Persons:
  434. first: 1
  435. second: 2`)
  436. assert.Nil(t, UnmarshalYamlBytes(content, &c))
  437. assert.Equal(t, 2, len(c.Persons))
  438. assert.Equal(t, 1, c.Persons["first"])
  439. assert.Equal(t, 2, c.Persons["second"])
  440. }
  441. func TestUnmarshalYamlBytesMapStruct(t *testing.T) {
  442. var c struct {
  443. Persons map[string]struct {
  444. ID int
  445. Name string `json:"name,optional"`
  446. }
  447. }
  448. content := []byte(`Persons:
  449. first:
  450. ID: 1
  451. name: kevin`)
  452. assert.Nil(t, UnmarshalYamlBytes(content, &c))
  453. assert.Equal(t, 1, len(c.Persons))
  454. assert.Equal(t, 1, c.Persons["first"].ID)
  455. assert.Equal(t, "kevin", c.Persons["first"].Name)
  456. }
  457. func TestUnmarshalYamlBytesMapStructPtr(t *testing.T) {
  458. var c struct {
  459. Persons map[string]*struct {
  460. ID int
  461. Name string `json:"name,optional"`
  462. }
  463. }
  464. content := []byte(`Persons:
  465. first:
  466. ID: 1
  467. name: kevin`)
  468. assert.Nil(t, UnmarshalYamlBytes(content, &c))
  469. assert.Equal(t, 1, len(c.Persons))
  470. assert.Equal(t, 1, c.Persons["first"].ID)
  471. assert.Equal(t, "kevin", c.Persons["first"].Name)
  472. }
  473. func TestUnmarshalYamlBytesMapStructMissingPartial(t *testing.T) {
  474. var c struct {
  475. Persons map[string]*struct {
  476. ID int
  477. Name string
  478. }
  479. }
  480. content := []byte(`Persons:
  481. first:
  482. ID: 1`)
  483. assert.NotNil(t, UnmarshalYamlBytes(content, &c))
  484. }
  485. func TestUnmarshalYamlBytesMapStructOptional(t *testing.T) {
  486. var c struct {
  487. Persons map[string]*struct {
  488. ID int
  489. Name string `json:"name,optional"`
  490. }
  491. }
  492. content := []byte(`Persons:
  493. first:
  494. ID: 1`)
  495. assert.Nil(t, UnmarshalYamlBytes(content, &c))
  496. assert.Equal(t, 1, len(c.Persons))
  497. assert.Equal(t, 1, c.Persons["first"].ID)
  498. }
  499. func TestUnmarshalYamlBytesMapStructSlice(t *testing.T) {
  500. var c struct {
  501. Persons map[string][]struct {
  502. ID int
  503. Name string `json:"name,optional"`
  504. }
  505. }
  506. content := []byte(`Persons:
  507. first:
  508. - ID: 1
  509. name: kevin`)
  510. assert.Nil(t, UnmarshalYamlBytes(content, &c))
  511. assert.Equal(t, 1, len(c.Persons))
  512. assert.Equal(t, 1, c.Persons["first"][0].ID)
  513. assert.Equal(t, "kevin", c.Persons["first"][0].Name)
  514. }
  515. func TestUnmarshalYamlBytesMapEmptyStructSlice(t *testing.T) {
  516. var c struct {
  517. Persons map[string][]struct {
  518. ID int
  519. Name string `json:"name,optional"`
  520. }
  521. }
  522. content := []byte(`Persons:
  523. first: []`)
  524. assert.Nil(t, UnmarshalYamlBytes(content, &c))
  525. assert.Equal(t, 1, len(c.Persons))
  526. assert.Empty(t, c.Persons["first"])
  527. }
  528. func TestUnmarshalYamlBytesMapStructPtrSlice(t *testing.T) {
  529. var c struct {
  530. Persons map[string][]*struct {
  531. ID int
  532. Name string `json:"name,optional"`
  533. }
  534. }
  535. content := []byte(`Persons:
  536. first:
  537. - ID: 1
  538. name: kevin`)
  539. assert.Nil(t, UnmarshalYamlBytes(content, &c))
  540. assert.Equal(t, 1, len(c.Persons))
  541. assert.Equal(t, 1, c.Persons["first"][0].ID)
  542. assert.Equal(t, "kevin", c.Persons["first"][0].Name)
  543. }
  544. func TestUnmarshalYamlBytesMapEmptyStructPtrSlice(t *testing.T) {
  545. var c struct {
  546. Persons map[string][]*struct {
  547. ID int
  548. Name string `json:"name,optional"`
  549. }
  550. }
  551. content := []byte(`Persons:
  552. first: []`)
  553. assert.Nil(t, UnmarshalYamlBytes(content, &c))
  554. assert.Equal(t, 1, len(c.Persons))
  555. assert.Empty(t, c.Persons["first"])
  556. }
  557. func TestUnmarshalYamlBytesMapStructPtrSliceMissingPartial(t *testing.T) {
  558. var c struct {
  559. Persons map[string][]*struct {
  560. ID int
  561. Name string
  562. }
  563. }
  564. content := []byte(`Persons:
  565. first:
  566. - ID: 1`)
  567. assert.NotNil(t, UnmarshalYamlBytes(content, &c))
  568. }
  569. func TestUnmarshalYamlBytesMapStructPtrSliceOptional(t *testing.T) {
  570. var c struct {
  571. Persons map[string][]*struct {
  572. ID int
  573. Name string `json:"name,optional"`
  574. }
  575. }
  576. content := []byte(`Persons:
  577. first:
  578. - ID: 1`)
  579. assert.Nil(t, UnmarshalYamlBytes(content, &c))
  580. assert.Equal(t, 1, len(c.Persons))
  581. assert.Equal(t, 1, c.Persons["first"][0].ID)
  582. }
  583. func TestUnmarshalYamlStructOptional(t *testing.T) {
  584. var c struct {
  585. Name string
  586. Etcd struct {
  587. Hosts []string
  588. Key string
  589. } `json:",optional"`
  590. }
  591. content := []byte(`Name: kevin`)
  592. err := UnmarshalYamlBytes(content, &c)
  593. assert.Nil(t, err)
  594. assert.Equal(t, "kevin", c.Name)
  595. }
  596. func TestUnmarshalYamlStructLowerCase(t *testing.T) {
  597. var c struct {
  598. Name string
  599. Etcd struct {
  600. Key string
  601. } `json:"etcd"`
  602. }
  603. content := []byte(`Name: kevin
  604. etcd:
  605. Key: the key`)
  606. err := UnmarshalYamlBytes(content, &c)
  607. assert.Nil(t, err)
  608. assert.Equal(t, "kevin", c.Name)
  609. assert.Equal(t, "the key", c.Etcd.Key)
  610. }
  611. func TestUnmarshalYamlWithStructAllOptionalWithEmpty(t *testing.T) {
  612. var c struct {
  613. Inner struct {
  614. Optional string `json:",optional"`
  615. }
  616. Else string
  617. }
  618. content := []byte(`Else: sure`)
  619. assert.Nil(t, UnmarshalYamlBytes(content, &c))
  620. }
  621. func TestUnmarshalYamlWithStructAllOptionalPtr(t *testing.T) {
  622. var c struct {
  623. Inner *struct {
  624. Optional string `json:",optional"`
  625. }
  626. Else string
  627. }
  628. content := []byte(`Else: sure`)
  629. assert.Nil(t, UnmarshalYamlBytes(content, &c))
  630. }
  631. func TestUnmarshalYamlWithStructOptional(t *testing.T) {
  632. type Inner struct {
  633. Must string
  634. }
  635. var c struct {
  636. In Inner `json:",optional"`
  637. Else string
  638. }
  639. content := []byte(`Else: sure`)
  640. assert.Nil(t, UnmarshalYamlBytes(content, &c))
  641. assert.Equal(t, "sure", c.Else)
  642. assert.Equal(t, "", c.In.Must)
  643. }
  644. func TestUnmarshalYamlWithStructPtrOptional(t *testing.T) {
  645. type Inner struct {
  646. Must string
  647. }
  648. var c struct {
  649. In *Inner `json:",optional"`
  650. Else string
  651. }
  652. content := []byte(`Else: sure`)
  653. assert.Nil(t, UnmarshalYamlBytes(content, &c))
  654. assert.Equal(t, "sure", c.Else)
  655. assert.Nil(t, c.In)
  656. }
  657. func TestUnmarshalYamlWithStructAllOptionalAnonymous(t *testing.T) {
  658. type Inner struct {
  659. Optional string `json:",optional"`
  660. }
  661. var c struct {
  662. Inner
  663. Else string
  664. }
  665. content := []byte(`Else: sure`)
  666. assert.Nil(t, UnmarshalYamlBytes(content, &c))
  667. }
  668. func TestUnmarshalYamlWithStructAllOptionalAnonymousPtr(t *testing.T) {
  669. type Inner struct {
  670. Optional string `json:",optional"`
  671. }
  672. var c struct {
  673. *Inner
  674. Else string
  675. }
  676. content := []byte(`Else: sure`)
  677. assert.Nil(t, UnmarshalYamlBytes(content, &c))
  678. }
  679. func TestUnmarshalYamlWithStructAllOptionalProvoidedAnonymous(t *testing.T) {
  680. type Inner struct {
  681. Optional string `json:",optional"`
  682. }
  683. var c struct {
  684. Inner
  685. Else string
  686. }
  687. content := []byte(`Else: sure
  688. Optional: optional`)
  689. assert.Nil(t, UnmarshalYamlBytes(content, &c))
  690. assert.Equal(t, "sure", c.Else)
  691. assert.Equal(t, "optional", c.Optional)
  692. }
  693. func TestUnmarshalYamlWithStructAllOptionalProvoidedAnonymousPtr(t *testing.T) {
  694. type Inner struct {
  695. Optional string `json:",optional"`
  696. }
  697. var c struct {
  698. *Inner
  699. Else string
  700. }
  701. content := []byte(`Else: sure
  702. Optional: optional`)
  703. assert.Nil(t, UnmarshalYamlBytes(content, &c))
  704. assert.Equal(t, "sure", c.Else)
  705. assert.Equal(t, "optional", c.Optional)
  706. }
  707. func TestUnmarshalYamlWithStructAnonymous(t *testing.T) {
  708. type Inner struct {
  709. Must string
  710. }
  711. var c struct {
  712. Inner
  713. Else string
  714. }
  715. content := []byte(`Else: sure
  716. Must: must`)
  717. assert.Nil(t, UnmarshalYamlBytes(content, &c))
  718. assert.Equal(t, "sure", c.Else)
  719. assert.Equal(t, "must", c.Must)
  720. }
  721. func TestUnmarshalYamlWithStructAnonymousPtr(t *testing.T) {
  722. type Inner struct {
  723. Must string
  724. }
  725. var c struct {
  726. *Inner
  727. Else string
  728. }
  729. content := []byte(`Else: sure
  730. Must: must`)
  731. assert.Nil(t, UnmarshalYamlBytes(content, &c))
  732. assert.Equal(t, "sure", c.Else)
  733. assert.Equal(t, "must", c.Must)
  734. }
  735. func TestUnmarshalYamlWithStructAnonymousOptional(t *testing.T) {
  736. type Inner struct {
  737. Must string
  738. }
  739. var c struct {
  740. Inner `json:",optional"`
  741. Else string
  742. }
  743. content := []byte(`Else: sure`)
  744. assert.Nil(t, UnmarshalYamlBytes(content, &c))
  745. assert.Equal(t, "sure", c.Else)
  746. assert.Equal(t, "", c.Must)
  747. }
  748. func TestUnmarshalYamlWithStructPtrAnonymousOptional(t *testing.T) {
  749. type Inner struct {
  750. Must string
  751. }
  752. var c struct {
  753. *Inner `json:",optional"`
  754. Else string
  755. }
  756. content := []byte(`Else: sure`)
  757. assert.Nil(t, UnmarshalYamlBytes(content, &c))
  758. assert.Equal(t, "sure", c.Else)
  759. assert.Nil(t, c.Inner)
  760. }
  761. func TestUnmarshalYamlWithZeroValues(t *testing.T) {
  762. type inner struct {
  763. False bool `json:"negative"`
  764. Int int `json:"int"`
  765. String string `json:"string"`
  766. }
  767. content := []byte(`negative: false
  768. int: 0
  769. string: ""`)
  770. var in inner
  771. ast := assert.New(t)
  772. ast.Nil(UnmarshalYamlBytes(content, &in))
  773. ast.False(in.False)
  774. ast.Equal(0, in.Int)
  775. ast.Equal("", in.String)
  776. }
  777. func TestUnmarshalYamlBytesError(t *testing.T) {
  778. payload := `abcd:
  779. - cdef`
  780. var v struct {
  781. Any []string `json:"abcd"`
  782. }
  783. err := UnmarshalYamlBytes([]byte(payload), &v)
  784. assert.Nil(t, err)
  785. assert.Equal(t, 1, len(v.Any))
  786. assert.Equal(t, "cdef", v.Any[0])
  787. }
  788. func TestUnmarshalYamlReaderError(t *testing.T) {
  789. var v struct {
  790. Any string
  791. }
  792. reader := strings.NewReader(`abcd: cdef`)
  793. err := UnmarshalYamlReader(reader, &v)
  794. assert.NotNil(t, err)
  795. reader = strings.NewReader("foo")
  796. assert.Error(t, UnmarshalYamlReader(reader, &v))
  797. }
  798. func TestUnmarshalYamlBadReader(t *testing.T) {
  799. var v struct {
  800. Any string
  801. }
  802. err := UnmarshalYamlReader(new(badReader), &v)
  803. assert.NotNil(t, err)
  804. }
  805. func TestUnmarshalYamlMapBool(t *testing.T) {
  806. text := `machine:
  807. node1: true
  808. node2: true
  809. node3: true
  810. `
  811. var v struct {
  812. Machine map[string]bool `json:"machine,optional"`
  813. }
  814. reader := strings.NewReader(text)
  815. assert.Nil(t, UnmarshalYamlReader(reader, &v))
  816. assert.True(t, v.Machine["node1"])
  817. assert.True(t, v.Machine["node2"])
  818. assert.True(t, v.Machine["node3"])
  819. }
  820. func TestUnmarshalYamlMapInt(t *testing.T) {
  821. text := `machine:
  822. node1: 1
  823. node2: 2
  824. node3: 3
  825. `
  826. var v struct {
  827. Machine map[string]int `json:"machine,optional"`
  828. }
  829. reader := strings.NewReader(text)
  830. assert.Nil(t, UnmarshalYamlReader(reader, &v))
  831. assert.Equal(t, 1, v.Machine["node1"])
  832. assert.Equal(t, 2, v.Machine["node2"])
  833. assert.Equal(t, 3, v.Machine["node3"])
  834. }
  835. func TestUnmarshalYamlMapByte(t *testing.T) {
  836. text := `machine:
  837. node1: 1
  838. node2: 2
  839. node3: 3
  840. `
  841. var v struct {
  842. Machine map[string]byte `json:"machine,optional"`
  843. }
  844. reader := strings.NewReader(text)
  845. assert.Nil(t, UnmarshalYamlReader(reader, &v))
  846. assert.Equal(t, byte(1), v.Machine["node1"])
  847. assert.Equal(t, byte(2), v.Machine["node2"])
  848. assert.Equal(t, byte(3), v.Machine["node3"])
  849. }
  850. func TestUnmarshalYamlMapRune(t *testing.T) {
  851. text := `machine:
  852. node1: 1
  853. node2: 2
  854. node3: 3
  855. `
  856. var v struct {
  857. Machine map[string]rune `json:"machine,optional"`
  858. }
  859. reader := strings.NewReader(text)
  860. assert.Nil(t, UnmarshalYamlReader(reader, &v))
  861. assert.Equal(t, rune(1), v.Machine["node1"])
  862. assert.Equal(t, rune(2), v.Machine["node2"])
  863. assert.Equal(t, rune(3), v.Machine["node3"])
  864. }
  865. func TestUnmarshalYamlBadInput(t *testing.T) {
  866. var v struct {
  867. Any string
  868. }
  869. assert.Error(t, UnmarshalYamlBytes([]byte("':foo"), &v))
  870. }
  871. type badReader struct{}
  872. func (b *badReader) Read(_ []byte) (n int, err error) {
  873. return 0, io.ErrLimitReached
  874. }