1
|
|
|
from hamcrest.core.base_matcher import BaseMatcher |
2
|
|
|
import operator |
3
|
|
|
|
4
|
|
|
__author__ = "Jon Reid" |
5
|
|
|
__copyright__ = "Copyright 2011 hamcrest.org" |
6
|
|
|
__license__ = "BSD, see License.txt" |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
class OrderingComparison(BaseMatcher): |
10
|
|
|
|
11
|
|
|
def __init__(self, value, comparison_function, comparison_description): |
12
|
|
|
self.value = value |
13
|
|
|
self.comparison_function = comparison_function |
14
|
|
|
self.comparison_description = comparison_description |
15
|
|
|
|
16
|
|
|
def _matches(self, item): |
17
|
|
|
return self.comparison_function(item, self.value) |
18
|
|
|
|
19
|
|
|
def describe_to(self, description): |
20
|
|
|
description.append_text('a value ') \ |
21
|
|
|
.append_text(self.comparison_description) \ |
22
|
|
|
.append_text(' ') \ |
23
|
|
|
.append_description_of(self.value) |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
def greater_than(value): |
27
|
|
|
"""Matches if object is greater than a given value. |
28
|
|
|
|
29
|
|
|
:param value: The value to compare against. |
30
|
|
|
|
31
|
|
|
""" |
32
|
|
|
return OrderingComparison(value, operator.gt, 'greater than') |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
def greater_than_or_equal_to(value): |
36
|
|
|
"""Matches if object is greater than or equal to a given value. |
37
|
|
|
|
38
|
|
|
:param value: The value to compare against. |
39
|
|
|
|
40
|
|
|
""" |
41
|
|
|
return OrderingComparison(value, operator.ge, 'greater than or equal to') |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
def less_than(value): |
45
|
|
|
"""Matches if object is less than a given value. |
46
|
|
|
|
47
|
|
|
:param value: The value to compare against. |
48
|
|
|
|
49
|
|
|
""" |
50
|
|
|
return OrderingComparison(value, operator.lt, 'less than') |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
def less_than_or_equal_to(value): |
54
|
|
|
"""Matches if object is less than or equal to a given value. |
55
|
|
|
|
56
|
|
|
:param value: The value to compare against. |
57
|
|
|
|
58
|
|
|
""" |
59
|
|
|
return OrderingComparison(value, operator.le, 'less than or equal to') |
60
|
|
|
|