2020-09-03 13:48:39 +08:00
|
|
|
package eval_test
|
2014-01-28 15:02:48 +08:00
|
|
|
|
|
|
|
import (
|
2021-01-03 23:20:31 +08:00
|
|
|
"bytes"
|
2014-09-21 05:39:16 +08:00
|
|
|
"reflect"
|
2021-01-02 10:39:38 +08:00
|
|
|
"strings"
|
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-04 02:51:29 +08:00
|
|
|
"github.com/elves/elvish/pkg/eval"
|
2020-09-03 13:48:39 +08:00
|
|
|
. "github.com/elves/elvish/pkg/eval"
|
|
|
|
|
|
|
|
. "github.com/elves/elvish/pkg/eval/evaltest"
|
2021-01-05 05:04:17 +08:00
|
|
|
"github.com/elves/elvish/pkg/eval/vals"
|
2020-04-26 01:26:17 +08:00
|
|
|
"github.com/elves/elvish/pkg/parse"
|
2020-04-27 04:24:30 +08:00
|
|
|
"github.com/elves/elvish/pkg/prog"
|
2020-09-03 11:55:16 +08:00
|
|
|
"github.com/elves/elvish/pkg/testutil"
|
2014-01-28 15:02:48 +08:00
|
|
|
)
|
|
|
|
|
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))
|
|
|
|
TestWithSetup(t,
|
|
|
|
func(ev *Evaler) { ev.SetArgs([]string{"foo", "bar"}) },
|
|
|
|
That("put $args").Puts(vals.MakeList("foo", "bar")))
|
|
|
|
}
|
|
|
|
|
2020-04-26 20:14:51 +08:00
|
|
|
func TestEvalTimeDeprecate(t *testing.T) {
|
2020-04-27 04:24:30 +08:00
|
|
|
restore := prog.SetShowDeprecations(true)
|
|
|
|
defer restore()
|
2020-09-03 11:55:16 +08:00
|
|
|
_, cleanup := testutil.InTestDir()
|
2020-04-26 20:14:51 +08:00
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
TestWithSetup(t, func(ev *Evaler) {
|
2020-12-25 01:39:51 +08:00
|
|
|
ev.Global = NsBuilder{}.AddGoFn("", "dep", func(fm *Frame) {
|
2020-08-16 05:52:50 +08:00
|
|
|
fm.Deprecate("deprecated", nil)
|
2020-12-25 01:39:51 +08:00
|
|
|
}).Ns()
|
2020-04-26 20:14:51 +08:00
|
|
|
},
|
|
|
|
That("dep").PrintsStderrWith("deprecated"),
|
|
|
|
// Deprecation message is only shown once.
|
|
|
|
That("dep 2> tmp.txt; dep").DoesNothing(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCompileTimeDeprecation(t *testing.T) {
|
2020-04-27 04:24:30 +08:00
|
|
|
restore := prog.SetShowDeprecations(true)
|
|
|
|
defer restore()
|
|
|
|
|
2020-04-26 20:14:51 +08:00
|
|
|
ev := NewEvaler()
|
2021-01-03 23:20:31 +08:00
|
|
|
errOutput := new(bytes.Buffer)
|
2021-01-02 10:39:38 +08:00
|
|
|
|
2021-01-03 23:20:31 +08:00
|
|
|
parseErr, compileErr := ev.Check(parse.Source{Code: "ord a"}, errOutput)
|
|
|
|
if parseErr != nil {
|
|
|
|
t.Errorf("got parse err %v", parseErr)
|
|
|
|
}
|
|
|
|
if compileErr != nil {
|
|
|
|
t.Errorf("got compile err %v", compileErr)
|
2020-04-26 20:14:51 +08:00
|
|
|
}
|
2021-01-02 10:39:38 +08:00
|
|
|
|
2021-01-03 23:20:31 +08:00
|
|
|
warning := errOutput.String()
|
2021-01-02 10:39:38 +08:00
|
|
|
wantWarning := `the "ord" command is deprecated`
|
|
|
|
if !strings.Contains(warning, wantWarning) {
|
|
|
|
t.Errorf("got warning %q, want warning containing %q", warning, wantWarning)
|
2020-04-26 20:14:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-15 06:57:36 +08:00
|
|
|
func TestMultipleEval(t *testing.T) {
|
|
|
|
texts := []string{"x=hello", "put $x"}
|
2020-09-03 13:48:39 +08:00
|
|
|
r := EvalAndCollect(t, NewEvaler(), texts)
|
2018-03-03 14:08:09 +08:00
|
|
|
wantOuts := []interface{}{"hello"}
|
2020-09-03 13:48:39 +08:00
|
|
|
if r.Exception != nil {
|
|
|
|
t.Errorf("eval %s => %v, want nil", texts, r.Exception)
|
2017-08-15 06:57:36 +08:00
|
|
|
}
|
2020-09-03 13:48:39 +08:00
|
|
|
if !reflect.DeepEqual(r.ValueOut, wantOuts) {
|
|
|
|
t.Errorf("eval %s outputs %v, want %v", texts, r.ValueOut, wantOuts)
|
2017-08-15 06:57:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-13 05:52:12 +08:00
|
|
|
func TestConcurrentEval(t *testing.T) {
|
|
|
|
// Run this test with "go test -race".
|
|
|
|
ev := NewEvaler()
|
2021-01-02 08:10:26 +08:00
|
|
|
src := parse.Source{Name: "[test]", Code: ""}
|
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() {
|
2021-01-02 08:10:26 +08:00
|
|
|
ev.Eval(src, EvalCfg{})
|
2020-01-13 05:52:12 +08:00
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
go func() {
|
2021-01-02 08:10:26 +08:00
|
|
|
ev.Eval(src, EvalCfg{})
|
2020-01-13 05:52:12 +08:00
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
wg.Wait()
|
|
|
|
}
|
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,
|
|
|
|
eval.CallCfg{
|
|
|
|
Args: []interface{}{passedArg},
|
|
|
|
Opts: map[string]interface{}{"opt": passedOpt},
|
|
|
|
From: "[TestCall]"},
|
|
|
|
eval.EvalCfg{})
|
|
|
|
|
|
|
|
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) {
|
|
|
|
parseErr, compileErr := ev.Check(parse.Source{Code: test.code}, nil)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|