github.com/CanonicalLtd/go-sqlite3@v1.6.0/callback_test.go (about)

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