diff options
author | zhanyong.wan <zhanyong.wan@8415998a-534a-0410-bf83-d39667b30386> | 2009-05-07 20:38:25 +0000 |
---|---|---|
committer | zhanyong.wan <zhanyong.wan@8415998a-534a-0410-bf83-d39667b30386> | 2009-05-07 20:38:25 +0000 |
commit | 84b8e4c65d0847ab4262bb70619182292482529a (patch) | |
tree | c17044f73f5a1cc1310222b7ae2495cdf51a6d01 /scripts/generator/cpp/gmock_class.py | |
parent | ce60784fb51a5a0e28c14edd53bacbf0d2abb36b (diff) | |
download | googletest-84b8e4c65d0847ab4262bb70619182292482529a.tar.gz googletest-84b8e4c65d0847ab4262bb70619182292482529a.tar.bz2 googletest-84b8e4c65d0847ab4262bb70619182292482529a.zip |
Cleans up the mock generator script:
- updates the doc string.
- adds a version number.
- fixes the condition for error messages in _GenerateMocks().
Diffstat (limited to 'scripts/generator/cpp/gmock_class.py')
-rwxr-xr-x | scripts/generator/cpp/gmock_class.py | 51 |
1 files changed, 30 insertions, 21 deletions
diff --git a/scripts/generator/cpp/gmock_class.py b/scripts/generator/cpp/gmock_class.py index ab2da32d..ba11f9e6 100755 --- a/scripts/generator/cpp/gmock_class.py +++ b/scripts/generator/cpp/gmock_class.py @@ -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__': |