mirror of
https://github.com/beetbox/beets.git
synced 2026-02-24 08:12:54 +01:00
epsilon tolerance in float equality for showdiff
This commit is contained in:
parent
42e0b6c950
commit
e0f66d6f18
2 changed files with 17 additions and 0 deletions
|
|
@ -65,8 +65,15 @@ def _do_query(lib, query, album, also_items=True):
|
|||
|
||||
return items, albums
|
||||
|
||||
FLOAT_EPSILON = 0.01
|
||||
def _showdiff(field, oldval, newval, color):
|
||||
"""Prints out a human-readable field difference line."""
|
||||
# Considering floats incomparable for perfect equality, introduce
|
||||
# an epsilon tolerance.
|
||||
if isinstance(oldval, float) and isinstance(newval, float) and \
|
||||
abs(oldval - newval) < FLOAT_EPSILON:
|
||||
return
|
||||
|
||||
if newval != oldval:
|
||||
oldval = unicode(oldval)
|
||||
newval = unicode(newval)
|
||||
|
|
|
|||
|
|
@ -530,6 +530,16 @@ class UtilTest(unittest.TestCase):
|
|||
self.assertTrue('old' in out)
|
||||
self.assertTrue('new' in out)
|
||||
|
||||
def test_showdiff_floats_close_to_identical(self):
|
||||
commands._showdiff('field', 1.999, 2.001, True)
|
||||
out = self.io.getoutput()
|
||||
self.assertFalse('field' in out)
|
||||
|
||||
def test_showdiff_floats_differenct(self):
|
||||
commands._showdiff('field', 1.999, 4.001, True)
|
||||
out = self.io.getoutput()
|
||||
self.assertTrue('field' in out)
|
||||
|
||||
def suite():
|
||||
return unittest.TestLoader().loadTestsFromName(__name__)
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue