diff options
author | Devtools Arcadia <arcadia-devtools@yandex-team.ru> | 2022-02-07 18:08:42 +0300 |
---|---|---|
committer | Devtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net> | 2022-02-07 18:08:42 +0300 |
commit | 1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch) | |
tree | e26c9fed0de5d9873cce7e00bc214573dc2195b7 /contrib/python/PyHamcrest/src/hamcrest/library/text | |
download | ydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz |
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'contrib/python/PyHamcrest/src/hamcrest/library/text')
9 files changed, 342 insertions, 0 deletions
diff --git a/contrib/python/PyHamcrest/src/hamcrest/library/text/__init__.py b/contrib/python/PyHamcrest/src/hamcrest/library/text/__init__.py new file mode 100644 index 0000000000..39d0e8b382 --- /dev/null +++ b/contrib/python/PyHamcrest/src/hamcrest/library/text/__init__.py @@ -0,0 +1,14 @@ +from __future__ import absolute_import +"""Matchers that perform text comparisons.""" + +from .isequal_ignoring_case import equal_to_ignoring_case +from .isequal_ignoring_whitespace import equal_to_ignoring_whitespace +from .stringcontains import contains_string +from .stringendswith import ends_with +from .stringstartswith import starts_with +from .stringmatches import matches_regexp +from .stringcontainsinorder import string_contains_in_order + +__author__ = "Jon Reid" +__copyright__ = "Copyright 2011 hamcrest.org" +__license__ = "BSD, see License.txt" diff --git a/contrib/python/PyHamcrest/src/hamcrest/library/text/isequal_ignoring_case.py b/contrib/python/PyHamcrest/src/hamcrest/library/text/isequal_ignoring_case.py new file mode 100644 index 0000000000..d1ee2d17fc --- /dev/null +++ b/contrib/python/PyHamcrest/src/hamcrest/library/text/isequal_ignoring_case.py @@ -0,0 +1,43 @@ +__author__ = "Jon Reid" +__copyright__ = "Copyright 2011 hamcrest.org" +__license__ = "BSD, see License.txt" + +from hamcrest.core.base_matcher import BaseMatcher + +import six + +class IsEqualIgnoringCase(BaseMatcher): + + def __init__(self, string): + if not isinstance(string, six.string_types): + raise TypeError('IsEqualIgnoringCase requires string') + self.original_string = string + self.lowered_string = string.lower() + + def _matches(self, item): + if not isinstance(item, six.string_types): + return False + return self.lowered_string == item.lower() + + def describe_to(self, description): + description.append_description_of(self.original_string) \ + .append_text(' ignoring case') + + +def equal_to_ignoring_case(string): + """Matches if object is a string equal to a given string, ignoring case + differences. + + :param string: The string to compare against as the expected value. + + This matcher first checks whether the evaluated object is a string. If so, + it compares it with ``string``, ignoring differences of case. + + Example:: + + equal_to_ignoring_case("hello world") + + will match "heLLo WorlD". + + """ + return IsEqualIgnoringCase(string) diff --git a/contrib/python/PyHamcrest/src/hamcrest/library/text/isequal_ignoring_whitespace.py b/contrib/python/PyHamcrest/src/hamcrest/library/text/isequal_ignoring_whitespace.py new file mode 100644 index 0000000000..86fa997601 --- /dev/null +++ b/contrib/python/PyHamcrest/src/hamcrest/library/text/isequal_ignoring_whitespace.py @@ -0,0 +1,57 @@ +__author__ = "Jon Reid" +__copyright__ = "Copyright 2011 hamcrest.org" +__license__ = "BSD, see License.txt" + +from hamcrest.core.base_matcher import BaseMatcher + +import six + +def stripspace(string): + result = '' + last_was_space = True + for character in string: + if character.isspace(): + if not last_was_space: + result += ' ' + last_was_space = True + else: + result += character + last_was_space = False + return result.strip() + + +class IsEqualIgnoringWhiteSpace(BaseMatcher): + + def __init__(self, string): + if not isinstance(string, six.string_types): + raise TypeError('IsEqualIgnoringWhiteSpace requires string') + self.original_string = string + self.stripped_string = stripspace(string) + + def _matches(self, item): + if not isinstance(item, six.string_types): + return False + return self.stripped_string == stripspace(item) + + def describe_to(self, description): + description.append_description_of(self.original_string) \ + .append_text(' ignoring whitespace') + + +def equal_to_ignoring_whitespace(string): + """Matches if object is a string equal to a given string, ignoring + differences in whitespace. + + :param string: The string to compare against as the expected value. + + This matcher first checks whether the evaluated object is a string. If so, + it compares it with ``string``, ignoring differences in runs of whitespace. + + Example:: + + equal_to_ignoring_whitespace("hello world") + + will match ``"hello world"``. + + """ + return IsEqualIgnoringWhiteSpace(string) diff --git a/contrib/python/PyHamcrest/src/hamcrest/library/text/stringcontains.py b/contrib/python/PyHamcrest/src/hamcrest/library/text/stringcontains.py new file mode 100644 index 0000000000..58ffd283c6 --- /dev/null +++ b/contrib/python/PyHamcrest/src/hamcrest/library/text/stringcontains.py @@ -0,0 +1,38 @@ +from hamcrest.library.text.substringmatcher import SubstringMatcher +from hamcrest.core.helpers.hasmethod import hasmethod + +__author__ = "Jon Reid" +__copyright__ = "Copyright 2011 hamcrest.org" +__license__ = "BSD, see License.txt" + + +class StringContains(SubstringMatcher): + + def __init__(self, substring): + super(StringContains, self).__init__(substring) + + def _matches(self, item): + if not hasmethod(item, 'find'): + return False + return item.find(self.substring) >= 0 + + def relationship(self): + return 'containing' + + +def contains_string(substring): + """Matches if object is a string containing a given string. + + :param string: The string to search for. + + This matcher first checks whether the evaluated object is a string. If so, + it checks whether it contains ``string``. + + Example:: + + contains_string("def") + + will match "abcdefg". + + """ + return StringContains(substring) diff --git a/contrib/python/PyHamcrest/src/hamcrest/library/text/stringcontainsinorder.py b/contrib/python/PyHamcrest/src/hamcrest/library/text/stringcontainsinorder.py new file mode 100644 index 0000000000..516b1043ab --- /dev/null +++ b/contrib/python/PyHamcrest/src/hamcrest/library/text/stringcontainsinorder.py @@ -0,0 +1,52 @@ +__author__ = "Romilly Cocking"
+__copyright__ = "Copyright 2011 hamcrest.org"
+__license__ = "BSD, see License.txt"
+
+from hamcrest.core.base_matcher import BaseMatcher
+from hamcrest.core.helpers.hasmethod import hasmethod
+
+import six
+
+class StringContainsInOrder(BaseMatcher):
+
+ def __init__(self, *substrings):
+ for substring in substrings:
+ if not isinstance(substring, six.string_types):
+ raise TypeError(self.__class__.__name__
+ + ' requires string arguments')
+ self.substrings = substrings
+
+ def _matches(self, item):
+ if not hasmethod(item, 'find'):
+ return False
+ from_index = 0
+ for substring in self.substrings:
+ from_index = item.find(substring, from_index)
+ if from_index == -1:
+ return False
+ return True
+
+ def describe_to(self, description):
+ description.append_list('a string containing ', ', ', ' in order',
+ self.substrings)
+
+
+def string_contains_in_order(*substrings):
+ """Matches if object is a string containing a given list of substrings in
+ relative order.
+
+ :param string1,...: A comma-separated list of strings.
+
+ This matcher first checks whether the evaluated object is a string. If so,
+ it checks whether it contains a given list of strings, in relative order to
+ each other. The searches are performed starting from the beginning of the
+ evaluated string.
+
+ Example::
+
+ string_contains_in_order("bc", "fg", "jkl")
+
+ will match "abcdefghijklm".
+
+ """
+ return StringContainsInOrder(*substrings)
diff --git a/contrib/python/PyHamcrest/src/hamcrest/library/text/stringendswith.py b/contrib/python/PyHamcrest/src/hamcrest/library/text/stringendswith.py new file mode 100644 index 0000000000..43f9c3d302 --- /dev/null +++ b/contrib/python/PyHamcrest/src/hamcrest/library/text/stringendswith.py @@ -0,0 +1,39 @@ +from hamcrest.library.text.substringmatcher import SubstringMatcher +from hamcrest.core.helpers.hasmethod import hasmethod + +__author__ = "Jon Reid" +__copyright__ = "Copyright 2011 hamcrest.org" +__license__ = "BSD, see License.txt" + + +class StringEndsWith(SubstringMatcher): + + def __init__(self, substring): + super(StringEndsWith, self).__init__(substring) + + def _matches(self, item): + if not hasmethod(item, 'endswith'): + return False + return item.endswith(self.substring) + + def relationship(self): + return 'ending with' + + +def ends_with(string): + """Matches if object is a string ending with a given string. + + :param string: The string to search for. + + This matcher first checks whether the evaluated object is a string. If so, + it checks if ``string`` matches the ending characters of the evaluated + object. + + Example:: + + ends_with("bar") + + will match "foobar". + + """ + return StringEndsWith(string) diff --git a/contrib/python/PyHamcrest/src/hamcrest/library/text/stringmatches.py b/contrib/python/PyHamcrest/src/hamcrest/library/text/stringmatches.py new file mode 100644 index 0000000000..2a16e29729 --- /dev/null +++ b/contrib/python/PyHamcrest/src/hamcrest/library/text/stringmatches.py @@ -0,0 +1,40 @@ +__author__ = "Chris Rose" +__copyright__ = "Copyright 2011 hamcrest.org" +__license__ = "BSD, see License.txt" + +import re + +import six + +from hamcrest.core.base_matcher import BaseMatcher +from hamcrest.core.helpers.hasmethod import hasmethod + +class StringMatchesPattern(BaseMatcher): + + def __init__(self, pattern): + self.pattern = pattern + + def describe_to(self, description): + description.append_text("a string matching '") \ + .append_text(self.pattern.pattern) \ + .append_text("'") + + def _matches(self, item): + return self.pattern.search(item) is not None + + +def matches_regexp(pattern): + """Matches if object is a string containing a match for a given regular + expression. + + :param pattern: The regular expression to search for. + + This matcher first checks whether the evaluated object is a string. If so, + it checks if the regular expression ``pattern`` matches anywhere within the + evaluated object. + + """ + if isinstance(pattern, six.string_types): + pattern = re.compile(pattern) + + return StringMatchesPattern(pattern) diff --git a/contrib/python/PyHamcrest/src/hamcrest/library/text/stringstartswith.py b/contrib/python/PyHamcrest/src/hamcrest/library/text/stringstartswith.py new file mode 100644 index 0000000000..a0af49c9c2 --- /dev/null +++ b/contrib/python/PyHamcrest/src/hamcrest/library/text/stringstartswith.py @@ -0,0 +1,39 @@ +__author__ = "Jon Reid" +__copyright__ = "Copyright 2011 hamcrest.org" +__license__ = "BSD, see License.txt" + +from hamcrest.library.text.substringmatcher import SubstringMatcher +from hamcrest.core.helpers.hasmethod import hasmethod + + +class StringStartsWith(SubstringMatcher): + + def __init__(self, substring): + super(StringStartsWith, self).__init__(substring) + + def _matches(self, item): + if not hasmethod(item, 'startswith'): + return False + return item.startswith(self.substring) + + def relationship(self): + return 'starting with' + + +def starts_with(substring): + """Matches if object is a string starting with a given string. + + :param string: The string to search for. + + This matcher first checks whether the evaluated object is a string. If so, + it checks if ``string`` matches the beginning characters of the evaluated + object. + + Example:: + + starts_with("foo") + + will match "foobar". + + """ + return StringStartsWith(substring) diff --git a/contrib/python/PyHamcrest/src/hamcrest/library/text/substringmatcher.py b/contrib/python/PyHamcrest/src/hamcrest/library/text/substringmatcher.py new file mode 100644 index 0000000000..63ea359a51 --- /dev/null +++ b/contrib/python/PyHamcrest/src/hamcrest/library/text/substringmatcher.py @@ -0,0 +1,20 @@ +__author__ = "Jon Reid" +__copyright__ = "Copyright 2011 hamcrest.org" +__license__ = "BSD, see License.txt" + +from hamcrest.core.base_matcher import BaseMatcher + +import six + +class SubstringMatcher(BaseMatcher): + + def __init__(self, substring): + if not isinstance(substring, six.string_types): + raise TypeError(self.__class__.__name__ + ' requires string') + self.substring = substring + + def describe_to(self, description): + description.append_text('a string ') \ + .append_text(self.relationship()) \ + .append_text(' ') \ + .append_description_of(self.substring) |