diff options
author | Abseil Team <absl-team@google.com> | 2020-12-29 12:26:08 (GMT) |
---|---|---|
committer | Derek Mauro <dmauro@google.com> | 2021-01-14 01:58:49 (GMT) |
commit | 95a9bdd9f91222e34d7ef50757e24351d73927b9 (patch) | |
tree | e94b0aadc9f8e700147293332524477dfc6deaa7 | |
parent | d72813110ce1d52820227ea0377462d4552d00d1 (diff) | |
download | googletest-95a9bdd9f91222e34d7ef50757e24351d73927b9.zip googletest-95a9bdd9f91222e34d7ef50757e24351d73927b9.tar.gz googletest-95a9bdd9f91222e34d7ef50757e24351d73927b9.tar.bz2 |
Googletest export
Use an OrderedDict to store templated_types in the AST so that gmock knows how to properly construct the templated Mock class.
This is necessary for functions that make use of the templated typename as an argument or return type.
PiperOrigin-RevId: 349405731
-rwxr-xr-x | googlemock/scripts/generator/cpp/ast.py | 3 | ||||
-rwxr-xr-x | googlemock/scripts/generator/cpp/gmock_class.py | 9 | ||||
-rwxr-xr-x | googlemock/scripts/generator/cpp/gmock_class_test.py | 22 |
3 files changed, 27 insertions, 7 deletions
diff --git a/googlemock/scripts/generator/cpp/ast.py b/googlemock/scripts/generator/cpp/ast.py index cc9f89a..db20de4 100755 --- a/googlemock/scripts/generator/cpp/ast.py +++ b/googlemock/scripts/generator/cpp/ast.py @@ -36,6 +36,7 @@ except ImportError: # Python 2.x import __builtin__ as builtins +import collections import sys import traceback @@ -1433,7 +1434,7 @@ class AstBuilder(object): pass # Not needed yet. def _GetTemplatedTypes(self): - result = {} + result = collections.OrderedDict() tokens = list(self._GetMatchingChar('<', '>')) len_tokens = len(tokens) - 1 # Ignore trailing '>'. i = 0 diff --git a/googlemock/scripts/generator/cpp/gmock_class.py b/googlemock/scripts/generator/cpp/gmock_class.py index 488cc15..c4d2793 100755 --- a/googlemock/scripts/generator/cpp/gmock_class.py +++ b/googlemock/scripts/generator/cpp/gmock_class.py @@ -159,12 +159,13 @@ def _GenerateMocks(filename, source, ast_list, desired_class_names): # Add template args for templated classes. if class_node.templated_types: - # TODO(paulchang): The AST doesn't preserve template argument order, - # so we have to make up names here. # TODO(paulchang): Handle non-type template arguments (e.g. # template<typename T, int N>). - template_arg_count = len(class_node.templated_types.keys()) - template_args = ['T%d' % n for n in range(template_arg_count)] + + # class_node.templated_types is an OrderedDict from strings to a tuples. + # The key is the name of the template, and the value is + # (type_name, default). Both type_name and default could be None. + template_args = class_node.templated_types.keys() template_decls = ['typename ' + arg for arg in template_args] lines.append('template <' + ', '.join(template_decls) + '>') parent_name += '<' + ', '.join(template_args) + '>' diff --git a/googlemock/scripts/generator/cpp/gmock_class_test.py b/googlemock/scripts/generator/cpp/gmock_class_test.py index 527182c..7d5e2f1 100755 --- a/googlemock/scripts/generator/cpp/gmock_class_test.py +++ b/googlemock/scripts/generator/cpp/gmock_class_test.py @@ -428,8 +428,8 @@ class Test { }; """ expected = """\ -template <typename T0, typename T1> -class MockTest : public Test<T0, T1> { +template <typename S, typename T> +class MockTest : public Test<S, T> { public: MOCK_METHOD(void, Foo, (), (override)); }; @@ -454,6 +454,24 @@ MOCK_METHOD(void, Bar, (const FooType& test_arg), (override)); self.assertEqualIgnoreLeadingWhitespace(expected, self.GenerateMocks(source)) + def testTemplatedClassWithTemplatedArguments(self): + source = """ +template <typename S, typename T, typename U, typename V, typename W> +class Test { + public: + virtual U Foo(T some_arg); +}; +""" + expected = """\ +template <typename S, typename T, typename U, typename V, typename W> +class MockTest : public Test<S, T, U, V, W> { +public: +MOCK_METHOD(U, Foo, (T some_arg), (override)); +}; +""" + self.assertEqualIgnoreLeadingWhitespace(expected, + self.GenerateMocks(source)) + def testTemplateInATemplateTypedefWithComma(self): source = """ class Test { |