stringendswith.py 980 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. from hamcrest.library.text.substringmatcher import SubstringMatcher
  2. from hamcrest.core.helpers.hasmethod import hasmethod
  3. __author__ = "Jon Reid"
  4. __copyright__ = "Copyright 2011 hamcrest.org"
  5. __license__ = "BSD, see License.txt"
  6. class StringEndsWith(SubstringMatcher):
  7. def __init__(self, substring):
  8. super(StringEndsWith, self).__init__(substring)
  9. def _matches(self, item):
  10. if not hasmethod(item, 'endswith'):
  11. return False
  12. return item.endswith(self.substring)
  13. def relationship(self):
  14. return 'ending with'
  15. def ends_with(string):
  16. """Matches if object is a string ending with a given string.
  17. :param string: The string to search for.
  18. This matcher first checks whether the evaluated object is a string. If so,
  19. it checks if ``string`` matches the ending characters of the evaluated
  20. object.
  21. Example::
  22. ends_with("bar")
  23. will match "foobar".
  24. """
  25. return StringEndsWith(string)