-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathpackage_bulitin_test.go
More file actions
258 lines (239 loc) · 6.89 KB
/
package_bulitin_test.go
File metadata and controls
258 lines (239 loc) · 6.89 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package convert
import (
"go/token"
"go/types"
"testing"
"github.com/goplus/gogen"
"github.com/goplus/llcppg/_xtool/llcppsymg/tool/name"
"github.com/goplus/llcppg/ast"
"github.com/goplus/llcppg/cl/internal/cltest"
llcppg "github.com/goplus/llcppg/config"
ctoken "github.com/goplus/llcppg/token"
"github.com/goplus/mod/gopmod"
)
func emptyPkg() *Package {
mod, err := gopmod.Load(".")
if err != nil {
panic(err)
}
pkg, err := NewPackage(&PackageConfig{
PkgBase: PkgBase{
PkgPath: ".",
Pubs: make(map[string]string),
},
Name: "testpkg",
Mod: mod,
GenConf: &gogen.Config{},
ConvSym: cltest.NewConvSym(),
LibCommand: "${pkg-config --libs xxx}",
})
if err != nil {
panic(err)
}
return pkg
}
func TestTypeRefIncompleteFail(t *testing.T) {
pkg := emptyPkg()
tempFile := &HeaderFile{
File: "temp.h",
FileType: llcppg.Inter,
}
pkg.SetCurFile(tempFile)
t.Run("defer write third type not found", func(t *testing.T) {
pkg.locMap.Add(&ast.Ident{Name: "Bar"}, &ast.Location{File: "Bar"})
pkg.incompleteTypes.Add(&Incomplete{cname: "Bar"})
err := pkg.NewTypedefDecl(&ast.TypedefDecl{
Object: ast.Object{
Name: &ast.Ident{Name: "Foo"},
},
Type: &ast.TagExpr{
Name: &ast.Ident{Name: "Bar"},
},
})
if err != nil {
t.Fatal("NewTypedefDecl failed:", err)
}
pkg.incompleteTypes.Complete("Bar")
err = pkg.Complete()
if err == nil {
t.Fatal("expect a error")
}
})
t.Run("ref tag incomplete fail", func(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Fatal("Expected panic, got nil")
}
}()
pkg.handleTyperefIncomplete(&ast.TagExpr{
Tag: 0,
Name: &ast.ScopingExpr{
X: &ast.Ident{Name: "Bar"},
},
}, nil, "NewBar")
})
}
func TestRedefPubName(t *testing.T) {
pkg := emptyPkg()
tempFile := &HeaderFile{
File: "temp.h",
FileType: llcppg.Inter,
}
pkg.SetCurFile(tempFile)
// mock a function name which is not register in processsymbol
pkg.p.NewFuncDecl(token.NoPos, "Foo", types.NewSignatureType(nil, nil, nil, types.NewTuple(), types.NewTuple(), false))
pkg.p.NewFuncDecl(token.NoPos, "Bar", types.NewSignatureType(nil, nil, nil, types.NewTuple(), types.NewTuple(), false))
t.Run("enum type redefine pubname", func(t *testing.T) {
err := pkg.NewEnumTypeDecl(&ast.EnumTypeDecl{
Object: ast.Object{
Loc: &ast.Location{File: "temp.h"},
Name: nil,
},
Type: &ast.EnumType{
Items: []*ast.EnumItem{
{Name: &ast.Ident{Name: "Foo"}, Value: &ast.BasicLit{Kind: ast.IntLit, Value: "0"}},
},
},
})
if err == nil {
t.Fatal("expect a error")
}
})
t.Run("macro redefine pubname", func(t *testing.T) {
err := pkg.NewMacro(&ast.Macro{
Loc: &ast.Location{File: "temp.h"},
Name: "Bar",
Tokens: []*ast.Token{{Token: ctoken.IDENT, Lit: "Bar"}, {Token: ctoken.LITERAL, Lit: "1"}},
})
if err == nil {
t.Fatal("expect a error")
}
})
}
func TestPubMethodName(t *testing.T) {
name := types.NewTypeName(0, nil, "Foo", nil)
named := types.NewNamed(name, nil, nil)
ptrRecv := types.NewPointer(named)
fnName := "Foo"
pubName := pubMethodName(ptrRecv, &GoFuncSpec{GoSymbName: fnName, FnName: fnName, PtrRecv: true, IsMethod: true})
if pubName != "(*Foo).Foo" {
t.Fatal("Expected pubName to be '(*Foo).Foo', got", pubName)
}
valRecv := named
pubName = pubMethodName(valRecv, &GoFuncSpec{GoSymbName: fnName, FnName: fnName, IsMethod: true})
if pubName != "Foo.Foo" {
t.Fatal("Expected pubName to be 'Foo.Foo', got", pubName)
}
unknownRecv := types.NewStruct(nil, []string{})
pubName = pubMethodName(unknownRecv, &GoFuncSpec{GoSymbName: fnName, FnName: fnName, IsMethod: false})
if pubName != "Foo" {
t.Fatal("Expected pubName to be 'Foo', got", pubName)
}
}
func TestGetNameType(t *testing.T) {
named := types.NewNamed(types.NewTypeName(0, nil, "Foo", nil), nil, nil)
ptrNamed := types.NewPointer(named)
customSturct := types.NewStruct(nil, nil)
namedRes := getNamedType(named)
if namedRes != named {
t.Fatal("Expected namedRes to be *types.Named, got", namedRes)
}
ptrNamedRes := getNamedType(ptrNamed)
if ptrNamedRes != named {
t.Fatal("Expected ptrNamedRes to be *types.Named, got", ptrNamedRes)
}
customRes := getNamedType(customSturct)
if customRes != nil {
t.Fatal("Expected nil, got", customRes)
}
}
func TestTrimPrefixes(t *testing.T) {
mod, err := gopmod.Load(".")
if err != nil {
panic(err)
}
pkg, err := NewPackage(&PackageConfig{
PkgBase: PkgBase{
PkgPath: ".",
Pubs: make(map[string]string),
},
Name: "testpkg",
GenConf: &gogen.Config{},
ConvSym: cltest.NewConvSym(),
TrimPrefixes: []string{"prefix1", "prefix2"},
LibCommand: "${pkg-config --libs xxx}",
Mod: mod,
})
if err != nil {
t.Fatal("NewPackage failed:", err)
}
pkg.curFile = &HeaderFile{
FileType: llcppg.Inter,
}
result := pkg.trimPrefixes()
expected := []string{"prefix1", "prefix2"}
if len(result) != len(expected) || (len(result) > 0 && result[0] != expected[0]) {
t.Errorf("Expected %v, got %v", expected, result)
}
pkg.curFile.FileType = llcppg.Third
result = pkg.trimPrefixes()
if len(result) != 0 {
t.Errorf("Expected Empty TrimPrefix")
}
}
func TestMarkUseFail(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Fatal("Expected panic, got nil")
}
}()
pkg, err := NewPackage(&PackageConfig{
PkgBase: PkgBase{
PkgPath: ".",
Pubs: make(map[string]string),
},
LibCommand: "${pkg-config --libs xxx}",
})
if err != nil {
t.Fatal("NewPackage failed:", err)
}
pkg.markUseDeps(&PkgDepLoader{})
}
func TestProcessSymbol(t *testing.T) {
toCamel := func(trimprefix []string) NameMethod {
return func(cname string) string {
return name.PubName(name.RemovePrefixedName(cname, trimprefix))
}
}
toExport := func(trimprefix []string) NameMethod {
return func(cname string) string {
return name.ExportName(name.RemovePrefixedName(cname, trimprefix))
}
}
sym := NewProcessSymbol()
testCases := []struct {
name string
trimPrefixes []string
nameMethod func(trimprefix []string) NameMethod
expected string
expectChange bool
}{
{"lua_closethread", []string{"lua_", "luaL_"}, toCamel, "Closethread", true},
{"luaL_checknumber", []string{"lua_", "luaL_"}, toCamel, "Checknumber", true},
{"_gmp_err", []string{}, toCamel, "X_gmpErr", true},
{"fn_123illegal", []string{"fn_"}, toCamel, "X123illegal", true},
{"fts5_tokenizer", []string{}, toCamel, "Fts5Tokenizer", true},
{"Fts5Tokenizer", []string{}, toCamel, "Fts5Tokenizer__1", true},
{"normal_var", []string{}, toExport, "Normal_var", true},
{"Cameled", []string{}, toExport, "Cameled", false},
}
for _, tc := range testCases {
pubName := sym.Register(Node{name: tc.name, kind: TypeDecl}, tc.expected)
if pubName != tc.expected {
t.Errorf("Expected %s, but got %s", tc.expected, pubName)
}
if tc.expectChange && pubName == tc.name {
t.Errorf("Expected Change, but got same name")
}
}
}