| 1 |
/* |
| 2 |
Copyright 2015 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package types |
| 18 |
|
| 19 |
import ( |
| 20 |
"testing" |
| 21 |
) |
| 22 |
|
| 23 |
func TestGetBuiltin(t *testing.T) { |
| 24 |
u := Universe{} |
| 25 |
if builtinPkg := u.Package(""); builtinPkg.Has("string") { |
| 26 |
t.Errorf("Expected builtin package to not have builtins until they're asked for explicitly. %#v", builtinPkg) |
| 27 |
} |
| 28 |
s := u.Type(Name{Package: "", Name: "string"}) |
| 29 |
if s != String { |
| 30 |
t.Errorf("Expected canonical string type.") |
| 31 |
} |
| 32 |
if builtinPkg := u.Package(""); !builtinPkg.Has("string") { |
| 33 |
t.Errorf("Expected builtin package to exist and have builtins by default. %#v", builtinPkg) |
| 34 |
} |
| 35 |
if builtinPkg := u.Package(""); len(builtinPkg.Types) != 1 { |
| 36 |
t.Errorf("Expected builtin package to not have builtins until they're asked for explicitly. %#v", builtinPkg) |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
func TestGetMarker(t *testing.T) { |
| 41 |
u := Universe{} |
| 42 |
n := Name{Package: "path/to/package", Name: "Foo"} |
| 43 |
f := u.Type(n) |
| 44 |
if f == nil || f.Name != n { |
| 45 |
t.Errorf("Expected marker type.") |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
func Test_Type_IsPrimitive(t *testing.T) { |
| 50 |
testCases := []struct { |
| 51 |
typ Type |
| 52 |
expect bool |
| 53 |
}{ |
| 54 |
{ |
| 55 |
typ: Type{ |
| 56 |
Name: Name{Package: "pkgname", Name: "typename"}, |
| 57 |
Kind: Builtin, |
| 58 |
}, |
| 59 |
expect: true, |
| 60 |
}, |
| 61 |
{ |
| 62 |
typ: Type{ |
| 63 |
Name: Name{Package: "pkgname", Name: "typename"}, |
| 64 |
Kind: Alias, |
| 65 |
Underlying: &Type{ |
| 66 |
Name: Name{Package: "pkgname", Name: "underlying"}, |
| 67 |
Kind: Builtin, |
| 68 |
}, |
| 69 |
}, |
| 70 |
expect: true, |
| 71 |
}, |
| 72 |
{ |
| 73 |
typ: Type{ |
| 74 |
Name: Name{Package: "pkgname", Name: "typename"}, |
| 75 |
Kind: Pointer, |
| 76 |
Elem: &Type{ |
| 77 |
Name: Name{Package: "pkgname", Name: "pointee"}, |
| 78 |
Kind: Builtin, |
| 79 |
}, |
| 80 |
}, |
| 81 |
expect: false, |
| 82 |
}, |
| 83 |
{ |
| 84 |
typ: Type{ |
| 85 |
Name: Name{Package: "pkgname", Name: "typename"}, |
| 86 |
Kind: Struct, |
| 87 |
}, |
| 88 |
expect: false, |
| 89 |
}, |
| 90 |
} |
| 91 |
|
| 92 |
for i, tc := range testCases { |
| 93 |
r := tc.typ.IsPrimitive() |
| 94 |
if r != tc.expect { |
| 95 |
t.Errorf("case[%d]: expected %t, got %t", i, tc.expect, r) |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
func Test_Type_IsAssignable(t *testing.T) { |
| 101 |
testCases := []struct { |
| 102 |
typ Type |
| 103 |
expect bool |
| 104 |
}{ |
| 105 |
{ |
| 106 |
typ: Type{ |
| 107 |
Name: Name{Package: "pkgname", Name: "typename"}, |
| 108 |
Kind: Builtin, |
| 109 |
}, |
| 110 |
expect: true, |
| 111 |
}, |
| 112 |
{ |
| 113 |
typ: Type{ |
| 114 |
Name: Name{Package: "pkgname", Name: "typename"}, |
| 115 |
Kind: Alias, |
| 116 |
Underlying: &Type{ |
| 117 |
Name: Name{Package: "pkgname", Name: "underlying"}, |
| 118 |
Kind: Builtin, |
| 119 |
}, |
| 120 |
}, |
| 121 |
expect: true, |
| 122 |
}, |
| 123 |
{ |
| 124 |
typ: Type{ |
| 125 |
Name: Name{Package: "pkgname", Name: "typename"}, |
| 126 |
Kind: Pointer, |
| 127 |
Elem: &Type{ |
| 128 |
Name: Name{Package: "pkgname", Name: "pointee"}, |
| 129 |
Kind: Builtin, |
| 130 |
}, |
| 131 |
}, |
| 132 |
expect: false, |
| 133 |
}, |
| 134 |
{ |
| 135 |
typ: Type{ |
| 136 |
Name: Name{Package: "pkgname", Name: "typename"}, |
| 137 |
Kind: Struct, // Empty struct |
| 138 |
}, |
| 139 |
expect: true, |
| 140 |
}, |
| 141 |
{ |
| 142 |
typ: Type{ |
| 143 |
Name: Name{Package: "pkgname", Name: "typename"}, |
| 144 |
Kind: Struct, |
| 145 |
Members: []Member{ |
| 146 |
{ |
| 147 |
Name: "m1", |
| 148 |
Type: &Type{ |
| 149 |
Name: Name{Package: "pkgname", Name: "typename"}, |
| 150 |
Kind: Builtin, |
| 151 |
}, |
| 152 |
}, |
| 153 |
{ |
| 154 |
Name: "m2", |
| 155 |
Type: &Type{ |
| 156 |
Name: Name{Package: "pkgname", Name: "typename"}, |
| 157 |
Kind: Alias, |
| 158 |
Underlying: &Type{ |
| 159 |
Name: Name{Package: "pkgname", Name: "underlying"}, |
| 160 |
Kind: Builtin, |
| 161 |
}, |
| 162 |
}, |
| 163 |
}, |
| 164 |
{ |
| 165 |
Name: "m3", |
| 166 |
Type: &Type{ |
| 167 |
Name: Name{Package: "pkgname", Name: "typename"}, |
| 168 |
Kind: Struct, // Empty struct |
| 169 |
}, |
| 170 |
}, |
| 171 |
}, |
| 172 |
}, |
| 173 |
expect: true, |
| 174 |
}, |
| 175 |
{ |
| 176 |
typ: Type{ |
| 177 |
Name: Name{Package: "pkgname", Name: "typename"}, |
| 178 |
Kind: Struct, |
| 179 |
Members: []Member{ |
| 180 |
{ |
| 181 |
Name: "m1", |
| 182 |
Type: &Type{ |
| 183 |
Name: Name{Package: "pkgname", Name: "typename"}, |
| 184 |
Kind: Builtin, |
| 185 |
}, |
| 186 |
}, |
| 187 |
{ |
| 188 |
Name: "m2", |
| 189 |
Type: &Type{ |
| 190 |
Name: Name{Package: "pkgname", Name: "typename"}, |
| 191 |
Kind: Pointer, |
| 192 |
Elem: &Type{ |
| 193 |
Name: Name{Package: "pkgname", Name: "pointee"}, |
| 194 |
Kind: Builtin, |
| 195 |
}, |
| 196 |
}, |
| 197 |
}, |
| 198 |
}, |
| 199 |
}, |
| 200 |
expect: false, |
| 201 |
}, |
| 202 |
} |
| 203 |
|
| 204 |
for i, tc := range testCases { |
| 205 |
r := tc.typ.IsAssignable() |
| 206 |
if r != tc.expect { |
| 207 |
t.Errorf("case[%d]: expected %t, got %t", i, tc.expect, r) |
| 208 |
} |
| 209 |
} |
| 210 |
} |