From: Vivien Maisonneuve Date: Wed, 2 Jul 2014 07:24:23 +0000 (+0200) Subject: Helper module for sympy in unitary tests X-Git-Tag: 1.0~169 X-Git-Url: https://scm.cri.mines-paristech.fr/git/linpy.git/commitdiff_plain/e27fd6a18fd36b8ec81f37cd800399703bf7ef4f?hp=b7d06b55d601833b4bef47afd732334ed77587fb Helper module for sympy in unitary tests --- diff --git a/pypol/tests/libhelper.py b/pypol/tests/libhelper.py new file mode 100644 index 0000000..e75b4fb --- /dev/null +++ b/pypol/tests/libhelper.py @@ -0,0 +1,20 @@ +import functools +import unittest + +try: + + import sympy + + def requires_sympy(func): + @functools.wraps(func) + def wrapper(self): + return func(self) + return wrapper + +except ImportError: + + def requires_sympy(func): + @functools.wraps(func) + def wrapper(self): + raise unittest.SkipTest('SymPy is not available') + return wrapper diff --git a/pypol/tests/test_linexprs.py b/pypol/tests/test_linexprs.py index a3e81b0..bc062b6 100644 --- a/pypol/tests/test_linexprs.py +++ b/pypol/tests/test_linexprs.py @@ -4,21 +4,7 @@ import unittest from fractions import Fraction from ..linexprs import * - - -try: - import sympy - def _requires_sympy(func): - @functools.wraps(func) - def wrapper(self): - return func(self) - return wrapper -except ImportError: - def _requires_sympy(func): - @functools.wraps(func) - def wrapper(self): - raise unittest.SkipTest('SymPy is not available') - return wrapper +from .libhelper import requires_sympy class TestExpression(unittest.TestCase): @@ -154,8 +140,8 @@ class TestExpression(unittest.TestCase): self.assertEqual(self.pi.subs('x', 3), self.pi) self.assertEqual(self.expr.subs('x', -3), -2 * self.y) self.assertEqual(self.expr.subs([('x', self.y), ('y', self.x)]), 3 - self.x) - self.assertEqual(self.expr.subs({'x': self.y, 'y': self.x}), 3 - self.x) - self.assertEqual(self.expr.subs({self.x: self.z, self.y: self.z}), -self.z + 3) + self.assertEqual(self.expr.subs({'x': self.z, 'y': self.z}), 3 - self.z) + self.assertEqual(self.expr.subs({self.x: self.z, self.y: self.z}), 3 - self.z) def test_fromstring(self): self.assertEqual(Expression.fromstring('x'), self.x) @@ -179,8 +165,9 @@ class TestExpression(unittest.TestCase): self.assertEqual(repr(self.x + self.one), "Expression('x + 1')") self.assertEqual(repr(self.expr), "Expression('x - 2*y + 3')") - @_requires_sympy + @requires_sympy def test_fromsympy(self): + import sympy sp_x, sp_y = sympy.symbols('x y') self.assertEqual(Expression.fromsympy(sp_x), self.x) self.assertEqual(Expression.fromsympy(sympy.Rational(22, 7)), self.pi) @@ -188,8 +175,9 @@ class TestExpression(unittest.TestCase): with self.assertRaises(ValueError): Expression.fromsympy(sp_x*sp_y) - @_requires_sympy + @requires_sympy def test_tosympy(self): + import sympy sp_x, sp_y = sympy.symbols('x y') self.assertEqual(self.x.tosympy(), sp_x) self.assertEqual(self.pi.tosympy(), sympy.Rational(22, 7)) @@ -225,8 +213,9 @@ class TestSymbol(unittest.TestCase): def test_repr(self): self.assertEqual(repr(self.x), "Symbol('x')") - @_requires_sympy + @requires_sympy def test_fromsympy(self): + import sympy sp_x = sympy.Symbol('x') self.assertEqual(Symbol.fromsympy(sp_x), self.x) with self.assertRaises(TypeError): @@ -274,8 +263,9 @@ class TestConstant(unittest.TestCase): self.assertEqual(repr(self.one), 'Constant(1)') self.assertEqual(repr(self.pi), 'Constant(22, 7)') - @_requires_sympy + @requires_sympy def test_fromsympy(self): + import sympy self.assertEqual(Constant.fromsympy(sympy.Rational(22, 7)), self.pi) with self.assertRaises(TypeError): Constant.fromsympy(sympy.Symbol('x')) diff --git a/pypol/tests/test_polyhedra.py b/pypol/tests/test_polyhedra.py index c74e25f..c7a58a4 100644 --- a/pypol/tests/test_polyhedra.py +++ b/pypol/tests/test_polyhedra.py @@ -3,21 +3,7 @@ import unittest from ..linexprs import symbols from ..polyhedra import * - - -try: - import sympy - def _requires_sympy(func): - @functools.wraps(func) - def wrapper(self): - return func(self) - return wrapper -except ImportError: - def _requires_sympy(func): - @functools.wraps(func) - def wrapper(self): - raise unittest.SkipTest('SymPy is not available') - return wrapper +from .libhelper import requires_sympy class TestPolyhedron(unittest.TestCase): @@ -50,14 +36,16 @@ class TestPolyhedron(unittest.TestCase): def test_isuniverse(self): self.assertFalse(self.square.isuniverse()) - @_requires_sympy + @requires_sympy def test_fromsympy(self): + import sympy sp_x, sp_y = sympy.symbols('x y') self.assertEqual(Polyhedron.fromsympy((sp_x >= 0) & (sp_x <= 1) & (sp_y >= 0) & (sp_y <= 1)), self.square) - @_requires_sympy + @requires_sympy def test_tosympy(self): + import sympy sp_x, sp_y = sympy.symbols('x y') self.assertEqual(self.square.tosympy(), sympy.And(-sp_x + 1 >= 0, -sp_y + 1 >= 0, sp_x >= 0, sp_y >= 0))