elvish/eval/value_test.go

64 lines
1.7 KiB
Go
Raw Normal View History

package eval
import (
"errors"
"reflect"
"testing"
"github.com/elves/elvish/eval/types"
"github.com/elves/elvish/glob"
)
var reprTests = []struct {
v types.Value
want string
}{
{"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"},
{&Exception{nil, nil}, "$ok"},
2017-03-07 09:07:14 +08:00
{&Exception{errors.New("foo bar"), nil}, "?(fail 'foo bar')"},
{&Exception{
PipelineError{[]*Exception{{nil, nil}, {errors.New("lorem"), nil}}}, nil},
2017-03-07 09:07:14 +08:00
"?(multi-error $ok ?(fail lorem))"},
{&Exception{Return, nil}, "?(return)"},
2018-01-01 05:01:21 +08:00
{types.EmptyList, "[]"},
{types.MakeList("bash", types.Bool(false)), "[bash $false]"},
2018-01-01 05:08:37 +08:00
{types.MakeMap(map[types.Value]types.Value{}), "[&]"},
{types.MakeMap(map[types.Value]types.Value{&Exception{nil, nil}: "elvish"}), "[&$ok=elvish]"},
// TODO: test maps of more elements
}
func TestRepr(t *testing.T) {
for _, test := range reprTests {
repr := types.Repr(test.v, types.NoPretty)
if repr != test.want {
t.Errorf("Repr = %s, want %s", repr, test.want)
}
}
}
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"}}},
}
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)
}
}
}