Fixes tests on x86; commit from upstream From 3bd978bf0f011d8c7ee6ff21fb04815d6d80427d Mon Sep 17 00:00:00 2001 From: Cosimo Lupo Date: Thu, 24 Oct 2019 12:17:34 +0100 Subject: [PATCH] pens_test: compare float coordinates up to 12 decimal precision Fixes #186 --- tests/pens_test.py | 2 +- tests/utils.py | 16 +++++++++++----- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/tests/pens_test.py b/tests/pens_test.py index c861934..18e63a8 100644 --- a/tests/pens_test.py +++ b/tests/pens_test.py @@ -39,7 +39,7 @@ def convert_glyph(self, glyph, **kwargs): def expect_glyph(self, source, expected): converted = self.convert_glyph(source) self.assertNotEqual(converted, source) - if converted != expected: + if not converted.approx(expected): print(self.diff(expected, converted)) self.fail("converted glyph is different from expected") diff --git a/tests/utils.py b/tests/utils.py index 2706a71..b0d6c11 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -112,7 +112,7 @@ def __ne__(self, other): """Return True if 'other' glyph's outline is different from self.""" return not (self == other) - def approx(self, other): + def approx(self, other, rel_tol=1e-12): if hasattr(other, 'outline'): outline2 == other.outline elif hasattr(other, 'draw'): @@ -132,7 +132,10 @@ def approx(self, other): if not arg2 or not isinstance(arg2[0], tuple): return False for (x1, y1), (x2, y2) in zip(arg1, arg2): - if not isclose(x1, x2) or not isclose(y1, y2): + if ( + not isclose(x1, x2, rel_tol=rel_tol) or + not isclose(y1, y2, rel_tol=rel_tol) + ): return False elif arg1 != arg2: return False @@ -208,9 +211,12 @@ def _repr_pen_commands(commands): for cmd, args, kwargs in commands: if args: if isinstance(args[0], tuple): - # cast float to int if there're no digits after decimal point - args = [tuple((int(v) if int(v) == v else v) for v in pt) - for pt in args] + # cast float to int if there're no digits after decimal point, + # and round floats to 12 decimal digits (more than enough) + args = [ + tuple((int(v) if int(v) == v else round(v, 12)) for v in pt) + for pt in args + ] args = ", ".join(repr(a) for a in args) if kwargs: kwargs = ", ".join("%s=%r" % (k, v)