/usr/lib/python2.7/dist-packages/doublex/matchers.py is in python-doublex 1.8.2-1build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 | # -*- coding:utf-8; tab-width:4; mode:python -*-
# doublex
#
# Copyright © 2012,2013 David Villa Alises
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
import time
import hamcrest
from hamcrest.core.matcher import Matcher
from hamcrest.core.base_matcher import BaseMatcher
from hamcrest import is_, instance_of
from .internal import (
Method, InvocationContext, ANY_ARG, MockBase, SpyBase,
PropertyGet, PropertySet, WrongApiUsage, Invocation)
__all__ = ['called',
'never',
'verify', 'any_order_verify',
'property_got', 'property_set',
'assert_that', 'wait_that',
'is_', 'instance_of']
# just hamcrest aliases
at_least = hamcrest.greater_than_or_equal_to
at_most = hamcrest.less_than_or_equal_to
any_time = hamcrest.greater_than(0)
class MatcherRequiredError(Exception):
pass
def assert_that(actual, matcher=None, reason=''):
if matcher and not isinstance(matcher, Matcher):
raise MatcherRequiredError("%s should be a hamcrest Matcher" % str(matcher))
return hamcrest.assert_that(actual, matcher, reason)
def wait_that(actual, matcher, reason='', delta=1, timeout=5):
'''
Poll the given matcher each 'delta' seconds until 'matcher'
matches 'actual' or 'timeout' is reached.
'''
exc = None
init = time.time()
timeout_reached = False
while 1:
try:
if time.time() - init > timeout:
timeout_reached = True
break
assert_that(actual, matcher, reason)
break
except AssertionError as e:
time.sleep(delta)
exc = e
if timeout_reached:
msg = exc.args[0] + ' after {0} seconds'.format(timeout)
exc.args = msg,
raise exc
class OperationMatcher(BaseMatcher):
pass
class MethodCalled(OperationMatcher):
def __init__(self, context=None, times=any_time):
self.context = context or InvocationContext(ANY_ARG)
self._times = times
self._async_timeout = None
def _matches(self, method):
self._assure_is_spied_method(method)
self.method = method
if not self._async_timeout:
return method._was_called(self.context, self._times)
if self._async_timeout:
if self._times != any_time:
raise WrongApiUsage("'times' and 'async' are exclusive")
self.method._event.wait(self._async_timeout)
return method._was_called(self.context, self._times)
def _assure_is_spied_method(self, method):
if not isinstance(method, Method) or not isinstance(method.double, SpyBase):
raise WrongApiUsage("takes a spy method (got %s instead)" % method)
def describe_to(self, description):
description.append_text('these calls:\n')
description.append_text(self.method._show(indent=10))
description.append_text(str(self.context))
if self._times != any_time:
description.append_text(' -- times: %s' % self._times)
def describe_mismatch(self, actual, description):
description.append_text("calls that actually ocurred were:\n")
description.append_text(self.method.double._recorded.show(indent=10))
def with_args(self, *args, **kargs):
self.context.update_args(args, kargs)
return self
def with_some_args(self, **kargs):
self.context.update_args(tuple(), kargs)
self.context.check_some_args = True
return self
def async(self, timeout):
self._async_timeout = timeout
return self
def times(self, n):
self._times = n
return self
def called():
return MethodCalled()
class never(BaseMatcher):
def __init__(self, matcher):
if not isinstance(matcher, OperationMatcher):
raise WrongApiUsage(
"takes called/called_with instance (got %s instead)" % matcher)
self.matcher = matcher
def _matches(self, item):
return not self.matcher.matches(item)
def describe_to(self, description):
description.append_text('none of ').append_description_of(self.matcher)
def describe_mismatch(self, actual, description):
self.matcher.describe_mismatch(actual, description)
class MockIsExpectedInvocation(BaseMatcher):
'assert the invocation is a mock expectation'
def __init__(self, invocation):
self.invocation = invocation
def _matches(self, mock):
self.mock = mock
return self.invocation in mock._stubs
def describe_to(self, description):
description.append_text("these calls:\n")
description.append_text(self.mock._stubs.show(indent=10))
def describe_mismatch(self, actual, description):
description.append_text("this call was not expected:\n")
description.append_text(self.invocation._show(indent=10))
class verify(BaseMatcher):
def _matches(self, mock):
if not isinstance(mock, MockBase):
raise WrongApiUsage("takes Mock instance (got %s instead)" % mock)
self.mock = mock
return self._expectations_match()
def _expectations_match(self):
return self.mock._stubs == self.mock._recorded
def describe_to(self, description):
description.append_text("these calls:\n")
description.append_text(self.mock._stubs.show(indent=10))
def describe_mismatch(self, actual, description):
description.append_text('calls that actually ocurred were:\n')
description.append_text(self.mock._recorded.show(indent=10))
class any_order_verify(verify):
def _expectations_match(self):
return sorted(self.mock._stubs) == sorted(self.mock._recorded)
class property_got(OperationMatcher):
def __init__(self, propname, times=any_time):
super(property_got, self).__init__()
self.propname = propname
self._times = times
def _matches(self, double):
self.double = double
self.operation = PropertyGet(self.double, self.propname)
return double._received_invocation(
self.operation, 1, cmp_pred=Invocation.__eq__)
def times(self, n):
self._times = n
return self
def describe_to(self, description):
description.append_text('these calls:\n')
description.append_text(self.operation._show(indent=10))
if self._times != any_time:
description.append_text(' -- times: %s' % self._times)
def describe_mismatch(self, actual, description):
description.append_text('calls that actually ocurred were:\n')
description.append_text(self.double._recorded.show(indent=10))
class property_set(OperationMatcher):
def __init__(self, property_name, value=hamcrest.anything(), times=any_time):
super(property_set, self).__init__()
self.property_name = property_name
self.value = value
self._times = times
def _matches(self, double):
self.double = double
self.operation = PropertySet(self.double, self.property_name, self.value)
return self.double._received_invocation(
self.operation, self._times, cmp_pred=Invocation.__eq__)
def to(self, value):
self.value = value
return self
def times(self, n):
self._times = n
return self
def describe_to(self, description):
description.append_text('these calls:\n')
description.append_text(self.operation._show(indent=10))
if self._times != any_time:
description.append_text(' -- times: %s' % self._times)
def describe_mismatch(self, actual, description):
description.append_text('calls that actually ocurred were:\n')
description.append_text(self.double._recorded.show(indent=10))
|