1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
|
package gotypes
import (
"go/token"
"go/types"
"strings"
"testing"
"golang.org/x/tools/go/packages"
)
func TestLookupSignature(t *testing.T) {
pkg := LoadPackageTypes(t, "math")
s, err := LookupSignature(pkg, "Frexp")
if err != nil {
t.Fatal(err)
}
expect, err := ParseSignature("func(f float64) (frac float64, exp int)")
if err != nil {
t.Fatal(err)
}
if s.String() != expect.String() {
t.Errorf("\n got: %s\nexpect: %s\n", s, expect)
}
}
func TestLookupSignatureErrors(t *testing.T) {
cases := []struct {
PackagePath string
FunctionName string
ExpectedError string
}{
{"runtime", "HmmIdk", "could not find function \"HmmIdk\""},
{"crypto", "Decrypter", "object \"Decrypter\" does not have signature type"},
{"encoding/base64", "StdEncoding", "object \"StdEncoding\" does not have signature type"},
}
for _, c := range cases {
pkg := LoadPackageTypes(t, c.PackagePath)
_, err := LookupSignature(pkg, c.FunctionName)
if err == nil {
t.Fatalf("expected error looking up '%s' in package '%s'", c.FunctionName, c.PackagePath)
}
if err.Error() != c.ExpectedError {
t.Fatalf("wrong error message\n got: %q\nexpect: %q", err.Error(), c.ExpectedError)
}
}
}
func LoadPackageTypes(t *testing.T, path string) *types.Package {
t.Helper()
cfg := &packages.Config{
Mode: packages.NeedTypes | packages.NeedDeps | packages.NeedImports,
}
pkgs, err := packages.Load(cfg, path)
if err != nil {
t.Fatal(err)
}
if len(pkgs) != 1 {
t.Fatal("expected to load exactly one package")
}
return pkgs[0].Types
}
func TestParseSignature(t *testing.T) {
cases := []struct {
Expr string
ExpectParams *types.Tuple
ExpectReturn *types.Tuple
}{
{
Expr: "func()",
},
{
Expr: "func(x, y uint64)",
ExpectParams: types.NewTuple(
types.NewParam(token.NoPos, nil, "x", types.Typ[types.Uint64]),
types.NewParam(token.NoPos, nil, "y", types.Typ[types.Uint64]),
),
},
{
Expr: "func(n int, s []string) byte",
ExpectParams: types.NewTuple(
types.NewParam(token.NoPos, nil, "n", types.Typ[types.Int]),
types.NewParam(token.NoPos, nil, "s", types.NewSlice(types.Typ[types.String])),
),
ExpectReturn: types.NewTuple(
types.NewParam(token.NoPos, nil, "", types.Typ[types.Byte]),
),
},
{
Expr: "func(x, y int) (x0, y0 int, s string)",
ExpectParams: types.NewTuple(
types.NewParam(token.NoPos, nil, "x", types.Typ[types.Int]),
types.NewParam(token.NoPos, nil, "y", types.Typ[types.Int]),
),
ExpectReturn: types.NewTuple(
types.NewParam(token.NoPos, nil, "x0", types.Typ[types.Int]),
types.NewParam(token.NoPos, nil, "y0", types.Typ[types.Int]),
types.NewParam(token.NoPos, nil, "s", types.Typ[types.String]),
),
},
}
for _, c := range cases {
s, err := ParseSignature(c.Expr)
if err != nil {
t.Fatal(err)
}
if !TypesTuplesEqual(s.sig.Params(), c.ExpectParams) {
t.Errorf("parameter mismatch\ngot %#v\nexpect %#v\n", s.sig.Params(), c.ExpectParams)
}
if !TypesTuplesEqual(s.sig.Results(), c.ExpectReturn) {
t.Errorf("return value(s) mismatch\ngot %#v\nexpect %#v\n", s.sig.Results(), c.ExpectReturn)
}
}
}
func TestParseSignatureErrors(t *testing.T) {
cases := []struct {
Expr string
ErrorContains string
}{
//{"idkjklol", "undeclared name"}, https://github.com/mmcloughlin/avo/issues/367
{"struct{}", "not a function signature"},
{"uint32(0xfeedbeef)", "should have nil value"},
}
for _, c := range cases {
s, err := ParseSignature(c.Expr)
if s != nil || err == nil || !strings.Contains(err.Error(), c.ErrorContains) {
t.Errorf("expect error from expression %s\ngot: %s\nexpect substring: %s\n", c.Expr, err, c.ErrorContains)
}
}
}
func TypesTuplesEqual(a, b *types.Tuple) bool {
if a.Len() != b.Len() {
return false
}
n := a.Len()
for i := 0; i < n; i++ {
if !TypesVarsEqual(a.At(i), b.At(i)) {
return false
}
}
return true
}
func TypesVarsEqual(a, b *types.Var) bool {
return a.Name() == b.Name() && types.Identical(a.Type(), b.Type())
}
func TestSignatureSizes(t *testing.T) {
cases := []struct {
Expr string
ArgSize int
}{
{"func()", 0},
{"func(uint64) uint64", 16},
{"func([7]byte) byte", 9},
{"func(uint64, uint64) (uint64, uint64)", 32},
{"func(uint16)", 2}, // issue #191
{"func(*uint64, uint32)", 12}, // issue #195
}
for _, c := range cases {
s, err := ParseSignature(c.Expr)
if err != nil {
t.Fatal(err)
}
if s.Bytes() != c.ArgSize {
t.Errorf("%s: size %d expected %d", s, s.Bytes(), c.ArgSize)
}
}
}
|