-
Notifications
You must be signed in to change notification settings - Fork 2
/
dump_test.go
107 lines (91 loc) · 2 KB
/
dump_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
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
package goast_test
import (
"go/ast"
"io"
"strings"
"testing"
"github.com/m-mizutani/goast"
"github.com/m-mizutani/gt"
)
const dumpExampleCode = `package main
func Add(a, b int) int {
return a + b
}
func Sub(a, b int) int {
d := a - b
return d
}
`
func TestDumpFile(t *testing.T) {
code := strings.NewReader(dumpExampleCode)
var called int
g := goast.New(
goast.WithInspectOptions(
goast.WithRootOnly(),
goast.WithLine(7), // does not work
goast.WithFuncName("Add"), // does not work
),
goast.WithDumpHook(func(node *goast.Node, w io.Writer) error {
called++
gt.Value(t, node.Kind).Equal("File")
return nil
}),
)
gt.NoError(t, g.Dump("test.go", code, nil)).Must()
gt.Value(t, called).Equal(1)
}
func TestDumpLine(t *testing.T) {
code := strings.NewReader(dumpExampleCode)
g := goast.New(
goast.WithInspectOptions(
goast.WithLine(7),
),
goast.WithDumpHook(func(node *goast.Node, w io.Writer) error {
gt.V(t, node.Kind).Equal("FuncDecl")
f := gt.Cast[*ast.FuncDecl](t, node.Node)
gt.V(t, f.Name.Name).Equal("Sub")
gt.A(t, f.Body.List).Length(2)
return nil
}),
)
gt.NoError(t, g.Dump("test.go", code, nil)).Must()
}
func TestDumpLineAllNode(t *testing.T) {
code := strings.NewReader(dumpExampleCode)
var cnt int
g := goast.New(
goast.WithInspectOptions(
goast.WithLine(8),
goast.WithAllMatched(),
),
goast.WithDumpHook(func(node *goast.Node, w io.Writer) error {
// nodes of `d := a - b`
kinds := []string{
"AssignStmt",
"Ident",
"BinaryExpr",
"Ident",
"Ident",
}
gt.V(t, node.Kind).Equal(kinds[cnt])
cnt++
return nil
}),
)
gt.NoError(t, g.Dump("test.go", code, nil)).Must()
gt.V(t, cnt).Equal(5)
}
/*
func TestStructAccess(t *testing.T) {
codePath := createTestCode(t, `package main
import "model"
func main() {
var u0 *model.User
u1 := &model.User{}
u2 := model.User{}
}
`)
var w bytes.Buffer
require.NoError(t, usecase.DumpWriter([]string{codePath}, &w, usecase.WithDumpLine(7)))
}
*/