elvish/pkg/eval/vals/iterate_test.go

38 lines
896 B
Go
Raw Normal View History

2019-04-20 01:48:45 +08:00
package vals
import (
"testing"
. "src.elv.sh/pkg/tt"
2019-04-20 01:48:45 +08:00
)
// An implementation of Iterator.
type iterator struct{ elements []interface{} }
func (i iterator) Iterate(f func(interface{}) bool) {
2020-09-03 12:22:06 +08:00
Feed(f, i.elements...)
2019-04-20 01:48:45 +08:00
}
// A non-implementation of Iterator.
type nonIterator struct{}
func TestCanIterate(t *testing.T) {
Test(t, Fn("CanIterate", CanIterate), Table{
2019-04-20 01:48:45 +08:00
Args("foo").Rets(true),
Args(MakeList("foo", "bar")).Rets(true),
Args(iterator{vs("a", "b")}).Rets(true),
Args(nonIterator{}).Rets(false),
})
}
func TestCollect(t *testing.T) {
Test(t, Fn("Collect", Collect), Table{
2019-04-20 01:48:45 +08:00
Args("foo").Rets(vs("f", "o", "o"), nil),
Args(MakeList("foo", "bar")).Rets(vs("foo", "bar"), nil),
Args(iterator{vs("a", "b")}).Rets(vs("a", "b"), nil),
Args(nonIterator{}).Rets(vs(), cannotIterate{"!!vals.nonIterator"}),
2019-04-20 01:48:45 +08:00
})
}
// Iterate is tested indirectly by the test against Collect.