hasstring.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. from hamcrest.core.base_matcher import BaseMatcher
  2. from hamcrest.core.helpers.wrap_matcher import wrap_matcher
  3. __author__ = "Jon Reid"
  4. __copyright__ = "Copyright 2011 hamcrest.org"
  5. __license__ = "BSD, see License.txt"
  6. class HasString(BaseMatcher):
  7. def __init__(self, str_matcher):
  8. self.str_matcher = str_matcher
  9. def _matches(self, item):
  10. return self.str_matcher.matches(str(item))
  11. def describe_to(self, description):
  12. description.append_text('an object with str ') \
  13. .append_description_of(self.str_matcher)
  14. def has_string(match):
  15. """Matches if ``str(item)`` satisfies a given matcher.
  16. :param match: The matcher to satisfy, or an expected value for
  17. :py:func:`~hamcrest.core.core.isequal.equal_to` matching.
  18. This matcher invokes the :py:func:`str` function on the evaluated object to
  19. get its length, passing the result to a given matcher for evaluation. If
  20. the ``match`` argument is not a matcher, it is implicitly wrapped in an
  21. :py:func:`~hamcrest.core.core.isequal.equal_to` matcher to check for
  22. equality.
  23. Examples::
  24. has_string(starts_with('foo'))
  25. has_string('bar')
  26. """
  27. return HasString(wrap_matcher(match))