Skip to content

Instantly share code, notes, and snippets.

@jgosmann
Created July 18, 2013 15:16
Show Gist options
  • Save jgosmann/6030156 to your computer and use it in GitHub Desktop.
Save jgosmann/6030156 to your computer and use it in GitHub Desktop.
PyHamcrest matcher for matching empty sequences.
from hamcrest.core.base_matcher import BaseMatcher
class Empty(BaseMatcher):
def _matches(self, item):
if hasattr(item, '__len__'):
return len(item) == 0
elif hasattr(item, 'count'):
return item.count() == 0
raise TypeError('%s cannot be tested for emptiness.' % (
type(item).__name__))
def describe_to(self, description):
description.append_text('empty')
def empty():
return Empty()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment