| 1 |
|
| 2 |
|
| 3 |
from random import random, randint as _randint |
| 4 |
from os.path import split, realpath, abspath |
| 5 |
import sys |
| 6 |
|
| 7 |
from wxgeometrie.param import tolerance as EPSILON |
| 8 |
|
| 9 |
_module_path = split(realpath(sys._getframe().f_code.co_filename))[0] |
| 10 |
ROOTDIR = abspath(_module_path + '/..') |
| 11 |
WXGEODIR = ROOTDIR + '/wxgeometrie' |
| 12 |
|
| 13 |
def randint(a, b = None): |
| 14 |
if b is None: |
| 15 |
b = a |
| 16 |
a = 0 |
| 17 |
return _randint(a, b) |
| 18 |
|
| 19 |
def rand(): |
| 20 |
return randint(50) - randint(50) + random() |
| 21 |
|
| 22 |
def assertAlmostEqual(x, y): |
| 23 |
if type(x) == type(y) and isinstance(x, (tuple, list)): |
| 24 |
for x_elt, y_elt in zip(x, y): |
| 25 |
assertAlmostEqual(x_elt, y_elt) |
| 26 |
else: |
| 27 |
TEST = abs(y - x) < EPSILON |
| 28 |
if not TEST: |
| 29 |
print(''' |
| 30 |
-------------- |
| 31 |
*** FAIL *** |
| 32 |
-> Output: |
| 33 |
%s |
| 34 |
-> Expected: |
| 35 |
%s |
| 36 |
-------------- |
| 37 |
''' %(repr(x), repr(y))) |
| 38 |
assert TEST |
| 39 |
|
| 40 |
def assertNotAlmostEqual(x, y): |
| 41 |
|
| 42 |
TEST = abs(y - x) > EPSILON |
| 43 |
if not TEST: |
| 44 |
print(x, "==", y) |
| 45 |
assert TEST |
| 46 |
|
| 47 |
def _yellow(s): |
| 48 |
return '\033[0;33m' + s + '\033[0m' |
| 49 |
|
| 50 |
def assertEqual(x, y, additionnal_msg=None, _raise=True): |
| 51 |
if x != y: |
| 52 |
rx = repr(x) |
| 53 |
ry = repr(y) |
| 54 |
if isinstance(x, str) and isinstance(y, str): |
| 55 |
for i in range(min(len(rx), len(ry))): |
| 56 |
if rx[i] != ry[i]: |
| 57 |
break |
| 58 |
ry = ry[:i] + _yellow(ry[i:]) |
| 59 |
|
| 60 |
print(''' |
| 61 |
-------------- |
| 62 |
*** FAIL *** |
| 63 |
-> Output: |
| 64 |
%s |
| 65 |
-> Expected: |
| 66 |
%s |
| 67 |
--------------''' %(rx, ry)) |
| 68 |
if additionnal_msg is not None: |
| 69 |
print(additionnal_msg) |
| 70 |
if _raise: |
| 71 |
assert False |
| 72 |
return False |
| 73 |
return True |
| 74 |
|
| 75 |
assertEq = assertEqual |
| 76 |
|
| 77 |
def assertEqualAny(result, list_of_acceptable_results, _raise=True): |
| 78 |
l = list_of_acceptable_results |
| 79 |
if all((result != r) for r in l): |
| 80 |
msg = ('Other accepted results: %s' % l[1:] if len(l) > 1 else None) |
| 81 |
return assertEqual(result, l[0], additionnal_msg=msg, _raise=_raise) |
| 82 |
return True |
| 83 |
|
| 84 |
def assertRaises(error, f, *args, **kw): |
| 85 |
try: |
| 86 |
f(*args, **kw) |
| 87 |
except Exception as e: |
| 88 |
assert type(e) == error |
| 89 |
else: |
| 90 |
raise AssertionError('%s should be raised.' %type(e).__name__) |