auto_stub.SimpleMock: add sanity - sort kwargs in expectations.
R=sergiyb@chromium.org
BUG=
Review URL: https://codereview.chromium.org/2186023002 .
diff --git a/testing_support/auto_stub.py b/testing_support/auto_stub.py
index bcede87..e4fa647 100644
--- a/testing_support/auto_stub.py
+++ b/testing_support/auto_stub.py
@@ -32,14 +32,19 @@
class SimpleMock(object):
"""Really simple manual class mock."""
- def __init__(self, unit_test):
+ def __init__(self, unit_test, sorted_kwargs=False):
"""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
@@ -59,7 +64,11 @@
"""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)
- str_kwargs = ', '.join('%s=%r' % (k, v) for k, v in kwargs.iteritems())
+
+ 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]))))
diff --git a/testing_support/tests/auto_stub_test.py b/testing_support/tests/auto_stub_test.py
index a3d6aef..3322da9 100644
--- a/testing_support/tests/auto_stub_test.py
+++ b/testing_support/tests/auto_stub_test.py
@@ -19,6 +19,17 @@
obj.method1(1, param=2)
obj.check_calls(['method1(1, param=2)'])
+ def test_auto_mock_sorted(self):
+ obj = MockedObject(self)
+ obj.method1(1, c=4, a=2, b=3)
+ # tandrii@ is very surprised this is actually deterministic.
+ obj.check_calls(['method1(1, a=2, c=4, b=3)'])
+ # The proper and sane way is to always sort them.
+ obj = MockedObject(self, sorted_kwargs=True)
+ obj.method1(1, c=4, a=2, b=3)
+ obj.check_calls(['method1(1, a=2, b=3, c=4)'])
+
+
def return_one():
return 1