diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2014-06-25 21:41:58 +0200 |
---|---|---|
committer | Victor Stinner <victor.stinner@gmail.com> | 2014-06-25 21:41:58 +0200 |
commit | 975735f729df6b7767556d2d389b560dbc7500ac (patch) | |
tree | 8579740ca947c8323088a452d659e4c0cb130a2e /Lib/test/test_asyncio/test_futures.py | |
parent | 65c623de74cba3eff6d8f5f4c37cfec89c0fde43 (diff) | |
download | cpython-git-975735f729df6b7767556d2d389b560dbc7500ac.tar.gz |
asyncio, Tulip issue 177: Rewite repr() of Future, Task, Handle and TimerHandle
- Uniformize repr() output to format "<Class ...>"
- On Python 3.5+, repr(Task) uses the qualified name instead of the short name
of the coroutine
Diffstat (limited to 'Lib/test/test_asyncio/test_futures.py')
-rw-r--r-- | Lib/test/test_asyncio/test_futures.py | 56 |
1 files changed, 42 insertions, 14 deletions
diff --git a/Lib/test/test_asyncio/test_futures.py b/Lib/test/test_asyncio/test_futures.py index a230d61476..8485a5e27f 100644 --- a/Lib/test/test_asyncio/test_futures.py +++ b/Lib/test/test_asyncio/test_futures.py @@ -1,6 +1,7 @@ """Tests for futures.py.""" import concurrent.futures +import re import threading import unittest from unittest import mock @@ -12,6 +13,12 @@ from asyncio import test_utils def _fakefunc(f): return f +def first_cb(): + pass + +def last_cb(): + pass + class FutureTests(test_utils.TestCase): @@ -95,39 +102,60 @@ class FutureTests(test_utils.TestCase): # The second "yield from f" does not yield f. self.assertEqual(next(g), ('C', 42)) # yield 'C', y. - def test_repr(self): + def test_future_repr(self): f_pending = asyncio.Future(loop=self.loop) - self.assertEqual(repr(f_pending), 'Future<PENDING>') + self.assertEqual(repr(f_pending), '<Future pending>') f_pending.cancel() f_cancelled = asyncio.Future(loop=self.loop) f_cancelled.cancel() - self.assertEqual(repr(f_cancelled), 'Future<CANCELLED>') + self.assertEqual(repr(f_cancelled), '<Future cancelled>') f_result = asyncio.Future(loop=self.loop) f_result.set_result(4) - self.assertEqual(repr(f_result), 'Future<result=4>') + self.assertEqual(repr(f_result), '<Future finished result=4>') self.assertEqual(f_result.result(), 4) exc = RuntimeError() f_exception = asyncio.Future(loop=self.loop) f_exception.set_exception(exc) - self.assertEqual(repr(f_exception), 'Future<exception=RuntimeError()>') + self.assertEqual(repr(f_exception), '<Future finished exception=RuntimeError()>') self.assertIs(f_exception.exception(), exc) - f_few_callbacks = asyncio.Future(loop=self.loop) - f_few_callbacks.add_done_callback(_fakefunc) - self.assertIn('Future<PENDING, [<function _fakefunc', - repr(f_few_callbacks)) - f_few_callbacks.cancel() + def func_repr(func): + filename, lineno = test_utils.get_function_source(func) + text = '%s() at %s:%s' % (func.__qualname__, filename, lineno) + return re.escape(text) + + f_one_callbacks = asyncio.Future(loop=self.loop) + f_one_callbacks.add_done_callback(_fakefunc) + fake_repr = func_repr(_fakefunc) + self.assertRegex(repr(f_one_callbacks), + r'<Future pending cb=\[%s\]>' % fake_repr) + f_one_callbacks.cancel() + self.assertEqual(repr(f_one_callbacks), + '<Future cancelled>') + + f_two_callbacks = asyncio.Future(loop=self.loop) + f_two_callbacks.add_done_callback(first_cb) + f_two_callbacks.add_done_callback(last_cb) + first_repr = func_repr(first_cb) + last_repr = func_repr(last_cb) + self.assertRegex(repr(f_two_callbacks), + r'<Future pending cb=\[%s, %s\]>' + % (first_repr, last_repr)) f_many_callbacks = asyncio.Future(loop=self.loop) - for i in range(20): + f_many_callbacks.add_done_callback(first_cb) + for i in range(8): f_many_callbacks.add_done_callback(_fakefunc) - r = repr(f_many_callbacks) - self.assertIn('Future<PENDING, [<function _fakefunc', r) - self.assertIn('<18 more>', r) + f_many_callbacks.add_done_callback(last_cb) + cb_regex = r'%s, <8 more>, %s' % (first_repr, last_repr) + self.assertRegex(repr(f_many_callbacks), + r'<Future pending cb=\[%s\]>' % cb_regex) f_many_callbacks.cancel() + self.assertEqual(repr(f_many_callbacks), + '<Future cancelled>') def test_copy_state(self): # Test the internal _copy_state method since it's being directly |