test_point.py 1016 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. p = Point(1.5, 1.5)
  11. assert p.x == 1.5 and p.y == 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. with pytest.raises(NotImplementedError):
  31. p == {0, 0}