def main(argv=sys.argv): if len(argv) != 3: sys.stdout.write(__doc__) return 1 global _INDENT try: _INDENT = int(os.environ['INDENT']) except KeyError: pass except: sys.stderr.write('Unable to use indent of %s\n' % os.environ.get('INDENT')) filename, class_name = argv[1:] source = utils.ReadFile(filename) if source is None: return 1 builder = ast.BuilderFromSource(source, filename) try: entire_ast = filter(None, builder.Generate()) except KeyboardInterrupt: return except: # An error message was already printed since we couldn't parse. pass else: _GenerateMock(filename, source, entire_ast, class_name)
def main(argv=sys.argv): if len(argv) < 2: sys.stderr.write('Google Mock Class Generator v%s\n\n' % '.'.join(map(str, _VERSION))) sys.stderr.write(__doc__) return 1 global _INDENT try: _INDENT = int(os.environ['INDENT']) except KeyError: pass except: sys.stderr.write('Unable to use indent of %s\n' % os.environ.get('INDENT')) filename = argv[1] desired_class_names = None # None means all classes in the src file. if len(argv) >= 3: desired_class_names = set(argv[2:]) source = utils.ReadFile(filename) if source is None: return 1 builder = ast.BuilderFromSource(source, filename) try: entire_ast = filter(None, builder.Generate()) except KeyboardInterrupt: return except: # An error message was already printed since we couldn't parse. sys.exit(1) else: lines = _GenerateMocks(filename, source, entire_ast, desired_class_names) sys.stdout.write('\n'.join(lines))
def _GetModule(self, filename): if filename in self._module_cache: # The cache survives across all instances, but the symbol table # is per instance, so we need to make sure the symbol table # is updated even if the module was in the cache. module = self._module_cache[filename] self._UpdateSymbolTable(module) return module source, actual_filename = headers.ReadSource(filename) if source is None: module = Module(filename, None) print('Unable to find %s' % filename) else: builder = ast.BuilderFromSource(source, filename) try: module = Module(filename, filter(None, builder.Generate())) except KeyboardInterrupt: sys.exit(1) except: print('Exception while processing %s' % filename) module = Module(filename, None) else: self._UpdateSymbolTable(module) self._module_cache[filename] = module return module
def run(filepath): global _INDENT try: _INDENT = int(os.environ['INDENT']) except KeyError: pass except: sys.stderr.write('Unable to use indent of %s\n' % os.environ.get('INDENT')) desired_class_names = None # None means all classes in the source file. source = utils.ReadFile(filepath) if source is None: return 1 builder = ast.BuilderFromSource(source, filepath) try: entire_ast = filter(None, builder.Generate()) except KeyboardInterrupt: return except: # An error message was already printed since we couldn't parse. sys.exit(1) else: lines = _GenerateTests(filepath, source, entire_ast) return lines
def main(argv=sys.argv): if len(argv) < 3: sys.stderr.write('Cyphesis Stub Class Generator v%s\n\n' % '.'.join(map(str, _VERSION))) sys.stderr.write(__doc__) return 1 global _INDENT try: _INDENT = int(os.environ['INDENT']) except KeyError: pass except: sys.stderr.write('Unable to use indent of %s\n' % os.environ.get('INDENT')) filename = argv[1] stubdirectory = argv[2] source = utils.ReadFile(filename) if source is None: return 1 builder = ast.BuilderFromSource(source, filename) try: entire_ast = filter(None, builder.Generate()) except KeyboardInterrupt: return except: # An error message was already printed since we couldn't parse. sys.exit(1) else: filenameSegments = filename.split("/") customFileName = 'stub' + ''.join(filenameSegments[-1].split('.')[0:-1]) + '_custom.h' if len(filenameSegments) > 1: stubpath = stubdirectory + '/' + '/'.join(filenameSegments[0:-1]) + "/stub" + filenameSegments[-1] stubCustomPath = stubdirectory + '/' + '/'.join(filenameSegments[0:-1]) + "/" + customFileName else: stubpath = stubdirectory + "/stub" + filenameSegments[-1] stubCustomPath = stubdirectory + "/" + customFileName try: os.makedirs(os.path.dirname(stubpath)) except OSError as exc: # Python >2.5 if exc.errno == errno.EEXIST and os.path.isdir(os.path.dirname(stubpath)): pass else: raise lines = _GenerateStubs(filename, source, entire_ast) with open(stubpath, 'w') as stubFile: stubFile.write('\n'.join(lines)) sys.stdout.write("Wrote to %s\n" % stubpath) if not os.path.isfile(stubCustomPath): with open(stubCustomPath, 'w') as stubCustomFile: stubCustomFile.write('//Add custom implementations of stubbed functions here; this file won\'t be rewritten when re-generating stubs.\n') sys.stdout.write("Also created empty custom stub file at %s\n" % stubCustomPath)
def GenerateMethodSource(cpp_source): """Convert C++ source to Google Mock output source lines.""" method_source_lines = [] # <test> is a pseudo-filename, it is not read or written. builder = ast.BuilderFromSource(cpp_source, '<test>') ast_list = list(builder.Generate()) gmock_class._GenerateMethods(method_source_lines, cpp_source, ast_list[0]) return '\n'.join(method_source_lines)
def GenerateMocks(cpp_source): """Convert C++ source to complete Google Mock output source.""" # <test> is a pseudo-filename, it is not read or written. filename = '<test>' builder = ast.BuilderFromSource(cpp_source, filename) ast_list = list(builder.Generate()) lines = gmock_class._GenerateMocks(filename, cpp_source, ast_list, None) return '\n'.join(lines)
def main(argv): for filename in argv[1:]: source = utils.ReadFile(filename) if source is None: continue print('Processing %s' % filename) builder = ast.BuilderFromSource(source, filename) entire_ast = list(filter(None, builder.Generate())) hunter = WarningHunter(filename, source, entire_ast) hunter.FindWarnings() hunter.ShowWarnings()
def main(argv): for filename in argv[1:]: source = utils.ReadFile(filename) if source is None: continue print 'Processing', filename builder = ast.BuilderFromSource(source, filename) try: entire_ast = filter(None, builder.Generate()) except KeyboardInterrupt: return except: # An error message was already printed since we couldn't parse. pass else: _FindWarnings(filename, source, entire_ast, True)
def GetHeaders(filename): source, actual_filename = ReadSource(filename) if source is None: print('Unable to find %s' % filename) return [] included_files = [] print('Processing %s' % actual_filename) builder = ast.BuilderFromSource(source, filename) for node in builder.Generate(): if isinstance(node, ast.Include): if not node.system: print(node.filename) included_files.append(node.filename) # Transitively process all the files that were included. if _TRANSITIVE: for filename in included_files: included_files.extend(GetHeaders(filename)) return included_files
def main(argv=sys.argv): parser = argparse.ArgumentParser(description="Simple generator for gmock functions", epilog=__doc__) parser.add_argument('--bases', dest='bases', action='store_true', help='include functions from base classes') parser.add_argument('header', nargs='?', help='header file', default='') parser.add_argument('classes', metavar='class_name', nargs='*', help='generate mocks for only these classes') args = parser.parse_args() if not args.header: sys.stderr.write('Google Mock Class Generator v%s\n\n' % '.'.join(map(str, _VERSION))) parser.print_help() return 1 global _INDENT try: _INDENT = int(os.environ['INDENT']) except KeyError: pass except: sys.stderr.write('Unable to use indent of %s\n' % os.environ.get('INDENT')) filename = args.header desired_class_names = set(args.classes) # None means all classes in the source file. do_bases = args.bases source = utils.ReadFile(filename) if source is None: return 1 builder = ast.BuilderFromSource(source, filename) try: entire_ast = filter(None, builder.Generate()) except KeyboardInterrupt: return except: # An error message was already printed since we couldn't parse. sys.exit(1) else: lines = _GenerateMocks(filename, source, entire_ast, desired_class_names, do_bases) sys.stdout.write('\n'.join(lines))
def main(): # Parse options. usage = '\nGoogle Mock Class Generator v%s\n\n' % '.'.join( map(str, _VERSION)) usage += __doc__ usage = usage.rstrip() parser = OptionParser(usage) parser.add_option( '-p', '--partial', action='store_true', dest='partial', help= 'Generate a partial mock instead of just a mock (*NOTE* methods with unnamed parameters are unsupported and will cause problems.' ) (options, parsed_args) = parser.parse_args() # Set global flag for generated file type. global _PARTIAL if options.partial: _PARTIAL = True else: _PARTIAL = False # Create arguments, taking out any options. argv = [sys.argv[0]] + parsed_args if len(argv) < 2: parser.print_help() return 1 global _INDENT try: _INDENT = int(os.environ['INDENT']) except KeyError: pass except: sys.stderr.write('Unable to use indent of %s\n' % os.environ.get('INDENT')) filename = argv[1] desired_class_names = None # None means all classes in the source file. if len(argv) >= 3: desired_class_names = set(argv[2:]) source = utils.ReadFile(filename) if source is None: return 1 builder = ast.BuilderFromSource(source, filename) try: entire_ast = filter(None, builder.Generate()) except KeyboardInterrupt: return except: # An error message was already printed since we couldn't parse. pass else: lines = _GenerateMocks(filename, source, entire_ast, desired_class_names) sys.stdout.write('\n'.join(lines))