testing - ActiveState ActiveGo 1.8

Package testing

import "google.golang.org/api/iterator/testing"

Overview ▾

Package testing provides support functions for testing iterators conforming to the standard pattern. See package google.golang.org/api/iterator and https://github.com/GoogleCloudPlatform/gcloud-golang/wiki/Iterator-Guidelines.

func TestIterator

func TestIterator(want interface{}, create func() interface{}, next func(interface{}) (interface{}, error)) (string, bool)

TestIterator tests the Next method of a standard iterator. It assumes that the underlying sequence to be iterated over already exists.

The want argument should be a slice that contains the elements of this sequence. It may be an empty slice, but it must not be the nil interface value. The elements must be comparable with reflect.DeepEqual.

The create function should create and return a new iterator. It will typically look like

func() interface{} { return client.Items(ctx) }

The next function takes the return value of create and should return the result of calling Next on the iterator. It can usually be defined as

func(it interface{}) (interface{}, error) { return it.(*ItemIterator).Next() }

TestIterator checks that the iterator returns all the elements of want in order, followed by (zero, done). It also confirms that subsequent calls to next also return (zero, done).

If the iterator implements the method

PageInfo() *iterator.PageInfo

then exact pagination with iterator.Pager is also tested. Pagination testing will be more informative if the want slice contains at least three elements.

On success, TestIterator returns ("", true). On failure, it returns a suitable error message and false.