gotypes,build: pointer dereference (#61)

Provides a method on `gotypes.Component` to allow pointer dereference. This will enable `gotypes` helpers to be used with struct pointer arguments, for example.

Updates #53 
Fixes #54
This commit is contained in:
Michael McLoughlin
2019-01-27 19:22:21 -08:00
committed by GitHub
parent eb225e9d2c
commit 9eb409b935
9 changed files with 137 additions and 29 deletions

View File

@@ -2,7 +2,12 @@ package gotypes
import (
"go/types"
"strings"
"testing"
"github.com/mmcloughlin/avo/reg"
"github.com/mmcloughlin/avo/operand"
)
func TestBasicKindsArePrimitive(t *testing.T) {
@@ -33,8 +38,61 @@ func TestPointersArePrimitive(t *testing.T) {
}
func AssertPrimitive(t *testing.T, typ types.Type) {
c := NewComponent("primitive", typ, 0)
c := NewComponent(typ, operand.NewParamAddr("primitive", 0))
if _, err := c.Resolve(); err != nil {
t.Errorf("expected type %s to be primitive: got error '%s'", typ, err)
}
}
func TestComponentErrors(t *testing.T) {
comp := NewComponent(types.Typ[types.Uint32], operand.Mem{})
cases := []struct {
Component Component
ErrorSubstring string
}{
{comp.Base(), "only slices and strings"},
{comp.Len(), "only slices and strings"},
{comp.Cap(), "only slices have"},
{comp.Real(), "only complex"},
{comp.Imag(), "only complex"},
{comp.Index(42), "not array type"},
{comp.Field("a"), "not struct type"},
{comp.Dereference(reg.R12), "not pointer type"},
}
for _, c := range cases {
_, err := c.Component.Resolve()
if err == nil {
t.Fatal("expected error")
}
if !strings.Contains(err.Error(), c.ErrorSubstring) {
t.Fatalf("error message %q; expected substring %q", err.Error(), c.ErrorSubstring)
}
}
}
func TestComponentErrorChaining(t *testing.T) {
// Build a component with an error.
comp := NewComponent(types.Typ[types.Uint32], operand.Mem{}).Index(3)
_, expect := comp.Resolve()
if expect == nil {
t.Fatal("expected error")
}
// Confirm that the error is preserved through chaining operations.
cases := []Component{
comp.Dereference(reg.R13),
comp.Base(),
comp.Len(),
comp.Cap(),
comp.Real(),
comp.Imag(),
comp.Index(42),
comp.Field("field"),
}
for _, c := range cases {
_, err := c.Resolve()
if err != expect {
t.Fatal("chaining should preserve error")
}
}
}