-
Notifications
You must be signed in to change notification settings - Fork 546
/
z_test.go
165 lines (149 loc) · 4.07 KB
/
z_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
158
159
160
161
162
163
164
165
/*
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package doc
import (
"bytes"
"fmt"
"go/ast"
"go/doc"
"go/format"
"go/parser"
"go/token"
"os"
"path"
"strings"
"testing"
)
func TestToIndex(t *testing.T) {
if ret := toIndex('a'); ret != 10 {
t.Fatal("toIndex:", ret)
}
defer func() {
if e := recover(); e != "invalid character out of [0-9,a-z]" {
t.Fatal("panic:", e)
}
}()
toIndex('A')
}
func TestCheckTypeMethod(t *testing.T) {
if ret := checkTypeMethod("_Foo_a"); ret.typ != "" || ret.name != "Foo_a" {
t.Fatal("checkTypeMethod:", ret)
}
if ret := checkTypeMethod("Foo_a"); ret.typ != "Foo" || ret.name != "a" {
t.Fatal("checkTypeMethod:", ret)
}
}
func TestIsGopPackage(t *testing.T) {
if isGopPackage(&doc.Package{}) {
t.Fatal("isGopPackage: true?")
}
}
func TestDocRecv(t *testing.T) {
if _, ok := docRecv(&ast.Field{}); ok {
t.Fatal("docRecv: ok?")
}
}
func printVal(parts []string, format string, val any) []string {
return append(parts, fmt.Sprintf(format, val))
}
func printFuncDecl(parts []string, fset *token.FileSet, decl *ast.FuncDecl) []string {
var b bytes.Buffer
if e := format.Node(&b, fset, decl); e != nil {
panic(e)
}
return append(parts, b.String())
}
func printFunc(parts []string, fset *token.FileSet, format string, fn *doc.Func) []string {
parts = printVal(parts, format, fn.Name)
if fn.Recv != "" {
parts = printVal(parts, "Recv: %s", fn.Recv)
}
parts = printVal(parts, "Doc: %s", fn.Doc)
parts = printFuncDecl(parts, fset, fn.Decl)
return parts
}
func printFuncs(parts []string, fset *token.FileSet, fns []*doc.Func) []string {
for _, fn := range fns {
parts = printFunc(parts, fset, "== Func %s ==", fn)
}
return parts
}
func printType(parts []string, fset *token.FileSet, typ *doc.Type) []string {
parts = append(parts, fmt.Sprintf("== Type %s ==", typ.Name))
for _, fn := range typ.Funcs {
parts = printFunc(parts, fset, "- Func %s -", fn)
}
for _, fn := range typ.Methods {
parts = printFunc(parts, fset, "- Method %s -", fn)
}
return parts
}
func printTypes(parts []string, fset *token.FileSet, types []*doc.Type) []string {
for _, typ := range types {
parts = printType(parts, fset, typ)
}
return parts
}
func printPkg(fset *token.FileSet, in *doc.Package) string {
var parts []string
parts = printFuncs(parts, fset, in.Funcs)
parts = printTypes(parts, fset, in.Types)
return strings.Join(append(parts, ""), "\n")
}
func testPkg(t *testing.T, filename string, src any, expected string) {
t.Helper()
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, filename, src, parser.ParseComments)
if err != nil {
t.Fatal(err)
}
pkg, err := doc.NewFromFiles(fset, []*ast.File{f}, "foo")
if err != nil {
t.Fatal(err)
}
pkg = Transform(pkg)
if ret := printPkg(fset, pkg); ret != expected {
t.Fatalf("got:\n%s\nexpected:\n%s\n", ret, expected)
}
}
func testFromDir(t *testing.T, sel, relDir string) {
dir, err := os.Getwd()
if err != nil {
t.Fatal("Getwd failed:", err)
}
dir = path.Join(dir, relDir)
fis, err := os.ReadDir(dir)
if err != nil {
t.Fatal("ReadDir failed:", err)
}
for _, fi := range fis {
name := fi.Name()
if strings.HasPrefix(name, "_") || !fi.IsDir() || (sel != "" && !strings.Contains(name, sel)) {
continue
}
t.Run(name, func(t *testing.T) {
testDir := dir + "/" + name
out, e := os.ReadFile(testDir + "/out.expect")
if e != nil {
t.Fatal(e)
}
testPkg(t, testDir+"/in.go", nil, string(out))
})
}
}
func TestFromTestdata(t *testing.T) {
testFromDir(t, "", "./_testdata")
}