提交 22c88148 编写于 作者: chai2010's avatar chai2010

单元测试支持 output 验证

上级 8e8ebcad
......@@ -3,6 +3,7 @@
package app
import (
"bytes"
"fmt"
"os"
"strings"
......@@ -41,8 +42,12 @@ func (p *App) RunTest(pkgpath string, appArgs ...string) error {
return err
}
stdoutStderr, err := apputil.RunWasm(cfg, "a.out.wat", appArgs...)
if err == nil {
stdout, stderr, err := apputil.RunWasmEx(cfg, "a.out.wat", appArgs...)
stdout = bytes.TrimSpace(stdout)
bOutputOK := t.Output == string(stdout)
if err == nil && bOutputOK {
continue
}
......@@ -52,13 +57,26 @@ func (p *App) RunTest(pkgpath string, appArgs ...string) error {
}
if _, ok := err.(*sys.ExitError); ok {
fmt.Printf("---- %s.%s\n", prog.Manifest.MainPkg, t.Name)
if s := sWithPrefix(string(stdoutStderr), " "); s != "" {
if s := sWithPrefix(string(stdout), " "); s != "" {
fmt.Println(s)
}
if s := sWithPrefix(string(stderr), " "); s != "" {
fmt.Println(s)
}
} else {
fmt.Println(err)
}
}
if t.Output != "" {
if expect, got := t.Output, string(stdout); expect != got {
if firstError == nil {
firstError = fmt.Errorf("expect = %q, got = %q", expect, got)
}
fmt.Printf("---- %s.%s\n", prog.Manifest.MainPkg, t.Name)
fmt.Printf(" expect = %q, got = %q\n", expect, got)
}
}
}
for _, t := range mainPkg.TestInfo.Examples {
output, err := compiler_wat.New().Compile(prog, t.Name)
......@@ -70,8 +88,12 @@ func (p *App) RunTest(pkgpath string, appArgs ...string) error {
return err
}
stdoutStderr, err := apputil.RunWasm(cfg, "a.out.wat", appArgs...)
if err == nil {
stdout, stderr, err := apputil.RunWasmEx(cfg, "a.out.wat", appArgs...)
stdout = bytes.TrimSpace(stdout)
bOutputOK := t.Output == string(stdout)
if err == nil && bOutputOK {
continue
}
......@@ -81,20 +103,33 @@ func (p *App) RunTest(pkgpath string, appArgs ...string) error {
}
if _, ok := err.(*sys.ExitError); ok {
fmt.Printf("---- %s.%s\n", prog.Manifest.MainPkg, t.Name)
if s := sWithPrefix(string(stdoutStderr), " "); s != "" {
if s := sWithPrefix(string(stdout), " "); s != "" {
fmt.Println(s)
}
if s := sWithPrefix(string(stderr), " "); s != "" {
fmt.Println(s)
}
} else {
fmt.Println(err)
}
}
if t.Output != "" {
if expect, got := t.Output, string(stdout); expect != got {
if firstError == nil {
firstError = fmt.Errorf("expect = %q, got = %q", expect, got)
}
fmt.Printf("---- %s.%s\n", prog.Manifest.MainPkg, t.Name)
fmt.Printf(" expect = %q, got = %q\n", expect, got)
}
}
}
if firstError != nil {
fmt.Printf("FAIL %s %v\n", prog.Manifest.MainPkg, time.Now().Sub(startTime).Round(time.Microsecond))
fmt.Printf("FAIL %s %v\n", prog.Manifest.MainPkg, time.Now().Sub(startTime).Round(time.Millisecond))
os.Exit(1)
}
fmt.Printf("ok %s %v\n", prog.Manifest.MainPkg, time.Now().Sub(startTime).Round(time.Microsecond))
fmt.Printf("ok %s %v\n", prog.Manifest.MainPkg, time.Now().Sub(startTime).Round(time.Millisecond))
return nil
}
......
......@@ -29,8 +29,13 @@ func getWatAbsDir(filename string) string {
}
func RunWasm(cfg *config.Config, filename string, wasmArgs ...string) (stdoutStderr []byte, err error) {
stdout, strerr, err := runWasm(cfg, filename, wasmArgs...)
stdoutStderr = append(stdout, strerr...)
stdout, stderr, err := runWasm(cfg, filename, wasmArgs...)
stdoutStderr = append(stdout, stderr...)
return
}
func RunWasmEx(cfg *config.Config, filename string, wasmArgs ...string) (stdout, stderr []byte, err error) {
stdout, stderr, err = runWasm(cfg, filename, wasmArgs...)
return
}
......
// 版权 @2023 凹语言 作者。保留所有权利。
func TestFailed {
func TestFailed_assert {
ok := true
assert(ok)
ok = false
assert(ok, "message")
}
func TestFailed_output {
println("abc 123")
// Output:
// abc 1232
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册