Skip to content

Commit

Permalink
Keyword argument matcher.
Browse files Browse the repository at this point in the history
  • Loading branch information
brunns committed Jul 18, 2018
1 parent 6f13ae5 commit 83330a8
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
36 changes: 34 additions & 2 deletions src/matchers/mock.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
from itertools import chain
from numbers import Number

from hamcrest import equal_to
from hamcrest.core.base_matcher import BaseMatcher
from hamcrest.core.matcher import Matcher
from six.moves import zip_longest


def call_has_arg(index, expected):
return CallHasPositionalArg(index, expected)
def call_has_arg(arg, expected):
if isinstance(arg, Number):
return CallHasPositionalArg(arg, expected)
else:
return CallHasKeywordArg(arg, expected)


class CallHasPositionalArg(BaseMatcher):
Expand Down Expand Up @@ -38,6 +42,34 @@ def describe_mismatch(self, actual_call, mismatch_description):
)


class CallHasKeywordArg(BaseMatcher):
def __init__(self, key, expected):
super(CallHasKeywordArg, self).__init__()
self.key = key
self.expected = expected if isinstance(expected, Matcher) else equal_to(expected)

def _matches(self, actual_call):
args = actual_call[2]
return self.key in args and self.expected.matches(args[self.key])

def describe_to(self, description):
description.append_text("mock.call with keyword argument ").append_description_of(self.key).append_text(
" matching "
)
self.expected.describe_to(description)

def describe_mismatch(self, actual_call, mismatch_description):
args = actual_call[2]
if self.key in args:
mismatch_description.append_text("got mock.call with keyword argument ").append_description_of(
self.key
).append_text(" with value ").append_description_of(args[self.key])
else:
mismatch_description.append_text("got mock.call with without keyword argument ").append_description_of(
self.key
)


def has_call(call_matcher):
return HasCall(call_matcher)

Expand Down
22 changes: 20 additions & 2 deletions tests/unit/matchers/test_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,26 @@ def test_call_has_positional_arg():


def test_call_has_keyword_arg():
# TODO
pass
# Given
m = MagicMock()

# When
m(f="first", s="second", t="third")
call = m.mock_calls[0]

# Then
assert_that(call, call_has_arg("s", "second"))
assert_that(call, not_(call_has_arg("s", "nope")))
assert_that(call, not_(call_has_arg("w", "nope")))
assert_that(call, call_has_arg("s", contains_string("eco")))
assert_that(
call_has_arg("s", contains_string("eco")),
has_string("mock.call with keyword argument 's' matching a string containing 'eco'"),
)
assert_that(
call_has_arg("s", "fifth"), mismatches_with(call, "got mock.call with keyword argument 's' with value 'second'")
)
assert_that(call_has_arg("n", "nope"), mismatches_with(call, "got mock.call with without keyword argument 'n'"))


def test_call_has_args():
Expand Down

0 comments on commit 83330a8

Please sign in to comment.