aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorOlivier Tilloy <olivier@tilloy.net>2010-12-24 16:56:11 +0100
committerOlivier Tilloy <olivier@tilloy.net>2010-12-24 16:56:11 +0100
commitf178386b9ec255d7456977bb23b205802a0452e7 (patch)
tree5d76b10274b6013d20af5ddf9f8b2108cc43d614 /test
parent27f94886b6c043609366dd2f5604566d1264852d (diff)
downloadpyexiv2-f178386b9ec255d7456977bb23b205802a0452e7.tar.gz
is_fraction and make_fraction: convenience functions to handle transparently
the conditional availability of the fractions module in the standard library (Python ≥ 2.6).
Diffstat (limited to 'test')
-rwxr-xr-xtest/TestsRunner.py3
-rw-r--r--test/utils.py42
2 files changed, 43 insertions, 2 deletions
diff --git a/test/TestsRunner.py b/test/TestsRunner.py
index 198bb27..ba5162c 100755
--- a/test/TestsRunner.py
+++ b/test/TestsRunner.py
@@ -38,7 +38,7 @@ from xmp import TestXmpTag, TestXmpNamespaces
from metadata import TestImageMetadata
from buffer import TestBuffer
from encoding import TestEncodings
-from utils import TestConversions
+from utils import TestConversions, TestFractions
from usercomment import TestUserCommentReadWrite, TestUserCommentAdd
from pickling import TestPicklingTags
@@ -58,6 +58,7 @@ def run_unit_tests():
suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(TestBuffer))
suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(TestEncodings))
suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(TestConversions))
+ suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(TestFractions))
suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(TestUserCommentReadWrite))
suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(TestUserCommentAdd))
suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(TestPicklingTags))
diff --git a/test/utils.py b/test/utils.py
index 2f0d19b..ceddcc3 100644
--- a/test/utils.py
+++ b/test/utils.py
@@ -26,7 +26,8 @@
import unittest
-from pyexiv2.utils import undefined_to_string, string_to_undefined
+from pyexiv2.utils import undefined_to_string, string_to_undefined, \
+ Rational, Fraction, is_fraction, make_fraction
class TestConversions(unittest.TestCase):
@@ -48,3 +49,42 @@ class TestConversions(unittest.TestCase):
value = "48 50 50 49"
self.assertEqual(string_to_undefined(undefined_to_string(value)), value)
+
+class TestFractions(unittest.TestCase):
+
+ def test_is_fraction(self):
+ if Fraction is not None:
+ self.failUnless(is_fraction(Fraction()))
+ self.failUnless(is_fraction(Fraction(3, 5)))
+ self.failUnless(is_fraction(Fraction(Fraction(4, 5))))
+ self.failUnless(is_fraction(Fraction('3/2')))
+ self.failUnless(is_fraction(Fraction('-4/5')))
+ self.failUnless(is_fraction(Rational(3, 5)))
+ self.failUnless(is_fraction(Rational(-4, 5)))
+ self.failUnless(is_fraction(Rational.from_string("3/5")))
+ self.failUnless(is_fraction(Rational.from_string("-4/5")))
+
+ self.failIf(is_fraction(3 / 5))
+ self.failIf(is_fraction('3/5'))
+ self.failIf(is_fraction('2.7'))
+ self.failIf(is_fraction(2.7))
+ self.failIf(is_fraction('notafraction'))
+ self.failIf(is_fraction(None))
+
+ def test_make_fraction(self):
+ if Fraction is not None:
+ self.assertEqual(make_fraction(3, 5), Fraction(3, 5))
+ self.assertEqual(make_fraction(-3, 5), Fraction(-3, 5))
+ self.assertEqual(make_fraction('3/2'), Fraction(3, 2))
+ self.assertEqual(make_fraction('-3/4'), Fraction(-3, 4))
+ else:
+ self.assertEqual(make_fraction(3, 5), Rational(3, 5))
+ self.assertEqual(make_fraction(-3, 5), Rational(-3, 5))
+ self.assertEqual(make_fraction('3/2'), Rational(3, 2))
+ self.assertEqual(make_fraction('-3/4'), Rational(-3, 4))
+
+ self.assertRaises(ZeroDivisionError, make_fraction, 3, 0)
+ self.assertRaises(ZeroDivisionError, make_fraction, '3/0')
+ self.assertRaises(TypeError, make_fraction, 5, 3, 2)
+ self.assertRaises(TypeError, make_fraction, None)
+