-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathgogen_test.go
More file actions
167 lines (139 loc) · 3.85 KB
/
Copy pathgogen_test.go
File metadata and controls
167 lines (139 loc) · 3.85 KB
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
166
167
/*
Copyright 2026 The XGo Authors (xgo.dev)
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 gogen_test
import (
"bytes"
"go/ast"
"go/token"
"go/types"
"log"
"strings"
"sync"
"testing"
"github.com/goplus/gogen"
"github.com/goplus/gogen/packages"
)
var (
gblFset *token.FileSet
gblImp types.Importer
handleErr func(err error)
)
func init() {
gogen.SetDebug(gogen.DbgFlagAll)
gblFset = token.NewFileSet()
gblImp = packages.NewImporter(gblFset)
}
func ctxRef(pkg *gogen.Package, name string) gogen.Ref {
_, o := pkg.CB().Scope().LookupParent(name, token.NoPos)
return o
}
type eventRecorder struct{}
func (p eventRecorder) Member(id ast.Node, obj types.Object) {}
func (p eventRecorder) Call(fn ast.Node, obj types.Object) {}
type txtNode struct {
Msg string
pos token.Pos
}
func (p *txtNode) Pos() token.Pos {
return p.pos
}
func (p *txtNode) End() token.Pos {
return p.pos + token.Pos(len(p.Msg))
}
var (
mutex sync.Mutex
pos2Positions = map[token.Pos]token.Position{}
)
// text, line, column
func source(text string, args ...interface{}) ast.Node {
if len(args) < 2 {
return &txtNode{Msg: text}
}
pos := position(args[0].(int), args[1].(int))
return &txtNode{Msg: text, pos: pos}
}
const (
posBits = 16
posMask = (1 << posBits) - 1
)
func position(line, column int) token.Pos {
mutex.Lock()
defer mutex.Unlock()
pos := token.Pos(len(pos2Positions)+1) << posBits
pos2Positions[pos] = token.Position{Filename: "./foo.gop", Line: line, Column: column}
return pos
}
type nodeInterp struct{}
func (p nodeInterp) Position(pos token.Pos) (ret token.Position) {
mutex.Lock()
defer mutex.Unlock()
ret = pos2Positions[(pos &^ posMask)]
ret.Column += int(pos & posMask)
return
}
func (p nodeInterp) Caller(node ast.Node) string {
t := node.(*txtNode)
idx := strings.Index(t.Msg, "(")
if idx > 0 {
return t.Msg[:idx]
}
return t.Msg
}
func (p nodeInterp) LoadExpr(node ast.Node) string {
t := node.(*txtNode)
return t.Msg
}
func newParam(pkg *gogen.Package, pos token.Pos, name string, typ types.Type) *types.Var {
return types.NewParam(pos, pkg.Types, name, typ)
}
func newMainPackage(
implicitCast ...func(pkg *gogen.Package, V, T types.Type, pv *gogen.Element) bool) *gogen.Package {
return newPackage("main", implicitCast...)
}
func newPackage(
name string, implicitCast ...func(pkg *gogen.Package, V, T types.Type, pv *gogen.Element) bool) *gogen.Package {
conf := &gogen.Config{
Fset: gblFset,
Importer: gblImp,
Recorder: eventRecorder{},
NodeInterpreter: nodeInterp{},
DbgPositioner: nodeInterp{},
}
if len(implicitCast) > 0 {
conf.CanImplicitCast = implicitCast[0]
}
if handleErr != nil {
conf.HandleErr = handleErr
handleErr = nil
}
return gogen.NewPackage("", name, conf)
}
func domTest(t *testing.T, pkg *gogen.Package, expected string) {
domTestEx(t, pkg, expected, "")
}
func domTestEx(t *testing.T, pkg *gogen.Package, expected string, fname string) {
doDomTest(t, pkg, expected, fname)
log.Printf("====================== %s End =========================\n", t.Name())
}
func doDomTest(t *testing.T, pkg *gogen.Package, expected string, fname string) {
var b bytes.Buffer
t.Helper()
err := gogen.WriteTo(&b, pkg, fname)
if err != nil {
t.Fatal("gogen.WriteTo failed:", err)
}
result := b.String()
if result != expected {
t.Fatalf("\ngogen.WriteTo Result:\n%s\nExpected:\n%s\n", result, expected)
}
}