Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4)

Side by Side Diff: testing/gmock/scripts/generator/cpp/gmock_class.py

Issue 115846: Retry to checkin a version of gmock, modified to use our boost_tuple in VS2005. (Closed)
Patch Set: Created 11 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 #!/usr/bin/env python
2 #
3 # Copyright 2008 Google Inc.
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
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__ = 'nnorwitz@google.com (Neal Norwitz)'
30
31
32 import os
33 import re
34 import sets
35 import sys
36
37 from cpp import ast
38 from cpp import utils
39
40 _VERSION = (1, 0, 1) # The version of this script.
41 # How many spaces to indent. Can set me with the INDENT environment variable.
42 _INDENT = 2
43
44
45 def _GenerateMethods(output_lines, source, class_node):
46 function_type = ast.FUNCTION_VIRTUAL | ast.FUNCTION_PURE_VIRTUAL
47 ctor_or_dtor = ast.FUNCTION_CTOR | ast.FUNCTION_DTOR
48
49 for node in class_node.body:
50 # We only care about virtual functions.
51 if (isinstance(node, ast.Function) and
52 node.modifiers & function_type and
53 not node.modifiers & ctor_or_dtor):
54 # Pick out all the elements we need from the original function.
55 const = ''
56 if node.modifiers & ast.FUNCTION_CONST:
57 const = 'CONST_'
58 return_type = 'void'
59 if node.return_type:
60 # Add modifiers like 'const'.
61 modifiers = ''
62 if node.return_type.modifiers:
63 modifiers = ' '.join(node.return_type.modifiers) + ' '
64 return_type = modifiers + node.return_type.name
65 if node.return_type.pointer:
66 return_type += '*'
67 if node.return_type.reference:
68 return_type += '&'
69 prefix = 'MOCK_%sMETHOD%d' % (const, len(node.parameters))
70 args = ''
71 if node.parameters:
72 # Get the full text of the parameters from the start
73 # of the first parameter to the end of the last parameter.
74 start = node.parameters[0].start
75 end = node.parameters[-1].end
76 args = re.sub(' +', ' ', source[start:end].replace('\n', ''))
77
78 # Create the prototype.
79 indent = ' ' * _INDENT
80 line = ('%s%s(%s,\n%s%s(%s));' %
81 (indent, prefix, node.name, indent*3, return_type, args))
82 output_lines.append(line)
83
84
85 def _GenerateMocks(filename, source, ast_list, desired_class_names):
86 processed_class_names = sets.Set()
87 lines = []
88 for node in ast_list:
89 if (isinstance(node, ast.Class) and node.body and
90 # desired_class_names being None means that all classes are selected.
91 (not desired_class_names or node.name in desired_class_names)):
92 class_name = node.name
93 processed_class_names.add(class_name)
94 class_node = node
95 # Add namespace before the class.
96 if class_node.namespace:
97 lines.extend(['namespace %s {' % n for n in class_node.namespace]) # }
98 lines.append('')
99
100 # Add the class prolog.
101 lines.append('class Mock%s : public %s {' % (class_name, class_name)) # }
102 lines.append('%spublic:' % (' ' * (_INDENT // 2)))
103
104 # Add all the methods.
105 _GenerateMethods(lines, source, class_node)
106
107 # Close the class.
108 if lines:
109 # If there are no virtual methods, no need for a public label.
110 if len(lines) == 2:
111 del lines[-1]
112
113 # Only close the class if there really is a class.
114 lines.append('};')
115 lines.append('') # Add an extra newline.
116
117 # Close the namespace.
118 if class_node.namespace:
119 for i in range(len(class_node.namespace)-1, -1, -1):
120 lines.append('} // namespace %s' % class_node.namespace[i])
121 lines.append('') # Add an extra newline.
122
123 sys.stdout.write('\n'.join(lines))
124
125 if desired_class_names:
126 missing_class_name_list = list(desired_class_names - processed_class_names)
127 if missing_class_name_list:
128 missing_class_name_list.sort()
129 sys.stderr.write('Class(es) not found in %s: %s\n' %
130 (filename, ', '.join(missing_class_name_list)))
131 elif not processed_class_names:
132 sys.stderr.write('No class found in %s\n' % filename)
133
134
135 def main(argv=sys.argv):
136 if len(argv) < 2:
137 sys.stderr.write('Google Mock Class Generator v%s\n\n' %
138 '.'.join(map(str, _VERSION)))
139 sys.stderr.write(__doc__)
140 return 1
141
142 global _INDENT
143 try:
144 _INDENT = int(os.environ['INDENT'])
145 except KeyError:
146 pass
147 except:
148 sys.stderr.write('Unable to use indent of %s\n' % os.environ.get('INDENT'))
149
150 filename = argv[1]
151 desired_class_names = None # None means all classes in the source file.
152 if len(argv) >= 3:
153 desired_class_names = sets.Set(argv[2:])
154 source = utils.ReadFile(filename)
155 if source is None:
156 return 1
157
158 builder = ast.BuilderFromSource(source, filename)
159 try:
160 entire_ast = filter(None, builder.Generate())
161 except KeyboardInterrupt:
162 return
163 except:
164 # An error message was already printed since we couldn't parse.
165 pass
166 else:
167 _GenerateMocks(filename, source, entire_ast, desired_class_names)
168
169
170 if __name__ == '__main__':
171 main(sys.argv)
OLDNEW
« no previous file with comments | « testing/gmock/scripts/generator/cpp/ast.py ('k') | testing/gmock/scripts/generator/cpp/keywords.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698