| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
"""Generate Google Mock classes from base classes. |
| 18 |
|
| 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. |
| 22 |
|
| 23 |
Usage: |
| 24 |
gmock_class.py header-file.h [ClassName]... |
| 25 |
|
| 26 |
Output is sent to stdout. |
| 27 |
""" |
| 28 |
|
| 29 |
__author__ = '[email protected] (Neal Norwitz)' |
| 30 |
|
| 31 |
|
| 32 |
import os |
| 33 |
import re |
| 34 |
import sys |
| 35 |
|
| 36 |
from cpp import ast |
| 37 |
from cpp import utils |
| 38 |
|
| 39 |
|
| 40 |
try: |
| 41 |
_dummy = set |
| 42 |
except NameError: |
| 43 |
import sets |
| 44 |
set = sets.Set |
| 45 |
|
| 46 |
_VERSION = (1, 0, 1) |
| 47 |
|
| 48 |
_INDENT = 2 |
| 49 |
|
| 50 |
|
| 51 |
def _GenerateMethods(output_lines, source, class_node): |
| 52 |
function_type = (ast.FUNCTION_VIRTUAL | ast.FUNCTION_PURE_VIRTUAL | |
| 53 |
ast.FUNCTION_OVERRIDE) |
| 54 |
ctor_or_dtor = ast.FUNCTION_CTOR | ast.FUNCTION_DTOR |
| 55 |
indent = ' ' * _INDENT |
| 56 |
|
| 57 |
for node in class_node.body: |
| 58 |
|
| 59 |
if (isinstance(node, ast.Function) and |
| 60 |
node.modifiers & function_type and |
| 61 |
not node.modifiers & ctor_or_dtor): |
| 62 |
|
| 63 |
const = '' |
| 64 |
if node.modifiers & ast.FUNCTION_CONST: |
| 65 |
const = 'CONST_' |
| 66 |
return_type = 'void' |
| 67 |
if node.return_type: |
| 68 |
|
| 69 |
modifiers = '' |
| 70 |
if node.return_type.modifiers: |
| 71 |
modifiers = ' '.join(node.return_type.modifiers) + ' ' |
| 72 |
return_type = modifiers + node.return_type.name |
| 73 |
template_args = [arg.name for arg in node.return_type.templated_types] |
| 74 |
if template_args: |
| 75 |
return_type += '<' + ', '.join(template_args) + '>' |
| 76 |
if len(template_args) > 1: |
| 77 |
for line in [ |
| 78 |
'// The following line won\'t really compile, as the return', |
| 79 |
'// type has multiple template arguments. To fix it, use a', |
| 80 |
'// typedef for the return type.']: |
| 81 |
output_lines.append(indent + line) |
| 82 |
if node.return_type.pointer: |
| 83 |
return_type += '*' |
| 84 |
if node.return_type.reference: |
| 85 |
return_type += '&' |
| 86 |
num_parameters = len(node.parameters) |
| 87 |
if len(node.parameters) == 1: |
| 88 |
first_param = node.parameters[0] |
| 89 |
if source[first_param.start:first_param.end].strip() == 'void': |
| 90 |
|
| 91 |
num_parameters = 0 |
| 92 |
tmpl = '' |
| 93 |
if class_node.templated_types: |
| 94 |
tmpl = '_T' |
| 95 |
mock_method_macro = 'MOCK_%sMETHOD%d%s' % (const, num_parameters, tmpl) |
| 96 |
|
| 97 |
args = '' |
| 98 |
if node.parameters: |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
if len([param for param in node.parameters if param.default]) > 0: |
| 107 |
args = ', '.join(param.type.name for param in node.parameters) |
| 108 |
else: |
| 109 |
|
| 110 |
|
| 111 |
start = node.parameters[0].start |
| 112 |
end = node.parameters[-1].end |
| 113 |
|
| 114 |
args_strings = re.sub(r'//.*', '', source[start:end]) |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
args = re.sub(' +', ' ', args_strings.replace('\n', ' ')) |
| 120 |
|
| 121 |
|
| 122 |
output_lines.extend(['%s%s(%s,' % (indent, mock_method_macro, node.name), |
| 123 |
'%s%s(%s));' % (indent*3, return_type, args)]) |
| 124 |
|
| 125 |
|
| 126 |
def _GenerateMocks(filename, source, ast_list, desired_class_names): |
| 127 |
processed_class_names = set() |
| 128 |
lines = [] |
| 129 |
for node in ast_list: |
| 130 |
if (isinstance(node, ast.Class) and node.body and |
| 131 |
|
| 132 |
(not desired_class_names or node.name in desired_class_names)): |
| 133 |
class_name = node.name |
| 134 |
parent_name = class_name |
| 135 |
processed_class_names.add(class_name) |
| 136 |
class_node = node |
| 137 |
|
| 138 |
if class_node.namespace: |
| 139 |
lines.extend(['namespace %s {' % n for n in class_node.namespace]) |
| 140 |
lines.append('') |
| 141 |
|
| 142 |
|
| 143 |
if class_node.templated_types: |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
template_arg_count = len(class_node.templated_types.keys()) |
| 149 |
template_args = ['T%d' % n for n in range(template_arg_count)] |
| 150 |
template_decls = ['typename ' + arg for arg in template_args] |
| 151 |
lines.append('template <' + ', '.join(template_decls) + '>') |
| 152 |
parent_name += '<' + ', '.join(template_args) + '>' |
| 153 |
|
| 154 |
|
| 155 |
lines.append('class Mock%s : public %s {' |
| 156 |
% (class_name, parent_name)) |
| 157 |
lines.append('%spublic:' % (' ' * (_INDENT // 2))) |
| 158 |
|
| 159 |
|
| 160 |
_GenerateMethods(lines, source, class_node) |
| 161 |
|
| 162 |
|
| 163 |
if lines: |
| 164 |
|
| 165 |
if len(lines) == 2: |
| 166 |
del lines[-1] |
| 167 |
|
| 168 |
|
| 169 |
lines.append('};') |
| 170 |
lines.append('') |
| 171 |
|
| 172 |
|
| 173 |
if class_node.namespace: |
| 174 |
for i in range(len(class_node.namespace)-1, -1, -1): |
| 175 |
lines.append('} // namespace %s' % class_node.namespace[i]) |
| 176 |
lines.append('') |
| 177 |
|
| 178 |
if desired_class_names: |
| 179 |
missing_class_name_list = list(desired_class_names - processed_class_names) |
| 180 |
if missing_class_name_list: |
| 181 |
missing_class_name_list.sort() |
| 182 |
sys.stderr.write('Class(es) not found in %s: %s\n' % |
| 183 |
(filename, ', '.join(missing_class_name_list))) |
| 184 |
elif not processed_class_names: |
| 185 |
sys.stderr.write('No class found in %s\n' % filename) |
| 186 |
|
| 187 |
return lines |
| 188 |
|
| 189 |
|
| 190 |
def main(argv=sys.argv): |
| 191 |
if len(argv) < 2: |
| 192 |
sys.stderr.write('Google Mock Class Generator v%s\n\n' % |
| 193 |
'.'.join(map(str, _VERSION))) |
| 194 |
sys.stderr.write(__doc__) |
| 195 |
return 1 |
| 196 |
|
| 197 |
global _INDENT |
| 198 |
try: |
| 199 |
_INDENT = int(os.environ['INDENT']) |
| 200 |
except KeyError: |
| 201 |
pass |
| 202 |
except: |
| 203 |
sys.stderr.write('Unable to use indent of %s\n' % os.environ.get('INDENT')) |
| 204 |
|
| 205 |
filename = argv[1] |
| 206 |
desired_class_names = None |
| 207 |
if len(argv) >= 3: |
| 208 |
desired_class_names = set(argv[2:]) |
| 209 |
source = utils.ReadFile(filename) |
| 210 |
if source is None: |
| 211 |
return 1 |
| 212 |
|
| 213 |
builder = ast.BuilderFromSource(source, filename) |
| 214 |
try: |
| 215 |
entire_ast = filter(None, builder.Generate()) |
| 216 |
except KeyboardInterrupt: |
| 217 |
return |
| 218 |
except: |
| 219 |
|
| 220 |
sys.exit(1) |
| 221 |
else: |
| 222 |
lines = _GenerateMocks(filename, source, entire_ast, desired_class_names) |
| 223 |
sys.stdout.write('\n'.join(lines)) |
| 224 |
|
| 225 |
|
| 226 |
if __name__ == '__main__': |
| 227 |
main(sys.argv) |