2020-09-03 13:48:39 +08:00
|
|
|
package eval_test
|
2014-01-28 15:02:48 +08:00
|
|
|
|
|
|
|
import (
|
2021-01-05 12:07:35 +08:00
|
|
|
"strconv"
|
2020-01-13 05:52:12 +08:00
|
|
|
"sync"
|
2021-01-05 12:07:35 +08:00
|
|
|
"syscall"
|
2014-01-29 21:17:04 +08:00
|
|
|
"testing"
|
2018-01-01 05:01:21 +08:00
|
|
|
|
2021-01-27 09:28:38 +08:00
|
|
|
. "src.elv.sh/pkg/eval"
|
2022-04-12 04:45:57 +08:00
|
|
|
"src.elv.sh/pkg/prog"
|
2020-09-03 13:48:39 +08:00
|
|
|
|
2021-01-27 09:28:38 +08:00
|
|
|
. "src.elv.sh/pkg/eval/evaltest"
|
|
|
|
"src.elv.sh/pkg/eval/vals"
|
|
|
|
"src.elv.sh/pkg/eval/vars"
|
|
|
|
"src.elv.sh/pkg/parse"
|
|
|
|
"src.elv.sh/pkg/testutil"
|
2014-01-28 15:02:48 +08:00
|
|
|
)
|
|
|
|
|
2021-01-05 12:07:35 +08:00
|
|
|
func TestPid(t *testing.T) {
|
|
|
|
pid := strconv.Itoa(syscall.Getpid())
|
|
|
|
Test(t, That("put $pid").Puts(pid))
|
|
|
|
}
|
|
|
|
|
2018-10-10 00:39:24 +08:00
|
|
|
func TestNumBgJobs(t *testing.T) {
|
|
|
|
Test(t,
|
|
|
|
That("put $num-bg-jobs").Puts("0"),
|
|
|
|
// TODO(xiaq): Test cases where $num-bg-jobs > 0. This cannot be done
|
|
|
|
// with { put $num-bg-jobs }& because the output channel may have
|
|
|
|
// already been closed when the closure is run.
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-01-05 05:04:17 +08:00
|
|
|
func TestArgs(t *testing.T) {
|
|
|
|
Test(t,
|
|
|
|
That("put $args").Puts(vals.EmptyList))
|
2023-07-23 06:04:18 +08:00
|
|
|
TestWithEvalerSetup(t,
|
2022-06-19 02:01:31 +08:00
|
|
|
func(ev *Evaler) { ev.Args = vals.MakeList("foo", "bar") },
|
2021-01-05 05:04:17 +08:00
|
|
|
That("put $args").Puts(vals.MakeList("foo", "bar")))
|
|
|
|
}
|
|
|
|
|
2020-04-26 20:14:51 +08:00
|
|
|
func TestEvalTimeDeprecate(t *testing.T) {
|
2022-04-12 04:45:57 +08:00
|
|
|
testutil.Set(t, &prog.DeprecationLevel, 42)
|
2021-08-07 05:18:09 +08:00
|
|
|
testutil.InTempDir(t)
|
2020-04-26 20:14:51 +08:00
|
|
|
|
2023-07-23 06:04:18 +08:00
|
|
|
TestWithEvalerSetup(t, func(ev *Evaler) {
|
2021-10-24 04:44:11 +08:00
|
|
|
ev.ExtendGlobal(BuildNs().AddGoFn("dep", func(fm *Frame) {
|
2021-01-20 05:37:36 +08:00
|
|
|
fm.Deprecate("deprecated", nil, 42)
|
2021-10-24 04:44:11 +08:00
|
|
|
}))
|
2020-04-26 20:14:51 +08:00
|
|
|
},
|
|
|
|
That("dep").PrintsStderrWith("deprecated"),
|
2022-03-20 17:31:38 +08:00
|
|
|
// Deprecation message from the same location is only shown once.
|
|
|
|
That("fn f { dep }", "f 2> tmp.txt; f").DoesNothing(),
|
2020-04-26 20:14:51 +08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2017-08-15 06:57:36 +08:00
|
|
|
func TestMultipleEval(t *testing.T) {
|
2021-01-10 08:08:58 +08:00
|
|
|
Test(t,
|
2022-01-03 08:10:40 +08:00
|
|
|
That("var x = hello").Then("put $x").Puts("hello"),
|
2021-01-10 08:54:02 +08:00
|
|
|
|
|
|
|
// Shadowing with fn. Regression test for #1213.
|
|
|
|
That("fn f { put old }").Then("fn f { put new }").Then("f").
|
|
|
|
Puts("new"),
|
|
|
|
// Variable deletion. Regression test for #1213.
|
2022-10-02 12:01:39 +08:00
|
|
|
That("var x = foo").Then("del x").Then("put $x").DoesNotCompile("variable $x not found"),
|
2021-01-10 08:08:58 +08:00
|
|
|
)
|
2017-08-15 06:57:36 +08:00
|
|
|
}
|
|
|
|
|
2021-01-17 08:36:18 +08:00
|
|
|
func TestEval_AlternativeGlobal(t *testing.T) {
|
|
|
|
ev := NewEvaler()
|
2021-10-24 04:44:11 +08:00
|
|
|
g := BuildNs().AddVar("a", vars.NewReadOnly("")).Ns()
|
2022-05-23 10:20:18 +08:00
|
|
|
err := ev.Eval(parse.Source{Name: "[test]", Code: "nop $a"}, EvalCfg{Global: g})
|
2021-01-17 08:36:18 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("got error %v, want nil", err)
|
|
|
|
}
|
|
|
|
// Regression test for #1223
|
2021-10-24 01:18:31 +08:00
|
|
|
if ev.Global().HasKeyString("a") {
|
2021-01-17 08:36:18 +08:00
|
|
|
t.Errorf("$a from alternative global leaked into Evaler global")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEval_Concurrent(t *testing.T) {
|
2020-01-13 05:52:12 +08:00
|
|
|
ev := NewEvaler()
|
2020-04-15 07:04:23 +08:00
|
|
|
|
2020-01-13 05:52:12 +08:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(2)
|
|
|
|
go func() {
|
2022-05-23 10:20:18 +08:00
|
|
|
ev.Eval(parse.Source{Name: "[test]", Code: "var a"}, EvalCfg{})
|
2020-01-13 05:52:12 +08:00
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
go func() {
|
2022-05-23 10:20:18 +08:00
|
|
|
ev.Eval(parse.Source{Name: "[test]", Code: "var b"}, EvalCfg{})
|
2020-01-13 05:52:12 +08:00
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
wg.Wait()
|
2021-01-17 08:36:18 +08:00
|
|
|
g := ev.Global()
|
2021-10-24 01:18:31 +08:00
|
|
|
if !g.HasKeyString("a") {
|
2021-01-17 08:36:18 +08:00
|
|
|
t.Errorf("variable $a not created")
|
|
|
|
}
|
2021-10-24 01:18:31 +08:00
|
|
|
if !g.HasKeyString("b") {
|
2021-01-17 08:36:18 +08:00
|
|
|
t.Errorf("variable $b not created")
|
|
|
|
}
|
2020-01-13 05:52:12 +08:00
|
|
|
}
|
2021-01-03 23:20:31 +08:00
|
|
|
|
2021-01-04 02:51:29 +08:00
|
|
|
type fooOpts struct{ Opt string }
|
|
|
|
|
|
|
|
func (*fooOpts) SetDefaultOptions() {}
|
|
|
|
|
|
|
|
func TestCall(t *testing.T) {
|
|
|
|
ev := NewEvaler()
|
|
|
|
var gotOpt, gotArg string
|
|
|
|
fn := NewGoFn("foo", func(fm *Frame, opts fooOpts, arg string) {
|
|
|
|
gotOpt = opts.Opt
|
|
|
|
gotArg = arg
|
|
|
|
})
|
|
|
|
|
|
|
|
passedArg := "arg value"
|
|
|
|
passedOpt := "opt value"
|
|
|
|
ev.Call(fn,
|
2021-01-06 00:05:34 +08:00
|
|
|
CallCfg{
|
2022-03-20 23:50:25 +08:00
|
|
|
Args: []any{passedArg},
|
|
|
|
Opts: map[string]any{"opt": passedOpt},
|
2021-01-04 02:51:29 +08:00
|
|
|
From: "[TestCall]"},
|
2021-01-06 00:05:34 +08:00
|
|
|
EvalCfg{})
|
2021-01-04 02:51:29 +08:00
|
|
|
|
|
|
|
if gotArg != passedArg {
|
|
|
|
t.Errorf("got arg %q, want %q", gotArg, passedArg)
|
|
|
|
}
|
|
|
|
if gotOpt != passedOpt {
|
|
|
|
t.Errorf("got opt %q, want %q", gotOpt, passedOpt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-03 23:20:31 +08:00
|
|
|
var checkTests = []struct {
|
|
|
|
name string
|
|
|
|
code string
|
|
|
|
wantParseErr bool
|
|
|
|
wantCompileErr bool
|
|
|
|
}{
|
|
|
|
{name: "no error", code: "put $nil"},
|
|
|
|
{name: "parse error only", code: "put [",
|
|
|
|
wantParseErr: true},
|
|
|
|
{name: "compile error only", code: "put $x",
|
|
|
|
wantCompileErr: true},
|
|
|
|
{name: "both parse and compile error", code: "put [$x",
|
|
|
|
wantParseErr: true, wantCompileErr: true},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCheck(t *testing.T) {
|
|
|
|
ev := NewEvaler()
|
|
|
|
for _, test := range checkTests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
2022-12-11 22:21:09 +08:00
|
|
|
parseErr, _, compileErr := ev.Check(parse.Source{Name: "[test]", Code: test.code}, nil)
|
2021-01-03 23:20:31 +08:00
|
|
|
if (parseErr != nil) != test.wantParseErr {
|
|
|
|
t.Errorf("got parse error %v, when wantParseErr = %v",
|
|
|
|
parseErr, test.wantParseErr)
|
|
|
|
}
|
|
|
|
if (compileErr != nil) != test.wantCompileErr {
|
|
|
|
t.Errorf("got compile error %v, when wantCompileErr = %v",
|
|
|
|
compileErr, test.wantCompileErr)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|