Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
libmonsoon-dev committed Aug 14, 2022
1 parent 8fd2252 commit c655a50
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
34 changes: 31 additions & 3 deletions cl/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,15 @@ import (
fmt "fmt"
os "os"
iox "github.com/goplus/gop/builtin/iox"
errors "github.com/qiniu/x/errors"
)
func main() {
for _gop_it := iox.EnumLines(func() (_gop_ret *os.File) {
var _gop_err error
_gop_ret, _gop_err = os.Open("foo.txt")
if _gop_err != nil {
_gop_err = errors.NewFrame(_gop_err, "open(\"foo.txt\")", "/foo/bar.gop", 2, "main.main")
panic(_gop_err)
}
return
Expand Down Expand Up @@ -912,7 +914,10 @@ func main() {
println(a, b)
}`, `package main
import fmt "fmt"
import (
fmt "fmt"
errors "github.com/qiniu/x/errors"
)
func t() (int, int, error) {
return 0, 0, nil
Expand All @@ -922,6 +927,7 @@ func main() {
var _gop_err error
_gop_ret, _gop_ret2, _gop_err = t()
if _gop_err != nil {
_gop_err = errors.NewFrame(_gop_err, "t()", "/foo/bar.gop", 9, "main.main")
panic(_gop_err)
}
return
Expand All @@ -943,6 +949,8 @@ func main() {
t()!
}`, `package main
import errors "github.com/qiniu/x/errors"
func t() error {
return nil
}
Expand All @@ -951,6 +959,7 @@ func main() {
var _gop_err error
_gop_err = t()
if _gop_err != nil {
_gop_err = errors.NewFrame(_gop_err, "t()", "/foo/bar.gop", 9, "main.main")
panic(_gop_err)
}
return
Expand Down Expand Up @@ -1454,6 +1463,7 @@ func foo(script string) {
import (
fmt "fmt"
goptest "github.com/goplus/gop/ast/goptest"
errors "github.com/qiniu/x/errors"
gopq "github.com/goplus/gop/ast/gopq"
)
Expand All @@ -1462,6 +1472,7 @@ func foo(script string) {
var _gop_err error
_gop_ret, _gop_err = goptest.New(script)
if _gop_err != nil {
_gop_err = errors.NewFrame(_gop_err, "goptest.New(script)", "/foo/bar.gop", 4, "main.foo")
panic(_gop_err)
}
return
Expand All @@ -1486,6 +1497,7 @@ func foo(script string) {
import (
fmt "fmt"
goptest "github.com/goplus/gop/ast/goptest"
errors "github.com/qiniu/x/errors"
gopq "github.com/goplus/gop/ast/gopq"
)
Expand All @@ -1494,6 +1506,7 @@ func foo(script string) {
var _gop_err error
_gop_ret, _gop_err = goptest.New(script)
if _gop_err != nil {
_gop_err = errors.NewFrame(_gop_err, "goptest.New(script)", "/foo/bar.gop", 4, "main.foo")
panic(_gop_err)
}
return
Expand All @@ -1513,14 +1526,18 @@ func add(x, y string) (int, error) {
}
`, `package main
import strconv "strconv"
import (
strconv "strconv"
errors "github.com/qiniu/x/errors"
)
func add(x string, y string) (int, error) {
var _autoGo_1 int
{
var _gop_err error
_autoGo_1, _gop_err = strconv.Atoi(x)
if _gop_err != nil {
_gop_err = errors.NewFrame(_gop_err, "strconv.Atoi(x)", "/foo/bar.gop", 5, "main.add")
return 0, _gop_err
}
goto _autoGo_2
Expand All @@ -1531,6 +1548,7 @@ func add(x string, y string) (int, error) {
var _gop_err error
_autoGo_3, _gop_err = strconv.Atoi(y)
if _gop_err != nil {
_gop_err = errors.NewFrame(_gop_err, "strconv.Atoi(y)", "/foo/bar.gop", 5, "main.add")
return 0, _gop_err
}
goto _autoGo_4
Expand Down Expand Up @@ -1577,12 +1595,16 @@ func TestErrWrapPanic(t *testing.T) {
var ret int = println("Hi")!
`, `package main
import fmt "fmt"
import (
fmt "fmt"
errors "github.com/qiniu/x/errors"
)
var ret int = func() (_gop_ret int) {
var _gop_err error
_gop_ret, _gop_err = fmt.Println("Hi")
if _gop_err != nil {
_gop_err = errors.NewFrame(_gop_err, "println(\"Hi\")", "/foo/bar.gop", 2, "main.main")
panic(_gop_err)
}
return
Expand All @@ -1599,6 +1621,8 @@ func mkdir(name string) error {
mkdir! "foo"
`, `package main
import errors "github.com/qiniu/x/errors"
func mkdir(name string) error {
return nil
}
Expand All @@ -1607,6 +1631,7 @@ func main() {
var _gop_err error
_gop_err = mkdir("foo")
if _gop_err != nil {
_gop_err = errors.NewFrame(_gop_err, "mkdir \"foo\"", "/foo/bar.gop", 6, "main.main")
panic(_gop_err)
}
return
Expand All @@ -1624,6 +1649,8 @@ func foo() (func(), error) {
foo()!()
`, `package main
import errors "github.com/qiniu/x/errors"
func foo() (func(), error) {
return nil, nil
}
Expand All @@ -1632,6 +1659,7 @@ func main() {
var _gop_err error
_gop_ret, _gop_err = foo()
if _gop_err != nil {
_gop_err = errors.NewFrame(_gop_err, "foo()", "/foo/bar.gop", 6, "main.main")
panic(_gop_err)
}
return
Expand Down
9 changes: 8 additions & 1 deletion cl/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -1021,14 +1021,21 @@ func compileErrWrapExpr(ctx *blockCtx, v *ast.ErrWrapExpr, inFlags int) {
currentFunc := ctx.cb.Func().Ancestor()
const newFrameArgs = 5

currentFuncName := currentFunc.Name()
if currentFuncName == "" {
currentFuncName = "main"
}

currentFuncName = strings.Join([]string{currentFunc.Pkg().Name(), currentFuncName}, ".")

cb.
VarRef(err).
Val(pkg.Import(errorPkgPath).Ref("NewFrame")).
Val(err).
Val(sprintAst(pkg.Fset, v.X)).
Val(pos.Filename).
Val(pos.Line).
Val(strings.Join([]string{currentFunc.Pkg().Name(), currentFunc.Name()}, ".")).
Val(currentFuncName).
Call(newFrameArgs).
Assign(1)
}
Expand Down

0 comments on commit c655a50

Please sign in to comment.