pkg/eval/vals: Move the "any" matcher to pkg/tt.

This commit is contained in:
Qi Xiao 2020-01-06 23:19:00 +00:00
parent 843e7f3d99
commit b8513f75b3
5 changed files with 26 additions and 26 deletions

View File

@ -33,14 +33,14 @@ func TestScanToGo(t *testing.T) {
Args(nil, nil).Rets(nil),
Args(0.5, 0).Rets(0, errMustBeInteger),
Args("x", someType{}).Rets(any, wrongType{"!!vals.someType", "string"}),
Args(someType{}, 0).Rets(any, errMustBeInteger),
Args("x", 0).Rets(any, cannotParseAs{"integer", "x"}),
Args(someType{}, 0.0).Rets(any, errMustBeNumber),
Args("x", 0.0).Rets(any, cannotParseAs{"number", "x"}),
Args(someType{}, ' ').Rets(any, errMustBeString),
Args("\xc3\x28", ' ').Rets(any, errMustBeValidUTF8), // Invalid UTF8
Args("ab", ' ').Rets(any, errMustHaveSingleRune),
Args("x", someType{}).Rets(Any, wrongType{"!!vals.someType", "string"}),
Args(someType{}, 0).Rets(Any, errMustBeInteger),
Args("x", 0).Rets(Any, cannotParseAs{"integer", "x"}),
Args(someType{}, 0.0).Rets(Any, errMustBeNumber),
Args("x", 0.0).Rets(Any, cannotParseAs{"number", "x"}),
Args(someType{}, ' ').Rets(Any, errMustBeString),
Args("\xc3\x28", ' ').Rets(Any, errMustBeValidUTF8), // Invalid UTF8
Args("ab", ' ').Rets(Any, errMustHaveSingleRune),
})
}

View File

@ -19,7 +19,7 @@ func TestIndex(t *testing.T) {
Args("abc", 0.0).Rets("a", nil),
Args("你好", "0").Rets("你", nil),
Args("你好", "3").Rets("好", nil),
Args("你好", "2").Rets(any, errIndexNotAtRuneBoundary),
Args("你好", "2").Rets(Any, errIndexNotAtRuneBoundary),
Args("abc", "1:2").Rets("b", nil),
Args("abc", "1:").Rets("bc", nil),
Args("abc", ":").Rets("abc", nil),
@ -32,15 +32,15 @@ func TestIndex(t *testing.T) {
// Simple indicies: 0 <= i < n.
Args(li4, "0").Rets("foo", nil),
Args(li4, "3").Rets("ipsum", nil),
Args(li0, "0").Rets(any, errIndexOutOfRange),
Args(li4, "4").Rets(any, errIndexOutOfRange),
Args(li4, "5").Rets(any, errIndexOutOfRange),
Args(li0, "0").Rets(Any, errIndexOutOfRange),
Args(li4, "4").Rets(Any, errIndexOutOfRange),
Args(li4, "5").Rets(Any, errIndexOutOfRange),
// Negative indices: -n <= i < 0.
Args(li4, "-1").Rets("ipsum", nil),
Args(li4, "-4").Rets("foo", nil),
Args(li4, "-5").Rets(any, errIndexOutOfRange), // Out of range.
Args(li4, "-5").Rets(Any, errIndexOutOfRange), // Out of range.
// Decimal indicies are not allowed even if the value is an integer.
Args(li4, "0.0").Rets(any, errIndexMustBeInteger),
Args(li4, "0.0").Rets(Any, errIndexMustBeInteger),
// Float64 indicies are allowed as long as they are integers.
Args(li4, 0.0).Rets("foo", nil),
@ -48,7 +48,7 @@ func TestIndex(t *testing.T) {
Args(li4, 5.0).Rets(nil, errIndexOutOfRange),
Args(li4, -1.0).Rets("ipsum", nil),
Args(li4, -5.0).Rets(nil, errIndexOutOfRange),
Args(li4, 0.5).Rets(any, errIndexMustBeInteger),
Args(li4, 0.5).Rets(Any, errIndexMustBeInteger),
// Slice indicies: 0 <= i <= j <= n.
Args(li4, "1:3").Rets(eq(MakeList("bar", "lorem")), nil),
@ -71,15 +71,15 @@ func TestIndex(t *testing.T) {
Args(li4, "3:2").Rets(nil, errIndexOutOfRange),
// Malformed list indices.
Args(li4, "a").Rets(any, errIndexMustBeInteger),
Args(li4, "a").Rets(Any, errIndexMustBeInteger),
// TODO(xiaq): Make the error more accurate.
Args(li4, "1:3:2").Rets(any, errIndexMustBeInteger),
Args(li4, "1:3:2").Rets(Any, errIndexMustBeInteger),
// Map indicies
// ============
Args(m, "foo").Rets("bar", nil),
Args(m, "bad").Rets(any, NoSuchKey("bad")),
Args(m, "bad").Rets(Any, NoSuchKey("bad")),
// StructMap indicies
Args(testStructMap{"foo", 1.0}, "name").Rets("foo", nil),

View File

@ -33,6 +33,6 @@ func TestIterateKeys(t *testing.T) {
Args(testStructMap{}).Rets(vs("name", "score-number")),
Args(keysIterator{vs("lorem", "ipsum")}).Rets(vs("lorem", "ipsum")),
Args(nonKeysIterator{}).Rets(
any, cannotIterateKeysOf{"!!vals.nonKeysIterator"}),
Any, cannotIterateKeysOf{"!!vals.nonKeysIterator"}),
})
}

View File

@ -2,13 +2,6 @@ package vals
import "github.com/elves/elvish/pkg/tt"
// anyType matches anything.
type anyType struct{}
var any = anyType{}
func (anyType) Match(tt.RetValue) bool { return true }
// equalMatcher matches the return value using Equal.
type equalMatcher struct {
r interface{}

View File

@ -103,6 +103,13 @@ type Matcher interface {
Match(RetValue) bool
}
// Any is a Matcher that matches any value.
var Any Matcher = anyMatcher{}
type anyMatcher struct{}
func (anyMatcher) Match(RetValue) bool { return true }
func match(matchers, actual []interface{}) bool {
for i, matcher := range matchers {
if !matchOne(matcher, actual[i]) {