2016-02-07 07:55:42 +08:00
|
|
|
package eval
|
|
|
|
|
|
|
|
import (
|
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
|
|
|
|
2021-01-27 09:28:38 +08:00
|
|
|
"src.elv.sh/pkg/eval/vals"
|
|
|
|
"src.elv.sh/pkg/glob"
|
2016-02-07 07:55:42 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var reprTests = []struct {
|
2022-03-20 23:50:25 +08:00
|
|
|
v any
|
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-27 23:58:27 +08:00
|
|
|
{true, "$true"},
|
|
|
|
{false, "$false"},
|
2018-02-15 17:14:05 +08:00
|
|
|
{vals.EmptyList, "[]"},
|
|
|
|
{vals.MakeList("bash", false), "[bash $false]"},
|
2019-04-20 01:26:27 +08:00
|
|
|
{vals.EmptyMap, "[&]"},
|
2021-01-08 08:56:45 +08:00
|
|
|
{vals.MakeMap(&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 {
|
2022-01-01 05:05:07 +08:00
|
|
|
repr := vals.ReprPlain(test.v)
|
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{}},
|
2020-08-28 13:10:51 +08:00
|
|
|
{"a", []glob.Segment{glob.Literal{Data: "a"}}},
|
|
|
|
{"/a", []glob.Segment{glob.Slash{}, glob.Literal{Data: "a"}}},
|
|
|
|
{"a/", []glob.Segment{glob.Literal{Data: "a"}, glob.Slash{}}},
|
|
|
|
{"/a/", []glob.Segment{glob.Slash{}, glob.Literal{Data: "a"}, glob.Slash{}}},
|
|
|
|
{"a//b", []glob.Segment{glob.Literal{Data: "a"}, glob.Slash{}, glob.Literal{Data: "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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|