2020-09-03 13:48:39 +08:00
|
|
|
package eval_test
|
2014-01-28 15:02:48 +08:00
|
|
|
|
|
|
|
import (
|
2020-01-13 05:52:12 +08:00
|
|
|
"sync"
|
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"
|
2020-09-03 13:48:39 +08:00
|
|
|
|
2021-01-27 09:28:38 +08:00
|
|
|
"src.elv.sh/pkg/eval/vars"
|
|
|
|
"src.elv.sh/pkg/parse"
|
2014-01-28 15:02:48 +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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|