-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_test.go
157 lines (147 loc) · 3.62 KB
/
read_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
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
package srcdom_test
import (
"io/fs"
"path/filepath"
"runtime"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/koron-go/srcdom"
)
func TestReadDir(t *testing.T) {
p, err := srcdom.ReadDir(".", false)
if err != nil {
t.Fatalf("failed to read target package: %s", err)
}
if p.Name != "srcdom" {
t.Errorf("mimatch name of target package: want=%s got=%s", "srcdorm", p.Name)
}
p2, err := srcdom.ReadDir(".", true)
if err != nil {
t.Fatalf("failed to read test package: %s", err)
}
if p2.Name != "srcdom_test" {
t.Errorf("mimatch name of target package: want=%s got=%s", "srcdorm_test", p.Name)
}
}
func TestReadGOROOT(t *testing.T) {
srcdir := filepath.Join(runtime.GOROOT(), "src")
err := filepath.WalkDir(srcdir, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if !d.IsDir() {
return nil
}
switch d.Name() {
case "internal", "testdata", "vendor":
return fs.SkipDir
}
_, err = srcdom.ReadDir(path, false)
if err != nil {
t.Errorf("failed to read pacakge: %s", err)
}
return nil
})
if err != nil {
t.Fatal(err)
}
}
func TestReadFile(t *testing.T) {
got, err := srcdom.Read(filepath.Join("_testdata", "test1.go"))
if err != nil {
t.Fatal(err)
}
want := srcdom.Package{
Name: "testdata",
Funcs: []*srcdom.Func{
{
Name: "Func1",
Params: []*srcdom.Var{{Name: "arg1", Type: "string"}},
Results: []*srcdom.Var{{Type: "error"}},
},
{Name: "Func0"},
},
Values: []*srcdom.Value{
{Name: "VarFoo", Type: "int"},
{Name: "VarBar", Type: "string"},
{Name: "varPriv", Type: "float64"},
},
}
if d := cmp.Diff(&want, got, cmpopts.IgnoreUnexported(srcdom.Package{})); d != "" {
t.Errorf("unmatch srcdom.Package: -want +got\n%s", d)
}
pkg := got
t.Run("Value", func(t *testing.T) {
for _, c := range []struct {
name string
ispub bool
want srcdom.Value
}{
{"VarFoo", true, srcdom.Value{Name: "VarFoo", Type: "int"}},
{"VarBar", true, srcdom.Value{Name: "VarBar", Type: "string"}},
{"varPriv", false, srcdom.Value{Name: "varPriv", Type: "float64"}},
} {
got, ok := pkg.Value(c.name)
if !ok {
t.Errorf("value:%s not found", c.name)
continue
}
if d := cmp.Diff(&c.want, got); d != "" {
t.Errorf("value:%s unmatch: -want +got\n%s", c.name, d)
}
gotPub := got.IsPublic()
if gotPub != c.ispub {
t.Errorf("value:%s IsPublic unmatch: want=%t got=%t", c.name, c.ispub, gotPub)
}
}
})
t.Run("ValueNames", func(t *testing.T) {
got := pkg.ValueNames()
want := []string{
"VarBar",
"VarFoo",
"varPriv",
}
if d := cmp.Diff(want, got); d != "" {
t.Errorf("unmatch ValueNames() result: -want +got\n%s", d)
}
})
t.Run("Func", func(t *testing.T) {
for _, c := range []struct {
name string
ispub bool
want srcdom.Func
}{
{"Func1", true, srcdom.Func{
Name: "Func1",
Params: []*srcdom.Var{{Name: "arg1", Type: "string"}},
Results: []*srcdom.Var{{Type: "error"}},
}},
{"Func0", true, srcdom.Func{Name: "Func0"}},
} {
got, ok := pkg.Func(c.name)
if !ok {
t.Errorf("func:%s not found", c.name)
continue
}
if d := cmp.Diff(&c.want, got); d != "" {
t.Errorf("func:%s unmatch: -want +got\n%s", c.name, d)
}
gotPub := got.IsPublic()
if gotPub != c.ispub {
t.Errorf("func:%s IsPublic unmatch: want=%t got=%t", c.name, c.ispub, gotPub)
}
}
})
t.Run("FuncNames", func(t *testing.T) {
got := pkg.FuncNames()
want := []string{
"Func0",
"Func1",
}
if d := cmp.Diff(want, got); d != "" {
t.Errorf("unmatch FuncNames() result: -want +got\n%s", d)
}
})
}