| 1 |
|
| 2 |
|
| 3 |
> |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
from __future__ import unicode_literals |
| 18 |
import os |
| 19 |
import re |
| 20 |
import traceback |
| 21 |
import inspect |
| 22 |
from copy import deepcopy |
| 23 |
from pprint import pformat |
| 24 |
from functools import wraps |
| 25 |
|
| 26 |
try: |
| 27 |
import __builtin__ as builtins |
| 28 |
except ImportError: |
| 29 |
import builtins |
| 30 |
|
| 31 |
try: |
| 32 |
from re import Pattern |
| 33 |
except ImportError: |
| 34 |
Pattern = re._pattern_type |
| 35 |
|
| 36 |
from six import string_types, text_type |
| 37 |
|
| 38 |
from sure.core import DeepComparison |
| 39 |
from sure.core import _get_file_name |
| 40 |
from sure.core import _get_line_number |
| 41 |
from sure.core import itemize_length |
| 42 |
from sure.compat import Iterable |
| 43 |
|
| 44 |
|
| 45 |
def identify_callable_location(callable_object): |
| 46 |
filename = os.path.relpath(callable_object.__code__.co_filename) |
| 47 |
lineno = callable_object.__code__.co_firstlineno |
| 48 |
callable_name = callable_object.__code__.co_name |
| 49 |
return '{0} [{1} line {2}]'.format(callable_name, filename, lineno).encode() |
| 50 |
|
| 51 |
|
| 52 |
def is_iterable(obj): |
| 53 |
return hasattr(obj, '__iter__') and not isinstance(obj, string_types) |
| 54 |
|
| 55 |
|
| 56 |
def all_integers(obj): |
| 57 |
if not is_iterable(obj): |
| 58 |
return |
| 59 |
|
| 60 |
for element in obj: |
| 61 |
if not isinstance(element, int): |
| 62 |
return |
| 63 |
|
| 64 |
return True |
| 65 |
|
| 66 |
|
| 67 |
def explanation(msg): |
| 68 |
def dec(func): |
| 69 |
@wraps(func) |
| 70 |
def wrap(self, what): |
| 71 |
ret = func(self, what) |
| 72 |
assert ret, msg % (self._src, what) |
| 73 |
return True |
| 74 |
|
| 75 |
return wrap |
| 76 |
|
| 77 |
return dec |
| 78 |
|
| 79 |
|
| 80 |
class AssertionHelper(object): |
| 81 |
def __init__(self, src, |
| 82 |
within_range=None, |
| 83 |
with_args=None, |
| 84 |
with_kwargs=None, |
| 85 |
and_kwargs=None): |
| 86 |
|
| 87 |
self._src = src |
| 88 |
self._attribute = None |
| 89 |
self._eval = None |
| 90 |
self._range = None |
| 91 |
if all_integers(within_range): |
| 92 |
if len(within_range) != 2: |
| 93 |
raise TypeError( |
| 94 |
'within_range parameter must be a tuple with 2 objects', |
| 95 |
) |
| 96 |
|
| 97 |
self._range = within_range |
| 98 |
|
| 99 |
self._callable_args = [] |
| 100 |
if isinstance(with_args, (list, tuple)): |
| 101 |
self._callable_args = list(with_args) |
| 102 |
|
| 103 |
self._callable_kw = {} |
| 104 |
if isinstance(with_kwargs, dict): |
| 105 |
self._callable_kw.update(with_kwargs) |
| 106 |
|
| 107 |
if isinstance(and_kwargs, dict): |
| 108 |
self._callable_kw.update(and_kwargs) |
| 109 |
|
| 110 |
@classmethod |
| 111 |
def is_a_matcher(cls, func): |
| 112 |
def match(self, *args, **kw): |
| 113 |
return func(self._src, *args, **kw) |
| 114 |
|
| 115 |
new_matcher = deepcopy(match) |
| 116 |
new_matcher.__name__ = func.__name__ |
| 117 |
setattr(cls, func.__name__, new_matcher) |
| 118 |
|
| 119 |
return new_matcher |
| 120 |
|
| 121 |
def raises(self, exc, msg=None): |
| 122 |
if not callable(self._src): |
| 123 |
raise TypeError('%r is not callable' % self._src) |
| 124 |
|
| 125 |
try: |
| 126 |
self._src(*self._callable_args, **self._callable_kw) |
| 127 |
except BaseException as e: |
| 128 |
if isinstance(exc, string_types): |
| 129 |
msg = exc |
| 130 |
exc = type(e) |
| 131 |
|
| 132 |
elif isinstance(exc, Pattern): |
| 133 |
msg = exc |
| 134 |
exc = type(e) |
| 135 |
|
| 136 |
err = text_type(e) |
| 137 |
|
| 138 |
if isinstance(exc, type) and issubclass(exc, BaseException): |
| 139 |
if not isinstance(e, exc): |
| 140 |
raise AssertionError( |
| 141 |
'%r should raise %r, but raised %r:\nORIGINAL EXCEPTION:\n\n%s' % ( |
| 142 |
self._src, exc, e.__class__, traceback.format_exc())) |
| 143 |
|
| 144 |
if isinstance(msg, string_types) and msg not in err: |
| 145 |
raise AssertionError(''' |
| 146 |
%r raised %s, but the exception message does not |
| 147 |
match.\n\nEXPECTED:\n%s\n\nGOT:\n%s'''.strip() % ( |
| 148 |
self._src, |
| 149 |
type(e).__name__, |
| 150 |
msg, err)) |
| 151 |
|
| 152 |
elif isinstance(msg, Pattern) and not msg.search(err): |
| 153 |
raise AssertionError( |
| 154 |
'When calling %r the exception message does not match. ' \ |
| 155 |
'Expected to match regex: %r\n against:\n %r' % (identify_callable_location(self._src), msg.pattern, err)) |
| 156 |
|
| 157 |
elif isinstance(msg, string_types) and msg not in err: |
| 158 |
raise AssertionError( |
| 159 |
'When calling %r the exception message does not match. ' \ |
| 160 |
'Expected: %r\n got:\n %r' % (self._src, msg, err)) |
| 161 |
|
| 162 |
elif isinstance(msg, Pattern) and not msg.search(err): |
| 163 |
raise AssertionError( |
| 164 |
'When calling %r the exception message does not match. ' \ |
| 165 |
'Expected to match regex: %r\n against:\n %r' % (identify_callable_location(self._src), msg.pattern, err)) |
| 166 |
|
| 167 |
else: |
| 168 |
raise e |
| 169 |
else: |
| 170 |
if inspect.isbuiltin(self._src): |
| 171 |
_src_filename = '<built-in function>' |
| 172 |
else: |
| 173 |
_src_filename = _get_file_name(self._src) |
| 174 |
|
| 175 |
if inspect.isfunction(self._src): |
| 176 |
_src_lineno = _get_line_number(self._src) |
| 177 |
raise AssertionError( |
| 178 |
'calling function %s(%s at line: "%d") with args %r and kwargs %r did not raise %r' % ( |
| 179 |
self._src.__name__, |
| 180 |
_src_filename, _src_lineno, |
| 181 |
self._callable_args, |
| 182 |
self._callable_kw, exc)) |
| 183 |
else: |
| 184 |
raise AssertionError( |
| 185 |
'at %s:\ncalling %s() with args %r and kwargs %r did not raise %r' % ( |
| 186 |
_src_filename, |
| 187 |
self._src.__name__, |
| 188 |
self._callable_args, |
| 189 |
self._callable_kw, exc)) |
| 190 |
|
| 191 |
return True |
| 192 |
|
| 193 |
def deep_equals(self, dst): |
| 194 |
deep = DeepComparison(self._src, dst) |
| 195 |
comparison = deep.compare() |
| 196 |
if isinstance(comparison, bool): |
| 197 |
return comparison |
| 198 |
|
| 199 |
raise comparison.as_assertion(self._src, dst) |
| 200 |
|
| 201 |
def equals(self, dst): |
| 202 |
if self._attribute and is_iterable(self._src): |
| 203 |
msg = '%r[%d].%s should be %r, but is %r' |
| 204 |
|
| 205 |
for index, item in enumerate(self._src): |
| 206 |
if self._range: |
| 207 |
if index < self._range[0] or index > self._range[1]: |
| 208 |
continue |
| 209 |
|
| 210 |
attribute = getattr(item, self._attribute) |
| 211 |
error = msg % ( |
| 212 |
self._src, index, self._attribute, dst, attribute) |
| 213 |
if attribute != dst: |
| 214 |
raise AssertionError(error) |
| 215 |
else: |
| 216 |
return self.deep_equals(dst) |
| 217 |
|
| 218 |
return True |
| 219 |
|
| 220 |
def looks_like(self, dst): |
| 221 |
old_src = pformat(self._src) |
| 222 |
old_dst = pformat(dst) |
| 223 |
self._src = re.sub(r'\s', '', self._src).lower() |
| 224 |
dst = re.sub(r'\s', '', dst).lower() |
| 225 |
error = '%s does not look like %s' % (old_src, old_dst) |
| 226 |
assert self._src == dst, error |
| 227 |
return self._src == dst |
| 228 |
|
| 229 |
def every_one_is(self, dst): |
| 230 |
msg = 'all members of %r should be %r, but the %dth is %r' |
| 231 |
for index, item in enumerate(self._src): |
| 232 |
if self._range: |
| 233 |
if index < self._range[0] or index > self._range[1]: |
| 234 |
continue |
| 235 |
|
| 236 |
error = msg % (self._src, dst, index, item) |
| 237 |
if item != dst: |
| 238 |
raise AssertionError(error) |
| 239 |
|
| 240 |
return True |
| 241 |
|
| 242 |
@explanation('%r should differ from %r, but is the same thing') |
| 243 |
def differs(self, dst): |
| 244 |
return self._src != dst |
| 245 |
|
| 246 |
@explanation('%r should be a instance of %r, but is not') |
| 247 |
def is_a(self, dst): |
| 248 |
return isinstance(self._src, dst) |
| 249 |
|
| 250 |
def at(self, key): |
| 251 |
assert self.has(key) |
| 252 |
if isinstance(self._src, dict): |
| 253 |
return AssertionHelper(self._src[key]) |
| 254 |
|
| 255 |
else: |
| 256 |
return AssertionHelper(getattr(self._src, key)) |
| 257 |
|
| 258 |
@explanation('%r should have %r, but have not') |
| 259 |
def has(self, that): |
| 260 |
return that in self |
| 261 |
|
| 262 |
def _get_that(self, that): |
| 263 |
try: |
| 264 |
that = int(that) |
| 265 |
except TypeError: |
| 266 |
that = len(that) |
| 267 |
return that |
| 268 |
|
| 269 |
def len_greater_than(self, that): |
| 270 |
that = self._get_that(that) |
| 271 |
length = len(self._src) |
| 272 |
|
| 273 |
if length <= that: |
| 274 |
error = 'the length of the %s should be greater then %d, but is %d' % ( |
| 275 |
type(self._src).__name__, |
| 276 |
that, |
| 277 |
length, |
| 278 |
) |
| 279 |
raise AssertionError(error) |
| 280 |
|
| 281 |
return True |
| 282 |
|
| 283 |
def len_greater_than_or_equals(self, that): |
| 284 |
that = self._get_that(that) |
| 285 |
|
| 286 |
length = len(self._src) |
| 287 |
|
| 288 |
if length < that: |
| 289 |
error = 'the length of %r should be greater then or equals %d, but is %d' % ( |
| 290 |
self._src, |
| 291 |
that, |
| 292 |
length, |
| 293 |
) |
| 294 |
raise AssertionError(error) |
| 295 |
|
| 296 |
return True |
| 297 |
|
| 298 |
def len_lower_than(self, that): |
| 299 |
original_that = that |
| 300 |
if isinstance(that, Iterable): |
| 301 |
that = len(that) |
| 302 |
else: |
| 303 |
that = self._get_that(that) |
| 304 |
length = len(self._src) |
| 305 |
|
| 306 |
if length >= that: |
| 307 |
error = 'the length of %r should be lower then %r, but is %d' % ( |
| 308 |
self._src, |
| 309 |
original_that, |
| 310 |
length, |
| 311 |
) |
| 312 |
raise AssertionError(error) |
| 313 |
|
| 314 |
return True |
| 315 |
|
| 316 |
def len_lower_than_or_equals(self, that): |
| 317 |
that = self._get_that(that) |
| 318 |
|
| 319 |
length = len(self._src) |
| 320 |
error = 'the length of %r should be lower then or equals %d, but is %d' |
| 321 |
|
| 322 |
if length > that: |
| 323 |
msg = error % ( |
| 324 |
self._src, |
| 325 |
that, |
| 326 |
length, |
| 327 |
) |
| 328 |
raise AssertionError(msg) |
| 329 |
|
| 330 |
return True |
| 331 |
|
| 332 |
def len_is(self, that): |
| 333 |
that = self._get_that(that) |
| 334 |
length = len(self._src) |
| 335 |
|
| 336 |
if length != that: |
| 337 |
error = 'the length of %r should be %d, but is %d' % ( |
| 338 |
self._src, |
| 339 |
that, |
| 340 |
length, |
| 341 |
) |
| 342 |
raise AssertionError(error) |
| 343 |
|
| 344 |
return True |
| 345 |
|
| 346 |
def len_is_not(self, that): |
| 347 |
that = self._get_that(that) |
| 348 |
length = len(self._src) |
| 349 |
|
| 350 |
if length == that: |
| 351 |
error = 'the length of %r should not be %d' % ( |
| 352 |
self._src, |
| 353 |
that, |
| 354 |
) |
| 355 |
raise AssertionError(error) |
| 356 |
|
| 357 |
return True |
| 358 |
|
| 359 |
def like(self, that): |
| 360 |
return self.has(that) |
| 361 |
|
| 362 |
def the_attribute(self, attr): |
| 363 |
self._attribute = attr |
| 364 |
return self |
| 365 |
|
| 366 |
def in_each(self, attr): |
| 367 |
self._eval = attr |
| 368 |
return self |
| 369 |
|
| 370 |
def matches(self, items): |
| 371 |
msg = '%r[%d].%s should be %r, but is %r' |
| 372 |
get_eval = lambda item: eval( |
| 373 |
"%s.%s" % ('current', self._eval), {}, {'current': item}, |
| 374 |
) |
| 375 |
|
| 376 |
if self._eval and is_iterable(self._src): |
| 377 |
if isinstance(items, string_types): |
| 378 |
items = [items for x in range(len(items))] |
| 379 |
else: |
| 380 |
if len(items) != len(self._src): |
| 381 |
source = list(map(get_eval, self._src)) |
| 382 |
source_len = len(source) |
| 383 |
items_len = len(items) |
| 384 |
|
| 385 |
raise AssertionError( |
| 386 |
'%r has %d items, but the matching list has %d: %r' |
| 387 |
% (source, source_len, items_len, items), |
| 388 |
) |
| 389 |
|
| 390 |
for index, (item, other) in enumerate(zip(self._src, items)): |
| 391 |
if self._range: |
| 392 |
if index < self._range[0] or index > self._range[1]: |
| 393 |
continue |
| 394 |
|
| 395 |
value = get_eval(item) |
| 396 |
|
| 397 |
error = msg % (self._src, index, self._eval, other, value) |
| 398 |
if other != value: |
| 399 |
raise AssertionError(error) |
| 400 |
else: |
| 401 |
return self.equals(items) |
| 402 |
|
| 403 |
return True |
| 404 |
|
| 405 |
@builtins.property |
| 406 |
def is_empty(self): |
| 407 |
try: |
| 408 |
lst = list(self._src) |
| 409 |
length = len(lst) |
| 410 |
assert length == 0, \ |
| 411 |
'%r is not empty, it has %s' % (self._src, |
| 412 |
itemize_length(self._src)) |
| 413 |
return True |
| 414 |
|
| 415 |
except TypeError: |
| 416 |
raise AssertionError("%r is not iterable" % self._src) |
| 417 |
|
| 418 |
@builtins.property |
| 419 |
def are_empty(self): |
| 420 |
return self.is_empty |
| 421 |
|
| 422 |
def __contains__(self, what): |
| 423 |
if isinstance(self._src, dict): |
| 424 |
items = self._src.keys() |
| 425 |
|
| 426 |
if isinstance(self._src, Iterable): |
| 427 |
items = self._src |
| 428 |
else: |
| 429 |
items = dir(self._src) |
| 430 |
|
| 431 |
return what in items |
| 432 |
|
| 433 |
def contains(self, what): |
| •434 |
try: |
| 435 |
assert what in self._src, '%r should be in %r' % (what, self._src) |
| 436 |
except TypeError: |
| 437 |
if self._src is None: |
| 438 |
raise TypeError("argument of type 'NoneType' is not iterable") |
| 439 |
raise |
| 440 |
return True |
| 441 |
|
| 442 |
def does_not_contain(self, what): |
| 443 |
assert what not in self._src, \ |
| 444 |
'%r should NOT be in %r' % (what, self._src) |
| 445 |
|
| 446 |
return True |
| 447 |
|
| 448 |
doesnt_contain = does_not_contain |
| 449 |
|
| 450 |
|
| 451 |
that = AssertionHelper |