diff options
Diffstat (limited to 'test/utils.py')
-rw-r--r-- | test/utils.py | 42 |
1 files changed, 41 insertions, 1 deletions
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) + |