Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(719)

Unified Diff: testing_support/auto_stub.py

Issue 2190703002: Require sanity: sort kwargs expectations. NOT OPTIONAL. (Closed) Base URL: https://chromium.googlesource.com/infra/testing/testing_support.git@master
Patch Set: fix Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | testing_support/tests/auto_stub_test.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: testing_support/auto_stub.py
diff --git a/testing_support/auto_stub.py b/testing_support/auto_stub.py
index e4fa647877dd047c648ec72dc3ebf4f45f52057f..3465e64da30d2f786e1e59cee0f3a958fae3a0af 100644
--- a/testing_support/auto_stub.py
+++ b/testing_support/auto_stub.py
@@ -30,21 +30,24 @@ class AutoStubMixIn(object):
setattr(obj, member, previous_value)
+def format_call(caller_name, *args, **kwargs):
+ """Returns expectation suitable for passing to `SimpleMock.check_calls`."""
+ str_args = ', '.join(repr(arg) for arg in args)
+ str_kwargs = ', '.join('%s=%r' % (k, v)
+ for k, v in sorted(kwargs.iteritems()))
+ return '%s(%s)' % (
+ caller_name, ', '.join(x for x in [str_args, str_kwargs] if x))
+
class SimpleMock(object):
"""Really simple manual class mock."""
- def __init__(self, unit_test, sorted_kwargs=False):
+ def __init__(self, unit_test):
"""Do not call __init__ if you want to use the global call list to detect
ordering across different instances.
Args:
unit_test (unittest.TestCase): instance of a test class.
- sorted_kwargs (bool): if True, kwargs in expectations will always be
- sorted by key.
"""
- # TODO(tandrii): sorted_kwargs MUST BE REALLY TRUE by default, but i don't
- # want to fix all the tests, so keeping backwards compatability.
self.calls = []
- self.sorted_kwargs = sorted_kwargs
self.unit_test = unit_test
self.assertEqual = unit_test.assertEqual
@@ -63,14 +66,7 @@ class SimpleMock(object):
def _register_call(self, *args, **kwargs):
"""Registers the name of the caller function."""
caller_name = kwargs.pop('caller_name', None) or inspect.stack()[1][3]
- str_args = ', '.join(repr(arg) for arg in args)
-
- kwargs_items = kwargs.items()
- if self.sorted_kwargs:
- kwargs_items.sort()
- str_kwargs = ', '.join('%s=%r' % (k, v) for k, v in kwargs_items)
- self.calls.append('%s(%s)' % (
- caller_name, ', '.join(filter(None, [str_args, str_kwargs]))))
+ self.calls.append(format_call(caller_name, *args, **kwargs))
class TestCase(thread_watcher.TestCase, AutoStubMixIn):
« no previous file with comments | « no previous file | testing_support/tests/auto_stub_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698