neo-go/pkg/compiler/return_test.go

150 lines
2.1 KiB
Go
Raw Permalink Normal View History

package compiler_test
import (
2020-05-06 14:24:32 +00:00
"fmt"
"math/big"
"testing"
)
func TestReturnInt64(t *testing.T) {
src := `package foo
func Main() int64 {
return 1
}`
eval(t, src, big.NewInt(1))
}
func TestMultipleReturn1(t *testing.T) {
src := `
package hello
func two() (int, int) {
return 5, 9
}
func Main() int {
a, _ := two()
return a
}
`
eval(t, src, big.NewInt(5))
}
func TestMultipleReturn2(t *testing.T) {
src := `
package hello
func two() (int, int) {
return 5, 9
}
func Main() int {
_, b := two()
return b
}
`
eval(t, src, big.NewInt(9))
}
func TestMultipleReturnUnderscore(t *testing.T) {
src := `
package hello
func f3() (int, int, int) {
return 5, 6, 7
}
func Main() int {
a, _, c := f3()
return a+c
}
`
eval(t, src, big.NewInt(12))
}
func TestMultipleReturnWithArg(t *testing.T) {
src := `
package hello
func inc2(a int) (int, int) {
return a+1, a+2
}
func Main() int {
a, b := 3, 9
a, b = inc2(a)
return a+b
}
`
eval(t, src, big.NewInt(9))
}
func TestSingleReturn(t *testing.T) {
src := `
package hello
func inc(k int) int {
return k+1
}
func Main() int {
a, b := inc(3), inc(4)
return a+b
}
`
eval(t, src, big.NewInt(9))
}
2020-05-06 14:24:32 +00:00
func TestNamedReturn(t *testing.T) {
src := `package foo
func Main() int {
a, b := f()
return a + b
}
func f() (a int, b int) {
2020-05-06 14:24:32 +00:00
a = 1
b = 2
c := 3
_ = c
return %s
}`
runCase := func(ret string, result *big.Int) func(t *testing.T) {
2020-05-06 14:24:32 +00:00
return func(t *testing.T) {
src := fmt.Sprintf(src, ret)
eval(t, src, result)
2020-05-06 14:24:32 +00:00
}
}
t.Run("NormalReturn", runCase("a, b", big.NewInt(3)))
t.Run("EmptyReturn", runCase("", big.NewInt(3)))
t.Run("AnotherVariable", runCase("b, c", big.NewInt(5)))
2020-05-06 14:24:32 +00:00
}
func TestNamedReturnDefault(t *testing.T) {
src := `package foo
func Main() int {
a, b, c := f()
return a + b + c
}
func f() (_ int, b int, c int) {
b += 1
return
}`
eval(t, src, big.NewInt(1))
}
func TestTypeAssertReturn(t *testing.T) {
src := `
package main
func foo() any {
return 5
}
func Main() int {
return foo().(int)
}
`
eval(t, src, big.NewInt(5))
}