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