2016-02-07 07:55:42 +08:00
|
|
|
package eval
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2016-02-08 03:39:03 +08:00
|
|
|
"reflect"
|
2016-02-07 07:55:42 +08:00
|
|
|
"testing"
|
2016-02-08 03:39:03 +08:00
|
|
|
|
2018-01-01 04:31:45 +08:00
|
|
|
"github.com/elves/elvish/eval/types"
|
2016-02-08 03:39:03 +08:00
|
|
|
"github.com/elves/elvish/glob"
|
2016-02-07 07:55:42 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var reprTests = []struct {
|
2018-01-01 04:31:45 +08:00
|
|
|
v types.Value
|
2016-02-07 07:55:42 +08:00
|
|
|
want string
|
|
|
|
}{
|
2018-01-25 09:40:15 +08:00
|
|
|
{"233", "233"},
|
|
|
|
{"a\nb", `"a\nb"`},
|
|
|
|
{"foo bar", "'foo bar'"},
|
|
|
|
{"a\x00b", `"a\x00b"`},
|
2018-01-01 23:21:15 +08:00
|
|
|
{types.Bool(true), "$true"},
|
|
|
|
{types.Bool(false), "$false"},
|
2017-01-29 10:29:03 +08:00
|
|
|
{&Exception{nil, nil}, "$ok"},
|
2017-03-07 09:07:14 +08:00
|
|
|
{&Exception{errors.New("foo bar"), nil}, "?(fail 'foo bar')"},
|
2017-01-29 10:29:03 +08:00
|
|
|
{&Exception{
|
|
|
|
PipelineError{[]*Exception{{nil, nil}, {errors.New("lorem"), nil}}}, nil},
|
2017-03-07 09:07:14 +08:00
|
|
|
"?(multi-error $ok ?(fail lorem))"},
|
2017-01-29 10:29:03 +08:00
|
|
|
{&Exception{Return, nil}, "?(return)"},
|
2018-01-01 05:01:21 +08:00
|
|
|
{types.EmptyList, "[]"},
|
2018-01-25 09:40:15 +08:00
|
|
|
{types.MakeList("bash", types.Bool(false)), "[bash $false]"},
|
2018-01-01 05:08:37 +08:00
|
|
|
{types.MakeMap(map[types.Value]types.Value{}), "[&]"},
|
2018-01-25 09:40:15 +08:00
|
|
|
{types.MakeMap(map[types.Value]types.Value{&Exception{nil, nil}: "elvish"}), "[&$ok=elvish]"},
|
2016-02-07 07:55:42 +08:00
|
|
|
// TODO: test maps of more elements
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRepr(t *testing.T) {
|
|
|
|
for _, test := range reprTests {
|
2018-01-25 07:57:58 +08:00
|
|
|
repr := types.Repr(test.v, types.NoPretty)
|
2016-02-07 07:55:42 +08:00
|
|
|
if repr != test.want {
|
|
|
|
t.Errorf("Repr = %s, want %s", repr, test.want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-08 03:39:03 +08:00
|
|
|
|
|
|
|
var stringToSegmentsTests = []struct {
|
|
|
|
s string
|
|
|
|
want []glob.Segment
|
|
|
|
}{
|
|
|
|
{"", []glob.Segment{}},
|
2017-02-03 03:15:42 +08:00
|
|
|
{"a", []glob.Segment{glob.Literal{"a"}}},
|
|
|
|
{"/a", []glob.Segment{glob.Slash{}, glob.Literal{"a"}}},
|
|
|
|
{"a/", []glob.Segment{glob.Literal{"a"}, glob.Slash{}}},
|
|
|
|
{"/a/", []glob.Segment{glob.Slash{}, glob.Literal{"a"}, glob.Slash{}}},
|
|
|
|
{"a//b", []glob.Segment{glob.Literal{"a"}, glob.Slash{}, glob.Literal{"b"}}},
|
2016-02-08 03:39:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestStringToSegments(t *testing.T) {
|
|
|
|
for _, tc := range stringToSegmentsTests {
|
|
|
|
segs := stringToSegments(tc.s)
|
|
|
|
if !reflect.DeepEqual(segs, tc.want) {
|
|
|
|
t.Errorf("stringToSegments(%q) => %v, want %v", tc.s, segs, tc.want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|