class TestHandwrittenPath(unittest.TestCase):
    @staticmethod
    def init_path():
        return HandwrittenPath(curves=[
            Curve([Point(1, 1), Point(1, 1),
                   Point(1, 1)]),
            Curve(start=Point(10, 10)),
            Curve([Point(1, 1)])
        ])

    @staticmethod
    def init_absolute_points():
        return [
            Point(*elem) for elem in [(15, 15), (20, 15), (30, 40), (80, 60)]
        ]

    def setUp(self):
        self.path = self.init_path()
        self.curve = self.path[0]
        self.empty_path = HandwrittenPath()
        self.abs_points = self.init_absolute_points()

    def test_create_empty_path(self):
        self.assertEqual(self.empty_path.get_last_point(), Point(0, 0))
        self.assertTrue(self.empty_path.empty())

    def test_set_path_position(self):
        self.path.set_position(Point(10, 10))
        self.assertEqual(self.path.get_last_point(), Point(24, 24))

    def test_append_shift_to_path(self):
        self.empty_path.append_shift(Point(1, 1))
        self.assertEqual(self.empty_path.get_last_point(), Point(1, 1))
        self.empty_path.append_shift(Point(10, 10))
        self.assertEqual(self.empty_path.get_last_point(), Point(11, 11))

    def test_curve_last_point(self):
        self.assertEqual(self.curve.get_last_point(), Point(3, 3))
        self.assertEqual(self.curve.get_last_point(Point(10, 10)),
                         Point(13, 13))

    def test_path_to_bytes(self):
        a = copy.deepcopy(self.path)
        bt = a.get_bytes()

        b = HandwrittenPath.read_next(io.BytesIO(bt))
        self.assertEqual(a, b)

    def test_path_with_shift_to_bytes(self):
        self.path.set_position(Point(10, 10))
        a = copy.deepcopy(self.path)
        bt = a.get_bytes()

        b = HandwrittenPath.read_next(io.BytesIO(bt))
        self.assertEqual(a, b)

    def test_curve_from_absolute_points(self):
        curve = Curve.from_absolute(list(self.abs_points))
        curve_iter = CurvePointsIterator(curve, curve.start)
        abs_points_iter = iter(self.abs_points)
        while True:
            try:
                self.assertEqual(next(curve_iter), next(abs_points_iter))
            except StopIteration:
                break
Beispiel #2
0
class TestPathLinesIterator(TestCase):

    def setUp(self) -> None:
        self.path = HandwrittenPath()
        self.path.new_curve(Point(0, 0))
        self.path.append_shift(Point(1, 2))
        self.path.append_shift(Point(1, 2))
        self.path.new_curve(Point(10, 20))
        self.path.new_curve(Point(10, 20))
        self.path.append_shift(Point(1, 2))
        self.path.append_shift(Point(1, 2))
        self.path.get_last_point()

        self.path_iter = self.path.get_lines()

        self.expected_lines = [
            (Point(0, 0), Point(1, 2)),
            (Point(1, 2), Point(2, 4)),
            (Point(22, 44), Point(23, 46)),
            (Point(23, 46), Point(24, 48))
        ]

    @staticmethod
    def get_lines(path_iter):
        ls = []
        try:
            while True:
                ls.append(next(path_iter))
        except StopIteration:
            return ls

    def test_path_correct(self):
        self.assertListEqual(self.get_lines(self.path_iter), self.expected_lines)

    def test_create_new_curve_from_iterator(self):
        self.get_lines(self.path_iter)
        self.path_iter._new_curve(Point(50, 50))
        self.path_iter.append_absolute(Point(55, 55))
        self.path_iter.append_absolute(Point(60, 80))
        self.path_iter.append_absolute(Point(65, 85))

        lines = self.get_lines(self.path_iter)
        self.assertListEqual(lines, [
                                 (Point(50, 50), Point(55, 55)),
                                 (Point(55, 55), Point(60, 80)),
                                 (Point(60, 80), Point(65, 85))
                             ])

    def test_empty_path_filled(self):
        path = HandwrittenPath()

        itr = path.get_lines()

        itr._new_curve(Point(50, 50))
        itr.append_absolute(Point(55, 55))
        itr.append_absolute(Point(60, 80))
        itr.append_absolute(Point(65, 85))

        lines = self.get_lines(itr)
        self.assertListEqual(lines, [
            (Point(50, 50), Point(55, 55)),
            (Point(55, 55), Point(60, 80)),
            (Point(60, 80), Point(65, 85))
        ])