| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # | 2 # |
| 3 # Copyright 2008 Google Inc. | 3 # Copyright 2008 Google Inc. All Rights Reserved. |
| 4 # | 4 # |
| 5 # Licensed under the Apache License, Version 2.0 (the "License"); | 5 # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 # you may not use this file except in compliance with 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 | 7 # You may obtain a copy of the License at |
| 8 # | 8 # |
| 9 # http://www.apache.org/licenses/LICENSE-2.0 | 9 # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 # | 10 # |
| 11 # Unless required by applicable law or agreed to in writing, software | 11 # Unless required by applicable law or agreed to in writing, software |
| 12 # distributed under the License is distributed on an "AS IS" BASIS, | 12 # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 66 return_type += '*' | 66 return_type += '*' |
| 67 if node.return_type.reference: | 67 if node.return_type.reference: |
| 68 return_type += '&' | 68 return_type += '&' |
| 69 prefix = 'MOCK_%sMETHOD%d' % (const, len(node.parameters)) | 69 prefix = 'MOCK_%sMETHOD%d' % (const, len(node.parameters)) |
| 70 args = '' | 70 args = '' |
| 71 if node.parameters: | 71 if node.parameters: |
| 72 # Get the full text of the parameters from the start | 72 # Get the full text of the parameters from the start |
| 73 # of the first parameter to the end of the last parameter. | 73 # of the first parameter to the end of the last parameter. |
| 74 start = node.parameters[0].start | 74 start = node.parameters[0].start |
| 75 end = node.parameters[-1].end | 75 end = node.parameters[-1].end |
| 76 args = re.sub(' +', ' ', source[start:end].replace('\n', '')) | 76 # Remove // comments. |
| 77 args_strings = re.sub(r'//.*', '', source[start:end]) |
| 78 # Condense multiple spaces and eliminate newlines putting the |
| 79 # parameters together on a single line. Ensure there is a |
| 80 # space in an argument which is split by a newline without |
| 81 # intervening whitespace, e.g.: int\nBar |
| 82 args = re.sub(' +', ' ', args_strings.replace('\n', ' ')) |
| 77 | 83 |
| 78 # Create the prototype. | 84 # Create the prototype. |
| 79 indent = ' ' * _INDENT | 85 indent = ' ' * _INDENT |
| 80 line = ('%s%s(%s,\n%s%s(%s));' % | 86 line = ('%s%s(%s,\n%s%s(%s));' % |
| 81 (indent, prefix, node.name, indent*3, return_type, args)) | 87 (indent, prefix, node.name, indent*3, return_type, args)) |
| 82 output_lines.append(line) | 88 output_lines.append(line) |
| 83 | 89 |
| 84 | 90 |
| 85 def _GenerateMocks(filename, source, ast_list, desired_class_names): | 91 def _GenerateMocks(filename, source, ast_list, desired_class_names): |
| 86 processed_class_names = sets.Set() | 92 processed_class_names = sets.Set() |
| (...skipping 26 matching lines...) Expand all Loading... |
| 113 # Only close the class if there really is a class. | 119 # Only close the class if there really is a class. |
| 114 lines.append('};') | 120 lines.append('};') |
| 115 lines.append('') # Add an extra newline. | 121 lines.append('') # Add an extra newline. |
| 116 | 122 |
| 117 # Close the namespace. | 123 # Close the namespace. |
| 118 if class_node.namespace: | 124 if class_node.namespace: |
| 119 for i in range(len(class_node.namespace)-1, -1, -1): | 125 for i in range(len(class_node.namespace)-1, -1, -1): |
| 120 lines.append('} // namespace %s' % class_node.namespace[i]) | 126 lines.append('} // namespace %s' % class_node.namespace[i]) |
| 121 lines.append('') # Add an extra newline. | 127 lines.append('') # Add an extra newline. |
| 122 | 128 |
| 123 sys.stdout.write('\n'.join(lines)) | |
| 124 | |
| 125 if desired_class_names: | 129 if desired_class_names: |
| 126 missing_class_name_list = list(desired_class_names - processed_class_names) | 130 missing_class_name_list = list(desired_class_names - processed_class_names) |
| 127 if missing_class_name_list: | 131 if missing_class_name_list: |
| 128 missing_class_name_list.sort() | 132 missing_class_name_list.sort() |
| 129 sys.stderr.write('Class(es) not found in %s: %s\n' % | 133 sys.stderr.write('Class(es) not found in %s: %s\n' % |
| 130 (filename, ', '.join(missing_class_name_list))) | 134 (filename, ', '.join(missing_class_name_list))) |
| 131 elif not processed_class_names: | 135 elif not processed_class_names: |
| 132 sys.stderr.write('No class found in %s\n' % filename) | 136 sys.stderr.write('No class found in %s\n' % filename) |
| 137 |
| 138 return lines |
| 133 | 139 |
| 134 | 140 |
| 135 def main(argv=sys.argv): | 141 def main(argv=sys.argv): |
| 136 if len(argv) < 2: | 142 if len(argv) < 2: |
| 137 sys.stderr.write('Google Mock Class Generator v%s\n\n' % | 143 sys.stderr.write('Google Mock Class Generator v%s\n\n' % |
| 138 '.'.join(map(str, _VERSION))) | 144 '.'.join(map(str, _VERSION))) |
| 139 sys.stderr.write(__doc__) | 145 sys.stderr.write(__doc__) |
| 140 return 1 | 146 return 1 |
| 141 | 147 |
| 142 global _INDENT | 148 global _INDENT |
| (...skipping 14 matching lines...) Expand all Loading... |
| 157 | 163 |
| 158 builder = ast.BuilderFromSource(source, filename) | 164 builder = ast.BuilderFromSource(source, filename) |
| 159 try: | 165 try: |
| 160 entire_ast = filter(None, builder.Generate()) | 166 entire_ast = filter(None, builder.Generate()) |
| 161 except KeyboardInterrupt: | 167 except KeyboardInterrupt: |
| 162 return | 168 return |
| 163 except: | 169 except: |
| 164 # An error message was already printed since we couldn't parse. | 170 # An error message was already printed since we couldn't parse. |
| 165 pass | 171 pass |
| 166 else: | 172 else: |
| 167 _GenerateMocks(filename, source, entire_ast, desired_class_names) | 173 lines = _GenerateMocks(filename, source, entire_ast, desired_class_names) |
| 174 sys.stdout.write('\n'.join(lines)) |
| 168 | 175 |
| 169 | 176 |
| 170 if __name__ == '__main__': | 177 if __name__ == '__main__': |
| 171 main(sys.argv) | 178 main(sys.argv) |
| OLD | NEW |