stringstartswith.py 1007 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. __author__ = "Jon Reid"
  2. __copyright__ = "Copyright 2011 hamcrest.org"
  3. __license__ = "BSD, see License.txt"
  4. from hamcrest.library.text.substringmatcher import SubstringMatcher
  5. from hamcrest.core.helpers.hasmethod import hasmethod
  6. class StringStartsWith(SubstringMatcher):
  7. def __init__(self, substring):
  8. super(StringStartsWith, self).__init__(substring)
  9. def _matches(self, item):
  10. if not hasmethod(item, 'startswith'):
  11. return False
  12. return item.startswith(self.substring)
  13. def relationship(self):
  14. return 'starting with'
  15. def starts_with(substring):
  16. """Matches if object is a string starting 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 beginning characters of the evaluated
  20. object.
  21. Example::
  22. starts_with("foo")
  23. will match "foobar".
  24. """
  25. return StringStartsWith(substring)