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 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 [ClassName1] [ClassName2] ... gmock_gen.py header-file.h [ClassName]...
If no ClassNames are specified, all classes in the file are emitted. 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 # See the License for the specific language governing permissions and
# limitations under the License. # 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 This program will read in a C++ source file and output the Google Mock
for the specified class. classes for the specified classes. If no class is specified, all
classes in the source file are emitted.
Usage: Usage:
gmock_class.py header-file.h [ClassName1] [ClassName2] ... gmock_class.py header-file.h [ClassName]...
Output is sent to stdout. Output is sent to stdout.
""" """
@ -35,7 +36,8 @@ import sys
from cpp import ast from cpp import ast
from cpp import utils 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 _INDENT = 2
@ -54,7 +56,7 @@ def _GenerateMethods(output_lines, source, class_node):
const = 'CONST_' const = 'CONST_'
return_type = 'void' return_type = 'void'
if node.return_type: if node.return_type:
# Add modifier bits like const. # Add modifiers like 'const'.
modifiers = '' modifiers = ''
if node.return_type.modifiers: if node.return_type.modifiers:
modifiers = ' '.join(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) 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 = [] lines = []
for node in ast_list: for node in ast_list:
if (isinstance(node, ast.Class) and node.body and if (isinstance(node, ast.Class) and node.body and
(desired_class_names is None or node.name in desired_class_names)): # 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 class_name = node.name
processed_class_names.add(class_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:
@ -114,19 +119,23 @@ def _GenerateMock(filename, source, ast_list, desired_class_names):
lines.append('} // namespace %s' % class_node.namespace[i]) lines.append('} // namespace %s' % class_node.namespace[i])
lines.append('') # Add an extra newline. lines.append('') # Add an extra newline.
if lines:
sys.stdout.write('\n'.join(lines)) sys.stdout.write('\n'.join(lines))
else:
if desired_class_names is None: if desired_class_names:
sys.stderr.write('No classes not found\n') missing_class_names = ', '.join(
else: sorted(desired_class_names - processed_class_names))
class_names = ', '.join(sorted(desired_class_names)) if missing_class_names:
sys.stderr.write('Class(es) not found: %s\n' % 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): def main(argv=sys.argv):
if len(argv) < 2: 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 return 1
global _INDENT 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')) sys.stderr.write('Unable to use indent of %s\n' % os.environ.get('INDENT'))
filename = argv[1] filename = argv[1]
class_name = None desired_class_names = None # None means all classes in the source file.
if len(argv) >= 3: if len(argv) >= 3:
class_name = set(argv[2:]) desired_class_names = set(argv[2:])
source = utils.ReadFile(filename) source = utils.ReadFile(filename)
if source is None: if source is None:
return 1 return 1
@ -154,7 +163,7 @@ def main(argv=sys.argv):
# An error message was already printed since we couldn't parse. # An error message was already printed since we couldn't parse.
pass pass
else: else:
_GenerateMock(filename, source, entire_ast, class_name) _GenerateMocks(filename, source, entire_ast, desired_class_names)
if __name__ == '__main__': if __name__ == '__main__':