Skip to content

Commit 3d8e08e

Browse files
committed
Use approximate floating point comparison in tests
Allow minor floating point differences when comparing glyph digests. This ensures tests pass with both older fonttools versions (which had floating point drift in splitCubicAtT) and newer versions (which force exact endpoint matching). Uses math.isclose for float comparison within a recursive helper that handles the nested digest structure.
1 parent 21db903 commit 3d8e08e

1 file changed

Lines changed: 23 additions & 2 deletions

File tree

tests/test_BooleanGlyph.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from __future__ import print_function, division, absolute_import
22

3-
import sys
3+
import math
44
import os
5+
import sys
56
import unittest
67

78
import pytest
@@ -14,6 +15,23 @@
1415
VERBOSE = False
1516

1617

18+
def _approx_equal(a, b):
19+
"""Check if two values are approximately equal, handling nested structures.
20+
21+
Uses math.isclose for float comparison to handle minor differences between
22+
fonttools versions in splitCubicAtT precision.
23+
"""
24+
if type(a) != type(b):
25+
return False
26+
if isinstance(a, (tuple, list)):
27+
if len(a) != len(b):
28+
return False
29+
return all(_approx_equal(x, y) for x, y in zip(a, b))
30+
if isinstance(a, float):
31+
return math.isclose(a, b)
32+
return a == b
33+
34+
1735
class BooleanTests(unittest.TestCase):
1836

1937
pass
@@ -47,7 +65,10 @@ def test(self):
4765
func(*args, outPen=testPen)
4866
expectedPen = DigestPointPen()
4967
expectedGlyph.drawPoints(expectedPen)
50-
self.assertEqual(testPen.getDigest(), expectedPen.getDigest(), "Glyph name '%s' failed for '%s'." % (glyph.name, booleanMethodName))
68+
self.assertTrue(
69+
_approx_equal(testPen.getDigest(), expectedPen.getDigest()),
70+
"Glyph name '%s' failed for '%s'." % (glyph.name, booleanMethodName)
71+
)
5172

5273
return True, test
5374

0 commit comments

Comments
 (0)