上QQ阅读APP看书,第一时间看更新
Testing iterators for element equality
First, let's create the testIterEq function to test whether two collections are equivalent:
func testIterEq(t *testing.T, it1, it2 Iter) {
t.Log("Start")
for el1 := range it1 {
if el2, ok := <- it2; !ok {
t.Error("it2 shorter than it1!", el1)
return
} else if !reflect.DeepEqual(el1, el2) {
t.Error("Elements are not equal", el1, el2)
} else {
t.Log(el1, el2)
}
}
if el2, ok := <- it2; ok {
t.Error("it1 shorter than it2!", el2)
}
t.Log("Stop")
}
In our test function TestMap, we define a mapper function literal that is passed to our Map function to perform the transformation. The mapper function returns the length of each string passed to it:
func TestMap(t *testing.T) {
mapper := func (i interface{}) interface{} {
return len(i.(string))
}
testIterEq(t, New(3,5,10), Map(mapper, New("CRV", "IS250", "Highlander")))
}
Let's go to the directory with this test file and run the following to verify that the Map function works as we expect. Here's what my console output looks like:
~/clients/packt/dev/go/src/bitbucket.org/lsheehan/fp-in-go-work/chapter2/itertools $ go test
PASS
ok bitbucket.org/lsheehan/fp-in-go-work/chapter2/itertools 0.008s