cast_test.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright 2021 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. "fmt"
  17. "reflect"
  18. "testing"
  19. )
  20. func TestMapConvert_Funcs(t *testing.T) {
  21. source := map[interface{}]interface{}{
  22. "QUERY_TABLE": "VBAP",
  23. "ROWCOUNT": 10,
  24. "FIELDS": []interface{}{
  25. map[interface{}]interface{}{"FIELDNAME": "MANDT"},
  26. map[interface{}]interface{}{"FIELDNAME": "VBELN"},
  27. map[interface{}]interface{}{"FIELDNAME": "POSNR"},
  28. },
  29. }
  30. exp := map[string]interface{}{
  31. "QUERY_TABLE": "VBAP",
  32. "ROWCOUNT": 10,
  33. "FIELDS": []interface{}{
  34. map[string]interface{}{"FIELDNAME": "MANDT"},
  35. map[string]interface{}{"FIELDNAME": "VBELN"},
  36. map[string]interface{}{"FIELDNAME": "POSNR"},
  37. },
  38. }
  39. got := ConvertMap(source)
  40. if !reflect.DeepEqual(exp, got) {
  41. t.Errorf("result mismatch:\n\nexp=%s\n\ngot=%s\n\n", exp, got)
  42. }
  43. }
  44. func TestToTypedSlice(t *testing.T) {
  45. var tests = []struct {
  46. s interface{}
  47. r interface{}
  48. e string
  49. }{
  50. {
  51. s: []interface{}{"abc", 123},
  52. r: []string{"abc", "123"},
  53. },
  54. {
  55. s: []interface{}{"addd", "bbb"},
  56. r: []string{"addd", "bbb"},
  57. },
  58. {
  59. s: []interface{}{nil, "bbb", "ddd"},
  60. e: "cannot convert []interface {}([<nil> bbb ddd]) to string slice for the 0 element: <nil>",
  61. },
  62. }
  63. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  64. for i, tt := range tests {
  65. result, err := ToTypedSlice(tt.s, func(input interface{}, ssn Strictness) (interface{}, error) {
  66. if input == nil {
  67. return nil, nil
  68. } else {
  69. return fmt.Sprintf("%v", input), nil
  70. }
  71. }, "string", CONVERT_SAMEKIND)
  72. if !reflect.DeepEqual(tt.e, errstring(err)) {
  73. t.Errorf("%d: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.e, err)
  74. } else if tt.e == "" && !reflect.DeepEqual(tt.r, result) {
  75. t.Errorf("%d\n\nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.r, result)
  76. }
  77. }
  78. }
  79. func errstring(err error) string {
  80. if err != nil {
  81. return err.Error()
  82. }
  83. return ""
  84. }