2017-12-17 11:41:25 +08:00
|
|
|
package eval
|
|
|
|
|
2018-02-06 16:02:27 +08:00
|
|
|
import (
|
|
|
|
"errors"
|
2021-04-04 20:37:38 +08:00
|
|
|
"math/big"
|
2020-09-03 13:48:39 +08:00
|
|
|
"reflect"
|
2018-02-06 16:02:27 +08:00
|
|
|
"testing"
|
2020-04-13 07:50:03 +08:00
|
|
|
"unsafe"
|
2017-12-22 04:49:14 +08:00
|
|
|
|
2021-01-27 09:28:38 +08:00
|
|
|
"src.elv.sh/pkg/eval/errs"
|
2020-09-03 13:48:39 +08:00
|
|
|
|
2021-01-27 09:28:38 +08:00
|
|
|
"src.elv.sh/pkg/eval/vals"
|
2021-05-04 05:17:46 +08:00
|
|
|
"src.elv.sh/pkg/persistent/hash"
|
2018-02-06 16:02:27 +08:00
|
|
|
)
|
2017-12-24 00:27:49 +08:00
|
|
|
|
2020-04-13 07:50:03 +08:00
|
|
|
func TestGoFnAsValue(t *testing.T) {
|
|
|
|
fn1 := NewGoFn("fn1", func() {})
|
|
|
|
fn2 := NewGoFn("fn2", func() {})
|
|
|
|
vals.TestValue(t, fn1).
|
|
|
|
Kind("fn").
|
2020-09-05 05:30:42 +08:00
|
|
|
Hash(hash.Pointer(unsafe.Pointer(fn1.(*goFn)))).
|
2020-04-13 07:50:03 +08:00
|
|
|
Equal(fn1).
|
|
|
|
NotEqual(fn2).
|
|
|
|
Repr("<builtin fn1>")
|
|
|
|
}
|
|
|
|
|
2018-09-28 00:35:35 +08:00
|
|
|
type testOptions struct {
|
|
|
|
Foo string
|
|
|
|
Bar string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *testOptions) SetDefaultOptions() { o.Bar = "default" }
|
|
|
|
|
2020-05-04 06:38:06 +08:00
|
|
|
// TODO: Break down this test into multiple small ones, and test errors more
|
|
|
|
// strictly.
|
|
|
|
|
2019-04-19 05:57:14 +08:00
|
|
|
func TestGoFnCall(t *testing.T) {
|
2021-06-18 07:11:51 +08:00
|
|
|
theFrame := &Frame{ports: []*Port{{}, {Chan: make(chan interface{}, 10)}, {}}}
|
2018-02-06 16:02:27 +08:00
|
|
|
theOptions := map[string]interface{}{}
|
2017-12-17 11:41:25 +08:00
|
|
|
|
2018-02-06 16:02:27 +08:00
|
|
|
var f Callable
|
|
|
|
callGood := func(fm *Frame, args []interface{}, opts map[string]interface{}) {
|
2020-09-03 13:48:39 +08:00
|
|
|
t.Helper()
|
2018-02-06 16:02:27 +08:00
|
|
|
err := f.Call(fm, args, opts)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed to call f: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2020-04-13 07:47:33 +08:00
|
|
|
callBad := func(fm *Frame, args []interface{}, opts map[string]interface{}, wantErr error) {
|
2020-09-03 13:48:39 +08:00
|
|
|
t.Helper()
|
2018-02-06 16:02:27 +08:00
|
|
|
err := f.Call(fm, args, opts)
|
2020-04-13 07:47:33 +08:00
|
|
|
if !matchErr(wantErr, err) {
|
2021-06-14 05:54:47 +08:00
|
|
|
t.Errorf("Calling f returned error %v, want %v", err, wantErr)
|
2018-02-06 16:02:27 +08:00
|
|
|
}
|
|
|
|
}
|
2017-12-17 11:41:25 +08:00
|
|
|
|
2018-02-06 16:02:27 +08:00
|
|
|
// *Frame parameter gets the Frame.
|
2019-04-19 05:57:14 +08:00
|
|
|
f = NewGoFn("f", func(f *Frame) {
|
2018-02-06 16:02:27 +08:00
|
|
|
if f != theFrame {
|
|
|
|
t.Errorf("*Frame parameter doesn't get current frame")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
callGood(theFrame, nil, theOptions)
|
|
|
|
|
2018-09-28 00:35:35 +08:00
|
|
|
// RawOptions parameter gets options.
|
2019-04-19 05:57:14 +08:00
|
|
|
f = NewGoFn("f", func(opts RawOptions) {
|
2018-02-06 16:02:27 +08:00
|
|
|
if opts["foo"] != "bar" {
|
2018-09-28 00:35:35 +08:00
|
|
|
t.Errorf("RawOptions parameter doesn't get options")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
callGood(theFrame, nil, RawOptions{"foo": "bar"})
|
|
|
|
|
|
|
|
// ScanOptions parameters gets scanned options.
|
2019-04-19 05:57:14 +08:00
|
|
|
f = NewGoFn("f", func(opts testOptions) {
|
2018-09-28 00:35:35 +08:00
|
|
|
if opts.Foo != "bar" {
|
|
|
|
t.Errorf("ScanOptions parameter doesn't get options")
|
|
|
|
}
|
|
|
|
if opts.Bar != "default" {
|
|
|
|
t.Errorf("ScanOptions parameter doesn't use default value")
|
2018-02-06 16:02:27 +08:00
|
|
|
}
|
|
|
|
})
|
2018-02-15 21:25:17 +08:00
|
|
|
callGood(theFrame, nil, RawOptions{"foo": "bar"})
|
2018-02-06 16:02:27 +08:00
|
|
|
|
2018-09-28 00:35:35 +08:00
|
|
|
// Combination of Frame and RawOptions.
|
2019-04-19 05:57:14 +08:00
|
|
|
f = NewGoFn("f", func(f *Frame, opts RawOptions) {
|
2018-02-06 16:02:27 +08:00
|
|
|
if f != theFrame {
|
|
|
|
t.Errorf("*Frame parameter doesn't get current frame")
|
|
|
|
}
|
|
|
|
if opts["foo"] != "bar" {
|
2018-09-28 00:35:35 +08:00
|
|
|
t.Errorf("RawOptions parameter doesn't get options")
|
2018-02-06 16:02:27 +08:00
|
|
|
}
|
|
|
|
})
|
2018-02-15 21:25:17 +08:00
|
|
|
callGood(theFrame, nil, RawOptions{"foo": "bar"})
|
2018-02-06 16:02:27 +08:00
|
|
|
|
|
|
|
// Argument passing.
|
2019-04-19 05:57:14 +08:00
|
|
|
f = NewGoFn("f", func(x, y string) {
|
2018-02-06 16:02:27 +08:00
|
|
|
if x != "lorem" {
|
|
|
|
t.Errorf("Argument x not passed")
|
|
|
|
}
|
|
|
|
if y != "ipsum" {
|
|
|
|
t.Errorf("Argument y not passed")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
callGood(theFrame, []interface{}{"lorem", "ipsum"}, theOptions)
|
|
|
|
|
|
|
|
// Variadic arguments.
|
2019-04-19 05:57:14 +08:00
|
|
|
f = NewGoFn("f", func(x ...string) {
|
2018-02-06 16:02:27 +08:00
|
|
|
if len(x) != 2 || x[0] != "lorem" || x[1] != "ipsum" {
|
|
|
|
t.Errorf("Variadic argument not passed")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
callGood(theFrame, []interface{}{"lorem", "ipsum"}, theOptions)
|
|
|
|
|
|
|
|
// Conversion into int and float64.
|
2019-04-19 05:57:14 +08:00
|
|
|
f = NewGoFn("f", func(i int, f float64) {
|
2018-02-06 16:02:27 +08:00
|
|
|
if i != 314 {
|
|
|
|
t.Errorf("Integer argument i not passed")
|
|
|
|
}
|
|
|
|
if f != 1.25 {
|
|
|
|
t.Errorf("Float argument f not passed")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
callGood(theFrame, []interface{}{"314", "1.25"}, theOptions)
|
|
|
|
|
|
|
|
// Conversion of supplied inputs.
|
2019-04-19 05:57:14 +08:00
|
|
|
f = NewGoFn("f", func(i Inputs) {
|
2018-02-06 16:02:27 +08:00
|
|
|
var values []interface{}
|
|
|
|
i(func(x interface{}) {
|
|
|
|
values = append(values, x)
|
|
|
|
})
|
|
|
|
if len(values) != 2 || values[0] != "foo" || values[1] != "bar" {
|
|
|
|
t.Errorf("Inputs parameter didn't get supplied inputs")
|
|
|
|
}
|
|
|
|
})
|
2018-02-15 17:14:05 +08:00
|
|
|
callGood(theFrame, []interface{}{vals.MakeList("foo", "bar")}, theOptions)
|
2018-02-06 16:02:27 +08:00
|
|
|
|
|
|
|
// Conversion of implicit inputs.
|
|
|
|
ch := make(chan interface{}, 10)
|
|
|
|
ch <- "foo"
|
|
|
|
ch <- "bar"
|
|
|
|
close(ch)
|
2021-06-18 07:11:51 +08:00
|
|
|
inFrame := &Frame{ports: []*Port{{Chan: ch}, {}, {}}}
|
2019-04-19 05:57:14 +08:00
|
|
|
f = NewGoFn("f", func(i Inputs) {
|
2018-02-06 16:02:27 +08:00
|
|
|
var values []interface{}
|
|
|
|
i(func(x interface{}) {
|
|
|
|
values = append(values, x)
|
|
|
|
})
|
|
|
|
if len(values) != 2 || values[0] != "foo" || values[1] != "bar" {
|
|
|
|
t.Errorf("Inputs parameter didn't get implicit inputs")
|
|
|
|
}
|
|
|
|
})
|
2018-02-15 17:14:05 +08:00
|
|
|
callGood(inFrame, []interface{}{vals.MakeList("foo", "bar")}, theOptions)
|
2018-02-06 16:02:27 +08:00
|
|
|
|
|
|
|
// Outputting of return values.
|
|
|
|
outFrame := &Frame{ports: make([]*Port, 3)}
|
|
|
|
ch = make(chan interface{}, 10)
|
|
|
|
outFrame.ports[1] = &Port{Chan: ch}
|
2019-04-19 05:57:14 +08:00
|
|
|
f = NewGoFn("f", func() string { return "ret" })
|
2018-02-06 16:02:27 +08:00
|
|
|
callGood(outFrame, nil, theOptions)
|
|
|
|
select {
|
|
|
|
case ret := <-ch:
|
|
|
|
if ret != "ret" {
|
|
|
|
t.Errorf("Output is not the same as return value")
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
t.Errorf("Return value is not outputted")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Conversion of return values.
|
2021-04-04 20:37:38 +08:00
|
|
|
f = NewGoFn("f", func() *big.Int { return big.NewInt(314) })
|
2018-02-06 16:02:27 +08:00
|
|
|
callGood(outFrame, nil, theOptions)
|
|
|
|
select {
|
|
|
|
case ret := <-ch:
|
2021-04-04 20:37:38 +08:00
|
|
|
if ret != 314 {
|
|
|
|
t.Errorf("Return value is not converted to int")
|
2018-02-06 16:02:27 +08:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
t.Errorf("Return value is not outputted")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Passing of error return value.
|
|
|
|
theError := errors.New("the error")
|
2019-04-19 05:57:14 +08:00
|
|
|
f = NewGoFn("f", func() (string, error) {
|
2018-02-06 16:02:27 +08:00
|
|
|
return "x", theError
|
|
|
|
})
|
|
|
|
if f.Call(outFrame, nil, theOptions) != theError {
|
|
|
|
t.Errorf("Returned error is not passed")
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-ch:
|
|
|
|
t.Errorf("Return value is outputted when error is not nil")
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
// Too many arguments.
|
2019-04-19 05:57:14 +08:00
|
|
|
f = NewGoFn("f", func() {
|
2018-02-06 16:02:27 +08:00
|
|
|
t.Errorf("Function called when there are too many arguments")
|
|
|
|
})
|
2021-06-04 10:51:38 +08:00
|
|
|
callBad(theFrame, []interface{}{"x"}, theOptions, errs.ArityMismatch{What: "arguments",
|
|
|
|
ValidLow: 0, ValidHigh: 0, Actual: 1})
|
2018-02-06 16:02:27 +08:00
|
|
|
|
|
|
|
// Too few arguments.
|
2019-04-19 05:57:14 +08:00
|
|
|
f = NewGoFn("f", func(x string) {
|
2018-02-06 16:02:27 +08:00
|
|
|
t.Errorf("Function called when there are too few arguments")
|
|
|
|
})
|
2021-06-04 10:51:38 +08:00
|
|
|
callBad(theFrame, nil, theOptions, errs.ArityMismatch{What: "arguments",
|
|
|
|
ValidLow: 1, ValidHigh: 1, Actual: 0})
|
2019-04-19 05:57:14 +08:00
|
|
|
f = NewGoFn("f", func(x string, y ...string) {
|
2018-02-06 16:02:27 +08:00
|
|
|
t.Errorf("Function called when there are too few arguments")
|
|
|
|
})
|
2021-06-04 10:51:38 +08:00
|
|
|
callBad(theFrame, nil, theOptions, errs.ArityMismatch{What: "arguments",
|
|
|
|
ValidLow: 1, ValidHigh: -1, Actual: 0})
|
2018-02-06 16:02:27 +08:00
|
|
|
|
|
|
|
// Options when the function does not accept options.
|
2019-04-19 05:57:14 +08:00
|
|
|
f = NewGoFn("f", func() {
|
2018-02-06 16:02:27 +08:00
|
|
|
t.Errorf("Function called when there are extra options")
|
|
|
|
})
|
2021-05-22 11:29:36 +08:00
|
|
|
callBad(theFrame, nil, RawOptions{"foo": "bar"}, ErrNoOptAccepted)
|
2018-02-06 16:02:27 +08:00
|
|
|
|
|
|
|
// Wrong argument type.
|
2019-04-19 05:57:14 +08:00
|
|
|
f = NewGoFn("f", func(x string) {
|
2018-02-06 16:02:27 +08:00
|
|
|
t.Errorf("Function called when arguments have wrong type")
|
|
|
|
})
|
2020-04-13 07:47:33 +08:00
|
|
|
callBad(theFrame, []interface{}{1}, theOptions, anyError{})
|
2018-02-06 16:02:27 +08:00
|
|
|
|
|
|
|
// Wrong argument type: cannot convert to int.
|
2019-04-19 05:57:14 +08:00
|
|
|
f = NewGoFn("f", func(x int) {
|
2018-02-06 16:02:27 +08:00
|
|
|
t.Errorf("Function called when arguments have wrong type")
|
|
|
|
})
|
2020-04-13 07:47:33 +08:00
|
|
|
callBad(theFrame, []interface{}{"x"}, theOptions, anyError{})
|
2017-12-17 11:41:25 +08:00
|
|
|
|
2018-02-06 16:02:27 +08:00
|
|
|
// Wrong argument type: cannot convert to float64.
|
2019-04-19 05:57:14 +08:00
|
|
|
f = NewGoFn("f", func(x float64) {
|
2018-02-06 16:02:27 +08:00
|
|
|
t.Errorf("Function called when arguments have wrong type")
|
|
|
|
})
|
2020-04-13 07:47:33 +08:00
|
|
|
callBad(theFrame, []interface{}{"x"}, theOptions, anyError{})
|
2020-05-04 06:38:06 +08:00
|
|
|
|
|
|
|
// Invalid option; regression test for #958.
|
|
|
|
f = NewGoFn("f", func(opts testOptions) {})
|
|
|
|
callBad(theFrame, nil, RawOptions{"bad": ""}, anyError{})
|
|
|
|
|
|
|
|
// Invalid option type; regression test for #958.
|
|
|
|
f = NewGoFn("f", func(opts testOptions) {})
|
|
|
|
callBad(theFrame, nil, RawOptions{"foo": vals.EmptyList}, anyError{})
|
2017-12-17 11:41:25 +08:00
|
|
|
}
|
2020-09-03 13:48:39 +08:00
|
|
|
|
|
|
|
type anyError struct{}
|
|
|
|
|
|
|
|
func (anyError) Error() string { return "any error" }
|
|
|
|
|
|
|
|
func matchErr(want, got error) bool {
|
|
|
|
if (want == anyError{}) {
|
|
|
|
return got != nil
|
|
|
|
}
|
|
|
|
return reflect.DeepEqual(want, got)
|
|
|
|
}
|