callback_test.go 2.8 KB

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