| 1 |
|
| 2 |
|
| 3 |
from unittest import TestCase as _TestCase |
| 4 |
from wxgeometrie.mathlib.solvers import resoudre, positif, ensemble_definition |
| 5 |
|
| 6 |
def _yellow(s): |
| 7 |
return '\033[0;33m' + s + '\033[0m' |
| 8 |
|
| 9 |
def _green(s): |
| 10 |
return '\033[0;32m' + s + '\033[0m' |
| 11 |
|
| 12 |
class TestCase(_TestCase): |
| 13 |
|
| 14 |
def __init__(self, *args, **kw): |
| 15 |
_TestCase.__init__(self, *args, **kw) |
| 16 |
|
| 17 |
def assertAlmostEqual(self, x, y, **kw): |
| 18 |
""" |
| 19 |
extension récursive |
| 20 |
""" |
| 21 |
if type(x) == type(y) and isinstance(x, (tuple, list)): |
| 22 |
for x_elt, y_elt in zip(x, y): |
| 23 |
self.assertAlmostEqual(x_elt, y_elt, **kw) |
| 24 |
else: |
| 25 |
_TestCase.assertAlmostEqual(self, x, y, **kw) |
| 26 |
|
| 27 |
|
| 28 |
def assertEqualAny(self,result, list_of_acceptable_results): |
| 29 |
l = list_of_acceptable_results |
| 30 |
if all((result != r) for r in l): |
| 31 |
print('Other accepted results: %s' % l[1:] if len(l) > 1 else None) |
| 32 |
return self.assertEqual(result, l[0]) |
| 33 |
return True |
| 34 |
|
| 35 |
|
| 36 |
def assert_resoudre(self,x, y): |
| 37 |
if not isinstance(y, (list, tuple, set)): |
| 38 |
y = [y] |
| 39 |
if not self.assertEqualAny(str(resoudre(x)), y): |
| 40 |
print(_red('-> Test failed for %s' % repr(x))) |
| 41 |
self.assertFalse(True) |
| 42 |
|
| 43 |
|
| 44 |
def assert_positif(self,x, y): |
| 45 |
self.assertEqual(str(positif(x)), y) |
| 46 |
|
| 47 |
|
| 48 |
def assert_ens_def(self,x, y): |
| 49 |
self.assertEqual(str(ensemble_definition(x)), y) |
| 50 |
|
| 51 |
def assert_tableau(self, func, chaine, attendu, **options): |
| 52 |
resultat = func(chaine, **options) |
| 53 |
if resultat != attendu: |
| 54 |
print("-------") |
| 55 |
for i in range(min(len(resultat), len(attendu))): |
| 56 |
if resultat[i] != attendu[i]: |
| 57 |
break |
| 58 |
resultat = resultat[:i] + _yellow(resultat[i:]) |
| 59 |
attendu = attendu[:i] + _green(attendu[i:]) |
| 60 |
print("ERREUR:") |
| 61 |
print("Expected output was:") |
| 62 |
print(attendu) |
| 63 |
print("While actual result is:") |
| 64 |
print(resultat) |
| 65 |
print("-------") |
| 66 |
self.assertEqual(resultat, attendu) |
| 67 |
|