|
3 | 3 | # Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
|
4 | 4 | # 2011, 2012, 2013, 2014 Python Software Foundation; All Rights Reserved
|
5 | 5 | #
|
6 |
| -# Based on the "test/test_fractions" module in CPython 3.4. |
| 6 | +# Originally based on the "test/test_fractions" module in CPython 3.4. |
7 | 7 | # https://hg.python.org/cpython/file/b18288f24501/Lib/test/test_fractions.py
|
| 8 | +# |
| 9 | +# Updated to match the recent development in CPython. |
| 10 | +# https://github.com/python/cpython/blob/main/Lib/test/test_fractions.py |
8 | 11 |
|
9 | 12 | """Tests for Lib/fractions.py, slightly adapted for quicktions."""
|
10 | 13 |
|
@@ -279,6 +282,41 @@ def testInitFromDecimal(self):
|
279 | 282 | self.assertRaises(OverflowError, F, Decimal('inf'))
|
280 | 283 | self.assertRaises(OverflowError, F, Decimal('-inf'))
|
281 | 284 |
|
| 285 | + def testInitFromIntegerRatio(self): |
| 286 | + class Ratio: |
| 287 | + def __init__(self, ratio): |
| 288 | + self._ratio = ratio |
| 289 | + def as_integer_ratio(self): |
| 290 | + return self._ratio |
| 291 | + |
| 292 | + self.assertEqual((7, 3), _components(F(Ratio((7, 3))))) |
| 293 | + errmsg = "argument should be a string or a number" |
| 294 | + # the type also has an "as_integer_ratio" attribute. |
| 295 | + self.assertRaisesRegex(TypeError, errmsg, F, Ratio) |
| 296 | + # bad ratio |
| 297 | + self.assertRaises(TypeError, F, Ratio(7)) |
| 298 | + self.assertRaises(ValueError, F, Ratio((7,))) |
| 299 | + self.assertRaises(ValueError, F, Ratio((7, 3, 1))) |
| 300 | + # only single-argument form |
| 301 | + self.assertRaises(TypeError, F, Ratio((3, 7)), 11) |
| 302 | + self.assertRaises(TypeError, F, 2, Ratio((-10, 9))) |
| 303 | + |
| 304 | + # as_integer_ratio not defined in a class |
| 305 | + class A: |
| 306 | + pass |
| 307 | + a = A() |
| 308 | + a.as_integer_ratio = lambda: (9, 5) |
| 309 | + self.assertEqual((9, 5), _components(F(a))) |
| 310 | + |
| 311 | + # as_integer_ratio defined in a metaclass |
| 312 | + class M(type): |
| 313 | + def as_integer_ratio(self): |
| 314 | + return (11, 9) |
| 315 | + class B(metaclass=M): |
| 316 | + pass |
| 317 | + self.assertRaisesRegex(TypeError, errmsg, F, B) |
| 318 | + self.assertRaisesRegex(TypeError, errmsg, F, B()) |
| 319 | + |
282 | 320 | def testFromString(self):
|
283 | 321 | self.assertEqual((5, 1), _components(F("5")))
|
284 | 322 | self.assertEqual((3, 2), _components(F("3/2")))
|
|
0 commit comments