diff options
-rw-r--r-- | scss/tests/test_types.py | 16 | ||||
-rw-r--r-- | scss/types.py | 18 |
2 files changed, 27 insertions, 7 deletions
diff --git a/scss/tests/test_types.py b/scss/tests/test_types.py index 48f8223..026be02 100644 --- a/scss/tests/test_types.py +++ b/scss/tests/test_types.py @@ -89,6 +89,22 @@ def test_comparison_numeric(): assert not units < plain assert not units > plain + # Incompatible units have... rules. + ems = Number(100, "em") + pxs = Number(100, "px") + + with pytest.raises(ValueError): + ems < pxs + with pytest.raises(ValueError): + ems > pxs + with pytest.raises(ValueError): + ems <= pxs + with pytest.raises(ValueError): + ems >= pxs + + assert not ems == pxs + assert ems != pxs + def test_comparison_stringerific(): abc = String('abc') diff --git a/scss/types.py b/scss/types.py index a293d49..6a4b574 100644 --- a/scss/types.py +++ b/scss/types.py @@ -277,21 +277,21 @@ class Number(Value): def __eq__(self, other): if not isinstance(other, Number): return Boolean(False) - return Boolean(self._compare(other, operator.__eq__)) + return self._compare(other, operator.__eq__, soft_fail=True) def __lt__(self, other): - return Boolean(self._compare(other, operator.__lt__)) + return self._compare(other, operator.__lt__) def __le__(self, other): - return Boolean(self._compare(other, operator.__le__)) + return self._compare(other, operator.__le__) def __gt__(self, other): - return Boolean(self._compare(other, operator.__gt__)) + return self._compare(other, operator.__gt__) def __ge__(self, other): - return Boolean(self._compare(other, operator.__ge__)) + return self._compare(other, operator.__ge__) - def _compare(self, other, op): + def _compare(self, other, op, soft_fail=False): if not isinstance(other, Number): raise TypeError("Can't compare %r and %r" % (self, other)) @@ -307,7 +307,11 @@ class Number(Value): right = other.to_base_units() if left.unit_numer != right.unit_numer or left.unit_denom != right.unit_denom: - raise ValueError("Can't reconcile units: %r and %r" % (self, other)) + if soft_fail: + # Used for equality only, where == should never fail + return Boolean(False) + else: + raise ValueError("Can't reconcile units: %r and %r" % (self, other)) return Boolean(op(round(left.value, 5), round(right.value, 5))) |