|
| 1 | +"""This class contains the TestPoint class.""" |
| 2 | +import unittest |
| 3 | +from src.Point import Point |
| 4 | + |
| 5 | + |
| 6 | +class TestPoint(unittest.TestCase): |
| 7 | + """This class unit tests the Point class.""" |
| 8 | + |
| 9 | + def setUp(self): |
| 10 | + """Define two points for use in this test case.""" |
| 11 | + self.point1 = Point(2, 3) |
| 12 | + self.point2 = Point(6, 12) |
| 13 | + |
| 14 | + def test_x(self): |
| 15 | + """Test using the 'x' property.""" |
| 16 | + self.assertEqual(self.point1.x, 2) |
| 17 | + self.assertEqual(self.point2.x, 6) |
| 18 | + |
| 19 | + def test_y(self): |
| 20 | + """Test using the 'y' property.""" |
| 21 | + self.assertEqual(self.point1.y, 3) |
| 22 | + self.assertEqual(self.point2.y, 12) |
| 23 | + |
| 24 | + def test_add(self): |
| 25 | + """Test adding two Point objects.""" |
| 26 | + add_result = self.point1 + self.point2 |
| 27 | + self.assertTrue(add_result.is_equal_to(Point(8, 15))) |
| 28 | + |
| 29 | + def test_multiply(self): |
| 30 | + """Test multiplying two Point objects.""" |
| 31 | + multiply_result = self.point1 * self.point2 |
| 32 | + self.assertTrue(multiply_result.is_equal_to(Point(12, 36))) |
| 33 | + |
| 34 | + def test_divide(self): |
| 35 | + """Test dividing two Point objects.""" |
| 36 | + divide_result = self.point2 / self.point1 |
| 37 | + self.assertTrue(divide_result.is_equal_to(Point(3, 4))) |
| 38 | + |
| 39 | + def test_subtract(self): |
| 40 | + """Test subtracting two Point objects.""" |
| 41 | + subtract_result = self.point2 - self.point1 |
| 42 | + self.assertTrue(subtract_result.is_equal_to(Point(4, 9))) |
| 43 | + |
| 44 | + def test_scale(self): |
| 45 | + """Test scaling a Point object.""" |
| 46 | + scale_result = self.point1.scale(2) |
| 47 | + self.assertTrue(scale_result.is_equal_to(Point(4, 6))) |
| 48 | + |
| 49 | + def test_copy(self): |
| 50 | + """Test copying a Point object.""" |
| 51 | + copy_result = self.point1.copy() |
| 52 | + |
| 53 | + # Check the copied Point object is equal to the original but is not |
| 54 | + # the original object. |
| 55 | + self.assertTrue(copy_result.is_equal_to(self.point1)) |
| 56 | + self.assertTrue(copy_result is not self.point1) |
| 57 | + |
| 58 | + def test_is_equal(self): |
| 59 | + """Test the Point is_equal_to method.""" |
| 60 | + self.assertTrue(self.point1.is_equal_to(Point(2, 3))) |
| 61 | + |
| 62 | + |
| 63 | +if __name__ == "__main__": |
| 64 | + unittest.main() |
0 commit comments