github.com/kolbycrouch/elvish@v0.14.1-0.20210614162631-215b9ac1c423/pkg/eval/closure_test.go (about) 1 package eval_test 2 3 import ( 4 "testing" 5 6 "src.elv.sh/pkg/eval" 7 "src.elv.sh/pkg/eval/errs" 8 "src.elv.sh/pkg/tt" 9 10 . "src.elv.sh/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("x = { }; put [&$x= foo][$x]").Puts("foo"), 19 20 // Argument arity mismatch. 21 That("f = [x]{ }", "$f a b").Throws( 22 errs.ArityMismatch{ 23 What: "arguments here", 24 ValidLow: 1, ValidHigh: 1, Actual: 2}, 25 "$f a b"), 26 That("f = [x y]{ }", "$f a").Throws( 27 errs.ArityMismatch{ 28 What: "arguments here", 29 ValidLow: 2, ValidHigh: 2, Actual: 1}, 30 "$f a"), 31 That("f = [x y @rest]{ }", "$f a").Throws( 32 errs.ArityMismatch{ 33 What: "arguments here", 34 ValidLow: 2, ValidHigh: -1, Actual: 1}, 35 "$f a"), 36 37 // Unsupported option. 38 That("f = [&valid1=1 &valid2=2]{ }; $f &bad1=1 &bad2=2").Throws( 39 eval.UnsupportedOptionsError{[]string{"bad1", "bad2"}}, 40 "$f &bad1=1 &bad2=2"), 41 42 That("all [a b]{ }[arg-names]").Puts("a", "b"), 43 That("put [@r]{ }[rest-arg]").Puts("0"), 44 That("all [&opt=def]{ }[opt-names]").Puts("opt"), 45 That("all [&opt=def]{ }[opt-defaults]").Puts("def"), 46 That("put { body }[body]").Puts(" body "), 47 That("put [x @y]{ body }[def]").Puts("[x @y]{ body }"), 48 That("put { body }[src][code]"). 49 Puts("put { body }[src][code]"), 50 51 // Regression test for https://b.elv.sh/1126 52 That("fn f { body }; put $f~[body]").Puts(" body "), 53 ) 54 } 55 56 func TestUnsupportedOptionsError(t *testing.T) { 57 tt.Test(t, tt.Fn("Error", error.Error), tt.Table{ 58 tt.Args(eval.UnsupportedOptionsError{[]string{"sole-opt"}}).Rets( 59 "unsupported option: sole-opt"), 60 tt.Args(eval.UnsupportedOptionsError{[]string{"opt-foo", "opt-bar"}}).Rets( 61 "unsupported options: opt-foo, opt-bar"), 62 }) 63 }