Skip to content

Commit

Permalink
Fix default parameters with specifiers issue
Browse files Browse the repository at this point in the history
  • Loading branch information
hermas55 authored and mhermas committed Sep 21, 2019
1 parent 8036f12 commit 65032e2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 26 deletions.
37 changes: 16 additions & 21 deletions googlemock/scripts/generator/cpp/gmock_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -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([email protected]): 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),
Expand Down
17 changes: 12 additions & 5 deletions googlemock/scripts/generator/cpp/gmock_class_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand All @@ -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):
Expand Down

0 comments on commit 65032e2

Please sign in to comment.