summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorvladlosev <vladlosev@8415998a-534a-0410-bf83-d39667b30386>2011-05-20 21:44:14 +0000
committervladlosev <vladlosev@8415998a-534a-0410-bf83-d39667b30386>2011-05-20 21:44:14 +0000
commit73471a6d7e9c964a7f2ca933e9d191bcf0c2e5a3 (patch)
tree46b72b7a9450a7408f24cd49b66dc2793b34310f
parentdfc46e7b90421a8706c4e8075deb3378e037a522 (diff)
downloadgooglemock-73471a6d7e9c964a7f2ca933e9d191bcf0c2e5a3.tar.gz
Fixes issue 139 and issue 140.
git-svn-id: http://googlemock.googlecode.com/svn/trunk@393 8415998a-534a-0410-bf83-d39667b30386
-rwxr-xr-xscripts/generator/cpp/gmock_class.py40
-rwxr-xr-xscripts/generator/cpp/gmock_class_test.py45
2 files changed, 73 insertions, 12 deletions
diff --git a/scripts/generator/cpp/gmock_class.py b/scripts/generator/cpp/gmock_class.py
index 645c295..427d206 100755
--- a/scripts/generator/cpp/gmock_class.py
+++ b/scripts/generator/cpp/gmock_class.py
@@ -82,20 +82,36 @@ def _GenerateMethods(output_lines, source, class_node):
return_type += '*'
if node.return_type.reference:
return_type += '&'
- mock_method_macro = 'MOCK_%sMETHOD%d' % (const, len(node.parameters))
+ num_parameters = len(node.parameters)
+ if len(node.parameters) == 1:
+ first_param = node.parameters[0]
+ if source[first_param.start:first_param.end].strip() == 'void':
+ # We must treat T(void) as a function with no parameters.
+ num_parameters = 0
+ mock_method_macro = 'MOCK_%sMETHOD%d' % (const, num_parameters)
args = ''
if node.parameters:
- # 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', ' '))
+ # 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', ' '))
# Create the mock method definition.
output_lines.extend(['%s%s(%s,' % (indent, mock_method_macro, node.name),
diff --git a/scripts/generator/cpp/gmock_class_test.py b/scripts/generator/cpp/gmock_class_test.py
index 494720c..7aa7027 100755
--- a/scripts/generator/cpp/gmock_class_test.py
+++ b/scripts/generator/cpp/gmock_class_test.py
@@ -76,6 +76,17 @@ class Foo {
'MOCK_CONST_METHOD1(Bar,\nvoid(bool flag));',
self.GenerateMethodSource(source))
+ def testExplicitVoid(self):
+ source = """
+class Foo {
+ public:
+ virtual int Bar(void);
+};
+"""
+ self.assertEqualIgnoreLeadingWhitespace(
+ 'MOCK_METHOD0(Bar,\nint(void));',
+ self.GenerateMethodSource(source))
+
def testStrangeNewlineInParameter(self):
source = """
class Foo {
@@ -88,6 +99,40 @@ a) = 0;
'MOCK_METHOD1(Bar,\nvoid(int a));',
self.GenerateMethodSource(source))
+ def testDefaultParameters(self):
+ source = """
+class Foo {
+ public:
+ virtual void Bar(int a, char c = 'x') = 0;
+};
+"""
+ self.assertEqualIgnoreLeadingWhitespace(
+ 'MOCK_METHOD2(Bar,\nvoid(int, char));',
+ self.GenerateMethodSource(source))
+
+ def testMultipleDefaultParameters(self):
+ source = """
+class Foo {
+ public:
+ virtual void Bar(int a = 42, char c = 'x') = 0;
+};
+"""
+ self.assertEqualIgnoreLeadingWhitespace(
+ 'MOCK_METHOD2(Bar,\nvoid(int, char));',
+ self.GenerateMethodSource(source))
+
+ def testRemovesCommentsWhenDefaultsArePresent(self):
+ source = """
+class Foo {
+ public:
+ virtual void Bar(int a = 42 /* a comment */,
+ char /* other comment */ c= 'x') = 0;
+};
+"""
+ self.assertEqualIgnoreLeadingWhitespace(
+ 'MOCK_METHOD2(Bar,\nvoid(int, char));',
+ self.GenerateMethodSource(source))
+
def testDoubleSlashCommentsInParameterListAreRemoved(self):
source = """
class Foo {