Allow any number of ClassNames to be specified on the command line.

0 ClassNames means emit all classes found in the file.
This commit is contained in:
nnorwitz 2009-05-06 05:57:09 +00:00
parent 60df3efe39
commit ce60784fb5
2 changed files with 18 additions and 7 deletions

View File

@ -14,7 +14,9 @@ to generate a Google Mock class.
Make sure to install the scripts somewhere in your path. Then you can Make sure to install the scripts somewhere in your path. Then you can
run the program. run the program.
gmock_gen.py header-file.h ClassName gmock_gen.py header-file.h [ClassName1] [ClassName2] ...
If no ClassNames are specified, all classes in the file are emitted.
To change the indentation from the default of 2, set INDENT in To change the indentation from the default of 2, set INDENT in
the environment. For example to use an indent of 4 spaces: the environment. For example to use an indent of 4 spaces:

View File

@ -20,7 +20,7 @@ This program will read in a C++ source file and output the Google Mock class
for the specified class. for the specified class.
Usage: Usage:
gmock_class.py header-file.h ClassName gmock_class.py header-file.h [ClassName1] [ClassName2] ...
Output is sent to stdout. Output is sent to stdout.
""" """
@ -79,10 +79,12 @@ def _GenerateMethods(output_lines, source, class_node):
output_lines.append(line) output_lines.append(line)
def _GenerateMock(filename, source, ast_list, class_name): def _GenerateMock(filename, source, ast_list, desired_class_names):
lines = [] lines = []
for node in ast_list: for node in ast_list:
if isinstance(node, ast.Class) and node.body and node.name == class_name: if (isinstance(node, ast.Class) and node.body and
(desired_class_names is None or node.name in desired_class_names)):
class_name = node.name
class_node = node class_node = node
# Add namespace before the class. # Add namespace before the class.
if class_node.namespace: if class_node.namespace:
@ -115,11 +117,15 @@ def _GenerateMock(filename, source, ast_list, class_name):
if lines: if lines:
sys.stdout.write('\n'.join(lines)) sys.stdout.write('\n'.join(lines))
else: else:
sys.stderr.write('Class %s not found\n' % class_name) 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)
def main(argv=sys.argv): def main(argv=sys.argv):
if len(argv) != 3: if len(argv) < 2:
sys.stdout.write(__doc__) sys.stdout.write(__doc__)
return 1 return 1
@ -131,7 +137,10 @@ def main(argv=sys.argv):
except: except:
sys.stderr.write('Unable to use indent of %s\n' % os.environ.get('INDENT')) sys.stderr.write('Unable to use indent of %s\n' % os.environ.get('INDENT'))
filename, class_name = argv[1:] filename = argv[1]
class_name = None
if len(argv) >= 3:
class_name = set(argv[2:])
source = utils.ReadFile(filename) source = utils.ReadFile(filename)
if source is None: if source is None:
return 1 return 1