2020-09-03 13:48:39 +08:00
|
|
|
package eval_test
|
2014-01-28 15:02:48 +08:00
|
|
|
|
|
|
|
import (
|
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
|
|
|
|
2020-09-03 13:48:39 +08:00
|
|
|
. "github.com/elves/elvish/pkg/eval"
|
|
|
|
|
|
|
|
. "github.com/elves/elvish/pkg/eval/evaltest"
|
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.
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
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-02 10:39:38 +08:00
|
|
|
errPort, collect, err := CaptureStringPort()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ev.Eval(
|
|
|
|
parse.Source{Code: "ord a"},
|
|
|
|
EvalCfg{Ports: []*Port{nil, nil, errPort}, NoExecute: true})
|
|
|
|
warnings := collect()
|
2020-04-26 20:14:51 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("got err %v, want nil", err)
|
|
|
|
}
|
2021-01-02 10:39:38 +08:00
|
|
|
|
|
|
|
warning := warnings[0]
|
|
|
|
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-12-22 04:49:14 +08:00
|
|
|
func TestMiscEval(t *testing.T) {
|
2018-05-22 08:08:11 +08:00
|
|
|
Test(t,
|
2018-03-03 14:08:09 +08:00
|
|
|
// Pseudo-namespace E:
|
|
|
|
That("E:FOO=lorem; put $E:FOO").Puts("lorem"),
|
|
|
|
That("del E:FOO; put $E:FOO").Puts(""),
|
2018-05-22 08:08:11 +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()
|
|
|
|
}
|