2020-09-03 13:48:39 +08:00
|
|
|
package eval_test
|
2020-03-31 12:28:51 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2020-09-03 13:48:39 +08:00
|
|
|
. "github.com/elves/elvish/pkg/eval"
|
2020-09-03 13:55:14 +08:00
|
|
|
"github.com/elves/elvish/pkg/testutil"
|
2020-09-03 13:48:39 +08:00
|
|
|
|
2020-03-31 12:28:51 +08:00
|
|
|
"github.com/elves/elvish/pkg/eval/vals"
|
|
|
|
"github.com/elves/elvish/pkg/eval/vars"
|
|
|
|
"github.com/elves/elvish/pkg/parse"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestPurelyEvalCompound(t *testing.T) {
|
2020-09-03 13:55:14 +08:00
|
|
|
home, cleanup := testutil.InTempHome()
|
2020-03-31 12:28:51 +08:00
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
var tests = []struct {
|
|
|
|
code string
|
|
|
|
upto int
|
|
|
|
wantValue string
|
|
|
|
wantErr error
|
|
|
|
}{
|
|
|
|
{code: "foobar", wantValue: "foobar"},
|
|
|
|
{code: "'foobar'", wantValue: "foobar"},
|
|
|
|
{code: "foo'bar'", wantValue: "foobar"},
|
|
|
|
{code: "$x", wantValue: "bar"},
|
|
|
|
{code: "foo$x", wantValue: "foobar"},
|
|
|
|
{code: "foo$x", upto: 3, wantValue: "foo"},
|
|
|
|
{code: "~", wantValue: home},
|
|
|
|
{code: "~/foo", wantValue: home + "/foo"},
|
|
|
|
|
|
|
|
{code: "[abc]", wantErr: ErrImpure},
|
|
|
|
{code: "$y", wantErr: ErrImpure},
|
|
|
|
{code: "a[0]", wantErr: ErrImpure},
|
|
|
|
{code: "$@x", wantErr: ErrImpure},
|
|
|
|
}
|
|
|
|
|
|
|
|
ev := NewEvaler()
|
2021-01-05 08:48:25 +08:00
|
|
|
ev.AddGlobal(NsBuilder{
|
2020-12-25 01:39:51 +08:00
|
|
|
"x": vars.NewReadOnly("bar"),
|
|
|
|
"y": vars.NewReadOnly(vals.MakeList()),
|
2021-01-05 07:54:13 +08:00
|
|
|
}.Ns())
|
2020-03-31 12:28:51 +08:00
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.code, func(t *testing.T) {
|
|
|
|
n := &parse.Compound{}
|
2020-07-02 04:39:06 +08:00
|
|
|
err := parse.ParseAs(
|
|
|
|
parse.Source{Name: "[test]", Code: test.code}, n, nil)
|
2020-03-31 12:28:51 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
upto := test.upto
|
|
|
|
if upto == 0 {
|
|
|
|
upto = -1
|
|
|
|
}
|
|
|
|
value, err := ev.PurelyEvalPartialCompound(n, upto)
|
|
|
|
|
|
|
|
if value != test.wantValue {
|
|
|
|
t.Errorf("got value %q, want %q", value, test.wantValue)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(err, test.wantErr) {
|
|
|
|
t.Errorf("got error %v, want %q", err, test.wantErr)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|