src.hamcrest.library.collection.IsEmpty   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 20
Duplicated Lines 0 %
Metric Value
dl 0
loc 20
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
B matches() 0 15 5
A describe_to() 0 2 1
1
from hamcrest.core.base_matcher import BaseMatcher
2
3
__author__ = "Chris Rose"
4
__copyright__ = "Copyright 2012 hamcrest.org"
5
__license__ = "BSD, see License.txt"
6
7
8
class IsEmpty(BaseMatcher):
9
10
    def matches(self, item, mismatch_description=None):
11
        try:
12
            if len(item) == 0:
13
                return True
14
15
            if mismatch_description:
16
                mismatch_description \
17
                    .append_text('has %d item(s)' % len(item))
18
19
        except TypeError:
20
            if mismatch_description:
21
                mismatch_description \
22
                    .append_text('does not support length')
23
24
            return False
25
26
    def describe_to(self, description):
27
        description.append_text('an empty collection')
28
29
30
def empty():
31
    """
32
    This matcher matches any collection-like object that responds to the
33
    __len__ method, and has a length of 0.
34
    """
35
    return IsEmpty()
36