jaimegildesagredo/expects — pull request #4 by jvrsantacruz (2013-07-10 00:27:37+00:00)
Title: Added support to iterables in Be.empty

As an increasingly number of methods now return either iterators or view objects, it would be rather nice to be able to test them.

A very simple though inefficient way of testing emptiness on a generator is just consume it into a list:

```python
expect(list(generator())).to.be.empty
```

This certainly is a limited method, as it may trigger some unwanted effects due to the iteration over unknown in size, very large or even infinite collections.

The present approach just wastes the first element on the worst case, just by trying to iterate the generator right away, in a EAFP fashion.

```python
try:
next(generator())
except StopIteration:
return True
```

Any python2-3 implementation should respond to calling next on a generator by either handing over the next element or raising a StopIteration exception, allowing us to know the generator empty on the latter.

This sadly disposes the first element, which cannot be held or returned for further testing.

A "peek" function might be implemented, which saved the first extracted element and returned a composed generator which chained together the first value and the rest, thus simulating the whole collection. IMO this might result overly complicated and rather unecessary at this point.

```python
try:
element = next(generator)
except StopIteration:
return True
else:
return itertools.chain([element], generator)
```