elvish/pkg/eval/go_fn_test.go

257 lines
6.9 KiB
Go
Raw Normal View History

package eval
2018-02-06 16:02:27 +08:00
import (
"errors"
"math/big"
"reflect"
2018-02-06 16:02:27 +08:00
"testing"
"unsafe"
"src.elv.sh/pkg/eval/errs"
"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
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)))).
Equal(fn1).
NotEqual(fn2).
Repr("<builtin fn1>")
}
type testOptions struct {
Foo string
Bar string
}
func (o *testOptions) SetDefaultOptions() { o.Bar = "default" }
// 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) {
theFrame := &Frame{ports: []*Port{{}, {Chan: make(chan interface{}, 10)}, {}}}
2018-02-06 16:02:27 +08:00
theOptions := map[string]interface{}{}
2018-02-06 16:02:27 +08:00
var f Callable
callGood := func(fm *Frame, args []interface{}, opts map[string]interface{}) {
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)
}
}
callBad := func(fm *Frame, args []interface{}, opts map[string]interface{}, wantErr error) {
t.Helper()
2018-02-06 16:02:27 +08:00
err := f.Call(fm, args, opts)
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
}
}
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)
// 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" {
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) {
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
}
})
callGood(theFrame, nil, RawOptions{"foo": "bar"})
2018-02-06 16:02:27 +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" {
t.Errorf("RawOptions parameter doesn't get options")
2018-02-06 16:02:27 +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)
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.
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:
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")
})
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")
})
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")
})
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")
})
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")
})
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")
})
callBad(theFrame, []interface{}{"x"}, theOptions, anyError{})
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")
})
callBad(theFrame, []interface{}{"x"}, theOptions, anyError{})
// 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{})
}
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)
}