github.com/markusbkk/elvish@v0.0.0-20231204143114-91dc52438621/pkg/eval/closure_test.go (about) 1 package eval_test 2 3 import ( 4 "testing" 5 6 "github.com/markusbkk/elvish/pkg/eval" 7 "github.com/markusbkk/elvish/pkg/eval/errs" 8 "github.com/markusbkk/elvish/pkg/tt" 9 10 . "github.com/markusbkk/elvish/pkg/eval/evaltest" 11 ) 12 13 func TestClosureAsValue(t *testing.T) { 14 Test(t, 15 // Basic operations as a value. 16 That("kind-of { }").Puts("fn"), 17 That("eq { } { }").Puts(false), 18 That("var x = { }; put [&$x= foo][$x]").Puts("foo"), 19 20 // Argument arity mismatch. 21 That("var f = {|x| }", "$f a b").Throws( 22 errs.ArityMismatch{What: "arguments", 23 ValidLow: 1, ValidHigh: 1, Actual: 2}, 24 "$f a b"), 25 That("var f = {|x y| }", "$f a").Throws( 26 errs.ArityMismatch{What: "arguments", ValidLow: 2, ValidHigh: 2, Actual: 1}, 27 "$f a"), 28 That("var f = {|x y @rest| }", "$f a").Throws( 29 errs.ArityMismatch{What: "arguments", ValidLow: 2, ValidHigh: -1, Actual: 1}, 30 "$f a"), 31 32 // Unsupported option. 33 That("var f = {|&valid1=1 &valid2=2| }; $f &bad1=1 &bad2=2").Throws( 34 eval.UnsupportedOptionsError{[]string{"bad1", "bad2"}}, 35 "$f &bad1=1 &bad2=2"), 36 37 That("all {|a b| }[arg-names]").Puts("a", "b"), 38 That("put {|@r| }[rest-arg]").Puts("0"), 39 That("all {|&opt=def| }[opt-names]").Puts("opt"), 40 That("all {|&opt=def| }[opt-defaults]").Puts("def"), 41 That("put { body }[body]").Puts("body "), 42 That("put {|x @y| body }[def]").Puts("{|x @y| body }"), 43 That("put { body }[src][code]"). 44 Puts("put { body }[src][code]"), 45 46 // Regression test for https://b.elv.sh/1126 47 That("fn f { body }; put $f~[body]").Puts("body "), 48 ) 49 } 50 51 func TestUnsupportedOptionsError(t *testing.T) { 52 tt.Test(t, tt.Fn("Error", error.Error), tt.Table{ 53 tt.Args(eval.UnsupportedOptionsError{[]string{"sole-opt"}}).Rets( 54 "unsupported option: sole-opt"), 55 tt.Args(eval.UnsupportedOptionsError{[]string{"opt-foo", "opt-bar"}}).Rets( 56 "unsupported options: opt-foo, opt-bar"), 57 }) 58 }