When Size is not on the left it needs to call the correct Decimal method
and make sure it returns a Size object. This also adds a test to make
sure the results of add, subtract, multiply, divide, modulo and power
return the current behavior.
---
blivet/size.py | 1 +
tests/size_test.py | 29 +++++++++++++++++++++++++++++
2 files changed, 30 insertions(+)
diff --git a/blivet/size.py b/blivet/size.py
index 05a38b4..561c884 100644
--- a/blivet/size.py
+++ b/blivet/size.py
@@ -247,6 +247,7 @@ class Size(Decimal):
def __mul__(self, other, context=None):
return Size(Decimal.__mul__(self, other, context=context))
+ __rmul__ = __mul__
def __div__(self, other, context=None):
return Size(Decimal.__div__(self, other, context=context))
diff --git a/tests/size_test.py b/tests/size_test.py
index 9841b94..3bf3572 100644
--- a/tests/size_test.py
+++ b/tests/size_test.py
@@ -163,5 +163,34 @@ class SizeTestCase(unittest.TestCase):
os.environ['LANG'] = saved_lang
locale.setlocale(locale.LC_ALL, '')
+ def testArithmetic(self):
+ from decimal import Decimal
+ s = Size("2GiB")
+
+ # Make sure arithmatic operations with Size always result in Size
+ self.assertIsInstance(s+s, Size)
+ self.assertIsInstance(s-s, Size)
+ self.assertIsInstance(s*s, Size)
+ self.assertIsInstance(s/s, Size)
+ self.assertIsInstance(s**Size(2), Decimal)
+ self.assertIsInstance(s % Size(7), Size)
+
+
+ # Make sure operations with non-Size on the right result in Size
+ self.assertIsInstance(s+2, Size)
+ self.assertIsInstance(s-2, Size)
+ self.assertIsInstance(s*2, Size)
+ self.assertIsInstance(s/2, Size)
+ self.assertIsInstance(s**2, Decimal)
+ self.assertIsInstance(s % 127, Size)
+
+ # Make sure operations with non-Size on the left result in Size
+ self.assertIsInstance(2+s, Size)
+ self.assertIsInstance(2-s, Decimal)
+ self.assertIsInstance(2*s, Size)
+ self.assertIsInstance(2/s, Decimal)
+ self.assertIsInstance(2**Size(2), Decimal)
+ self.assertIsInstance(1024 % Size(127), Decimal)
+
if __name__ == "__main__":
unittest.main()
--
1.9.3