2019-12-24 13:46:43 +00:00
|
|
|
package compiler_test
|
|
|
|
|
|
|
|
import (
|
2020-05-06 14:24:32 +00:00
|
|
|
"fmt"
|
2019-12-24 13:46:43 +00:00
|
|
|
"math/big"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2020-04-28 13:42:29 +00:00
|
|
|
func TestReturnInt64(t *testing.T) {
|
|
|
|
src := `package foo
|
|
|
|
func Main() int64 {
|
|
|
|
return 1
|
|
|
|
}`
|
|
|
|
eval(t, src, big.NewInt(1))
|
|
|
|
}
|
|
|
|
|
2019-12-24 13:46:43 +00:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2020-01-29 14:04:49 +00:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2019-12-24 13:46:43 +00:00
|
|
|
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
|
2022-08-17 09:19:37 +00:00
|
|
|
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
|
|
|
|
}`
|
|
|
|
|
2022-08-17 09:19:37 +00:00
|
|
|
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)
|
2022-08-17 09:19:37 +00:00
|
|
|
eval(t, src, result)
|
2020-05-06 14:24:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-17 09:19:37 +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
|
|
|
}
|
2020-10-12 16:00:07 +00:00
|
|
|
|
2024-04-04 14:51:03 +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))
|
|
|
|
}
|
|
|
|
|
2020-10-12 16:00:07 +00:00
|
|
|
func TestTypeAssertReturn(t *testing.T) {
|
|
|
|
src := `
|
|
|
|
package main
|
|
|
|
|
2023-04-03 10:34:24 +00:00
|
|
|
func foo() any {
|
2020-10-12 16:00:07 +00:00
|
|
|
return 5
|
|
|
|
}
|
|
|
|
|
|
|
|
func Main() int {
|
|
|
|
return foo().(int)
|
|
|
|
}
|
|
|
|
`
|
|
|
|
eval(t, src, big.NewInt(5))
|
|
|
|
}
|