提交 4ca0062f 编写于 作者: chai2010's avatar chai2010

wa test 支持异常测试

上级 c2ceecec
......@@ -102,6 +102,23 @@ func runTest(cfg *config.Config, pkgpath string, appArgs ...string) {
tFuncName := mainPkg.Pkg.Path() + "." + t.Name
tFuncName = strings.ReplaceAll(tFuncName, "/", "$")
_, stdout, stderr, err := m.RunFunc(tFuncName)
if t.OutputPanic {
if exitCode, _ := wazero.AsExitError(err); exitCode == 0 {
fmt.Printf("---- %s.%s\n", prog.Manifest.MainPkg, t.Name)
fmt.Printf(" expect panic, got = nil\n")
}
stdout = bytes.TrimSpace(stdout)
expect, got := t.Output, string(stdout)
// panic: ${expect} (pos)
if !strings.HasPrefix(got, "panic: "+expect) {
fmt.Printf("---- %s.%s\n", prog.Manifest.MainPkg, t.Name)
fmt.Printf(" expect(panic) = %q, got = %q\n", expect, got)
}
continue
}
if err != nil {
if len(stdout) > 0 {
if s := sWithPrefix(string(stdout), " "); s != "" {
......@@ -158,6 +175,23 @@ func runTest(cfg *config.Config, pkgpath string, appArgs ...string) {
tFuncName := mainPkg.Pkg.Path() + "." + t.Name
tFuncName = strings.ReplaceAll(tFuncName, "/", "$")
_, stdout, stderr, err := m.RunFunc(tFuncName)
if t.OutputPanic {
if exitCode, _ := wazero.AsExitError(err); exitCode == 0 {
fmt.Printf("---- %s.%s\n", prog.Manifest.MainPkg, t.Name)
fmt.Printf(" expect panic, got = nil\n")
}
stdout = bytes.TrimSpace(stdout)
expect, got := t.Output, string(stdout)
// panic: ${expect} (pos)
if !strings.HasPrefix(got, "panic: "+expect) {
fmt.Printf("---- %s.%s\n", prog.Manifest.MainPkg, t.Name)
fmt.Printf(" expect(panic) = %q, got = %q\n", expect, got)
}
continue
}
if err != nil {
if len(stdout) > 0 {
if s := sWithPrefix(string(stdout), " "); s != "" {
......
......@@ -44,9 +44,10 @@ type TestInfo struct {
// 测试函数信息
type TestFuncInfo struct {
FuncPos token.Pos // 函数位置
Name string // 函数名, 不含包路径
Output string // 期望输出, 为空表示不验证
FuncPos token.Pos // 函数位置
Name string // 函数名, 不含包路径
Output string // 期望输出, 为空表示不验证
OutputPanic bool // 异常信息
}
// 汇编代码文件
......
......@@ -300,10 +300,12 @@ func (p *_Loader) parseTestInfo(pkg *Package, filenames []string) (*TestInfo, er
switch {
case strings.HasPrefix(name, "Test"):
output, isPanic := p.parseExampleOutputComment(file, fn)
tInfo.Tests = append(tInfo.Tests, TestFuncInfo{
FuncPos: obj.Pos(),
Name: name,
Output: p.parseExampleOutputComment(file, fn),
FuncPos: obj.Pos(),
Name: name,
Output: output,
OutputPanic: isPanic,
})
case strings.HasPrefix(name, "Bench"):
tInfo.Benchs = append(tInfo.Benchs, TestFuncInfo{
......@@ -311,10 +313,12 @@ func (p *_Loader) parseTestInfo(pkg *Package, filenames []string) (*TestInfo, er
Name: name,
})
case strings.HasPrefix(name, "Example"):
output, isPanic := p.parseExampleOutputComment(file, fn)
tInfo.Examples = append(tInfo.Examples, TestFuncInfo{
FuncPos: obj.Pos(),
Name: name,
Output: p.parseExampleOutputComment(file, fn),
FuncPos: obj.Pos(),
Name: name,
Output: output,
OutputPanic: isPanic,
})
}
}
......@@ -324,7 +328,7 @@ func (p *_Loader) parseTestInfo(pkg *Package, filenames []string) (*TestInfo, er
return tInfo, nil
}
func (p *_Loader) parseExampleOutputComment(f *ast.File, fn *ast.FuncDecl) string {
func (p *_Loader) parseExampleOutputComment(f *ast.File, fn *ast.FuncDecl) (output string, isPanic bool) {
for _, commentGroup := range f.Comments {
if commentGroup.Pos() <= fn.Body.Pos() {
continue
......@@ -334,23 +338,33 @@ func (p *_Loader) parseExampleOutputComment(f *ast.File, fn *ast.FuncDecl) strin
}
for j, comment := range commentGroup.List {
if comment.Text != "// Output:" {
continue
}
switch comment.Text {
case "// Output:":
var lineTexts []string
for _, x := range commentGroup.List[j+1:] {
if !strings.HasPrefix(x.Text, "//") {
break
}
lineTexts = append(lineTexts, strings.TrimSpace(x.Text[2:]))
}
var lineTexts []string
for _, x := range commentGroup.List[j+1:] {
if !strings.HasPrefix(x.Text, "//") {
break
return strings.Join(lineTexts, "\n"), false
case "// Output(panic):":
var lineTexts []string
for _, x := range commentGroup.List[j+1:] {
if !strings.HasPrefix(x.Text, "//") {
break
}
lineTexts = append(lineTexts, strings.TrimSpace(x.Text[2:]))
}
lineTexts = append(lineTexts, strings.TrimSpace(x.Text[2:]))
}
return strings.Join(lineTexts, "\n")
return strings.Join(lineTexts, "\n"), true
}
}
}
return ""
return "", false
}
func (p *_Loader) ParseDir_wsFiles(pkgpath string) (files []*WsFile, err error) {
......
......@@ -23,4 +23,11 @@ func ExampleApple {
// Output:
// apple-mvp
}
\ No newline at end of file
}
func ExamplePanic {
panic("fuck panic")
// Output(panic):
// fuck panic
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册