def test_get_enclosing_blocks(example, output): text = open(example).read() from refactorlib.cheetah.parse import parse lxmlnode = parse(text) tree = lxmlnode.getroottree() unique_contexts = {} for directive in lxmlnode.xpath('//Directive'): context = tuple( tree.getpath(block) for block in directive.get_enclosing_blocks() ) if context and context not in unique_contexts: unique_contexts[context] = directive new_output = [] for context, directive in sorted(unique_contexts.items()): new_output.append( 'Directive: %s' % tree.getpath(directive) ) for c in context: new_output.append(' ' + c) new_output.append('') new_output = '\n'.join(new_output) assert_same_content(output, new_output)
def test_find_end_directive(example, output): text = open(example).read() from refactorlib.cheetah.parse import parse lxmlnode = parse(text) tree = lxmlnode.getroottree() new_output = [] for directive in lxmlnode.xpath('//Directive'): new_output.append( 'Directive: %s' % tree.getpath(directive), ) if directive.is_multiline_directive: try: new_output.append( 'End: %s' % tree.getpath(directive.get_end_directive()), ) except: import pudb; pudb.set_trace() raise else: new_output.append( 'Single-line: %s' % directive.totext() ) new_output.append('') new_output = '\n'.join(new_output) assert_same_content(output, new_output)
def test_remove_foo(example, output): from refactorlib.cheetah.parse import parse example = open(example).read() example = parse(example) for decorator in example.find_decorators('@foo'): decorator.remove_self() # Check the text. example = example.totext() assert_same_content(output, example)
def test_can_remove_calls(example, output): example = open(example).read() example = parse(example) calls = example.find_calls('foo') assert len(calls) == 5 for call in calls: call.remove_call() # Check the text. example = example.totext() assert_same_content(output, example)
def test_replace_directive(example, output): from refactorlib.parse import parse lxmlnode = parse(example) for directive in lxmlnode.xpath('//Directive'): if directive.xpath('./EndDirective'): # Don't mess with #end statements continue if directive.var is None: directive.replace_directive('#{{{%s}}}' % directive.name) else: directive.replace_directive('#{{{%s}}} [%s]' % (directive.name, directive.var.totext(with_tail=False))) new_output = lxmlnode.totext() assert_same_content(output, new_output)
def test_replace_directive(example, output): from refactorlib.parse import parse lxmlnode = parse(example) for directive in lxmlnode.xpath('//Directive'): if directive.xpath('./EndDirective'): # Don't mess with #end statements continue if directive.var is None: directive.replace_directive('#{{{%s}}}' % directive.name) else: directive.replace_directive( '#{{{%s}}} [%s]' % (directive.name, directive.var.totext(with_tail=False))) new_output = lxmlnode.totext() assert_same_content(output, new_output)
def test_is_in_context(example, output): from refactorlib.parse import parse lxmlnode = parse(example) top_level_directives = lxmlnode.xpath('/cheetah/*/*[1][self::Directive]') top_level_directives = [ "#%s %s" % (d.name, d.var.totext(with_tail=False)) if d.var else "#%s" % (d.name) for d in top_level_directives ] #for each Placeholder, print if it's "in context" of each top-level directive new_output = [] for placeholder in lxmlnode.xpath('//Placeholder'): new_output.append('Placeholder: %s' % placeholder.totext(with_tail=False)) for d in top_level_directives: new_output.append(' %s %s' % (d, placeholder.is_in_context(d))) new_output.append('') new_output = '\n'.join(new_output) assert_same_content(output, new_output)
def test_get_enclosing_blocks(example, output): text = open(example).read() from refactorlib.cheetah.parse import parse lxmlnode = parse(text) tree = lxmlnode.getroottree() unique_contexts = {} for directive in lxmlnode.xpath('//Directive'): context = tuple( tree.getpath(block) for block in directive.get_enclosing_blocks()) if context and context not in unique_contexts: unique_contexts[context] = directive new_output = [] for context, directive in sorted(unique_contexts.items()): new_output.append('Directive: %s' % tree.getpath(directive)) for c in context: new_output.append(' ' + c) new_output.append('') new_output = '\n'.join(new_output) assert_same_content(output, new_output)
def test_find_end_directive(example, output): text = open(example).read() from refactorlib.cheetah.parse import parse lxmlnode = parse(text) tree = lxmlnode.getroottree() new_output = [] for directive in lxmlnode.xpath('//Directive'): new_output.append('Directive: %s' % tree.getpath(directive), ) if directive.is_multiline_directive: try: new_output.append( 'End: %s' % tree.getpath(directive.get_end_directive()), ) except: import pudb pudb.set_trace() raise else: new_output.append('Single-line: %s' % directive.totext()) new_output.append('') new_output = '\n'.join(new_output) assert_same_content(output, new_output)
def test_matches_known_good_parsing(example, output): example = parse(example).tostring() assert_same_content(output, example)