Fix error message
[linpy.git] / linpy / tests / test_linexprs.py
index 16b9cde..0c50e6c 100644 (file)
 # You should have received a copy of the GNU General Public License
 # along with LinPy.  If not, see <http://www.gnu.org/licenses/>.
 
 # You should have received a copy of the GNU General Public License
 # along with LinPy.  If not, see <http://www.gnu.org/licenses/>.
 
-import functools
 import unittest
 
 from fractions import Fraction
 
 import unittest
 
 from fractions import Fraction
 
-from ..linexprs import *
+from ..linexprs import Dummy, LinExpr, Rational, Symbol, symbols
 from .libhelper import requires_sympy
 
 
 from .libhelper import requires_sympy
 
 
-class TestExpression(unittest.TestCase):
+class TestLinExpr(unittest.TestCase):
 
     def setUp(self):
         self.x = Symbol('x')
         self.y = Symbol('y')
         self.z = Symbol('z')
 
     def setUp(self):
         self.x = Symbol('x')
         self.y = Symbol('y')
         self.z = Symbol('z')
-        self.zero = Expression(constant=0)
-        self.one = Expression(constant=1)
-        self.pi = Expression(constant=Fraction(22, 7))
+        self.zero = LinExpr(constant=0)
+        self.one = LinExpr(constant=1)
+        self.pi = LinExpr(constant=Fraction(22, 7))
         self.expr = self.x - 2*self.y + 3
 
     def test_new(self):
         self.expr = self.x - 2*self.y + 3
 
     def test_new(self):
-        self.assertIsInstance(Expression(coefficients={self.x: 1}), Symbol)
-        self.assertIsInstance(Expression(constant=self.pi), Rational)
+        self.assertIsInstance(LinExpr(coefficients={self.x: 1}), Symbol)
+        self.assertIsInstance(LinExpr(constant=self.pi), Rational)
         self.assertNotIsInstance(self.x + self.pi, Symbol)
         self.assertNotIsInstance(self.x + self.pi, Rational)
         self.assertNotIsInstance(self.x + self.pi, Symbol)
         self.assertNotIsInstance(self.x + self.pi, Rational)
-        xx = Expression({self.x: 2})
+        xx = LinExpr({self.x: 2})
         self.assertNotIsInstance(xx, Symbol)
         with self.assertRaises(TypeError):
         self.assertNotIsInstance(xx, Symbol)
         with self.assertRaises(TypeError):
-            Expression('x + y', 2)
+            LinExpr('x + y', 2)
         with self.assertRaises(TypeError):
         with self.assertRaises(TypeError):
-            Expression({0: 2})
+            LinExpr({0: 2})
         with self.assertRaises(TypeError):
         with self.assertRaises(TypeError):
-            Expression({'x': '2'})
-        self.assertEqual(Expression(constant=1), Expression(constant=self.one))
-        self.assertEqual(Expression(constant='1'), Expression(constant=self.one))
+            LinExpr({'x': '2'})
+        self.assertEqual(LinExpr(constant=1), LinExpr(constant=self.one))
+        self.assertEqual(LinExpr(constant='1'), LinExpr(constant=self.one))
         with self.assertRaises(ValueError):
         with self.assertRaises(ValueError):
-            Expression(constant='a')
+            LinExpr(constant='a')
 
     def test_coefficient(self):
         self.assertEqual(self.expr.coefficient(self.x), 1)
 
     def test_coefficient(self):
         self.assertEqual(self.expr.coefficient(self.x), 1)
@@ -76,7 +75,8 @@ class TestExpression(unittest.TestCase):
             self.expr[self.expr]
 
     def test_coefficients(self):
             self.expr[self.expr]
 
     def test_coefficients(self):
-        self.assertListEqual(list(self.expr.coefficients()), [(self.x, 1), (self.y, -2)])
+        self.assertListEqual(list(self.expr.coefficients()),
+                             [(self.x, 1), (self.y, -2)])
 
     def test_constant(self):
         self.assertEqual(self.x.constant, 0)
 
     def test_constant(self):
         self.assertEqual(self.x.constant, 0)
@@ -151,7 +151,7 @@ class TestExpression(unittest.TestCase):
 
     def test_scaleint(self):
         self.assertEqual((self.x + self.y/2 + self.z/3).scaleint(),
 
     def test_scaleint(self):
         self.assertEqual((self.x + self.y/2 + self.z/3).scaleint(),
-                6*self.x + 3*self.y + 2*self.z)
+                         6*self.x + 3*self.y + 2*self.z)
 
     def test_subs(self):
         self.assertEqual(self.x.subs(self.x, 3), 3)
 
     def test_subs(self):
         self.assertEqual(self.x.subs(self.x, 3), 3)
@@ -161,9 +161,12 @@ class TestExpression(unittest.TestCase):
         self.assertEqual(self.x.subs(self.y, 3), self.x)
         self.assertEqual(self.pi.subs(self.x, 3), self.pi)
         self.assertEqual(self.expr.subs(self.x, -3), -2 * self.y)
         self.assertEqual(self.x.subs(self.y, 3), self.x)
         self.assertEqual(self.pi.subs(self.x, 3), self.pi)
         self.assertEqual(self.expr.subs(self.x, -3), -2 * self.y)
-        self.assertEqual(self.expr.subs([(self.x, self.y), (self.y, self.x)]), 3 - self.x)
-        self.assertEqual(self.expr.subs({self.x: self.z, self.y: self.z}), 3 - self.z)
-        self.assertEqual(self.expr.subs({self.x: self.z, self.y: self.z}), 3 - self.z)
+        self.assertEqual(self.expr.subs([(self.x, self.y), (self.y, self.x)]),
+                         -2*self.x + self.y + 3)
+        self.assertEqual(self.expr.subs({self.x: self.z, self.y: self.z}),
+                         3 - self.z)
+        self.assertEqual(self.expr.subs({self.x: self.z, self.y: self.z}),
+                         3 - self.z)
         with self.assertRaises(TypeError):
             self.x.subs('x', 3)
         with self.assertRaises(TypeError):
         with self.assertRaises(TypeError):
             self.x.subs('x', 3)
         with self.assertRaises(TypeError):
@@ -174,15 +177,15 @@ class TestExpression(unittest.TestCase):
             self.expr.subs(self.x, 'x')
 
     def test_fromstring(self):
             self.expr.subs(self.x, 'x')
 
     def test_fromstring(self):
-        self.assertEqual(Expression.fromstring('x'), self.x)
-        self.assertEqual(Expression.fromstring('-x'), -self.x)
-        self.assertEqual(Expression.fromstring('22/7'), self.pi)
-        self.assertEqual(Expression.fromstring('x - 2y + 3'), self.expr)
-        self.assertEqual(Expression.fromstring('x - (3-1)y + 3'), self.expr)
-        self.assertEqual(Expression.fromstring('x - 2*y + 3'), self.expr)
+        self.assertEqual(LinExpr.fromstring('x'), self.x)
+        self.assertEqual(LinExpr.fromstring('-x'), -self.x)
+        self.assertEqual(LinExpr.fromstring('22/7'), self.pi)
+        self.assertEqual(LinExpr.fromstring('x - 2y + 3'), self.expr)
+        self.assertEqual(LinExpr.fromstring('x - (3-1)y + 3'), self.expr)
+        self.assertEqual(LinExpr.fromstring('x - 2*y + 3'), self.expr)
 
     def test_repr(self):
 
     def test_repr(self):
-        self.assertEqual(str(Expression()), '0')
+        self.assertEqual(str(LinExpr()), '0')
         self.assertEqual(str(self.x), 'x')
         self.assertEqual(str(-self.x), '-x')
         self.assertEqual(str(self.pi), '22/7')
         self.assertEqual(str(self.x), 'x')
         self.assertEqual(str(-self.x), '-x')
         self.assertEqual(str(self.pi), '22/7')
@@ -192,11 +195,11 @@ class TestExpression(unittest.TestCase):
     def test_fromsympy(self):
         import sympy
         sp_x, sp_y = sympy.symbols('x y')
     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)
-        self.assertEqual(Expression.fromsympy(sp_x - 2*sp_y + 3), self.expr)
-        with self.assertRaises(ValueError):
-            Expression.fromsympy(sp_x*sp_y)
+        self.assertEqual(LinExpr.fromsympy(sp_x), self.x)
+        self.assertEqual(LinExpr.fromsympy(sympy.Rational(22, 7)), self.pi)
+        self.assertEqual(LinExpr.fromsympy(sp_x - 2*sp_y + 3), self.expr)
+        with self.assertRaises(TypeError):
+            LinExpr.fromsympy(sp_x*sp_y)
 
     @requires_sympy
     def test_tosympy(self):
 
     @requires_sympy
     def test_tosympy(self):
@@ -214,11 +217,20 @@ class TestSymbol(unittest.TestCase):
         self.y = Symbol('y')
 
     def test_new(self):
         self.y = Symbol('y')
 
     def test_new(self):
-        self.assertEqual(Symbol(' x '), self.x)
+        self.assertEqual(Symbol('x'), self.x)
         with self.assertRaises(TypeError):
             Symbol(self.x)
         with self.assertRaises(TypeError):
             Symbol(1)
         with self.assertRaises(TypeError):
             Symbol(self.x)
         with self.assertRaises(TypeError):
             Symbol(1)
+        with self.assertRaises(SyntaxError):
+            Symbol('1')
+        with self.assertRaises(SyntaxError):
+            Symbol('x.1')
+        with self.assertRaises(SyntaxError):
+            Symbol('x 1')
+        Symbol('_')
+        Symbol('_x')
+        Symbol('x_1')
 
     def test_name(self):
         self.assertEqual(self.x.name, 'x')
 
     def test_name(self):
         self.assertEqual(self.x.name, 'x')