Update drop_dims, now works with more than 2 dims passed
[linpy.git] / pypol / tests / test_linexprs.py
index 1606ea0..bc062b6 100644 (file)
@@ -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):
@@ -145,6 +131,18 @@ class TestExpression(unittest.TestCase):
         self.assertEqual((self.x + self.y/2 + self.z/3)._toint(),
                 6*self.x + 3*self.y + 2*self.z)
 
+    def test_subs(self):
+        self.assertEqual(self.x.subs('x', 3), 3)
+        self.assertEqual(self.x.subs('x', self.x), self.x)
+        self.assertEqual(self.x.subs('x', self.y), self.y)
+        self.assertEqual(self.x.subs('x', self.x + self.y), self.x + self.y)
+        self.assertEqual(self.x.subs('y', 3), self.x)
+        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.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)
         self.assertEqual(Expression.fromstring('-x'), -self.x)
@@ -167,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)
@@ -176,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))
@@ -213,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):
@@ -262,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'))