def __init__(self, normal, offset): super().__init__() normal = Vector.convert(normal, "normal", required_length=3) if normal.is_zero: raise ValueError("Normal vector is zero") offset = number.convert(offset, "offset") self._normal = normal self._offset = offset
def test_convert(self): # Regular call without length check self.assertEqual(Vector.convert(Vector(1, 2, 3), "dummy"), Vector(1, 2, 3)) # From Vector self.assertEqual(Vector.convert([1, 2, 3], "dummy"), Vector(1, 2, 3)) # From list self.assertEqual(Vector.convert((1, 2, 3), "dummy"), Vector(1, 2, 3)) # From tuple # Calling with a vector does not construct a new vector v = Vector(1, 2, 3) self.assertIs(Vector.convert(v, "dummy"), v) # Empty self.assertEqual(Vector.convert([], "dummy"), Vector()) # Length check self.assertEqual(Vector.convert([1, 2, 3], "dummy", required_length=3), Vector(1, 2, 3)) # Success with self.assertRaises(ValueError): Vector.convert([1, 2, 3], "dummy", required_length=4) # Failure # Invalid values with self.assertRaises(TypeError): Vector.convert(None, "dummy", None) with self.assertRaises(TypeError): Vector.convert(1, "dummy", None) with self.assertRaises(TypeError): Vector.convert("", "dummy", None) with self.assertRaises(TypeError): Vector.convert("123", "dummy", None) with self.assertRaises(TypeError): Vector.convert([1, 2, "3"], "dummy", None)