ordering_comparison.py 1.7 KB

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