Exemplo n.º 1
0
def update_nodes(tree):
    """
    Update the tests AST tree (in-place):
      1. add a print message in each test_* function
      2. add arguments in calls to check() function
    """
    for node in ast.walk(tree):
        if isinstance(node, ast.FunctionDef) and \
                node.name.startswith('test_'):
            # add a print at the beginning of each test function
            node.body.insert(
                0, ast.parse('weechat.prnt("", "  > %s");' % node.name))
        elif isinstance(node, ast.Call) and \
                isinstance(node.func, ast.Name) and \
                node.func.id == 'check':
            # add two arguments in the call to "check" function:
            #   1. the string representation of the test
            #   2. the line number in source (as string)
            # for example if this test is on line 50:
            #   check(weechat.test() == 123)
            # it becomes:
            #   check(weechat.test() == 123, 'weechat.test() == 123', '50')
            output = StringIO()
            unparsed = UnparsePython(output=output)
            unparsed.add(node.args[0])
            node.args.append(ast.Str(output.getvalue()))
            node.args.append(ast.Str(str(node.func.lineno)))
Exemplo n.º 2
0
def update_nodes(tree):
    """
    Update the tests AST tree (in-place):
      1. add a print message in each test_* function
      2. add arguments in calls to check() function
    """
    for node in ast.walk(tree):
        if isinstance(node, ast.FunctionDef) and \
                node.name.startswith('test_'):
            # add a print at the beginning of each test function
            node.body.insert(
                0, ast.parse('weechat.prnt("", "  > %s");' % node.name))
        elif isinstance(node, ast.Call) and \
                isinstance(node.func, ast.Name) and \
                node.func.id == 'check':
            # add two arguments in the call to "check" function:
            #   1. the string representation of the test
            #   2. the line number in source (as string)
            # for example if this test is on line 50:
            #   check(weechat.test() == 123)
            # it becomes:
            #   check(weechat.test() == 123, 'weechat.test() == 123', '50')
            output = StringIO()
            unparsed = UnparsePython(output=output)
            unparsed.add(node.args[0])
            node.args.append(ast.Str(output.getvalue()))
            node.args.append(ast.Str(str(node.func.lineno)))