Skip to content

Commit

Permalink
#28 Implement Point.isBefore from Kolasu. Point and position comparis…
Browse files Browse the repository at this point in the history
…on 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.
  • Loading branch information
alessiostalla committed Oct 24, 2023
1 parent 8d2048f commit ae3e333
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pylasu/model/position.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ def __post_init__(self):
if self.column < 0:
raise Exception(f"Column {self.column} cannot be less than 0")

def is_before(self, other: "Point"):
return self < other

def __add__(self, other):
if isinstance(other, str):
if len(other) == 0:
Expand Down
Empty file added tests/model/__init__.py
Empty file.
File renamed without changes.
71 changes: 71 additions & 0 deletions tests/model/test_position.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import unittest

from pylasu.model import Point

START_LINE = 1
START_COLUMN = 0
START_POINT = Point(START_LINE, START_COLUMN)


class PositionTest(unittest.TestCase):
def test_point_compare(self):
p0 = START_POINT
p1 = Point(1, 1)
p2 = Point(1, 100)
p3 = Point(2, 90)

self.assertFalse(p0 < p0)
self.assertTrue(p0 <= p0)
self.assertTrue(p0 >= p0)
self.assertFalse(p0 > p0)

self.assertTrue(p0 < p1)
self.assertTrue(p0 <= p1)
self.assertFalse(p0 >= p1)
self.assertFalse(p0 > p1)

self.assertTrue(p0 < p2)
self.assertTrue(p0 <= p2)
self.assertFalse(p0 >= p2)
self.assertFalse(p0 > p2)

self.assertTrue(p0 < p3)
self.assertTrue(p0 <= p3)
self.assertFalse(p0 >= p3)
self.assertFalse(p0 > p3)

self.assertTrue(p1 < p2)
self.assertTrue(p1 <= p2)
self.assertFalse(p1 >= p2)
self.assertFalse(p1 > p2)

self.assertTrue(p1 < p3)
self.assertTrue(p1 <= p3)
self.assertFalse(p1 >= p3)
self.assertFalse(p1 > p3)

def test_is_before(self):
p0 = START_POINT
p1 = Point(1, 1)
p2 = Point(1, 100)
p3 = Point(2, 90)

self.assertFalse(p0.is_before(p0))
self.assertTrue(p0.is_before(p1))
self.assertTrue(p0.is_before(p2))
self.assertTrue(p0.is_before(p3))

self.assertFalse(p1.is_before(p0))
self.assertFalse(p1.is_before(p1))
self.assertTrue(p1.is_before(p2))
self.assertTrue(p1.is_before(p3))

self.assertFalse(p2.is_before(p0))
self.assertFalse(p2.is_before(p1))
self.assertFalse(p2.is_before(p2))
self.assertTrue(p2.is_before(p3))

self.assertFalse(p3.is_before(p0))
self.assertFalse(p3.is_before(p1))
self.assertFalse(p3.is_before(p2))
self.assertFalse(p3.is_before(p3))

0 comments on commit ae3e333

Please sign in to comment.