stringcontains.py 953 B

1234567891011121314151617181920212223242526272829303132333435363738
  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 StringContains(SubstringMatcher):
  7. def __init__(self, substring):
  8. super(StringContains, self).__init__(substring)
  9. def _matches(self, item):
  10. if not hasmethod(item, 'find'):
  11. return False
  12. return item.find(self.substring) >= 0
  13. def relationship(self):
  14. return 'containing'
  15. def contains_string(substring):
  16. """Matches if object is a string containing 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 whether it contains ``string``.
  20. Example::
  21. contains_string("def")
  22. will match "abcdefg".
  23. """
  24. return StringContains(substring)