Fork github.com/mattn/go-sqlite3 with adjustment for go1.16.2
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
3.2 KiB

  1. // Copyright (C) 2019 Yasuhiro Matsumoto <mattn.jp@gmail.com>.
  2. //
  3. // Use of this source code is governed by an MIT-style
  4. // license that can be found in the LICENSE file.
  5. // +build cgo
  6. package sqlite3
  7. import (
  8. "errors"
  9. "math"
  10. "reflect"
  11. "testing"
  12. )
  13. func TestCallbackArgCast(t *testing.T) {
  14. intConv := callbackSyntheticForTests(reflect.ValueOf(int64(math.MaxInt64)), nil)
  15. floatConv := callbackSyntheticForTests(reflect.ValueOf(float64(math.MaxFloat64)), nil)
  16. errConv := callbackSyntheticForTests(reflect.Value{}, errors.New("test"))
  17. tests := []struct {
  18. f callbackArgConverter
  19. o reflect.Value
  20. }{
  21. {intConv, reflect.ValueOf(int8(-1))},
  22. {intConv, reflect.ValueOf(int16(-1))},
  23. {intConv, reflect.ValueOf(int32(-1))},
  24. {intConv, reflect.ValueOf(uint8(math.MaxUint8))},
  25. {intConv, reflect.ValueOf(uint16(math.MaxUint16))},
  26. {intConv, reflect.ValueOf(uint32(math.MaxUint32))},
  27. // Special case, int64->uint64 is only 1<<63 - 1, not 1<<64 - 1
  28. {intConv, reflect.ValueOf(uint64(math.MaxInt64))},
  29. {floatConv, reflect.ValueOf(float32(math.Inf(1)))},
  30. }
  31. for _, test := range tests {
  32. conv := callbackArgCast{test.f, test.o.Type()}
  33. val, err := conv.Run(nil)
  34. if err != nil {
  35. t.Errorf("Couldn't convert to %s: %s", test.o.Type(), err)
  36. } else if !reflect.DeepEqual(val.Interface(), test.o.Interface()) {
  37. t.Errorf("Unexpected result from converting to %s: got %v, want %v", test.o.Type(), val.Interface(), test.o.Interface())
  38. }
  39. }
  40. conv := callbackArgCast{errConv, reflect.TypeOf(int8(0))}
  41. _, err := conv.Run(nil)
  42. if err == nil {
  43. t.Errorf("Expected error during callbackArgCast, but got none")
  44. }
  45. }
  46. func TestCallbackConverters(t *testing.T) {
  47. tests := []struct {
  48. v interface{}
  49. err bool
  50. }{
  51. // Unfortunately, we can't tell which converter was returned,
  52. // but we can at least check which types can be converted.
  53. {[]byte{0}, false},
  54. {"text", false},
  55. {true, false},
  56. {int8(0), false},
  57. {int16(0), false},
  58. {int32(0), false},
  59. {int64(0), false},
  60. {uint8(0), false},
  61. {uint16(0), false},
  62. {uint32(0), false},
  63. {uint64(0), false},
  64. {int(0), false},
  65. {uint(0), false},
  66. {float64(0), false},
  67. {float32(0), false},
  68. {func() {}, true},
  69. {complex64(complex(0, 0)), true},
  70. {complex128(complex(0, 0)), true},
  71. {struct{}{}, true},
  72. {map[string]string{}, true},
  73. {[]string{}, true},
  74. {(*int8)(nil), true},
  75. {make(chan int), true},
  76. }
  77. for _, test := range tests {
  78. _, err := callbackArg(reflect.TypeOf(test.v))
  79. if test.err && err == nil {
  80. t.Errorf("Expected an error when converting %s, got no error", reflect.TypeOf(test.v))
  81. } else if !test.err && err != nil {
  82. t.Errorf("Expected converter when converting %s, got error: %s", reflect.TypeOf(test.v), err)
  83. }
  84. }
  85. for _, test := range tests {
  86. _, err := callbackRet(reflect.TypeOf(test.v))
  87. if test.err && err == nil {
  88. t.Errorf("Expected an error when converting %s, got no error", reflect.TypeOf(test.v))
  89. } else if !test.err && err != nil {
  90. t.Errorf("Expected converter when converting %s, got error: %s", reflect.TypeOf(test.v), err)
  91. }
  92. }
  93. }
  94. func TestCallbackReturnAny(t *testing.T) {
  95. udf := func() interface{} {
  96. return 1
  97. }
  98. typ := reflect.TypeOf(udf)
  99. _, err := callbackRet(typ.Out(0))
  100. if err != nil {
  101. t.Errorf("Expected valid callback for any return type, got: %s", err)
  102. }
  103. }