| 1 | package gotypes |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "go/token" |
| 6 | "go/types" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
| 10 | "github.com/mmcloughlin/avo/operand" |
| 11 | "github.com/mmcloughlin/avo/reg" |
| 12 | ) |
| 13 | |
| 14 | func TestBasicKindsArePrimitive(t *testing.T) { |
| 15 | kinds := []types.BasicKind{ |
| 16 | types.Bool, |
| 17 | types.Int, |
| 18 | types.Int8, |
| 19 | types.Int16, |
| 20 | types.Int32, |
| 21 | types.Int64, |
| 22 | types.Uint, |
| 23 | types.Uint8, |
| 24 | types.Uint16, |
| 25 | types.Uint32, |
| 26 | types.Uint64, |
| 27 | types.Uintptr, |
| 28 | types.Float32, |
| 29 | types.Float64, |
| 30 | } |
| 31 | for _, k := range kinds { |
| 32 | AssertPrimitive(t, types.Typ[k]) |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | func TestPointersArePrimitive(t *testing.T) { |
| 37 | typ := types.NewPointer(types.Typ[types.Uint32]) |
| 38 | AssertPrimitive(t, typ) |
| 39 | } |
| 40 | |
| 41 | func AssertPrimitive(t *testing.T, typ types.Type) { |
| 42 | t.Helper() |
| 43 | c := NewComponent(typ, operand.NewParamAddr("primitive", 0)) |
| 44 | if _, err := c.Resolve(); err != nil { |
| 45 | t.Errorf("expected type %s to be primitive: got error '%s'", typ, err) |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | func TestComponentErrors(t *testing.T) { |
| 50 | comp := NewComponent(types.Typ[types.Uint32], operand.Mem{}) |
| 51 | cases := []struct { |
| 52 | Component Component |
| 53 | ErrorSubstring string |
| 54 | }{ |
| 55 | {comp.Base(), "only slices and strings"}, |
| 56 | {comp.Len(), "only slices and strings"}, |
| 57 | {comp.Cap(), "only slices have"}, |
| 58 | {comp.Real(), "only complex"}, |
| 59 | {comp.Imag(), "only complex"}, |
| 60 | {comp.Index(42), "not array type"}, |
| 61 | {comp.Field("a"), "not struct type"}, |
| 62 | {comp.Dereference(reg.R12), "not pointer type"}, |
| 63 | } |
| 64 | for _, c := range cases { |
| 65 | _, err := c.Component.Resolve() |
| 66 | if err == nil { |
| 67 | t.Fatal("expected error") |
| 68 | } |
| 69 | if !strings.Contains(err.Error(), c.ErrorSubstring) { |
| 70 | t.Fatalf("error message %q; expected substring %q", err.Error(), c.ErrorSubstring) |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | func TestComponentErrorChaining(t *testing.T) { |
| 76 | // Build a component with an error. |
| 77 | comp := NewComponent(types.Typ[types.Uint32], operand.Mem{}).Index(3) |
| 78 | _, expect := comp.Resolve() |
| 79 | if expect == nil { |
| 80 | t.Fatal("expected error") |
| 81 | } |
| 82 | |
| 83 | // Confirm that the error is preserved through chaining operations. |
| 84 | cases := []Component{ |
| 85 | comp.Dereference(reg.R13), |
| 86 | comp.Base(), |
| 87 | comp.Len(), |
| 88 | comp.Cap(), |
| 89 | comp.Real(), |
| 90 | comp.Imag(), |
| 91 | comp.Index(42), |
| 92 | comp.Field("field"), |
| 93 | } |
| 94 | for _, c := range cases { |
| 95 | _, err := c.Resolve() |
| 96 | if !errors.Is(err, expect) { |
| 97 | t.Fatal("chaining should preserve error") |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | func TestComponentDeconstruction(t *testing.T) { |
| 103 | cases := []struct { |
| 104 | Name string |
| 105 | Type types.Type |
| 106 | Chain func(Component) Component |
| 107 | Param string |
| 108 | Offset int |
| 109 | }{ |
| 110 | { |
| 111 | Name: "slice_base", |
| 112 | Type: types.NewSlice(types.Typ[types.Uint64]), |
| 113 | Chain: func(c Component) Component { return c.Base() }, |
| 114 | Param: "base", |
| 115 | Offset: 0, |
| 116 | }, |
| 117 | { |
| 118 | Name: "slice_len", |
| 119 | Type: types.NewSlice(types.Typ[types.Uint64]), |
| 120 | Chain: func(c Component) Component { return c.Len() }, |
| 121 | Param: "len", |
| 122 | Offset: 8, |
| 123 | }, |
| 124 | { |
| 125 | Name: "slice_cap", |
| 126 | Type: types.NewSlice(types.Typ[types.Uint64]), |
| 127 | Chain: func(c Component) Component { return c.Cap() }, |
| 128 | Param: "cap", |
| 129 | Offset: 16, |
| 130 | }, |
| 131 | { |
| 132 | Name: "string_base", |
| 133 | Type: types.Typ[types.String], |
| 134 | Chain: func(c Component) Component { return c.Base() }, |
| 135 | Param: "base", |
| 136 | Offset: 0, |
| 137 | }, |
| 138 | { |
| 139 | Name: "slice_len", |
| 140 | Type: types.Typ[types.String], |
| 141 | Chain: func(c Component) Component { return c.Len() }, |
| 142 | Param: "len", |
| 143 | Offset: 8, |
| 144 | }, |
| 145 | { |
| 146 | Name: "complex64_real", |
| 147 | Type: types.Typ[types.Complex64], |
| 148 | Chain: func(c Component) Component { return c.Real() }, |
| 149 | Param: "real", |
| 150 | Offset: 0, |
| 151 | }, |
| 152 | { |
| 153 | Name: "complex64_imag", |
| 154 | Type: types.Typ[types.Complex64], |
| 155 | Chain: func(c Component) Component { return c.Imag() }, |
| 156 | Param: "imag", |
| 157 | Offset: 4, |
| 158 | }, |
| 159 | { |
| 160 | Name: "complex128_real", |
| 161 | Type: types.Typ[types.Complex128], |
| 162 | Chain: func(c Component) Component { return c.Real() }, |
| 163 | Param: "real", |
| 164 | Offset: 0, |
| 165 | }, |
| 166 | { |
| 167 | Name: "complex128_imag", |
| 168 | Type: types.Typ[types.Complex128], |
| 169 | Chain: func(c Component) Component { return c.Imag() }, |
| 170 | Param: "imag", |
| 171 | Offset: 8, |
| 172 | }, |
| 173 | { |
| 174 | Name: "array", |
| 175 | Type: types.NewArray(types.Typ[types.Uint32], 7), |
| 176 | Chain: func(c Component) Component { return c.Index(3) }, |
| 177 | Param: "3", |
| 178 | Offset: 12, |
| 179 | }, |
| 180 | { |
| 181 | Name: "struct", |
| 182 | Type: types.NewStruct([]*types.Var{ |
| 183 | types.NewField(token.NoPos, nil, "Byte", types.Typ[types.Byte], false), |
| 184 | types.NewField(token.NoPos, nil, "Uint64", types.Typ[types.Uint64], false), |
| 185 | }, nil), |
| 186 | Chain: func(c Component) Component { return c.Field("Uint64") }, |
| 187 | Param: "Uint64", |
| 188 | Offset: 8, |
| 189 | }, |
| 190 | } |
| 191 | |
| 192 | // For every test case, generate the same case but when the type is wrapped in |
| 193 | // a named type. |
| 194 | n := len(cases) |
| 195 | for i := 0; i < n; i++ { |
| 196 | wrapped := cases[i] |
| 197 | wrapped.Name += "_wrapped" |
| 198 | wrapped.Type = types.NewNamed( |
| 199 | types.NewTypeName(token.NoPos, nil, "wrapped", nil), |
| 200 | wrapped.Type, |
| 201 | nil, |
| 202 | ) |
| 203 | cases = append(cases, wrapped) |
| 204 | } |
| 205 | |
| 206 | for _, c := range cases { |
| 207 | c := c // avoid scopelint error |
| 208 | t.Run(c.Name, func(t *testing.T) { |
| 209 | t.Log(c.Type) |
| 210 | base := operand.NewParamAddr("test", 0) |
| 211 | comp := NewComponent(c.Type, base) |
| 212 | comp = c.Chain(comp) |
| 213 | |
| 214 | b, err := comp.Resolve() |
| 215 | if err != nil { |
| 216 | t.Fatal(err) |
| 217 | } |
| 218 | |
| 219 | expectname := "test_" + c.Param |
| 220 | if b.Addr.Symbol.Name != expectname { |
| 221 | t.Errorf("parameter name %q; expected %q", b.Addr.Symbol.Name, expectname) |
| 222 | } |
| 223 | |
| 224 | if b.Addr.Disp != c.Offset { |
| 225 | t.Errorf("offset %d; expected %d", b.Addr.Disp, c.Offset) |
| 226 | } |
| 227 | }) |
| 228 | } |
| 229 | } |