2019-04-20 01:48:45 +08:00
|
|
|
package vals
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2022-03-25 10:41:49 +08:00
|
|
|
"src.elv.sh/pkg/tt"
|
2019-04-20 01:48:45 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// An implementation of Iterator.
|
2022-03-20 23:50:25 +08:00
|
|
|
type iterator struct{ elements []any }
|
2019-04-20 01:48:45 +08:00
|
|
|
|
2022-03-20 23:50:25 +08:00
|
|
|
func (i iterator) Iterate(f func(any) 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) {
|
2024-01-11 23:42:43 +08:00
|
|
|
tt.Test(t, CanIterate,
|
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),
|
2024-01-11 23:42:43 +08:00
|
|
|
)
|
2019-04-20 01:48:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCollect(t *testing.T) {
|
2024-01-11 23:42:43 +08:00
|
|
|
tt.Test(t, Collect,
|
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),
|
2020-01-07 07:02:26 +08:00
|
|
|
Args(nonIterator{}).Rets(vs(), cannotIterate{"!!vals.nonIterator"}),
|
2024-01-11 23:42:43 +08:00
|
|
|
)
|
2019-04-20 01:48:45 +08:00
|
|
|
}
|
|
|
|
|
2020-01-07 07:14:51 +08:00
|
|
|
// Iterate is tested indirectly by the test against Collect.
|