def test_properties(self): self.assertEqual(vec2(1, 2).x, 1) self.assertEqual(vec2(1, 2).y, 2) a = vec2() a.x = 5 self.assertEqual((5, 0), a) a.y = 6 self.assertEqual((5, 6), a)
def test_setitem(self): a = vec2(0) a[0] = 1 self.assertEqual(vec2(1, 0), a) a[1] = 2 self.assertEqual(vec2(1, 2), a) with self.assertRaises(IndexError): a[3] = 1
def test_round(self): self.assertEqual((0, 1), vec2(0.5, 0.51).round()) self.assertEqual((0, -1), vec2(-0.5, -0.51).round()) self.assertEqual((0.5, 0.5), vec2(0.5, 0.51).round(1)) self.assertEqual((-0.5, -0.5), vec2(-0.5, -0.51).round(1)) self.assertEqual((0.12346, 0.12341), vec2(0.123456, 0.1234123456789).round(5)) self.assertEqual((0, 1), vec2(0.5, 0.51).rounded()) self.assertEqual((0, -1), vec2(-0.5, -0.51).rounded())
def test_add(self): self.assertEqual(vec2(3), vec2(1) + 2) self.assertEqual(vec2(3), vec2(1) + vec2(2)) self.assertEqual(vec2(3, 5), vec2(1, 2) + vec2(2, 3)) self.assertEqual(vec2(3), vec2(1) + vec2(2)) self.assertEqual(vec2(2, 3), vec2(1) + [1, 2]) self.assertEqual(vec2(2, 3), vec2(1) + ["1", "2"]) self.assertEqual(vec2(3), 2 + vec2(1)) self.assertEqual(vec2(3), "2" + vec2(1)) self.assertEqual(vec2(2, 3), [1, 2] + vec2(1)) self.assertEqual(vec2(2, 3), ["1", "2"] + vec2(1)) with self.assertRaises(TypeError): vec2() + [1] a = vec2(1) a += 1 self.assertEqual(vec2(2), a) a += [1, 2] self.assertEqual(vec2(3, 4), a)
def test_floor(self): self.assertEqual(vec2(1, 2), vec2(1.4, 2.5).floor()) self.assertEqual(vec2(-2, -3), vec2(-1.4, -2.5).floor()) self.assertEqual(vec2(1, 2), vec2(1.4, 2.5).floored()) self.assertEqual(vec2(-2, -3), vec2(-1.4, -2.5).floored())
def test_abs(self): self.assertEqual(vec2(1, 2), abs(vec2(-1, -2))) self.assertEqual(vec2(1, 2), abs(vec2(1, -2)))
def test_iter(self): self.assertEqual([1, 2], [x for x in vec2(1, 2)])
def test_getitem(self): a = vec2(1, 2) self.assertEqual(1, a[0]) self.assertEqual(2, a[1]) with self.assertRaises(IndexError): b = a[3]
def test_equal(self): self.assertTrue(vec2(1) == vec2(1)) self.assertFalse(vec2(1) == vec2(2)) self.assertTrue(vec2(1) == (1, 1)) self.assertFalse(vec2(1) == (1, 1, 1))
def test_dot(self): self.assertEqual(2 * 4 + 3 * 5, vec2(2, 3).dot((4, 5))) with self.assertRaises(TypeError): vec2().dot((1, 2, 3))
def test_rotated(self): self.assertEqual(vec2(-2, 1), vec2(1, 2).rotated_z(90).rounded()) self.assertEqual(vec2(3, 2), vec2(2, -3).rotated_z(90).rounded())
def test_mod(self): self.assertEqual(vec2(1, 0), vec2(1, 2) % 2) self.assertEqual(vec2(1, .5), vec2(1, 3) % 2.5)
def test_div(self): self.assertEqual(vec2(1.5), vec2(3) / vec2(2)) self.assertEqual(vec2(1.5), vec2(3) / 2) self.assertEqual(vec2(2, 1), vec2(2) / (1, 2)) self.assertEqual(vec2(2, 1), vec2(2) / vec2((1, 2))) self.assertEqual(vec2(1.5), 3 / vec2(2)) self.assertEqual(vec2(.5, 1), (1, 2) / vec2(2)) a = vec2(8) a /= 2 self.assertEqual(vec2(4), a) a /= [1, 2] self.assertEqual(vec2((4, 2)), a)
def test_mul(self): self.assertEqual(vec2(6), vec2(2) * vec2(3)) self.assertEqual(vec2(6), vec2(2) * 3) self.assertEqual(vec2(2, 4), vec2(2) * (1, 2)) self.assertEqual(vec2(2, 4), vec2(2) * vec2(1, 2)) self.assertEqual(vec2(6), 2 * vec2(3)) self.assertEqual(vec2(3, 6), (1, 2) * vec2(3)) a = vec2(1) a *= 2 self.assertEqual(vec2(2), a) a *= [1, 2] self.assertEqual(vec2(2, 4), a)
def test_sub(self): self.assertEqual(vec2(1), vec2(3) - vec2(2)) self.assertEqual(vec2(1), vec2(3) - 2) self.assertEqual(vec2(2, 1), vec2(3) - (1, 2)) self.assertEqual(vec2(1), 3 - vec2(2)) self.assertEqual(vec2(-1, 0), (1, 2) - vec2(2)) a = vec2(1) a -= 2 self.assertEqual(vec2(-1), a) a -= [1, 2] self.assertEqual(vec2((-2, -3)), a)
def test_assignment(self): self.assertEqual( "vec2(0, 0)", str(vec2()), ) self.assertEqual("vec2(1, 1)", str(vec2(1))) self.assertEqual("vec2(5, 5)", str(vec2("5"))) self.assertEqual("vec2(1, 2)", str(vec2(1, 2))) self.assertEqual("vec2(1, 2)", str(vec2("1", "2"))) self.assertEqual("vec2(1, 0)", str(vec2((1, )))) self.assertEqual("vec2(1, 2)", str(vec2((1, 2)))) self.assertEqual("vec2(1, 2)", str(vec2((1, 2)))) self.assertEqual("vec2(1, 2)", str(vec2(1, (2, )))) self.assertEqual("vec2(1, 2)", str(vec2(("1", "2")))) self.assertEqual("vec2(1, 0)", str(vec2([1]))) self.assertEqual("vec2(1, 2)", str(vec2([1, 2]))) with self.assertRaises(ValueError): vec2(1, 2, 3) with self.assertRaises(ValueError): vec2((1, 2, 3, 4)) with self.assertRaises(TypeError): vec2("bla") with self.assertRaises(TypeError): vec2({"x": 23})