Skip to content

Commit

Permalink
Add 'includes' helper
Browse files Browse the repository at this point in the history
Closes #6
Closes #8
  • Loading branch information
jacebrowning committed Mar 29, 2020
1 parent 7e268e6 commit 3aba074
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 38 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# 2.1 (beta)

- Added support for matching identity via `expect(actual).is_(expected)`.
- Added `includes` helper as an alias of `contains`.

# 2.0 (2020-02-06)

Expand Down
4 changes: 2 additions & 2 deletions docs/checks.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ expect(42) > 0
# Contents

```python
expect("Hello, world!").contains("world")
expect("Hello, world!").excludes("foobar") # or does_not_contain
expect("Hello, world!").contains("world") # or 'includes'
expect("Hello, world!").excludes("foobar") # or 'does_not_contain'
expect("Hello, world!").icontains("hello")
expect("Hello, world!").iexcludes("FOOBAR")
```
Expand Down
17 changes: 17 additions & 0 deletions expecter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,23 @@ def icontains(self, other):

assert expected in actual, msg

def includes(self, other):
"""Same as ``contains`` but with alternate phrasing."""
__tracebackhide__ = _hidetraceback() # pylint: disable=unused-variable

if isinstance(self._actual, str) and '\n' in self._actual:
msg = "Given text:\n\n%s\n\nExpected to include %s but didn't" % (
self._actual.strip(),
repr(other),
)
else:
msg = "Expected %s to include %s but it didn't" % (
repr(self._actual),
repr(other),
)

assert other in self._actual, msg

def does_not_contain(self, other):
"""Opposite of ``contains``."""
__tracebackhide__ = _hidetraceback() # pylint: disable=unused-variable
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[tool.poetry]

name = "pytest-expecter"
version = "2.1b1"
version = "2.1b2"
description = "Better testing with expecter and pytest."

license = "BSD"
Expand All @@ -20,6 +20,7 @@ classifiers = [
"Environment :: Plugins",
"Framework :: Pytest",
"Intended Audience :: Developers",
"License :: OSI Approved :: BSD License",
"Operating System :: OS Independent",
"Programming Language :: Python",
"Programming Language :: Python :: 3.6",
Expand Down
31 changes: 0 additions & 31 deletions tests/test_comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,6 @@


def describe_expect():
def it_can_expect_equality():
expect(2) == 1 + 1

def _fails():
expect(1) == 2

with pytest.raises(AssertionError):
_fails()
assert fail_msg(_fails) == 'Expected 2 but got 1'

def it_can_compare_bytes():
null = bytes((0,))
expect(null) == null
data = bytes(range(9, 32))

def _fails():
expect(data) == data + null

with pytest.raises(AssertionError):
_fails()

def it_can_expect_inequality():
expect(1) != 2

def _fails():
expect(1) != 1

with pytest.raises(AssertionError):
_fails()
assert fail_msg(_fails) == 'Expected anything except 1 but got it'

def it_can_expect_less_than():
expect(1) < 2

Expand Down
10 changes: 10 additions & 0 deletions tests/test_containment.py → tests/test_contents.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ def _fails():
"Expected 'fooBar' to contain 'Qux' (ignoring case) but it didn't"
)

def it_can_expect_inclusion():
expect([1]).includes(1)

def _fails():
expect([2]).includes(1)

with pytest.raises(AssertionError):
_fails()
assert fail_msg(_fails) == ("Expected [2] to include 1 but it didn't")

def it_can_expect_non_containment():
expect([1]).does_not_contain(0)

Expand Down
39 changes: 39 additions & 0 deletions tests/test_equality.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# pylint: disable=unused-variable,expression-not-assigned

import pytest

from expecter import expect
from tests.utils import fail_msg


def describe_expect():
def it_can_expect_equality():
expect(2) == 1 + 1

def _fails():
expect(1) == 2

with pytest.raises(AssertionError):
_fails()
assert fail_msg(_fails) == 'Expected 2 but got 1'

def it_can_compare_bytes():
null = bytes((0,))
expect(null) == null
data = bytes(range(9, 32))

def _fails():
expect(data) == data + null

with pytest.raises(AssertionError):
_fails()

def it_can_expect_inequality():
expect(1) != 2

def _fails():
expect(1) != 1

with pytest.raises(AssertionError):
_fails()
assert fail_msg(_fails) == 'Expected anything except 1 but got it'
12 changes: 8 additions & 4 deletions tests/test_exceptions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# pylint: disable=unused-variable,expression-not-assigned

from nose.tools import assert_raises
import pytest

from expecter import expect
from tests.utils import fail_msg
Expand All @@ -16,7 +16,9 @@ def _expects_raise_but_doesnt_get_it():
with expect.raises(KeyError):
pass

assert_raises(AssertionError, _expects_raise_but_doesnt_get_it)
with pytest.raises(AssertionError):
_expects_raise_but_doesnt_get_it()

assert fail_msg(_expects_raise_but_doesnt_get_it) == (
'Expected an exception of type KeyError but got none'
)
Expand All @@ -26,7 +28,8 @@ def _expects_key_error_but_gets_value_error():
with expect.raises(KeyError):
raise ValueError

assert_raises(ValueError, _expects_key_error_but_gets_value_error)
with pytest.raises(ValueError):
_expects_key_error_but_gets_value_error()

def it_can_expect_any_exception():
with expect.raises():
Expand All @@ -41,7 +44,8 @@ def _fails():
with expect.raises(ValueError, 'my message'):
raise ValueError('wrong message')

assert_raises(AssertionError, _fails)
with pytest.raises(AssertionError):
_fails()
assert fail_msg(_fails) == (
"Expected ValueError('my message') but got ValueError('wrong message')"
)
File renamed without changes.

0 comments on commit 3aba074

Please sign in to comment.