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
84b8e4c6
Commit
84b8e4c6
authored
May 07, 2009
by
zhanyong.wan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Cleans up the mock generator script:
- updates the doc string. - adds a version number. - fixes the condition for error messages in _GenerateMocks().
parent
ce60784f
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
31 additions
and
22 deletions
+31
-22
README
scripts/generator/README
+1
-1
gmock_class.py
scripts/generator/cpp/gmock_class.py
+30
-21
No files found.
scripts/generator/README
View file @
84b8e4c6
...
...
@@ -14,7 +14,7 @@ to generate a Google Mock class.
Make sure to install the scripts somewhere in your path. Then you can
run the program.
gmock_gen.py header-file.h [ClassName
1] [ClassName2]
...
gmock_gen.py header-file.h [ClassName
]
...
If no ClassNames are specified, all classes in the file are emitted.
...
...
scripts/generator/cpp/gmock_class.py
View file @
84b8e4c6
...
...
@@ -14,13 +14,14 @@
# See the License for the specific language governing permissions and
# limitations under the License.
"""Generate
a Google Mock class from a production clas
s.
"""Generate
Google Mock classes from base classe
s.
This program will read in a C++ source file and output the Google Mock class
for the specified class.
This program will read in a C++ source file and output the Google Mock
classes for the specified classes. If no class is specified, all
classes in the source file are emitted.
Usage:
gmock_class.py header-file.h [ClassName
1] [ClassName2]
...
gmock_class.py header-file.h [ClassName
]
...
Output is sent to stdout.
"""
...
...
@@ -35,7 +36,8 @@ import sys
from
cpp
import
ast
from
cpp
import
utils
# How many spaces to indent. Can set me with INDENT environment variable.
_VERSION
=
(
1
,
0
,
1
)
# The version of this script.
# How many spaces to indent. Can set me with the INDENT environment variable.
_INDENT
=
2
...
...
@@ -54,7 +56,7 @@ def _GenerateMethods(output_lines, source, class_node):
const
=
'CONST_'
return_type
=
'void'
if
node
.
return_type
:
# Add modifier
bits like const
.
# Add modifier
s like 'const'
.
modifiers
=
''
if
node
.
return_type
.
modifiers
:
modifiers
=
' '
.
join
(
node
.
return_type
.
modifiers
)
+
' '
...
...
@@ -79,12 +81,15 @@ def _GenerateMethods(output_lines, source, class_node):
output_lines
.
append
(
line
)
def
_GenerateMock
(
filename
,
source
,
ast_list
,
desired_class_names
):
def
_GenerateMocks
(
filename
,
source
,
ast_list
,
desired_class_names
):
processed_class_names
=
set
()
lines
=
[]
for
node
in
ast_list
:
if
(
isinstance
(
node
,
ast
.
Class
)
and
node
.
body
and
(
desired_class_names
is
None
or
node
.
name
in
desired_class_names
)):
if
(
isinstance
(
node
,
ast
.
Class
)
and
node
.
body
and
# desired_class_names being None means that all classes are selected.
(
not
desired_class_names
or
node
.
name
in
desired_class_names
)):
class_name
=
node
.
name
processed_class_names
.
add
(
class_name
)
class_node
=
node
# Add namespace before the class.
if
class_node
.
namespace
:
...
...
@@ -114,19 +119,23 @@ def _GenerateMock(filename, source, ast_list, desired_class_names):
lines
.
append
(
'} // namespace
%
s'
%
class_node
.
namespace
[
i
])
lines
.
append
(
''
)
# Add an extra newline.
if
lines
:
sys
.
stdout
.
write
(
'
\n
'
.
join
(
lines
))
else
:
if
desired_class_names
is
None
:
sys
.
stderr
.
write
(
'No classes not found
\n
'
)
else
:
class_names
=
', '
.
join
(
sorted
(
desired_class_names
))
sys
.
stderr
.
write
(
'Class(es) not found:
%
s
\n
'
%
class_names
)
sys
.
stdout
.
write
(
'
\n
'
.
join
(
lines
))
if
desired_class_names
:
missing_class_names
=
', '
.
join
(
sorted
(
desired_class_names
-
processed_class_names
))
if
missing_class_names
:
sys
.
stderr
.
write
(
'Class(es) not found in
%
s:
%
s
\n
'
%
(
filename
,
missing_class_names
))
elif
not
processed_class_names
:
sys
.
stderr
.
write
(
'No class found in
%
s
\n
'
%
filename
)
def
main
(
argv
=
sys
.
argv
):
if
len
(
argv
)
<
2
:
sys
.
stdout
.
write
(
__doc__
)
sys
.
stderr
.
write
(
'Google Mock Class Generator v
%
s
\n\n
'
%
'.'
.
join
(
map
(
str
,
_VERSION
)))
sys
.
stderr
.
write
(
__doc__
)
return
1
global
_INDENT
...
...
@@ -138,9 +147,9 @@ def main(argv=sys.argv):
sys
.
stderr
.
write
(
'Unable to use indent of
%
s
\n
'
%
os
.
environ
.
get
(
'INDENT'
))
filename
=
argv
[
1
]
class_name
=
None
desired_class_names
=
None
# None means all classes in the source file.
if
len
(
argv
)
>=
3
:
class_name
=
set
(
argv
[
2
:])
desired_class_names
=
set
(
argv
[
2
:])
source
=
utils
.
ReadFile
(
filename
)
if
source
is
None
:
return
1
...
...
@@ -154,7 +163,7 @@ def main(argv=sys.argv):
# An error message was already printed since we couldn't parse.
pass
else
:
_GenerateMock
(
filename
,
source
,
entire_ast
,
class_name
)
_GenerateMock
s
(
filename
,
source
,
entire_ast
,
desired_class_names
)
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