cast_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  1. // Copyright 2021-2023 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package cast
  15. import (
  16. "encoding/hex"
  17. "errors"
  18. "fmt"
  19. "net"
  20. "reflect"
  21. "testing"
  22. "github.com/stretchr/testify/assert"
  23. )
  24. func TestToStringAlways(t *testing.T) {
  25. tests := []struct {
  26. input any
  27. want string
  28. }{
  29. {
  30. "test",
  31. "test",
  32. },
  33. {
  34. 100,
  35. "100",
  36. },
  37. {
  38. nil,
  39. "",
  40. },
  41. }
  42. for _, tt := range tests {
  43. assert.Equal(t, tt.want, ToStringAlways(tt.input))
  44. }
  45. }
  46. func TestToString(t *testing.T) {
  47. tests := []struct {
  48. input any
  49. sn Strictness
  50. want string
  51. }{
  52. {
  53. "test",
  54. CONVERT_SAMEKIND,
  55. "test",
  56. },
  57. {
  58. "test",
  59. CONVERT_ALL,
  60. "test",
  61. },
  62. {
  63. []byte("test"),
  64. CONVERT_SAMEKIND,
  65. "test",
  66. },
  67. {
  68. true,
  69. CONVERT_ALL,
  70. "true",
  71. },
  72. {
  73. nil,
  74. CONVERT_ALL,
  75. "",
  76. },
  77. {
  78. 100,
  79. CONVERT_ALL,
  80. "100",
  81. },
  82. {
  83. int8(100),
  84. CONVERT_ALL,
  85. "100",
  86. },
  87. {
  88. int16(100),
  89. CONVERT_ALL,
  90. "100",
  91. },
  92. {
  93. int32(100),
  94. CONVERT_ALL,
  95. "100",
  96. },
  97. {
  98. int64(100),
  99. CONVERT_ALL,
  100. "100",
  101. },
  102. {
  103. uint(100),
  104. CONVERT_ALL,
  105. "100",
  106. },
  107. {
  108. uint8(100),
  109. CONVERT_ALL,
  110. "100",
  111. },
  112. {
  113. uint16(100),
  114. CONVERT_ALL,
  115. "100",
  116. },
  117. {
  118. uint32(100),
  119. CONVERT_ALL,
  120. "100",
  121. },
  122. {
  123. uint64(100),
  124. CONVERT_ALL,
  125. "100",
  126. },
  127. {
  128. float32(100.001),
  129. CONVERT_ALL,
  130. "100.001",
  131. },
  132. {
  133. 100.001,
  134. CONVERT_ALL,
  135. "100.001",
  136. },
  137. {
  138. // Stringer test case
  139. net.IPv4(0, 0, 0, 0),
  140. CONVERT_ALL,
  141. "0.0.0.0",
  142. },
  143. {
  144. errors.New("test"),
  145. CONVERT_ALL,
  146. "test",
  147. },
  148. }
  149. for _, tt := range tests {
  150. got, err := ToString(tt.input, tt.sn)
  151. assert.NoError(t, err)
  152. assert.Equal(t, tt.want, got)
  153. }
  154. _, err := ToString(struct{}{}, STRICT)
  155. assert.Error(t, err)
  156. }
  157. func TestToIntResult(t *testing.T) {
  158. tests := []struct {
  159. input any
  160. want int64
  161. }{
  162. {
  163. 100,
  164. 100,
  165. },
  166. {
  167. int8(100),
  168. 100,
  169. },
  170. {
  171. int16(100),
  172. 100,
  173. },
  174. {
  175. int32(100),
  176. 100,
  177. },
  178. {
  179. int64(100),
  180. 100,
  181. },
  182. {
  183. uint(100),
  184. 100,
  185. },
  186. {
  187. uint8(100),
  188. 100,
  189. },
  190. {
  191. uint16(100),
  192. 100,
  193. },
  194. {
  195. uint32(100),
  196. 100,
  197. },
  198. {
  199. uint64(100),
  200. 100,
  201. },
  202. {
  203. float32(100),
  204. 100,
  205. },
  206. {
  207. float64(100),
  208. 100,
  209. },
  210. {
  211. "100",
  212. 100,
  213. },
  214. {
  215. false,
  216. 0,
  217. },
  218. {
  219. true,
  220. 1,
  221. },
  222. {
  223. nil,
  224. 0,
  225. },
  226. }
  227. for _, tt := range tests {
  228. var (
  229. got any
  230. err error
  231. )
  232. got, err = ToInt(tt.input, CONVERT_ALL)
  233. assert.NoError(t, err)
  234. assert.Equal(t, int(tt.want), got)
  235. got, err = ToInt8(tt.input, CONVERT_ALL)
  236. assert.NoError(t, err)
  237. assert.Equal(t, int8(tt.want), got)
  238. got, err = ToInt16(tt.input, CONVERT_ALL)
  239. assert.NoError(t, err)
  240. assert.Equal(t, int16(tt.want), got)
  241. got, err = ToInt32(tt.input, CONVERT_ALL)
  242. assert.NoError(t, err)
  243. assert.Equal(t, int32(tt.want), got)
  244. got, err = ToInt64(tt.input, CONVERT_ALL)
  245. assert.NoError(t, err)
  246. assert.Equal(t, tt.want, got)
  247. }
  248. errTests := []any{
  249. true,
  250. nil,
  251. "1",
  252. }
  253. for _, input := range errTests {
  254. _, err := ToInt(input, STRICT)
  255. assert.Error(t, err)
  256. _, err = ToInt8(input, STRICT)
  257. assert.Error(t, err)
  258. _, err = ToInt16(input, STRICT)
  259. assert.Error(t, err)
  260. _, err = ToInt32(input, STRICT)
  261. assert.Error(t, err)
  262. _, err = ToInt64(input, STRICT)
  263. assert.Error(t, err)
  264. }
  265. }
  266. func TestToFloatResult(t *testing.T) {
  267. tests := []struct {
  268. input any
  269. want float64
  270. }{
  271. {
  272. 100,
  273. 100,
  274. },
  275. {
  276. int8(100),
  277. 100,
  278. },
  279. {
  280. int16(100),
  281. 100,
  282. },
  283. {
  284. int32(100),
  285. 100,
  286. },
  287. {
  288. int64(100),
  289. 100,
  290. },
  291. {
  292. uint(100),
  293. 100,
  294. },
  295. {
  296. uint8(100),
  297. 100,
  298. },
  299. {
  300. uint16(100),
  301. 100,
  302. },
  303. {
  304. uint32(100),
  305. 100,
  306. },
  307. {
  308. uint64(100),
  309. 100,
  310. },
  311. {
  312. float32(100),
  313. 100,
  314. },
  315. {
  316. float64(100),
  317. 100,
  318. },
  319. {
  320. "100",
  321. 100,
  322. },
  323. {
  324. false,
  325. 0,
  326. },
  327. {
  328. true,
  329. 1,
  330. },
  331. }
  332. for _, tt := range tests {
  333. var (
  334. got any
  335. err error
  336. )
  337. got, err = ToFloat32(tt.input, CONVERT_ALL)
  338. assert.NoError(t, err)
  339. assert.Equal(t, float32(tt.want), got)
  340. got, err = ToFloat64(tt.input, CONVERT_ALL)
  341. assert.NoError(t, err)
  342. assert.Equal(t, tt.want, got)
  343. }
  344. errTests := []any{
  345. 1,
  346. int8(1),
  347. int16(1),
  348. int32(1),
  349. int64(1),
  350. uint(1),
  351. uint8(1),
  352. uint16(1),
  353. uint32(1),
  354. uint64(1),
  355. true,
  356. nil,
  357. "1",
  358. }
  359. for _, input := range errTests {
  360. _, err := ToFloat32(input, STRICT)
  361. assert.Error(t, err)
  362. _, err = ToFloat64(input, STRICT)
  363. assert.Error(t, err)
  364. }
  365. }
  366. func TestToUintResult(t *testing.T) {
  367. tests := []struct {
  368. input any
  369. want uint64
  370. }{
  371. {
  372. 100,
  373. 100,
  374. },
  375. {
  376. int8(100),
  377. 100,
  378. },
  379. {
  380. int16(100),
  381. 100,
  382. },
  383. {
  384. int32(100),
  385. 100,
  386. },
  387. {
  388. int64(100),
  389. 100,
  390. },
  391. {
  392. uint(100),
  393. 100,
  394. },
  395. {
  396. uint8(100),
  397. 100,
  398. },
  399. {
  400. uint16(100),
  401. 100,
  402. },
  403. {
  404. uint32(100),
  405. 100,
  406. },
  407. {
  408. uint64(100),
  409. 100,
  410. },
  411. {
  412. float32(100),
  413. 100,
  414. },
  415. {
  416. float64(100),
  417. 100,
  418. },
  419. {
  420. "100",
  421. 100,
  422. },
  423. {
  424. false,
  425. 0,
  426. },
  427. {
  428. true,
  429. 1,
  430. },
  431. {
  432. nil,
  433. 0,
  434. },
  435. }
  436. for _, tt := range tests {
  437. var (
  438. got any
  439. err error
  440. )
  441. got, err = ToUint8(tt.input, CONVERT_ALL)
  442. assert.NoError(t, err)
  443. assert.Equal(t, uint8(tt.want), got)
  444. got, err = ToUint16(tt.input, CONVERT_ALL)
  445. assert.NoError(t, err)
  446. assert.Equal(t, uint16(tt.want), got)
  447. got, err = ToUint32(tt.input, CONVERT_ALL)
  448. assert.NoError(t, err)
  449. assert.Equal(t, uint32(tt.want), got)
  450. got, err = ToUint64(tt.input, CONVERT_ALL)
  451. assert.NoError(t, err)
  452. assert.Equal(t, tt.want, got)
  453. }
  454. errTests := []any{
  455. -1,
  456. int8(-1),
  457. int16(-1),
  458. int32(-1),
  459. int64(-1),
  460. float32(-1),
  461. float64(-1),
  462. true,
  463. nil,
  464. "1",
  465. }
  466. for _, input := range errTests {
  467. _, err := ToUint8(input, STRICT)
  468. assert.Error(t, err)
  469. _, err = ToUint16(input, STRICT)
  470. assert.Error(t, err)
  471. _, err = ToUint32(input, STRICT)
  472. assert.Error(t, err)
  473. _, err = ToUint64(input, STRICT)
  474. assert.Error(t, err)
  475. }
  476. }
  477. func TestMapConvert(t *testing.T) {
  478. source := map[interface{}]interface{}{
  479. "QUERY_TABLE": "VBAP",
  480. "ROWCOUNT": 10,
  481. "FIELDS": []interface{}{
  482. map[interface{}]interface{}{"FIELDNAME": "MANDT"},
  483. map[interface{}]interface{}{"FIELDNAME": "VBELN"},
  484. map[interface{}]interface{}{"FIELDNAME": "POSNR"},
  485. },
  486. }
  487. assert.Equal(t, map[string]interface{}{
  488. "QUERY_TABLE": "VBAP",
  489. "ROWCOUNT": 10,
  490. "FIELDS": []interface{}{
  491. map[string]interface{}{"FIELDNAME": "MANDT"},
  492. map[string]interface{}{"FIELDNAME": "VBELN"},
  493. map[string]interface{}{"FIELDNAME": "POSNR"},
  494. },
  495. }, ConvertMap(source))
  496. }
  497. func TestToTypedSlice(t *testing.T) {
  498. tests := []struct {
  499. s interface{}
  500. r interface{}
  501. e string
  502. }{
  503. {
  504. s: []interface{}{"abc", 123},
  505. r: []string{"abc", "123"},
  506. },
  507. {
  508. s: []interface{}{"addd", "bbb"},
  509. r: []string{"addd", "bbb"},
  510. },
  511. {
  512. s: []interface{}{nil, "bbb", "ddd"},
  513. e: "cannot convert []interface {}([<nil> bbb ddd]) to string slice for the 0 element: <nil>",
  514. },
  515. }
  516. t.Logf("The test bucket size is %d.", len(tests))
  517. for i, tt := range tests {
  518. result, err := ToTypedSlice(tt.s, func(input interface{}, ssn Strictness) (interface{}, error) {
  519. if input == nil {
  520. return nil, nil
  521. } else {
  522. return fmt.Sprintf("%v", input), nil
  523. }
  524. }, "string", CONVERT_SAMEKIND)
  525. errString := func(err error) string {
  526. if err != nil {
  527. return err.Error()
  528. }
  529. return ""
  530. }
  531. if !reflect.DeepEqual(tt.e, errString(err)) {
  532. t.Errorf("%d: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.e, err)
  533. } else if tt.e == "" && !reflect.DeepEqual(tt.r, result) {
  534. t.Errorf("%d\n\nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.r, result)
  535. }
  536. }
  537. }
  538. func TestMapToStructStrict(t *testing.T) {
  539. type args struct {
  540. input interface{}
  541. output interface{}
  542. expect interface{}
  543. }
  544. type Result struct {
  545. Foo string `json:"foo"`
  546. Bar string `json:"bar"`
  547. }
  548. tests := []struct {
  549. name string
  550. args args
  551. wantErr bool
  552. }{
  553. {
  554. name: "normal parse",
  555. args: args{
  556. input: map[string]interface{}{
  557. "foo": "foo",
  558. "bar": "bar",
  559. },
  560. output: &Result{},
  561. expect: &Result{
  562. Foo: "foo",
  563. Bar: "bar",
  564. },
  565. },
  566. wantErr: false,
  567. },
  568. {
  569. name: "input have more than keys",
  570. args: args{
  571. input: map[string]interface{}{
  572. "foo": "foo",
  573. "bar": "bar",
  574. "foobar": "foobar",
  575. },
  576. output: &Result{},
  577. },
  578. wantErr: true,
  579. },
  580. {
  581. name: "input have less keys",
  582. args: args{
  583. input: map[string]interface{}{
  584. "foo": "foo",
  585. },
  586. output: &Result{},
  587. expect: &Result{
  588. Foo: "foo",
  589. },
  590. },
  591. wantErr: false,
  592. },
  593. {
  594. name: "input have unused keys",
  595. args: args{
  596. input: map[string]interface{}{
  597. "foo": "foo",
  598. "foobar": "foobar",
  599. },
  600. output: &Result{},
  601. },
  602. wantErr: true,
  603. },
  604. }
  605. for _, tt := range tests {
  606. t.Run(tt.name, func(t *testing.T) {
  607. err := MapToStructStrict(tt.args.input, tt.args.output)
  608. if (err != nil) != tt.wantErr {
  609. t.Errorf("MapToStructure() error = %v, wantErr %v", err, tt.wantErr)
  610. }
  611. if tt.wantErr == false && !reflect.DeepEqual(tt.args.output, tt.args.expect) {
  612. t.Errorf(" got = %v, want %v", tt.args.output, tt.args.expect)
  613. }
  614. })
  615. }
  616. }
  617. func TestMapToStruct(t *testing.T) {
  618. type args struct {
  619. input interface{}
  620. output interface{}
  621. expect interface{}
  622. }
  623. type Result struct {
  624. Foo string `json:"foo"`
  625. Bar string `json:"bar"`
  626. }
  627. tests := []struct {
  628. name string
  629. args args
  630. wantErr bool
  631. }{
  632. {
  633. name: "normal parse",
  634. args: args{
  635. input: map[string]interface{}{
  636. "foo": "foo",
  637. "bar": "bar",
  638. },
  639. output: &Result{},
  640. expect: &Result{
  641. Foo: "foo",
  642. Bar: "bar",
  643. },
  644. },
  645. wantErr: false,
  646. },
  647. {
  648. name: "input have more than keys",
  649. args: args{
  650. input: map[string]interface{}{
  651. "foo": "foo",
  652. "bar": "bar",
  653. "foobar": "foobar",
  654. },
  655. output: &Result{},
  656. expect: &Result{
  657. Foo: "foo",
  658. Bar: "bar",
  659. },
  660. },
  661. wantErr: false,
  662. },
  663. {
  664. name: "input have less keys",
  665. args: args{
  666. input: map[string]interface{}{
  667. "foo": "foo",
  668. },
  669. output: &Result{},
  670. expect: &Result{
  671. Foo: "foo",
  672. },
  673. },
  674. wantErr: false,
  675. },
  676. {
  677. name: "input have unused keys",
  678. args: args{
  679. input: map[string]interface{}{
  680. "foo": "foo",
  681. "foobar": "foobar",
  682. },
  683. output: &Result{},
  684. expect: &Result{
  685. Foo: "foo",
  686. },
  687. },
  688. wantErr: false,
  689. },
  690. }
  691. for _, tt := range tests {
  692. t.Run(tt.name, func(t *testing.T) {
  693. if err := MapToStruct(tt.args.input, tt.args.output); (err != nil) != tt.wantErr {
  694. t.Errorf("MapToStructure() error = %v, wantErr %v", err, tt.wantErr)
  695. }
  696. })
  697. }
  698. }
  699. func TestMapToStructNotCaseSensitive(t *testing.T) {
  700. type args struct {
  701. input interface{}
  702. output interface{}
  703. expect interface{}
  704. }
  705. type Result struct {
  706. Foo string `json:"foo"`
  707. Bar string
  708. }
  709. tests := []struct {
  710. name string
  711. args args
  712. wantErr bool
  713. }{
  714. {
  715. name: "normal parse",
  716. args: args{
  717. input: map[string]interface{}{
  718. "foo": "foo",
  719. "bar": "bar",
  720. },
  721. output: &Result{},
  722. expect: &Result{
  723. Foo: "foo",
  724. Bar: "bar",
  725. },
  726. },
  727. wantErr: false,
  728. },
  729. {
  730. name: "not case sensitive",
  731. args: args{
  732. input: map[string]interface{}{
  733. "FOO": "foo",
  734. "BAR": "bar",
  735. },
  736. output: &Result{},
  737. expect: &Result{
  738. Foo: "foo",
  739. Bar: "bar",
  740. },
  741. },
  742. wantErr: false,
  743. },
  744. {
  745. name: "keys must match",
  746. args: args{
  747. input: map[string]interface{}{
  748. "foo": "foo",
  749. "BARS": "bars",
  750. },
  751. output: &Result{},
  752. expect: &Result{
  753. Foo: "foo",
  754. },
  755. },
  756. wantErr: false,
  757. },
  758. }
  759. for _, tt := range tests {
  760. t.Run(tt.name, func(t *testing.T) {
  761. if err := MapToStruct(tt.args.input, tt.args.output); (err != nil) != tt.wantErr {
  762. t.Errorf("MapToStructure() error = %v, wantErr %v", err, tt.wantErr)
  763. }
  764. })
  765. }
  766. }
  767. func TestMapToStructTag(t *testing.T) {
  768. type args struct {
  769. input interface{}
  770. output interface{}
  771. expect interface{}
  772. }
  773. type Result struct {
  774. Foo string `json:"fo"`
  775. Bar string
  776. }
  777. tests := []struct {
  778. name string
  779. args args
  780. wantErr bool
  781. }{
  782. {
  783. name: "normal parse",
  784. args: args{
  785. input: map[string]interface{}{
  786. "fo": "foo",
  787. "bar": "bar",
  788. },
  789. output: &Result{},
  790. expect: &Result{
  791. Foo: "foo",
  792. Bar: "bar",
  793. },
  794. },
  795. wantErr: false,
  796. },
  797. {
  798. name: "key tag not match",
  799. args: args{
  800. input: map[string]interface{}{
  801. "FOO": "foo",
  802. "BAR": "bar",
  803. },
  804. output: &Result{},
  805. expect: &Result{
  806. Bar: "bar",
  807. },
  808. },
  809. wantErr: false,
  810. },
  811. {
  812. name: "key tag not match",
  813. args: args{
  814. input: map[string]interface{}{
  815. "foo": "foo",
  816. "BARS": "bars",
  817. },
  818. output: &Result{},
  819. expect: &Result{},
  820. },
  821. wantErr: false,
  822. },
  823. }
  824. for _, tt := range tests {
  825. t.Run(tt.name, func(t *testing.T) {
  826. if err := MapToStruct(tt.args.input, tt.args.output); (err != nil) != tt.wantErr {
  827. t.Errorf("MapToStructure() error = %v, wantErr %v", err, tt.wantErr)
  828. }
  829. })
  830. }
  831. }
  832. func TestToByteA(t *testing.T) {
  833. bytea, _ := hex.DecodeString("736f6d6520646174612077697468200020616e6420efbbbf")
  834. tests := []struct {
  835. input interface{}
  836. output []byte
  837. err string
  838. }{
  839. {
  840. input: "foo",
  841. err: "illegal string foo, must be base64 encoded string",
  842. }, {
  843. input: []byte("foo"),
  844. output: []byte("foo"),
  845. err: "",
  846. }, {
  847. input: 1,
  848. output: nil,
  849. err: "cannot convert int(1) to bytea",
  850. }, {
  851. input: "c29tZSBkYXRhIHdpdGggACBhbmQg77u/",
  852. output: bytea,
  853. },
  854. }
  855. for i, tt := range tests {
  856. r, err := ToByteA(tt.input, CONVERT_SAMEKIND)
  857. if err != nil {
  858. if err.Error() != tt.err {
  859. t.Errorf("%d, ToByteA() error = %v, wantErr %v", i, err, tt.err)
  860. continue
  861. }
  862. } else {
  863. if !reflect.DeepEqual(r, tt.output) {
  864. t.Errorf("%d: ToByteA() = %x, want %x", i, r, tt.output)
  865. }
  866. }
  867. }
  868. }