github.com/elves/elvish@v0.15.0/pkg/eval/vals/iterate_test.go (about)

     1  package vals
     2  
     3  import (
     4  	"testing"
     5  
     6  	. "github.com/elves/elvish/pkg/tt"
     7  )
     8  
     9  // An implementation of Iterator.
    10  type iterator struct{ elements []interface{} }
    11  
    12  func (i iterator) Iterate(f func(interface{}) bool) {
    13  	Feed(f, i.elements...)
    14  }
    15  
    16  // A non-implementation of Iterator.
    17  type nonIterator struct{}
    18  
    19  func TestCanIterate(t *testing.T) {
    20  	Test(t, Fn("CanIterate", CanIterate), Table{
    21  		Args("foo").Rets(true),
    22  		Args(MakeList("foo", "bar")).Rets(true),
    23  		Args(iterator{vs("a", "b")}).Rets(true),
    24  		Args(nonIterator{}).Rets(false),
    25  	})
    26  }
    27  
    28  func TestCollect(t *testing.T) {
    29  	Test(t, Fn("Collect", Collect), Table{
    30  		Args("foo").Rets(vs("f", "o", "o"), nil),
    31  		Args(MakeList("foo", "bar")).Rets(vs("foo", "bar"), nil),
    32  		Args(iterator{vs("a", "b")}).Rets(vs("a", "b"), nil),
    33  		Args(nonIterator{}).Rets(vs(), cannotIterate{"!!vals.nonIterator"}),
    34  	})
    35  }
    36  
    37  // Iterate is tested indirectly by the test against Collect.