Skip to content

Instantly share code, notes, and snippets.

@spoorcc
Created January 28, 2015 22:17
Show Gist options
  • Save spoorcc/16f587acbc28c01a192a to your computer and use it in GitHub Desktop.
Save spoorcc/16f587acbc28c01a192a to your computer and use it in GitHub Desktop.
#! /usr/bin/env python
import compiler
from compiler.visitor import ASTVisitor
class Function(object):
def __init__(self, name = None):
self.funcs_to_stub = []
self.decision_paths = 1
self.name = name or ""
class SimpleVisitor(ASTVisitor):
def __init__(self):
ASTVisitor.__init__(self)
self.current_func = Function("main")
def visitFunction(self, node):
parent_func = self.current_func
self.current_func = Function(node.name)
for node in node.getChildNodes():
self.visit(node)
print_needed_stubs(self.current_func)
self.current_func = parent_func
def visitCallFunc(self, node):
#print "%s is called from %s on line: %d" % (node.node.name, self.current_func, node.lineno)
current_stubs = [func.name for func in self.current_func.funcs_to_stub]
if node.node.name not in current_stubs:
self.current_func.funcs_to_stub += [Function(node.node.name)]
def visitIf(self, node):
#print "%s has an if statement on line %d" % (self.current_func, node.lineno)
self.current_func.decision_paths *= 2
def print_needed_stubs(func):
print "\n" + ">"*10 + " Function under test: " + func.name
print "Create %d testcases for 100%% decision coverage" % func.decision_paths
for stubfunc in func.funcs_to_stub:
print "Create %s stub " % (stubfunc.name)
def parse(filename):
parsed_file = compiler.parseFile(filename)
visitor = SimpleVisitor()
compiler.walk(parsed_file, visitor, walker=visitor)
parse("python_ast_test.py")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment