Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add contains_regex(regex_pattern) matcher #18

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
/*.egg-info
/.tox
/MANIFEST
.idea
19 changes: 19 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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``.
Expand Down Expand Up @@ -266,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 '[email protected]'), (an object with a property 'username' matching 'jim' and an object with a property 'email_address' matching '[email protected]')]
# but: item 0: an object with a property 'email_address' matching '[email protected]' property 'email_address' was '[email protected]'


Contributing
------------

.. code:: bash

# setup
make bootstrap

# run tests
make test
4 changes: 3 additions & 1 deletion precisely/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -14,6 +15,7 @@
"Matcher",
"is_matcher",
"contains_string",
"contains_regex",
"greater_than",
"greater_than_or_equal_to",
"less_than",
Expand Down
8 changes: 8 additions & 0 deletions precisely/comparison_matchers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import operator
import re

from .base import Matcher
from .results import matched, unmatched
Expand All @@ -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)

Expand Down
21 changes: 21 additions & 0 deletions tests/contains_regex_tests.py
Original file line number Diff line number Diff line change
@@ -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())