|
8 | 8 |
|
9 | 9 | package mysql
|
10 | 10 |
|
11 |
| -import "testing" |
| 11 | +import ( |
| 12 | + "bytes" |
| 13 | + "testing" |
| 14 | +) |
12 | 15 |
|
13 |
| -type customString string |
| 16 | +func TestConvertDerivedString(t *testing.T) { |
| 17 | + type derived string |
14 | 18 |
|
15 |
| -func TestConvertValueCustomTypes(t *testing.T) { |
16 |
| - var cstr customString = "string" |
17 |
| - c := converter{} |
18 |
| - if _, err := c.ConvertValue(cstr); err != nil { |
19 |
| - t.Errorf("custom string type should be valid") |
| 19 | + output, err := converter{}.ConvertValue(derived("value")) |
| 20 | + if err != nil { |
| 21 | + t.Fatal("Derived string type not convertible", err) |
| 22 | + } |
| 23 | + |
| 24 | + if output != "value" { |
| 25 | + t.Fatalf("Derived string type not converted, got %#v %T", output, output) |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +func TestConvertDerivedByteSlice(t *testing.T) { |
| 30 | + type derived []uint8 |
| 31 | + |
| 32 | + output, err := converter{}.ConvertValue(derived("value")) |
| 33 | + if err != nil { |
| 34 | + t.Fatal("Byte slice not convertible", err) |
| 35 | + } |
| 36 | + |
| 37 | + if bytes.Compare(output.([]byte), []byte("value")) != 0 { |
| 38 | + t.Fatalf("Byte slice not converted, got %#v %T", output, output) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func TestConvertDerivedUnsupportedSlice(t *testing.T) { |
| 43 | + type derived []int |
| 44 | + |
| 45 | + _, err := converter{}.ConvertValue(derived{1}) |
| 46 | + if err == nil || err.Error() != "unsupported type mysql.derived, a slice of int" { |
| 47 | + t.Fatal("Unexpected error", err) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +func TestConvertDerivedBool(t *testing.T) { |
| 52 | + type derived bool |
| 53 | + |
| 54 | + output, err := converter{}.ConvertValue(derived(true)) |
| 55 | + if err != nil { |
| 56 | + t.Fatal("Derived bool type not convertible", err) |
| 57 | + } |
| 58 | + |
| 59 | + if output != true { |
| 60 | + t.Fatalf("Derived bool type not converted, got %#v %T", output, output) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +func TestConvertPointer(t *testing.T) { |
| 65 | + str := "value" |
| 66 | + |
| 67 | + output, err := converter{}.ConvertValue(&str) |
| 68 | + if err != nil { |
| 69 | + t.Fatal("Pointer type not convertible", err) |
| 70 | + } |
| 71 | + |
| 72 | + if output != "value" { |
| 73 | + t.Fatalf("Pointer type not converted, got %#v %T", output, output) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +func TestConvertSignedIntegers(t *testing.T) { |
| 78 | + values := []interface{}{ |
| 79 | + int8(-42), |
| 80 | + int16(-42), |
| 81 | + int32(-42), |
| 82 | + int64(-42), |
| 83 | + int(-42), |
| 84 | + } |
| 85 | + |
| 86 | + for _, value := range values { |
| 87 | + output, err := converter{}.ConvertValue(value) |
| 88 | + if err != nil { |
| 89 | + t.Fatalf("%T type not convertible %s", value, err) |
| 90 | + } |
| 91 | + |
| 92 | + if output != int64(-42) { |
| 93 | + t.Fatalf("%T type not converted, got %#v %T", value, output, output) |
| 94 | + } |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +func TestConvertUnsignedIntegers(t *testing.T) { |
| 99 | + values := []interface{}{ |
| 100 | + uint8(42), |
| 101 | + uint16(42), |
| 102 | + uint32(42), |
| 103 | + uint64(42), |
| 104 | + uint(42), |
| 105 | + } |
| 106 | + |
| 107 | + for _, value := range values { |
| 108 | + output, err := converter{}.ConvertValue(value) |
| 109 | + if err != nil { |
| 110 | + t.Fatalf("%T type not convertible %s", value, err) |
| 111 | + } |
| 112 | + |
| 113 | + if output != int64(42) { |
| 114 | + t.Fatalf("%T type not converted, got %#v %T", value, output, output) |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + output, err := converter{}.ConvertValue(^uint64(0)) |
| 119 | + if err != nil { |
| 120 | + t.Fatal("uint64 high-bit not convertible", err) |
| 121 | + } |
| 122 | + |
| 123 | + if output != "18446744073709551615" { |
| 124 | + t.Fatalf("uint64 high-bit not converted, got %#v %T", output, output) |
20 | 125 | }
|
21 | 126 | }
|
0 commit comments