From 65032e28cba171c000accc85ffaf6f1e62921b86 Mon Sep 17 00:00:00 2001 From: mhermas Date: Sat, 21 Sep 2019 14:38:33 +0200 Subject: [PATCH] Fix default parameters with specifiers issue --- .../scripts/generator/cpp/gmock_class.py | 37 ++++++++----------- .../scripts/generator/cpp/gmock_class_test.py | 17 ++++++--- 2 files changed, 28 insertions(+), 26 deletions(-) diff --git a/googlemock/scripts/generator/cpp/gmock_class.py b/googlemock/scripts/generator/cpp/gmock_class.py index 974bae8d2a..bf872710b4 100755 --- a/googlemock/scripts/generator/cpp/gmock_class.py +++ b/googlemock/scripts/generator/cpp/gmock_class.py @@ -96,27 +96,22 @@ def _GenerateMethods(output_lines, source, class_node): args = '' if node.parameters: - # Due to the parser limitations, it is impossible to keep comments - # while stripping the default parameters. When defaults are - # present, we choose to strip them and comments (and produce - # compilable code). - # TODO(nnorwitz@google.com): Investigate whether it is possible to - # preserve parameter name when reconstructing parameter text from - # the AST. - if len([param for param in node.parameters if param.default]) > 0: - args = ', '.join(param.type.name for param in node.parameters) - else: - # Get the full text of the parameters from the start - # of the first parameter to the end of the last parameter. - start = node.parameters[0].start - end = node.parameters[-1].end - # Remove // comments. - args_strings = re.sub(r'//.*', '', source[start:end]) - # Condense multiple spaces and eliminate newlines putting the - # parameters together on a single line. Ensure there is a - # space in an argument which is split by a newline without - # intervening whitespace, e.g.: int\nBar - args = re.sub(' +', ' ', args_strings.replace('\n', ' ')) + # Get the full text of the parameters from the start + # of the first parameter to the end of the last parameter. + start = node.parameters[0].start + end = node.parameters[-1].end + # Remove // comments. + args_strings = re.sub(r'//.*', '', source[start:end]) + # Remove /* comments */. + args_strings = re.sub(r'/\*.*\*/', '', args_strings) + # Remove default arguments. + args_strings = re.sub(r'=.*,', ',', args_strings) + args_strings = re.sub(r'=.*', '', args_strings) + # Condense multiple spaces and eliminate newlines putting the + # parameters together on a single line. Ensure there is a + # space in an argument which is split by a newline without + # intervening whitespace, e.g.: int\nBar + args = re.sub(' +', ' ', args_strings.replace('\n', ' ')) # Create the mock method definition. output_lines.extend(['%s%s(%s,' % (indent, mock_method_macro, node.name), diff --git a/googlemock/scripts/generator/cpp/gmock_class_test.py b/googlemock/scripts/generator/cpp/gmock_class_test.py index 4715d3f951..fc89be35ca 100755 --- a/googlemock/scripts/generator/cpp/gmock_class_test.py +++ b/googlemock/scripts/generator/cpp/gmock_class_test.py @@ -181,18 +181,25 @@ class Foo { }; """ self.assertEqualIgnoreLeadingWhitespace( - 'MOCK_METHOD2(Bar,\nvoid(int, char));', + 'MOCK_METHOD2(Bar,\nvoid(int a, char c ));', self.GenerateMethodSource(source)) def testMultipleDefaultParameters(self): source = """ class Foo { public: - virtual void Bar(int a = 42, char c = 'x') = 0; + virtual void Bar( + int a = 42, + char c = 'x', + const int* const p = nullptr, + const std::string& s = "42", + char tab[] = {'4','2'}, + int const *& rp = aDefaultPointer) = 0; }; """ self.assertEqualIgnoreLeadingWhitespace( - 'MOCK_METHOD2(Bar,\nvoid(int, char));', + "MOCK_METHOD7(Bar,\n" + "void(int a , char c , const int* const p , const std::string& s , char tab[] , int const *& rp ));", self.GenerateMethodSource(source)) def testRemovesCommentsWhenDefaultsArePresent(self): @@ -204,7 +211,7 @@ class Foo { }; """ self.assertEqualIgnoreLeadingWhitespace( - 'MOCK_METHOD2(Bar,\nvoid(int, char));', + 'MOCK_METHOD2(Bar,\nvoid(int a , char c));', self.GenerateMethodSource(source)) def testDoubleSlashCommentsInParameterListAreRemoved(self): @@ -231,7 +238,7 @@ class Foo { }; """ self.assertEqualIgnoreLeadingWhitespace( - 'MOCK_METHOD2(Bar,\nconst string&(int /* keeper */, int b));', + 'MOCK_METHOD2(Bar,\nconst string&(int , int b));', self.GenerateMethodSource(source)) def testArgsOfTemplateTypes(self):