-
Notifications
You must be signed in to change notification settings - Fork 4
/
goastch_test.go
70 lines (59 loc) · 1.43 KB
/
goastch_test.go
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
package goastch_test
import (
"bytes"
"go/build"
"go/parser"
"go/printer"
"github.com/onsi/ginkgo"
"github.com/onsi/gomega"
"golang.org/x/tools/go/loader"
"github.com/helloyi/goastch"
. "github.com/helloyi/goastch/goastcher"
)
var _ = ginkgo.Describe("Goastch", func() {
var (
ger = HasDescendant(File(Has(ImportSpec(HasName(Equals("."))).Bind(""))))
fname = "<buffer>"
code = `
// Package foo
package foo
import (
"fmt"
. "golang.org/x/net/context"
)
type aaa struct {
ctx Context
}
func bar() bool {
xx := aaa{}
fmt.Println(xx)
return false
}`
)
ginkgo.It("test", func() {
loader := &loader.Config{
Build: &build.Default,
ParserMode: parser.ParseComments,
}
file, err := loader.ParseFile(fname, code)
gomega.Expect(err).Should(gomega.BeNil())
loader.CreateFromFiles(fname, file)
prog, err := loader.Load()
gomega.Expect(err).Should(gomega.BeNil())
pkgInfo := prog.Package(fname)
info := &pkgInfo.Info
fset := loader.Fset
result, err := goastch.Find(file, info, ger)
gomega.Expect(err).Should(gomega.BeNil())
buf := new(bytes.Buffer)
gomega.Expect(len(result)).Should(gomega.Equal(1))
for _, list := range result {
gomega.Expect(len(list)).Should(gomega.Equal(1))
for _, node := range list {
err = printer.Fprint(buf, fset, node)
gomega.Expect(err).Should(gomega.BeNil())
gomega.Expect(buf.String()).Should(gomega.Equal(`. "golang.org/x/net/context"`))
}
}
})
})