GoPLS Viewer

Home|gopls/go/types/typeutil/ui_test.go
1// Copyright 2016 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.
4
5package typeutil_test
6
7import (
8    "fmt"
9    "go/ast"
10    "go/parser"
11    "go/token"
12    "go/types"
13    "strings"
14    "testing"
15
16    "golang.org/x/tools/go/types/typeutil"
17)
18
19func TestIntuitiveMethodSet(t *testing.T) {
20    const source = `
21package P
22type A int
23func (A) f()
24func (*A) g()
25`
26
27    fset := token.NewFileSet()
28    ferr := parser.ParseFile(fset"hello.go"source0)
29    if err != nil {
30        t.Fatal(err)
31    }
32
33    var conf types.Config
34    pkgerr := conf.Check("P"fset, []*ast.File{f}, nil)
35    if err != nil {
36        t.Fatal(err)
37    }
38    qual := types.RelativeTo(pkg)
39
40    for _test := range []struct {
41        expr string // type expression
42        want string // intuitive method set
43    }{
44        {"A""(A).f (*A).g"},
45        {"*A""(*A).f (*A).g"},
46        {"error""(error).Error"},
47        {"*error"""},
48        {"struct{A}""(struct{A}).f (*struct{A}).g"},
49        {"*struct{A}""(*struct{A}).f (*struct{A}).g"},
50    } {
51        tverr := types.Eval(fsetpkg0test.expr)
52        if err != nil {
53            t.Errorf("Eval(%s) failed: %v"test.exprerr)
54        }
55        var names []string
56        for _m := range typeutil.IntuitiveMethodSet(tv.Typenil) {
57            name := fmt.Sprintf("(%s).%s"types.TypeString(m.Recv(), qual), m.Obj().Name())
58            names = append(namesname)
59        }
60        got := strings.Join(names" ")
61        if got != test.want {
62            t.Errorf("IntuitiveMethodSet(%s) = %q, want %q"test.exprgottest.want)
63        }
64    }
65}
66
MembersX
TestIntuitiveMethodSet.conf
TestIntuitiveMethodSet.pkg
TestIntuitiveMethodSet.RangeStmt_694.test
TestIntuitiveMethodSet.RangeStmt_694.BlockStmt.got
TestIntuitiveMethodSet.RangeStmt_694.BlockStmt.RangeStmt_1151.m
TestIntuitiveMethodSet.RangeStmt_694.BlockStmt.RangeStmt_1151.BlockStmt.name
TestIntuitiveMethodSet
TestIntuitiveMethodSet.t
TestIntuitiveMethodSet.source
TestIntuitiveMethodSet.qual
TestIntuitiveMethodSet.RangeStmt_694.BlockStmt.tv
TestIntuitiveMethodSet.fset
TestIntuitiveMethodSet.f
TestIntuitiveMethodSet.err
TestIntuitiveMethodSet.RangeStmt_694.BlockStmt.err
TestIntuitiveMethodSet.RangeStmt_694.BlockStmt.names
Members
X