1 // Copyright 2009 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.
17 var basicTypes = []typeT{
26 func getTypeUnlocked(name string, rt reflect.Type) gobType {
28 defer typeLock.Unlock()
29 t, err := getBaseType(name, rt)
31 panic("getTypeUnlocked: " + err.Error())
37 func TestBasic(t *testing.T) {
38 for _, tt := range basicTypes {
39 if tt.id.string() != tt.str {
40 t.Errorf("checkType: expected %q got %s", tt.str, tt.id.string())
43 t.Errorf("id for %q is zero", tt.str)
48 // Reregister some basic types to check registration is idempotent.
49 func TestReregistration(t *testing.T) {
50 newtyp := getTypeUnlocked("int", reflect.TypeOf(int(0)))
51 if newtyp != tInt.gobType() {
52 t.Errorf("reregistration of %s got new type", newtyp.string())
54 newtyp = getTypeUnlocked("uint", reflect.TypeOf(uint(0)))
55 if newtyp != tUint.gobType() {
56 t.Errorf("reregistration of %s got new type", newtyp.string())
58 newtyp = getTypeUnlocked("string", reflect.TypeOf("hello"))
59 if newtyp != tString.gobType() {
60 t.Errorf("reregistration of %s got new type", newtyp.string())
64 func TestArrayType(t *testing.T) {
66 a3int := getTypeUnlocked("foo", reflect.TypeOf(a3))
67 newa3int := getTypeUnlocked("bar", reflect.TypeOf(a3))
68 if a3int != newa3int {
69 t.Errorf("second registration of [3]int creates new type")
72 a4int := getTypeUnlocked("goo", reflect.TypeOf(a4))
74 t.Errorf("registration of [3]int creates same type as [4]int")
77 a3bool := getTypeUnlocked("", reflect.TypeOf(b3))
79 t.Errorf("registration of [3]bool creates same type as [3]int")
81 str := a3bool.string()
84 t.Errorf("array printed as %q; expected %q", str, expected)
88 func TestSliceType(t *testing.T) {
90 sint := getTypeUnlocked("slice", reflect.TypeOf(s))
92 newsint := getTypeUnlocked("slice1", reflect.TypeOf(news))
94 t.Errorf("second registration of []int creates new type")
97 sbool := getTypeUnlocked("", reflect.TypeOf(b))
99 t.Errorf("registration of []bool creates same type as []int")
101 str := sbool.string()
104 t.Errorf("slice printed as %q; expected %q", str, expected)
108 func TestMapType(t *testing.T) {
110 mapStringInt := getTypeUnlocked("map", reflect.TypeOf(m))
111 var newm map[string]int
112 newMapStringInt := getTypeUnlocked("map1", reflect.TypeOf(newm))
113 if mapStringInt != newMapStringInt {
114 t.Errorf("second registration of map[string]int creates new type")
116 var b map[string]bool
117 mapStringBool := getTypeUnlocked("", reflect.TypeOf(b))
118 if mapStringBool == mapStringInt {
119 t.Errorf("registration of map[string]bool creates same type as map[string]int")
121 str := mapStringBool.string()
122 expected := "map[string]bool"
124 t.Errorf("map printed as %q; expected %q", str, expected)
132 // This structure has pointers and refers to itself, making it a good test case.
135 B int32 // will become int
138 E *float64 // will become float64
139 F ****float64 // will become float64
141 H *Bar // should not interpolate the definition of Bar again
142 I *Foo // will not explode
145 func TestStructType(t *testing.T) {
146 sstruct := getTypeUnlocked("Foo", reflect.TypeOf(Foo{}))
147 str := sstruct.string()
148 // If we can print it correctly, we built it correctly.
149 expected := "Foo = struct { A int; B int; C string; D bytes; E float; F float; G Bar = struct { X string; }; H Bar; I Foo; }"
151 t.Errorf("struct printed as %q; expected %q", str, expected)
155 // Should be OK to register the same type multiple times, as long as they're
156 // at the same level of indirection.
157 func TestRegistration(t *testing.T) {
158 type T struct{ a int }