Add unitary tests for Point class
authorVivien Maisonneuve <v.maisonneuve@gmail.com>
Wed, 20 Aug 2014 14:02:01 +0000 (16:02 +0200)
committerVivien Maisonneuve <v.maisonneuve@gmail.com>
Sat, 23 Aug 2014 07:06:32 +0000 (09:06 +0200)
linpy/geometry.py
linpy/tests/test_geometry.py

index cc19a72..0b05493 100644 (file)
@@ -142,6 +142,14 @@ class Coordinates:
         """
         return any(self._coordinates.values())
 
         """
         return any(self._coordinates.values())
 
+    def __eq__(self, other):
+        """
+        Return True if two coordinate systems are equal.
+        """
+        if isinstance(other, self.__class__):
+            return self._coordinates == other._coordinates
+        return NotImplemented
+
     def __hash__(self):
         return hash(tuple(self.coordinates()))
 
     def __hash__(self):
         return hash(tuple(self.coordinates()))
 
@@ -206,14 +214,6 @@ class Point(Coordinates, GeometricObject):
             return Point(coordinates)
         return NotImplemented
 
             return Point(coordinates)
         return NotImplemented
 
-    def __eq__(self, other):
-        """
-        Test whether two points are equal.
-        """
-        if isinstance(other, Point):
-            return self._coordinates == other._coordinates
-        return NotImplemented
-
     def aspolyhedron(self):
         from .polyhedra import Polyhedron
         equalities = []
     def aspolyhedron(self):
         from .polyhedra import Polyhedron
         equalities = []
@@ -301,14 +301,6 @@ class Vector(Coordinates):
             return Vector(coordinates)
         return NotImplemented
 
             return Vector(coordinates)
         return NotImplemented
 
-    def __eq__(self, other):
-        """
-        Test whether two vectors are equal.
-        """
-        if isinstance(other, Vector):
-            return self._coordinates == other._coordinates
-        return NotImplemented
-
     def angle(self, other):
         """
         Retrieve the angle required to rotate the vector into the vector passed
     def angle(self, other):
         """
         Retrieve the angle required to rotate the vector into the vector passed
index 2f4db2b..0f3884c 100644 (file)
@@ -29,26 +29,71 @@ class TestPoint(unittest.TestCase):
         self.x = Symbol('x')
         self.y = Symbol('y')
         self.z = Symbol('z')
         self.x = Symbol('x')
         self.y = Symbol('y')
         self.z = Symbol('z')
+        self.t = Symbol('t')
         self.pt1 = Point({self.x: 10, self.y: 5, self.z: 1})
         self.pt2 = Point({self.x: 15, self.y: 40, self.z: 60})
         self.vec1 = Vector({self.x: 20, self.y: 30, self.z: 40})
 
         self.pt1 = Point({self.x: 10, self.y: 5, self.z: 1})
         self.pt2 = Point({self.x: 15, self.y: 40, self.z: 60})
         self.vec1 = Vector({self.x: 20, self.y: 30, self.z: 40})
 
+    def test_new(self):
+        self.assertEqual(Point({self.x: 10, self.y: 5, self.z: 1}), self.pt1)
+        self.assertEqual(Point([(self.x, 10), (self.y, 5), (self.z, 1)]),
+            self.pt1)
+
+    def test_symbols(self):
+        self.assertTupleEqual(self.pt1.symbols, (self.x, self.y, self.z))
+
+    def test_dimension(self):
+        self.assertEqual(self.pt1.dimension, 3)
+
+    def test_coordinate(self):
+        self.assertEqual(self.pt1.coordinate(self.x), 10)
+        with self.assertRaises(KeyError):
+            self.pt1.coordinate(self.t)
+
+    def test_getitem(self):
+        self.assertEqual(self.pt1[self.x], 10)
+        with self.assertRaises(KeyError):
+            self.pt1[self.t]
+
+    def test_coordinates(self):
+        self.assertListEqual(list(self.pt1.coordinates()),
+            [(self.x, 10), (self.y, 5), (self.z, 1)])
+
+    def test_values(self):
+        self.assertListEqual(list(self.pt1.values()),
+            [10, 5, 1])
+
+    def test_isorigin(self):
+        self.assertFalse(self.pt1.isorigin())
+        self.assertTrue(Point({}).isorigin())
+
+    def test_bool(self):
+        self.assertTrue(self.pt1)
+        self.assertFalse(Point({}))
+
     def test_add(self):
     def test_add(self):
-        self.assertEqual(self.pt1 + self.vec1, Point({self.x: 30, self.y: 35, self.z: 41}))
+        self.assertEqual(self.pt1 + self.vec1,
+            Point({self.x: 30, self.y: 35, self.z: 41}))
         with self.assertRaises(TypeError):
             self.pt1 + self.pt2
 
         with self.assertRaises(TypeError):
             self.pt1 + self.pt2
 
+    def test_sub(self):
+        self.assertEqual(self.pt1 - self.pt2,
+            Vector({self.x: -5, self.y: -35, self.z: -59}))
+        self.assertEqual(self.pt1 - self.vec1,
+            Point({self.x: -10, self.y: -25, self.z: -39}))
+
     def test_eq(self):
         self.assertEqual(self.pt1, self.pt1)
         self.assertNotEqual(self.pt1, self.pt2)
         self.assertNotEqual(self.pt1, self.vec1)
 
     def test_eq(self):
         self.assertEqual(self.pt1, self.pt1)
         self.assertNotEqual(self.pt1, self.pt2)
         self.assertNotEqual(self.pt1, self.vec1)
 
-    def test_sub(self):
-        self.assertEqual(self.pt1 - self.pt2, Vector({self.x: -5, self.y: -35, self.z: -59}))
-        self.assertEqual(self.pt1 - self.vec1, Point({self.x: -10, self.y: -25, self.z: -39}))
-
     def test_aspolyhedron(self):
     def test_aspolyhedron(self):
-        self.assertEqual(self.pt1.aspolyhedron(), Eq(self.x, 10) & Eq(self.y, 5) & Eq(self.z, 1))
+        self.assertEqual(self.pt1.aspolyhedron(),
+            Eq(self.x, 10) & Eq(self.y, 5) & Eq(self.z, 1))
+
+    def test_repr(self):
+        self.assertEqual(repr(self.pt1), 'Point({x: 10, y: 5, z: 1})')
 
 
 class TestVector(unittest.TestCase):
 
 
 class TestVector(unittest.TestCase):