Cleans up the mock generator script:

- updates the doc string.
- adds a version number.
- fixes the condition for error messages in _GenerateMocks().
This commit is contained in:
zhanyong.wan 2009-05-07 20:38:25 +00:00
parent ce60784fb5
commit 84b8e4c65d
2 changed files with 31 additions and 22 deletions

View File

@ -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 [ClassName1] [ClassName2] ...
gmock_gen.py header-file.h [ClassName]...
If no ClassNames are specified, all classes in the file are emitted.

View File

@ -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 class.
"""Generate Google Mock classes from base classes.
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 [ClassName1] [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 modifiers 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)
_GenerateMocks(filename, source, entire_ast, desired_class_names)
if __name__ == '__main__':