tests: use math.isclose to compare float coordinates

using the default tolerance which is 1e-9, i.e. assures that two floats are the same
within about 9 decimal digits

https://docs.python.org/3/library/math.html#math.isclose
This commit is contained in:
Cosimo Lupo 2018-09-26 12:13:39 +01:00
parent 36d1b69d7e
commit a807ab2b93
No known key found for this signature in database
GPG Key ID: 59D54DB0C9976482
2 changed files with 30 additions and 1 deletions

View File

@ -38,7 +38,7 @@ class _TestPenMixin(object):
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")

View File

@ -1,6 +1,7 @@
from __future__ import print_function, division, absolute_import
from . import CUBIC_GLYPHS
from ufoLib.pointPen import PointToSegmentPen, SegmentToPointPen
from fontTools.misc.py23 import isclose
import unittest
@ -111,6 +112,34 @@ class DummyGlyph(object):
"""Return True if 'other' glyph's outline is different from self."""
return not (self == other)
def approx(self, other):
if hasattr(other, 'outline'):
outline2 == other.outline
elif hasattr(other, 'draw'):
outline2 = self.__class__(other).outline
else:
raise TypeError(type(other).__name__)
outline1 = self.outline
if len(outline1) != len(outline2):
return False
for (cmd1, arg1, kwd1), (cmd2, arg2, kwd2) in zip(outline1, outline2):
if cmd1 != cmd2:
return False
if kwd1 != kwd2:
return False
if arg1:
if isinstance(arg1[0], tuple):
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):
return False
elif arg1 != arg2:
return False
elif arg2:
return False
return True
def __str__(self):
"""Return commands making up the glyph's outline as a string."""
return str(self._pen)