Created
July 18, 2013 15:16
-
-
Save jgosmann/6030156 to your computer and use it in GitHub Desktop.
PyHamcrest matcher for matching empty sequences.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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