Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
googletest
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Chen Yisong
googletest
Commits
360f5f70
Commit
360f5f70
authored
Feb 10, 2020
by
Abseil Team
Committed by
Mark Barolak
Feb 11, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Googletest export
Fix gmock_gen to use MOCK_METHOD instead of old style macros. PiperOrigin-RevId: 294332975
parent
139fa202
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
74 additions
and
109 deletions
+74
-109
gmock_class.py
googlemock/scripts/generator/cpp/gmock_class.py
+20
-40
gmock_class_test.py
googlemock/scripts/generator/cpp/gmock_class_test.py
+54
-69
No files found.
googlemock/scripts/generator/cpp/gmock_class.py
View file @
360f5f70
...
...
@@ -53,10 +53,8 @@ def _RenderType(ast_type):
ast_type: The AST of the type.
Returns:
Rendered string and a boolean to indicate whether we have multiple args
(which is not handled correctly).
Rendered string of the type
"""
has_multiarg_error
=
False
# Add modifiers like 'const'.
modifiers
=
''
if
ast_type
.
modifiers
:
...
...
@@ -66,59 +64,37 @@ def _RenderType(ast_type):
# Collect template args.
template_args
=
[]
for
arg
in
ast_type
.
templated_types
:
rendered_arg
,
e
=
_RenderType
(
arg
)
if
e
:
has_multiarg_error
=
True
rendered_arg
=
_RenderType
(
arg
)
template_args
.
append
(
rendered_arg
)
return_type
+=
'<'
+
', '
.
join
(
template_args
)
+
'>'
# We are actually not handling multi-template-args correctly. So mark it.
if
len
(
template_args
)
>
1
:
has_multiarg_error
=
True
if
ast_type
.
pointer
:
return_type
+=
'*'
if
ast_type
.
reference
:
return_type
+=
'&'
return
return_type
,
has_multiarg_error
def
_GetNumParameters
(
parameters
,
source
):
num_parameters
=
len
(
parameters
)
if
num_parameters
==
1
:
first_param
=
parameters
[
0
]
if
source
[
first_param
.
start
:
first_param
.
end
]
.
strip
()
==
'void'
:
# We must treat T(void) as a function with no parameters.
return
0
return
num_parameters
return
return_type
def
_GenerateMethods
(
output_lines
,
source
,
class_node
):
function_type
=
(
ast
.
FUNCTION_VIRTUAL
|
ast
.
FUNCTION_PURE_VIRTUAL
|
ast
.
FUNCTION_OVERRIDE
)
function_type
=
(
ast
.
FUNCTION_VIRTUAL
|
ast
.
FUNCTION_PURE_VIRTUAL
|
ast
.
FUNCTION_OVERRIDE
)
ctor_or_dtor
=
ast
.
FUNCTION_CTOR
|
ast
.
FUNCTION_DTOR
indent
=
' '
*
_INDENT
for
node
in
class_node
.
body
:
# We only care about virtual functions.
if
(
isinstance
(
node
,
ast
.
Function
)
and
node
.
modifiers
&
function_type
and
if
(
isinstance
(
node
,
ast
.
Function
)
and
node
.
modifiers
&
function_type
and
not
node
.
modifiers
&
ctor_or_dtor
):
# Pick out all the elements we need from the original function.
const
=
'
'
modifiers
=
'override
'
if
node
.
modifiers
&
ast
.
FUNCTION_CONST
:
const
=
'CONST_'
num_parameters
=
_GetNumParameters
(
node
.
parameters
,
source
)
modifiers
=
'const, '
+
modifiers
return_type
=
'void'
if
node
.
return_type
:
return_type
,
has_multiarg_error
=
_RenderType
(
node
.
return_type
)
if
has_multiarg_error
:
for
line
in
[
'// The following line won
\'
t really compile, as the return'
,
'// type has multiple template arguments. To fix it, use a'
,
'// typedef for the return type.'
]:
output_lines
.
append
(
indent
+
line
)
tmpl
=
''
if
class_node
.
templated_types
:
tmpl
=
'_T'
mock_method_macro
=
'MOCK_
%
sMETHOD
%
d
%
s'
%
(
const
,
num_parameters
,
tmpl
)
return_type
=
_RenderType
(
node
.
return_type
)
# commas mess with macros, so nest it in parens if it has one
if
','
in
return_type
:
return_type
=
'('
+
return_type
+
')'
args
=
''
if
node
.
parameters
:
...
...
@@ -137,11 +113,15 @@ def _GenerateMethods(output_lines, source, class_node):
# 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
'
,
' '
))
args_strings
=
re
.
sub
(
' +'
,
' '
,
args_strings
.
replace
(
'
\n
'
,
' '
))
# Remove spaces from the begining, end, and before commas
args
=
re
.
sub
(
' ,'
,
','
,
args_strings
)
.
strip
()
# Create the mock method definition.
output_lines
.
extend
([
'
%
s
%
s(
%
s,'
%
(
indent
,
mock_method_macro
,
node
.
name
),
'
%
s
%
s(
%
s));'
%
(
indent
*
3
,
return_type
,
args
)])
output_lines
.
extend
([
'
%
sMOCK_METHOD(
%
s,
%
s, (
%
s), (
%
s));'
%
(
indent
,
return_type
,
node
.
name
,
args
,
modifiers
)
])
def
_GenerateMocks
(
filename
,
source
,
ast_list
,
desired_class_names
):
...
...
googlemock/scripts/generator/cpp/gmock_class_test.py
View file @
360f5f70
...
...
@@ -61,7 +61,7 @@ class Foo {
};
"""
self
.
assertEqualIgnoreLeadingWhitespace
(
'MOCK_METHOD0(Bar,
\n
int(
));'
,
'MOCK_METHOD(int, Bar, (), (override
));'
,
self
.
GenerateMethodSource
(
source
))
def
testSimpleConstructorsAndDestructor
(
self
):
...
...
@@ -78,7 +78,7 @@ class Foo {
"""
# The constructors and destructor should be ignored.
self
.
assertEqualIgnoreLeadingWhitespace
(
'MOCK_METHOD0(Bar,
\n
int(
));'
,
'MOCK_METHOD(int, Bar, (), (override
));'
,
self
.
GenerateMethodSource
(
source
))
def
testVirtualDestructor
(
self
):
...
...
@@ -91,7 +91,7 @@ class Foo {
"""
# The destructor should be ignored.
self
.
assertEqualIgnoreLeadingWhitespace
(
'MOCK_METHOD0(Bar,
\n
int(
));'
,
'MOCK_METHOD(int, Bar, (), (override
));'
,
self
.
GenerateMethodSource
(
source
))
def
testExplicitlyDefaultedConstructorsAndDestructor
(
self
):
...
...
@@ -107,7 +107,7 @@ class Foo {
"""
# The constructors and destructor should be ignored.
self
.
assertEqualIgnoreLeadingWhitespace
(
'MOCK_METHOD0(Bar,
\n
int(
));'
,
'MOCK_METHOD(int, Bar, (), (override
));'
,
self
.
GenerateMethodSource
(
source
))
def
testExplicitlyDeletedConstructorsAndDestructor
(
self
):
...
...
@@ -123,7 +123,7 @@ class Foo {
"""
# The constructors and destructor should be ignored.
self
.
assertEqualIgnoreLeadingWhitespace
(
'MOCK_METHOD0(Bar,
\n
int(
));'
,
'MOCK_METHOD(int, Bar, (), (override
));'
,
self
.
GenerateMethodSource
(
source
))
def
testSimpleOverrideMethod
(
self
):
...
...
@@ -134,7 +134,7 @@ class Foo {
};
"""
self
.
assertEqualIgnoreLeadingWhitespace
(
'MOCK_METHOD0(Bar,
\n
int(
));'
,
'MOCK_METHOD(int, Bar, (), (override
));'
,
self
.
GenerateMethodSource
(
source
))
def
testSimpleConstMethod
(
self
):
...
...
@@ -145,7 +145,7 @@ class Foo {
};
"""
self
.
assertEqualIgnoreLeadingWhitespace
(
'MOCK_CONST_METHOD1(Bar,
\n
void(bool flag
));'
,
'MOCK_METHOD(void, Bar, (bool flag), (const, override
));'
,
self
.
GenerateMethodSource
(
source
))
def
testExplicitVoid
(
self
):
...
...
@@ -156,7 +156,7 @@ class Foo {
};
"""
self
.
assertEqualIgnoreLeadingWhitespace
(
'MOCK_METHOD0(Bar,
\n
int(void
));'
,
'MOCK_METHOD(int, Bar, (void), (override
));'
,
self
.
GenerateMethodSource
(
source
))
def
testStrangeNewlineInParameter
(
self
):
...
...
@@ -168,7 +168,7 @@ a) = 0;
};
"""
self
.
assertEqualIgnoreLeadingWhitespace
(
'MOCK_METHOD1(Bar,
\n
void(int a
));'
,
'MOCK_METHOD(void, Bar, (int a), (override
));'
,
self
.
GenerateMethodSource
(
source
))
def
testDefaultParameters
(
self
):
...
...
@@ -179,7 +179,7 @@ class Foo {
};
"""
self
.
assertEqualIgnoreLeadingWhitespace
(
'MOCK_METHOD2(Bar,
\n
void(int a, char c
));'
,
'MOCK_METHOD(void, Bar, (int a, char c), (override
));'
,
self
.
GenerateMethodSource
(
source
))
def
testMultipleDefaultParameters
(
self
):
...
...
@@ -196,9 +196,9 @@ class Foo {
};
"""
self
.
assertEqualIgnoreLeadingWhitespace
(
"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
))
'MOCK_METHOD(void, Bar, '
'(int a, char c, const int* const p, const std::string& s, char tab[], int const *& rp), '
'(override));'
,
self
.
GenerateMethodSource
(
source
))
def
testConstDefaultParameter
(
self
):
source
=
"""
...
...
@@ -207,9 +207,9 @@ class Test {
virtual bool Bar(const int test_arg = 42) = 0;
};
"""
expected
=
'MOCK_METHOD1(Bar,
\n
bool(const int test_arg ));'
self
.
assertEqualIgnoreLeadingWhitespace
(
expected
,
self
.
GenerateMethodSource
(
source
))
'MOCK_METHOD(bool, Bar, (const int test_arg), (override));'
,
self
.
GenerateMethodSource
(
source
))
def
testConstRefDefaultParameter
(
self
):
source
=
"""
...
...
@@ -218,9 +218,9 @@ class Test {
virtual bool Bar(const std::string& test_arg = "42" ) = 0;
};
"""
expected
=
'MOCK_METHOD1(Bar,
\n
bool(const std::string& test_arg ));'
self
.
assertEqualIgnoreLeadingWhitespace
(
expected
,
self
.
GenerateMethodSource
(
source
))
'MOCK_METHOD(bool, Bar, (const std::string& test_arg), (override));'
,
self
.
GenerateMethodSource
(
source
))
def
testRemovesCommentsWhenDefaultsArePresent
(
self
):
source
=
"""
...
...
@@ -231,7 +231,7 @@ class Foo {
};
"""
self
.
assertEqualIgnoreLeadingWhitespace
(
'MOCK_METHOD2(Bar,
\n
void(int a , char c
));'
,
'MOCK_METHOD(void, Bar, (int a, char c), (override
));'
,
self
.
GenerateMethodSource
(
source
))
def
testDoubleSlashCommentsInParameterListAreRemoved
(
self
):
...
...
@@ -244,7 +244,7 @@ class Foo {
};
"""
self
.
assertEqualIgnoreLeadingWhitespace
(
'MOCK_CONST_METHOD2(Bar,
\n
void(int a, int b
));'
,
'MOCK_METHOD(void, Bar, (int a, int b), (const, override
));'
,
self
.
GenerateMethodSource
(
source
))
def
testCStyleCommentsInParameterListAreNotRemoved
(
self
):
...
...
@@ -258,7 +258,7 @@ class Foo {
};
"""
self
.
assertEqualIgnoreLeadingWhitespace
(
'MOCK_METHOD2(Bar,
\n
const string&(int , int b
));'
,
'MOCK_METHOD(const string&, Bar, (int, int b), (override
));'
,
self
.
GenerateMethodSource
(
source
))
def
testArgsOfTemplateTypes
(
self
):
...
...
@@ -268,8 +268,7 @@ class Foo {
virtual int Bar(const vector<int>& v, map<int, string>* output);
};"""
self
.
assertEqualIgnoreLeadingWhitespace
(
'MOCK_METHOD2(Bar,
\n
'
'int(const vector<int>& v, map<int, string>* output));'
,
'MOCK_METHOD(int, Bar, (const vector<int>& v, map<int, string>* output), (override));'
,
self
.
GenerateMethodSource
(
source
))
def
testReturnTypeWithOneTemplateArg
(
self
):
...
...
@@ -279,7 +278,7 @@ class Foo {
virtual vector<int>* Bar(int n);
};"""
self
.
assertEqualIgnoreLeadingWhitespace
(
'MOCK_METHOD1(Bar,
\n
vector<int>*(int n
));'
,
'MOCK_METHOD(vector<int>*, Bar, (int n), (override
));'
,
self
.
GenerateMethodSource
(
source
))
def
testReturnTypeWithManyTemplateArgs
(
self
):
...
...
@@ -288,13 +287,8 @@ class Foo {
public:
virtual map<int, string> Bar();
};"""
# Comparing the comment text is brittle - we'll think of something
# better in case this gets annoying, but for now let's keep it simple.
self
.
assertEqualIgnoreLeadingWhitespace
(
'// The following line won
\'
t really compile, as the return
\n
'
'// type has multiple template arguments. To fix it, use a
\n
'
'// typedef for the return type.
\n
'
'MOCK_METHOD0(Bar,
\n
map<int, string>());'
,
'MOCK_METHOD((map<int, string>), Bar, (), (override));'
,
self
.
GenerateMethodSource
(
source
))
def
testSimpleMethodInTemplatedClass
(
self
):
...
...
@@ -306,7 +300,7 @@ class Foo {
};
"""
self
.
assertEqualIgnoreLeadingWhitespace
(
'MOCK_METHOD0_T(Bar,
\n
int(
));'
,
'MOCK_METHOD(int, Bar, (), (override
));'
,
self
.
GenerateMethodSource
(
source
))
def
testPointerArgWithoutNames
(
self
):
...
...
@@ -316,7 +310,7 @@ class Foo {
};
"""
self
.
assertEqualIgnoreLeadingWhitespace
(
'MOCK_METHOD1(Bar,
\n
int(C*
));'
,
'MOCK_METHOD(int, Bar, (C*), (override
));'
,
self
.
GenerateMethodSource
(
source
))
def
testReferenceArgWithoutNames
(
self
):
...
...
@@ -326,7 +320,7 @@ class Foo {
};
"""
self
.
assertEqualIgnoreLeadingWhitespace
(
'MOCK_METHOD1(Bar,
\n
int(C&
));'
,
'MOCK_METHOD(int, Bar, (C&), (override
));'
,
self
.
GenerateMethodSource
(
source
))
def
testArrayArgWithoutNames
(
self
):
...
...
@@ -336,7 +330,7 @@ class Foo {
};
"""
self
.
assertEqualIgnoreLeadingWhitespace
(
'MOCK_METHOD1(Bar,
\n
int(C[]
));'
,
'MOCK_METHOD(int, Bar, (C[]), (override
));'
,
self
.
GenerateMethodSource
(
source
))
...
...
@@ -372,15 +366,14 @@ namespace Baz {
class MockTest : public Test {
public:
MOCK_METHOD0(Foo,
void());
MOCK_METHOD(void, Foo, (), (override));
};
} // namespace Baz
} // namespace Foo
"""
self
.
assertEqualIgnoreLeadingWhitespace
(
expected
,
self
.
GenerateMocks
(
source
))
self
.
assertEqualIgnoreLeadingWhitespace
(
expected
,
self
.
GenerateMocks
(
source
))
def
testClassWithStorageSpecifierMacro
(
self
):
source
=
"""
...
...
@@ -392,12 +385,11 @@ class STORAGE_SPECIFIER Test {
expected
=
"""
\
class MockTest : public Test {
public:
MOCK_METHOD0(Foo,
void());
MOCK_METHOD(void, Foo, (), (override));
};
"""
self
.
assertEqualIgnoreLeadingWhitespace
(
expected
,
self
.
GenerateMocks
(
source
))
self
.
assertEqualIgnoreLeadingWhitespace
(
expected
,
self
.
GenerateMocks
(
source
))
def
testTemplatedForwardDeclaration
(
self
):
source
=
"""
...
...
@@ -410,12 +402,11 @@ class Test {
expected
=
"""
\
class MockTest : public Test {
public:
MOCK_METHOD0(Foo,
void());
MOCK_METHOD(void, Foo, (), (override));
};
"""
self
.
assertEqualIgnoreLeadingWhitespace
(
expected
,
self
.
GenerateMocks
(
source
))
self
.
assertEqualIgnoreLeadingWhitespace
(
expected
,
self
.
GenerateMocks
(
source
))
def
testTemplatedClass
(
self
):
source
=
"""
...
...
@@ -429,12 +420,11 @@ class Test {
template <typename T0, typename T1>
class MockTest : public Test<T0, T1> {
public:
MOCK_METHOD0_T(Foo,
void());
MOCK_METHOD(void, Foo, (), (override));
};
"""
self
.
assertEqualIgnoreLeadingWhitespace
(
expected
,
self
.
GenerateMocks
(
source
))
self
.
assertEqualIgnoreLeadingWhitespace
(
expected
,
self
.
GenerateMocks
(
source
))
def
testTemplateInATemplateTypedef
(
self
):
source
=
"""
...
...
@@ -447,12 +437,11 @@ class Test {
expected
=
"""
\
class MockTest : public Test {
public:
MOCK_METHOD1(Bar,
void(const FooType& test_arg));
MOCK_METHOD(void, Bar, (const FooType& test_arg), (override));
};
"""
self
.
assertEqualIgnoreLeadingWhitespace
(
expected
,
self
.
GenerateMocks
(
source
))
self
.
assertEqualIgnoreLeadingWhitespace
(
expected
,
self
.
GenerateMocks
(
source
))
def
testTemplateInATemplateTypedefWithComma
(
self
):
source
=
"""
...
...
@@ -466,12 +455,11 @@ class Test {
expected
=
"""
\
class MockTest : public Test {
public:
MOCK_METHOD1(Bar,
void(const FooType& test_arg));
MOCK_METHOD(void, Bar, (const FooType& test_arg), (override));
};
"""
self
.
assertEqualIgnoreLeadingWhitespace
(
expected
,
self
.
GenerateMocks
(
source
))
self
.
assertEqualIgnoreLeadingWhitespace
(
expected
,
self
.
GenerateMocks
(
source
))
def
testEnumType
(
self
):
source
=
"""
...
...
@@ -486,12 +474,11 @@ class Test {
expected
=
"""
\
class MockTest : public Test {
public:
MOCK_METHOD0(Foo,
void());
MOCK_METHOD(void, Foo, (), (override));
};
"""
self
.
assertEqualIgnoreLeadingWhitespace
(
expected
,
self
.
GenerateMocks
(
source
))
self
.
assertEqualIgnoreLeadingWhitespace
(
expected
,
self
.
GenerateMocks
(
source
))
def
testEnumClassType
(
self
):
source
=
"""
...
...
@@ -506,12 +493,11 @@ class Test {
expected
=
"""
\
class MockTest : public Test {
public:
MOCK_METHOD0(Foo,
void());
MOCK_METHOD(void, Foo, (), (override));
};
"""
self
.
assertEqualIgnoreLeadingWhitespace
(
expected
,
self
.
GenerateMocks
(
source
))
self
.
assertEqualIgnoreLeadingWhitespace
(
expected
,
self
.
GenerateMocks
(
source
))
def
testStdFunction
(
self
):
source
=
"""
...
...
@@ -528,12 +514,11 @@ class Test {
expected
=
"""
\
class MockTest : public Test {
public:
MOCK_METHOD0(foo,
std::function<int (std::string)>());
MOCK_METHOD(std::function<int (std::string)>, foo, (), (override));
};
"""
self
.
assertEqualIgnoreLeadingWhitespace
(
expected
,
self
.
GenerateMocks
(
source
))
self
.
assertEqualIgnoreLeadingWhitespace
(
expected
,
self
.
GenerateMocks
(
source
))
if
__name__
==
'__main__'
:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment