github.com/hongwozai/go-src-1.4.3@v0.0.0-20191127132709-dc3fce3dbccb/src/database/sql/driver/types_test.go (about) 1 // Copyright 2011 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package driver 6 7 import ( 8 "reflect" 9 "testing" 10 "time" 11 ) 12 13 type valueConverterTest struct { 14 c ValueConverter 15 in interface{} 16 out interface{} 17 err string 18 } 19 20 var now = time.Now() 21 var answer int64 = 42 22 23 var valueConverterTests = []valueConverterTest{ 24 {Bool, "true", true, ""}, 25 {Bool, "True", true, ""}, 26 {Bool, []byte("t"), true, ""}, 27 {Bool, true, true, ""}, 28 {Bool, "1", true, ""}, 29 {Bool, 1, true, ""}, 30 {Bool, int64(1), true, ""}, 31 {Bool, uint16(1), true, ""}, 32 {Bool, "false", false, ""}, 33 {Bool, false, false, ""}, 34 {Bool, "0", false, ""}, 35 {Bool, 0, false, ""}, 36 {Bool, int64(0), false, ""}, 37 {Bool, uint16(0), false, ""}, 38 {c: Bool, in: "foo", err: "sql/driver: couldn't convert \"foo\" into type bool"}, 39 {c: Bool, in: 2, err: "sql/driver: couldn't convert 2 into type bool"}, 40 {DefaultParameterConverter, now, now, ""}, 41 {DefaultParameterConverter, (*int64)(nil), nil, ""}, 42 {DefaultParameterConverter, &answer, answer, ""}, 43 {DefaultParameterConverter, &now, now, ""}, 44 } 45 46 func TestValueConverters(t *testing.T) { 47 for i, tt := range valueConverterTests { 48 out, err := tt.c.ConvertValue(tt.in) 49 goterr := "" 50 if err != nil { 51 goterr = err.Error() 52 } 53 if goterr != tt.err { 54 t.Errorf("test %d: %T(%T(%v)) error = %q; want error = %q", 55 i, tt.c, tt.in, tt.in, goterr, tt.err) 56 } 57 if tt.err != "" { 58 continue 59 } 60 if !reflect.DeepEqual(out, tt.out) { 61 t.Errorf("test %d: %T(%T(%v)) = %v (%T); want %v (%T)", 62 i, tt.c, tt.in, tt.in, out, out, tt.out, tt.out) 63 } 64 } 65 }