Skip to content

Commit

Permalink
fix(tests): matchers version 2.7
Browse files Browse the repository at this point in the history
  • Loading branch information
h2non committed Dec 12, 2016
1 parent e171318 commit a37dc12
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 15 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ language: python

python:
- 2.7
- 3.2
- 3.3
- 3.4
- 3.5
Expand Down
20 changes: 18 additions & 2 deletions pook/assertion.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import re
import sys
from unittest import TestCase
from .regex import isregex, strip_regex, isregex_expr

# If running Python 3
PY_3 = sys.version_info >= (3,)


def equal(x, y):
"""
Expand All @@ -16,7 +21,10 @@ def equal(x, y):
Returns:
bool
"""
return TestCase().assertEqual(x, y) or True
if PY_3:
return TestCase().assertEqual(x, y) or True

assert x == y


def matches(x, y, regex_expr=False):
Expand All @@ -39,8 +47,16 @@ def matches(x, y, regex_expr=False):
x = strip_regex(x) if regex_expr and isregex_expr(x) else x
# Retrieve original regex pattern
x = x.pattern if isregex(x) else x

# Run regex assertion
return TestCase().assertRegexpMatches(x, y) or True
if PY_3:
return TestCase().assertRegexpMatches(y, x) or True

# Primitive regex matching for Python 2.7
if isinstance(x, str):
x = re.compile(x, re.IGNORECASE)

assert x.match(y) is not None


def test(x, y, regex_expr=False):
Expand Down
28 changes: 17 additions & 11 deletions tests/unit/matchers/base_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,41 @@
from pook.matchers.base import BaseMatcher


class _BaseMatcher(BaseMatcher):

def match(self, x):
pass


def test_base_matcher_instance():
matcher = BaseMatcher('foo')
assert matcher.name is 'BaseMatcher'
matcher = _BaseMatcher('foo')
assert matcher.name is '_BaseMatcher'
assert matcher.negate is False
assert matcher.expectation == 'foo'
assert matcher.to_dict() == {'BaseMatcher': 'foo'}
assert matcher.__repr__() == 'BaseMatcher(foo)'
assert matcher.to_dict() == {'_BaseMatcher': 'foo'}
assert matcher.__repr__() == '_BaseMatcher(foo)'
assert matcher.__str__() == 'foo'


def test_base_matcher_compare():
assert BaseMatcher.compare(None, 'foo', 'foo')
assert _BaseMatcher('foo').compare('foo', 'foo')

assert BaseMatcher.compare(None, 'foo', 'foo')
assert _BaseMatcher('foo').compare('foo', 'foo')

with pytest.raises(AssertionError):
assert BaseMatcher.compare(None, 'foo', 'bar')
assert _BaseMatcher('foo').compare('foo', 'bar')


def test_base_matcher_exceptions():
assert _BaseMatcher('foo').match(None) is None

with pytest.raises(ValueError,
message='expectation argument cannot be empty'):
BaseMatcher(None)

assert BaseMatcher('foo').match(None) is None
_BaseMatcher(None)


def test_base_matcher_matcher():
assert BaseMatcher.matcher(lambda x: True)(BaseMatcher)

matcher = BaseMatcher('foo', negate=True)
matcher = _BaseMatcher('foo', negate=True)
assert BaseMatcher.matcher(lambda x: False)(matcher)
4 changes: 2 additions & 2 deletions tests/unit/matchers/url_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

def run_tests(cases, regex=False):
for test in cases:
url, match_url, matches = test
match_url, url, matches = test
req = Request(url=url)

if regex:
Expand Down Expand Up @@ -57,5 +57,5 @@ def test_url_matcher_regex():
('http://foo.com/foo', 'http://foo.com/bar', False),
('http://foo.com/foo/bar', 'http://foo.com/bar/foo', False),
('http://foo.com/foo/bar/baz', 'http://foo.com/baz/bar/foo', False),
('http://foo.com/foo\?x=[1-3]', 'http://foo.com/foo?x=5y', False),
('http://foo.com/foo\?x=[1-3]', 'http://foo.com/foo?x=5', False),
), regex=True)

0 comments on commit a37dc12

Please sign in to comment.