def test_scaling(self): v1 = Vector(1, 2, 3) v2 = Vector(4, 5, 6) v3 = v1 * 4 v4 = 2 * v2 self.assertEqual((v3.x, v3.y, v3.z), (4, 8, 12)) self.assertEqual((v4.x, v4.y, v4.z), (8, 10, 12))
def test_shifting(self): v1 = Vector(1, 2, 3) v2 = Vector(4, 5, 6) v3 = v2 + v1 v4 = v3 - v1 self.assertEqual((v3.x, v3.y, v3.z), (5, 7, 9)) self.assertEqual((v4.x, v4.y, v4.z), (v2.x, v2.y, v2.z))
def test_magnitude_property(self): v = Vector(2, 3, 6) self.assertEqual(v.magnitude, 7.0) try: v.y = 9 except AttributeError: v = Vector(2, 9, 6) self.assertEqual(v.magnitude, 11.0)
def test_equality_and_inequality(self): self.assertNotEqual(Vector(1, 2, 3), Vector(1, 2, 4)) self.assertEqual(Vector(1, 2, 3), Vector(1, 2, 3)) self.assertFalse(Vector(1, 2, 3) != Vector(1, 2, 3)) v1 = Vector(1, 2, 3) v2 = Vector(1, 2, 4) v3 = Vector(1, 2, 3) self.assertNotEqual(v1, v2) self.assertEqual(v1, v3)
def test_no_weird_extras(self): v1 = Vector(1, 2, 3) v2 = Vector(4, 5, 6) with self.assertRaises(TypeError): len(v1) with self.assertRaises(TypeError): v1 < v2 with self.assertRaises(TypeError): v1 > v2 with self.assertRaises(TypeError): v1 <= v2 with self.assertRaises(TypeError): v1 >= v2 with self.assertRaises(TypeError): v1 + (1, 2, 3) with self.assertRaises(TypeError): (1, 2, 3) + v1 with self.assertRaises(TypeError): v1 - (1, 2, 3) with self.assertRaises(TypeError): v1 * 'a' with self.assertRaises(TypeError): v1 / v2
def test_attributes(self): v = Vector(1, 2, 3) self.assertEqual((v.x, v.y, v.z), (1, 2, 3))
def test_immutability(self): v1 = Vector(1, 2, 3) with self.assertRaises(Exception): v1.x = 4 self.assertEqual(v1.x, 1)
def test_multiple_assignment(self): x, y, z = Vector(x=1, y=2, z=3) self.assertEqual((x, y, z), (1, 2, 3))