diff options
author | Eevee (Alex Munroe) <eevee.git@veekun.com> | 2013-08-21 15:23:59 -0700 |
---|---|---|
committer | Eevee (Alex Munroe) <eevee.git@veekun.com> | 2013-08-21 15:23:59 -0700 |
commit | 93385fec068470e50a7d6f67aaa13098dc2826b6 (patch) | |
tree | bbc573daa8efa06a18c530be42a44271a9993782 /scss/types.py | |
parent | 0e499cb186e3a5fc96558549fb008f218ed998d7 (diff) | |
download | pyscss-93385fec068470e50a7d6f67aaa13098dc2826b6.tar.gz |
Numbers can always compare to unitless numbers. Expand Number tests.
Diffstat (limited to 'scss/types.py')
-rw-r--r-- | scss/types.py | 34 |
1 files changed, 27 insertions, 7 deletions
diff --git a/scss/types.py b/scss/types.py index af67cd3..870f479 100644 --- a/scss/types.py +++ b/scss/types.py @@ -292,14 +292,21 @@ class Number(Value): if not isinstance(other, Number): raise TypeError("Can't compare %r and %r" % (self, other)) - left = self.to_base_units() - right = other.to_base_units() + # A unitless operand is treated as though it had the other operand's + # units, and zero values can cast to anything, so in both cases the + # units can be ignored + if (self.is_unitless or other.is_unitless or + self.value == 0 or other.value == 0): + left = self + right = other + else: + left = self.to_base_units() + right = other.to_base_units() - if (left.value != 0 or left.unit_numer or left.unit_denom) and (right.value != 0 or right.unit_numer or right.unit_denom): 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)) - return op(round(left.value, 5), round(right.value, 5)) + return Boolean(op(round(left.value, 5), round(right.value, 5))) def __pow__(self, exp): if not isinstance(exp, Number): @@ -367,13 +374,26 @@ class Number(Value): unit_denom=self.unit_denom or other.unit_denom, ) + # Likewise, if either side is zero, it can auto-cast to any units + if self.value == 0: + return Number( + op(self.value, other.value), + unit_numer=other.unit_numer, + unit_denom=other.unit_denom, + ) + elif other.value == 0: + return Number( + op(self.value, other.value), + unit_numer=self.unit_numer, + unit_denom=self.unit_denom, + ) + # Reduce both operands to the same units left = self.to_base_units() right = other.to_base_units() - if (left.value != 0 or left.unit_numer or left.unit_denom) and (right.value != 0 or right.unit_numer or right.unit_denom): - 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 left.unit_numer != right.unit_numer or left.unit_denom != right.unit_denom: + raise ValueError("Can't reconcile units: %r and %r" % (self, other)) new_amount = op(left.value, right.value) |