17 """Generate Google Mock classes from base classes. 19 This program will read in a C++ source file and output the Google Mock 20 classes for the specified classes. If no class is specified, all 21 classes in the source file are emitted. 24 gmock_class.py header-file.h [ClassName]... 26 Output is sent to stdout. 29 __author__ =
'nnorwitz@google.com (Neal Norwitz)' 52 function_type = ast.FUNCTION_VIRTUAL | ast.FUNCTION_PURE_VIRTUAL
53 ctor_or_dtor = ast.FUNCTION_CTOR | ast.FUNCTION_DTOR
54 indent =
' ' * _INDENT
56 for node
in class_node.body:
59 node.modifiers & function_type
and 60 not node.modifiers & ctor_or_dtor):
63 if node.modifiers & ast.FUNCTION_CONST:
69 if node.return_type.modifiers:
70 modifiers =
' '.join(node.return_type.modifiers) +
' ' 71 return_type = modifiers + node.return_type.name
72 template_args = [arg.name
for arg
in node.return_type.templated_types]
74 return_type +=
'<' +
', '.join(template_args) +
'>' 75 if len(template_args) > 1:
77 '// The following line won\'t really compile, as the return',
78 '// type has multiple template arguments. To fix it, use a',
79 '// typedef for the return type.']:
80 output_lines.append(indent + line)
81 if node.return_type.pointer:
83 if node.return_type.reference:
85 num_parameters = len(node.parameters)
86 if len(node.parameters) == 1:
87 first_param = node.parameters[0]
88 if source[first_param.start:first_param.end].strip() ==
'void':
92 if class_node.templated_types:
94 mock_method_macro =
'MOCK_%sMETHOD%d%s' % (const, num_parameters, tmpl)
105 if len([param
for param
in node.parameters
if param.default]) > 0:
106 args =
', '.join(param.type.name
for param
in node.parameters)
110 start = node.parameters[0].start
111 end = node.parameters[-1].end
113 args_strings = re.sub(
r'//.*',
'', source[start:end])
118 args = re.sub(
' +',
' ', args_strings.replace(
'\n',
' '))
121 output_lines.extend([
'%s%s(%s,' % (indent, mock_method_macro, node.name),
122 '%s%s(%s));' % (indent*3, return_type, args)])
126 processed_class_names =
set()
128 for node
in ast_list:
129 if (isinstance(node,
ast.Class)
and node.body
and 131 (
not desired_class_names
or node.name
in desired_class_names)):
132 class_name = node.name
133 parent_name = class_name
134 processed_class_names.add(class_name)
137 if class_node.namespace:
138 lines.extend([
'namespace %s {' % n
for n
in class_node.namespace])
142 if class_node.templated_types:
147 template_arg_count = len(list(class_node.templated_types.keys()))
148 template_args = [
'T%d' % n
for n
in range(template_arg_count)]
149 template_decls = [
'typename ' + arg
for arg
in template_args]
150 lines.append(
'template <' +
', '.join(template_decls) +
'>')
151 parent_name +=
'<' +
', '.join(template_args) +
'>' 154 lines.append(
'class Mock%s : public %s {' 155 % (class_name, parent_name))
156 lines.append(
'%spublic:' % (
' ' * (_INDENT // 2)))
172 if class_node.namespace:
173 for i
in range(len(class_node.namespace)-1, -1, -1):
174 lines.append(
'} // namespace %s' % class_node.namespace[i])
177 if desired_class_names:
178 missing_class_name_list = list(desired_class_names - processed_class_names)
179 if missing_class_name_list:
180 missing_class_name_list.sort()
181 sys.stderr.write(
'Class(es) not found in %s: %s\n' %
182 (filename,
', '.join(missing_class_name_list)))
183 elif not processed_class_names:
184 sys.stderr.write(
'No class found in %s\n' % filename)
191 sys.stderr.write(
'Google Mock Class Generator v%s\n\n' %
192 '.'.join(map(str, _VERSION)))
193 sys.stderr.write(__doc__)
198 _INDENT = int(os.environ[
'INDENT'])
202 sys.stderr.write(
'Unable to use indent of %s\n' % os.environ.get(
'INDENT'))
205 desired_class_names =
None 207 desired_class_names =
set(argv[2:])
208 source = utils.ReadFile(filename)
212 builder = ast.BuilderFromSource(source, filename)
214 entire_ast = [_f
for _f
in builder.Generate()
if _f]
215 except KeyboardInterrupt:
221 lines =
_GenerateMocks(filename, source, entire_ast, desired_class_names)
222 sys.stdout.write(
'\n'.join(lines))
225 if __name__ ==
'__main__':
def _GenerateMocks(filename, source, ast_list, desired_class_names)
def _GenerateMethods(output_lines, source, class_node)