yamlunmarshaler_test.go 18 KB

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