Rebase.
[official-gcc.git] / libgo / go / database / sql / driver / types_test.go
blob1ce0ff06541844a5d4155f6d9d779eb252674fe7
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.
5 package driver
7 import (
8 "reflect"
9 "testing"
10 "time"
13 type valueConverterTest struct {
14 c ValueConverter
15 in interface{}
16 out interface{}
17 err string
20 var now = time.Now()
21 var answer int64 = 42
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, ""},
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()
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)
57 if tt.err != "" {
58 continue
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)