Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
wa-lang
wa
提交
911e1bc4
wa
项目概览
wa-lang
/
wa
9 个月 前同步成功
通知
68
Star
655
Fork
45
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
wa
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
911e1bc4
编写于
5月 07, 2023
作者:
chai2010
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
loader: 完善测试和汇编相关的结构, 并完善测试信息解析
上级
a5f7fd38
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
126 addition
and
30 deletion
+126
-30
internal/loader/api.go
internal/loader/api.go
+31
-8
internal/loader/loader.go
internal/loader/loader.go
+95
-22
未找到文件。
internal/loader/api.go
浏览文件 @
911e1bc4
...
...
@@ -28,22 +28,45 @@ type Package struct {
Pkg
*
types
.
Package
// 类型检查后的包
Info
*
types
.
Info
// 包的类型检查信息
Files
[]
*
ast
.
File
// AST语法树
WsFiles
[]
WsFile
// 汇编代码
WsFiles
[]
*
WsFile
// 汇编代码
SSAPkg
*
ssa
.
Package
TestInfo
TestInfo
}
// 汇编代码文件
type
WsFile
struct
{
Name
string
// 文件名
Code
string
// 汇编代码
TestInfo
*
TestInfo
}
// 单元测试信息
type
TestInfo
struct
{
Files
[]
string
// 测试文件
Funcs
[]
string
// 测试函数
Tests
map
[
string
]
TestFuncInfo
Benchs
map
[
string
]
BenchFuncInfo
Examples
map
[
string
]
ExampleFuncInfo
}
// 测试函数信息
type
TestFuncInfo
struct
{
FuncPos
token
.
Pos
// 函数位置
Name
string
// 函数名, 不含包路径
}
// 基准函数信息
type
BenchFuncInfo
struct
{
FuncPos
token
.
Pos
// 函数位置
Name
string
// 函数名, 不含包路径
}
// 示例函数信息
type
ExampleFuncInfo
struct
{
FuncPos
token
.
Pos
// 函数位置
Name
string
// 函数名, 不含包路径
Output
string
// 期望输出, 为空表示不验证
}
// 汇编代码文件
type
WsFile
struct
{
Name
string
// 文件名
Code
string
// 汇编代码
}
// 加载程序
...
...
internal/loader/loader.go
浏览文件 @
911e1bc4
...
...
@@ -180,7 +180,7 @@ func (p *_Loader) Import(pkgpath string) (*types.Package, error) {
return
nil
,
err
}
// 解析当前包的 AST
// 解析当前包的 AST
, 隐含了是否测试模式
filenames
,
pkg
.
Files
,
err
=
p
.
ParseDir
(
pkgpath
)
if
err
!=
nil
{
logger
.
Tracef
(
&
config
.
EnableTrace_loader
,
"err: %v"
,
err
)
...
...
@@ -253,37 +253,110 @@ func (p *_Loader) Import(pkgpath string) (*types.Package, error) {
// 提取测试信息
if
p
.
cfg
.
UnitTest
{
for
_
,
filename
:=
range
filenames
{
if
!
p
.
isTestFile
(
filename
)
{
continue
}
pkg
.
TestInfo
.
Files
=
append
(
pkg
.
TestInfo
.
Files
,
filename
)
pkg
.
TestInfo
,
err
=
p
.
parseTestInfo
(
&
pkg
,
filenames
)
if
err
!=
nil
{
return
nil
,
err
}
for
_
,
name
:=
range
pkg
.
Pkg
.
Scope
()
.
Names
()
{
if
len
(
name
)
<
len
(
"Test?"
)
{
continue
}
logger
.
Tracef
(
&
config
.
EnableTrace_loader
,
"save pkgpath: %v"
,
pkgpath
)
p
.
prog
.
Pkgs
[
pkgpath
]
=
&
pkg
return
pkg
.
Pkg
,
nil
}
func
(
p
*
_Loader
)
parseTestInfo
(
pkg
*
Package
,
filenames
[]
string
)
(
*
TestInfo
,
error
)
{
tInfo
:=
&
TestInfo
{
Tests
:
make
(
map
[
string
]
TestFuncInfo
),
Benchs
:
make
(
map
[
string
]
BenchFuncInfo
),
Examples
:
make
(
map
[
string
]
ExampleFuncInfo
),
}
for
i
,
filename
:=
range
filenames
{
if
!
p
.
isTestFile
(
filename
)
{
continue
}
file
:=
pkg
.
Files
[
i
]
tInfo
.
Files
=
append
(
tInfo
.
Files
,
filename
)
// 提取测试/基准/示例函数
for
_
,
decl
:=
range
file
.
Decls
{
if
fn
,
ok
:=
decl
.
(
*
ast
.
FuncDecl
);
ok
{
name
:=
fn
.
Name
.
Name
// 函数参数均为空
obj
:=
pkg
.
Pkg
.
Scope
()
.
Lookup
(
name
)
{
var
bValidFuncType
=
false
if
fn
,
ok
:=
obj
.
(
*
types
.
Func
);
ok
{
if
sig
,
ok
:=
fn
.
Type
()
.
(
*
types
.
Signature
);
ok
{
if
sig
.
Recv
()
==
nil
&&
sig
.
Params
()
.
Len
()
==
0
&&
sig
.
Results
()
.
Len
()
==
0
{
bValidFuncType
=
true
}
}
}
if
!
bValidFuncType
{
continue
// skip
}
}
switch
{
case
strings
.
HasPrefix
(
name
,
"Test"
)
:
tInfo
.
Funcs
=
append
(
tInfo
.
Funcs
,
name
)
tInfo
.
Tests
[
name
]
=
TestFuncInfo
{
FuncPos
:
obj
.
Pos
(),
Name
:
name
,
}
case
strings
.
HasPrefix
(
name
,
"Bench"
)
:
tInfo
.
Benchs
[
name
]
=
BenchFuncInfo
{
FuncPos
:
obj
.
Pos
(),
Name
:
name
,
}
case
strings
.
HasPrefix
(
name
,
"Example"
)
:
tInfo
.
Examples
[
name
]
=
ExampleFuncInfo
{
FuncPos
:
obj
.
Pos
(),
Name
:
name
,
Output
:
p
.
parseExampleOutputComment
(
file
,
fn
),
}
}
}
if
!
strings
.
HasPrefix
(
name
,
"Test"
)
{
}
}
return
tInfo
,
nil
}
func
(
p
*
_Loader
)
parseExampleOutputComment
(
f
*
ast
.
File
,
fn
*
ast
.
FuncDecl
)
string
{
for
_
,
commentGroup
:=
range
f
.
Comments
{
if
commentGroup
.
Pos
()
<=
fn
.
Body
.
Pos
()
{
continue
}
if
commentGroup
.
End
()
>
fn
.
Body
.
End
()
{
break
}
for
j
,
comment
:=
range
commentGroup
.
List
{
if
comment
.
Text
!=
"// Output:"
{
continue
}
obj
:=
pkg
.
Pkg
.
Scope
()
.
Lookup
(
name
)
if
fn
,
ok
:=
obj
.
(
*
types
.
Func
);
ok
{
if
sig
,
ok
:=
fn
.
Type
()
.
(
*
types
.
Signature
);
ok
{
if
sig
.
Recv
()
==
nil
&&
sig
.
Params
()
.
Len
()
==
0
&&
sig
.
Results
()
.
Len
()
==
0
{
pkg
.
TestInfo
.
Funcs
=
append
(
pkg
.
TestInfo
.
Funcs
,
name
)
}
var
lineTexts
[]
string
for
_
,
x
:=
range
commentGroup
.
List
[
j
+
1
:
]
{
if
!
strings
.
HasPrefix
(
x
.
Text
,
"//"
)
{
break
}
lineTexts
=
append
(
lineTexts
,
strings
.
TrimSpace
(
x
.
Text
[
2
:
]))
}
return
strings
.
Join
(
lineTexts
,
"
\n
"
)
}
}
logger
.
Tracef
(
&
config
.
EnableTrace_loader
,
"save pkgpath: %v"
,
pkgpath
)
p
.
prog
.
Pkgs
[
pkgpath
]
=
&
pkg
return
pkg
.
Pkg
,
nil
return
""
}
func
(
p
*
_Loader
)
ParseDir_wsFiles
(
pkgpath
string
)
(
files
[]
WsFile
,
err
error
)
{
func
(
p
*
_Loader
)
ParseDir_wsFiles
(
pkgpath
string
)
(
files
[]
*
WsFile
,
err
error
)
{
logger
.
Tracef
(
&
config
.
EnableTrace_loader
,
"pkgpath: %v"
,
pkgpath
)
if
p
.
cfg
.
WaBackend
==
""
{
...
...
@@ -334,7 +407,7 @@ func (p *_Loader) ParseDir_wsFiles(pkgpath string) (files []WsFile, err error) {
}
for
i
:=
0
;
i
<
len
(
filenames
);
i
++
{
files
=
append
(
files
,
WsFile
{
files
=
append
(
files
,
&
WsFile
{
Name
:
filenames
[
i
],
Code
:
string
(
datas
[
i
]),
})
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录