2020-09-03 13:48:39 +08:00
|
|
|
package eval_test
|
2017-12-24 00:27:49 +08:00
|
|
|
|
2020-04-13 07:47:33 +08:00
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2021-01-27 09:28:38 +08:00
|
|
|
"src.elv.sh/pkg/eval"
|
|
|
|
"src.elv.sh/pkg/eval/errs"
|
|
|
|
"src.elv.sh/pkg/tt"
|
2020-09-03 13:48:39 +08:00
|
|
|
|
2021-01-27 09:28:38 +08:00
|
|
|
. "src.elv.sh/pkg/eval/evaltest"
|
2020-04-13 07:47:33 +08:00
|
|
|
)
|
2017-12-24 00:27:49 +08:00
|
|
|
|
2021-01-10 22:48:21 +08:00
|
|
|
func TestClosureAsValue(t *testing.T) {
|
2018-05-22 08:08:11 +08:00
|
|
|
Test(t,
|
2021-01-04 21:41:10 +08:00
|
|
|
// Basic operations as a value.
|
2018-03-03 13:06:03 +08:00
|
|
|
That("kind-of { }").Puts("fn"),
|
|
|
|
That("eq { } { }").Puts(false),
|
2022-01-03 08:10:40 +08:00
|
|
|
That("var x = { }; put [&$x= foo][$x]").Puts("foo"),
|
2020-04-13 07:47:33 +08:00
|
|
|
|
2021-01-04 21:41:10 +08:00
|
|
|
// Argument arity mismatch.
|
2022-01-03 08:10:40 +08:00
|
|
|
That("var f = {|x| }", "$f a b").Throws(
|
2021-06-04 10:51:38 +08:00
|
|
|
errs.ArityMismatch{What: "arguments",
|
2020-04-13 07:47:33 +08:00
|
|
|
ValidLow: 1, ValidHigh: 1, Actual: 2},
|
|
|
|
"$f a b"),
|
2022-01-03 08:10:40 +08:00
|
|
|
That("var f = {|x y| }", "$f a").Throws(
|
2021-06-04 10:51:38 +08:00
|
|
|
errs.ArityMismatch{What: "arguments", ValidLow: 2, ValidHigh: 2, Actual: 1},
|
2020-04-13 07:47:33 +08:00
|
|
|
"$f a"),
|
2022-01-03 08:10:40 +08:00
|
|
|
That("var f = {|x y @rest| }", "$f a").Throws(
|
2021-06-04 10:51:38 +08:00
|
|
|
errs.ArityMismatch{What: "arguments", ValidLow: 2, ValidHigh: -1, Actual: 1},
|
2020-04-13 07:47:33 +08:00
|
|
|
"$f a"),
|
|
|
|
|
2021-01-04 21:41:10 +08:00
|
|
|
// Unsupported option.
|
2022-01-03 08:10:40 +08:00
|
|
|
That("var f = {|&valid1=1 &valid2=2| }; $f &bad1=1 &bad2=2").Throws(
|
2021-01-04 21:41:10 +08:00
|
|
|
eval.UnsupportedOptionsError{[]string{"bad1", "bad2"}},
|
|
|
|
"$f &bad1=1 &bad2=2"),
|
2018-03-02 00:38:00 +08:00
|
|
|
|
2021-11-28 11:24:42 +08:00
|
|
|
That("all {|a b| }[arg-names]").Puts("a", "b"),
|
|
|
|
That("put {|@r| }[rest-arg]").Puts("0"),
|
|
|
|
That("all {|&opt=def| }[opt-names]").Puts("opt"),
|
|
|
|
That("all {|&opt=def| }[opt-defaults]").Puts("def"),
|
2021-10-14 05:39:45 +08:00
|
|
|
That("put { body }[body]").Puts("body "),
|
2021-11-28 11:24:42 +08:00
|
|
|
That("put {|x @y| body }[def]").Puts("{|x @y| body }"),
|
2018-07-06 08:32:42 +08:00
|
|
|
That("put { body }[src][code]").
|
|
|
|
Puts("put { body }[src][code]"),
|
2020-08-26 03:17:21 +08:00
|
|
|
|
|
|
|
// Regression test for https://b.elv.sh/1126
|
2021-10-14 05:39:45 +08:00
|
|
|
That("fn f { body }; put $f~[body]").Puts("body "),
|
2018-05-22 08:08:11 +08:00
|
|
|
)
|
2017-12-24 00:27:49 +08:00
|
|
|
}
|
2021-01-04 21:41:10 +08:00
|
|
|
|
|
|
|
func TestUnsupportedOptionsError(t *testing.T) {
|
|
|
|
tt.Test(t, tt.Fn("Error", error.Error), tt.Table{
|
|
|
|
tt.Args(eval.UnsupportedOptionsError{[]string{"sole-opt"}}).Rets(
|
|
|
|
"unsupported option: sole-opt"),
|
|
|
|
tt.Args(eval.UnsupportedOptionsError{[]string{"opt-foo", "opt-bar"}}).Rets(
|
|
|
|
"unsupported options: opt-foo, opt-bar"),
|
|
|
|
})
|
|
|
|
}
|