Fix Decimal() and __ne__() errors.

This commit is contained in:
Kale Kundert 2016-03-11 16:29:18 -08:00
parent 078448008c
commit 916c0a8b36
2 changed files with 12 additions and 3 deletions

View File

@ -1470,6 +1470,9 @@ class approx(object):
if len(actual) != len(self.expected): return False
return all(a == x for a, x in zip(actual, self.expected))
def __ne__(self, actual):
return not (actual == self)
@property
def expected(self):
# Regardless of whether the user-specified expected value is a number
@ -1546,6 +1549,9 @@ class ApproxNonIterable(object):
# Return true if the two numbers are within the tolerance.
return abs(self.expected - actual) <= self.tolerance
def __ne__(self, actual):
return not (actual == self)
@property
def tolerance(self):
set_default = lambda x, default: x if x is not None else default

View File

@ -31,7 +31,9 @@ class TestApprox:
def test_operator_overloading(self):
assert 1 == approx(1, rel=1e-6, abs=1e-12)
assert not (1 != approx(1, rel=1e-6, abs=1e-12))
assert 10 != approx(1, rel=1e-6, abs=1e-12)
assert not (10 == approx(1, rel=1e-6, abs=1e-12))
def test_exactly_equal(self):
examples = [
@ -41,7 +43,8 @@ class TestApprox:
(12345, 12345.0),
(0.0, -0.0),
(345678, 345678),
(Decimal(1.0001), Decimal(1.0001)),
(Decimal('1.0001'), Decimal('1.0001')),
(Fraction(1, 3), Fraction(-1, -3)),
]
for a, x in examples:
assert a == approx(x)
@ -258,8 +261,8 @@ class TestApprox:
(Decimal('-1.000001'), Decimal('-1.0')),
]
for a, x in within_1e6:
assert a == approx(x, rel=Decimal(5e-6), abs=0)
assert a != approx(x, rel=Decimal(5e-7), abs=0)
assert a == approx(x, rel=Decimal('5e-6'), abs=0)
assert a != approx(x, rel=Decimal('5e-7'), abs=0)
def test_fraction(self):
within_1e6 = [