Skip to content

Commit ae3e333

Browse files
committed
#28 Implement Point.isBefore from Kolasu. Point and position comparison was already implemented using dataclasses. Note that Position comparisons don't consider the source field, but neither does Position is Kolasu 1.5.
Add Point comparison tests as in Kolasu.
1 parent 8d2048f commit ae3e333

4 files changed

Lines changed: 74 additions & 0 deletions

File tree

pylasu/model/position.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ def __post_init__(self):
1313
if self.column < 0:
1414
raise Exception(f"Column {self.column} cannot be less than 0")
1515

16+
def is_before(self, other: "Point"):
17+
return self < other
18+
1619
def __add__(self, other):
1720
if isinstance(other, str):
1821
if len(other) == 0:

tests/model/__init__.py

Whitespace-only changes.

tests/model/test_position.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import unittest
2+
3+
from pylasu.model import Point
4+
5+
START_LINE = 1
6+
START_COLUMN = 0
7+
START_POINT = Point(START_LINE, START_COLUMN)
8+
9+
10+
class PositionTest(unittest.TestCase):
11+
def test_point_compare(self):
12+
p0 = START_POINT
13+
p1 = Point(1, 1)
14+
p2 = Point(1, 100)
15+
p3 = Point(2, 90)
16+
17+
self.assertFalse(p0 < p0)
18+
self.assertTrue(p0 <= p0)
19+
self.assertTrue(p0 >= p0)
20+
self.assertFalse(p0 > p0)
21+
22+
self.assertTrue(p0 < p1)
23+
self.assertTrue(p0 <= p1)
24+
self.assertFalse(p0 >= p1)
25+
self.assertFalse(p0 > p1)
26+
27+
self.assertTrue(p0 < p2)
28+
self.assertTrue(p0 <= p2)
29+
self.assertFalse(p0 >= p2)
30+
self.assertFalse(p0 > p2)
31+
32+
self.assertTrue(p0 < p3)
33+
self.assertTrue(p0 <= p3)
34+
self.assertFalse(p0 >= p3)
35+
self.assertFalse(p0 > p3)
36+
37+
self.assertTrue(p1 < p2)
38+
self.assertTrue(p1 <= p2)
39+
self.assertFalse(p1 >= p2)
40+
self.assertFalse(p1 > p2)
41+
42+
self.assertTrue(p1 < p3)
43+
self.assertTrue(p1 <= p3)
44+
self.assertFalse(p1 >= p3)
45+
self.assertFalse(p1 > p3)
46+
47+
def test_is_before(self):
48+
p0 = START_POINT
49+
p1 = Point(1, 1)
50+
p2 = Point(1, 100)
51+
p3 = Point(2, 90)
52+
53+
self.assertFalse(p0.is_before(p0))
54+
self.assertTrue(p0.is_before(p1))
55+
self.assertTrue(p0.is_before(p2))
56+
self.assertTrue(p0.is_before(p3))
57+
58+
self.assertFalse(p1.is_before(p0))
59+
self.assertFalse(p1.is_before(p1))
60+
self.assertTrue(p1.is_before(p2))
61+
self.assertTrue(p1.is_before(p3))
62+
63+
self.assertFalse(p2.is_before(p0))
64+
self.assertFalse(p2.is_before(p1))
65+
self.assertFalse(p2.is_before(p2))
66+
self.assertTrue(p2.is_before(p3))
67+
68+
self.assertFalse(p3.is_before(p0))
69+
self.assertFalse(p3.is_before(p1))
70+
self.assertFalse(p3.is_before(p2))
71+
self.assertFalse(p3.is_before(p3))

0 commit comments

Comments
 (0)