test_point.py 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import pytest
  2. from plib import Point, PointError
  3. @pytest.fixture
  4. def points():
  5. return Point(1, 2), Point(2, 2)
  6. class TestPoint:
  7. def test_creation(self):
  8. p = Point(1, 2)
  9. assert p.x == 1 and p.y == 2
  10. with pytest.raises(PointError):
  11. Point(1, 1.5)
  12. with pytest.raises(PointError):
  13. Point("a", 10)
  14. def test_addition(self, points):
  15. p1, p2 = points
  16. assert p1 + p2 == Point(3, 4)
  17. def test_subtract(self, points):
  18. p1, p2 = points
  19. assert p1 - p2 == Point(-1, 0)
  20. @pytest.mark.parametrize(
  21. "p1, p2, distance",
  22. [(Point(0, 0), Point(0, 10), 10),
  23. (Point(0, 0), Point(10, 0), 10),
  24. (Point(0, 0), Point(10, 10), 14)]
  25. )
  26. def test_distance_all_axis(self, p1, p2, distance):
  27. assert p1.distance_to(p2) == pytest.approx(distance, 1)
  28. def test_eq_with_other_type(self):
  29. p = Point(0,0)
  30. try:
  31. p == {0,0}
  32. except NotImplementedError:
  33. assert False
  34. assert True