From 8f1756432f3c335baea006c3bb828929aaae3fd9 Mon Sep 17 00:00:00 2001 From: David Tan Date: Tue, 19 Jul 2022 13:58:42 +1000 Subject: [PATCH 1/2] Add contains_regex(regex_pattern) matcher --- README.rst | 7 +++++++ precisely/__init__.py | 4 +++- precisely/comparison_matchers.py | 8 ++++++++ tests/contains_regex_tests.py | 21 +++++++++++++++++++++ 4 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 tests/contains_regex_tests.py diff --git a/README.rst b/README.rst index 6ffc41b..ac4a3ca 100644 --- a/README.rst +++ b/README.rst @@ -184,6 +184,13 @@ For instance, ``has_attrs(name="bob")`` is equivalent to ``has_attrs(name=equal_ * ``contains_string(substring)``: matches a string if it contains ``substring``. +* ``contains_regex(regex_pattern)``: matches a string if string matches regex ``regex_pattern``. + For instance: + + .. code:: python + + assert_that("Hello there", contains_regex("^Hello.*")) + * ``greater_than(value)``: matches values greater than ``value``. * ``greater_than_or_equal_to(value)``: matches values greater than or equal to ``value``. diff --git a/precisely/__init__.py b/precisely/__init__.py index 7e2722d..a90c2ef 100644 --- a/precisely/__init__.py +++ b/precisely/__init__.py @@ -1,5 +1,6 @@ from .base import Matcher, is_matcher -from .comparison_matchers import contains_string, greater_than, greater_than_or_equal_to, less_than, less_than_or_equal_to, starts_with, close_to +from .comparison_matchers import contains_string, contains_regex, greater_than, greater_than_or_equal_to, less_than, \ + less_than_or_equal_to, starts_with, close_to from .core_matchers import equal_to, anything, all_of, any_of, not_ from .object_matchers import has_attr, has_attrs, is_instance from .iterable_matchers import all_elements, contains_exactly, includes, is_sequence @@ -14,6 +15,7 @@ "Matcher", "is_matcher", "contains_string", + "contains_regex", "greater_than", "greater_than_or_equal_to", "less_than", diff --git a/precisely/comparison_matchers.py b/precisely/comparison_matchers.py index d70fd78..d455922 100644 --- a/precisely/comparison_matchers.py +++ b/precisely/comparison_matchers.py @@ -1,4 +1,5 @@ import operator +import re from .base import Matcher from .results import matched, unmatched @@ -8,6 +9,13 @@ def contains_string(value): return ComparisonMatcher(operator.contains, "contains the string", value) +def contains_regex(value): + def regex_match_operator(string, pattern): + return re.match(pattern, string) + + return ComparisonMatcher(regex_match_operator, "contains the regex pattern", value) + + def greater_than(value): return ComparisonMatcher(operator.gt, "greater than", value) diff --git a/tests/contains_regex_tests.py b/tests/contains_regex_tests.py new file mode 100644 index 0000000..c5f8ad4 --- /dev/null +++ b/tests/contains_regex_tests.py @@ -0,0 +1,21 @@ +from nose.tools import istest, assert_equal + +from precisely import contains_regex +from precisely.results import matched, unmatched + + +@istest +def contains_regex_matches_when_actual_string_matches_regex_pattern_passed_to_matcher(): + matcher = contains_regex(".*[Hh]ello$") + + assert_equal(matched(), matcher.match("hello")) + assert_equal(matched(), matcher.match("Hello")) + assert_equal(matched(), matcher.match("why hello")) + assert_equal(unmatched("was 'why hello there'"), matcher.match("why hello there")) + + +@istest +def contains_regex_description_describes_value(): + matcher = contains_regex(".*") + + assert_equal("contains the regex pattern '.*'", matcher.describe()) From 8411c148370ac284befd967955f22ed815f42f61 Mon Sep 17 00:00:00 2001 From: David Tan Date: Tue, 19 Jul 2022 13:59:57 +1000 Subject: [PATCH 2/2] Add contribution guide + update .gitignore --- .gitignore | 1 + README.rst | 12 ++++++++++++ 2 files changed, 13 insertions(+) diff --git a/.gitignore b/.gitignore index 0626a3d..380013a 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ /*.egg-info /.tox /MANIFEST +.idea \ No newline at end of file diff --git a/README.rst b/README.rst index ac4a3ca..9b157ea 100644 --- a/README.rst +++ b/README.rst @@ -273,3 +273,15 @@ messages that this project produces, but feel free to judge for yourself: # Hamcrest error: # Expected: a sequence containing [(an object with a property 'username' matching 'bob' and an object with a property 'email_address' matching 'bob@example.com'), (an object with a property 'username' matching 'jim' and an object with a property 'email_address' matching 'jim@example.com')] # but: item 0: an object with a property 'email_address' matching 'bob@example.com' property 'email_address' was 'jim@example.com' + + +Contributing +------------ + + .. code:: bash + + # setup + make bootstrap + + # run tests + make test