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 = [ b'#' + d.name.encode('UTF-8') + b' ' + d.var.totext(with_tail=False) if d.var is not None else b'#' + d.name.encode('UTF-8') 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( b'Placeholder: ' + placeholder.totext(with_tail=False) ) for d in top_level_directives: new_output.append( b' ' + d + b' ' + six.text_type(placeholder.is_in_context(d)).encode('UTF-8') ) new_output.append(b'') new_output = b'\n'.join(new_output) assert_same_content(output, new_output)
def test_cli_output(example, output): from refactorlib.cli.xmlfrom import xmlfrom from refactorlib.cli.xmlstrip import xmlstrip xml = xmlfrom(example) assert_same_content(output, xml, extra_suffix='.xmlfrom') stripped = xmlstrip(output) assert_same_content(example, stripped, extra_suffix='.xmlstrip')
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(b'Directive: ' + tree.getpath(directive).encode('UTF-8')) for c in context: new_output.append(b' ' + c.encode('UTF-8')) new_output.append(b'') new_output = b'\n'.join(new_output) 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 = [ b'#' + d.name.encode('UTF-8') + b' ' + d.var.totext(with_tail=False) if d.var is not None else b'#' + d.name.encode('UTF-8') 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(b'Placeholder: ' + placeholder.totext(with_tail=False)) for d in top_level_directives: new_output.append( b' ' + d + b' ' + str(placeholder.is_in_context(d)).encode('UTF-8')) new_output.append(b'') new_output = b'\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( b'Directive: ' + tree.getpath(directive).encode('UTF-8') ) for c in context: new_output.append(b' ' + c.encode('UTF-8')) new_output.append(b'') new_output = b'\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_replace_directive(example, output): from refactorlib.parse import parse lxmlnode = parse(example) for directive in lxmlnode.xpath('//Directive[not(starts-with(., "#end"))]'): 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).decode('UTF-8'))) new_output = lxmlnode.totext() 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): from refactorlib.cheetah.parse import parse example = open(example).read() example = parse(example) calls = example.find_calls('foo') assert calls 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[not(starts-with(., "#end"))]'): if directive.var is None: directive.replace_directive(f'#[[[{directive.name}]]]') else: directive.replace_directive( f'#[[[{directive.name}]]] [{directive.var.totext(with_tail=False).decode()}]' ) new_output = lxmlnode.totext() assert_same_content(output, new_output)
def test_can_remove_calls(example, output): from refactorlib.cheetah.parse import parse example = open(example).read() example = parse(example) calls = example.find_calls("foo") assert calls for call in calls: call.remove_call() # Check the text. example = example.totext() assert_same_content(output, example)
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( b'Directive: ' + tree.getpath(directive).encode('UTF-8'), ) if directive.is_multiline_directive: new_output.append( b'End: ' + tree.getpath(directive.get_end_directive()).encode('UTF-8'), ) else: new_output.append(b'Single-line: ' + directive.totext()) new_output.append(b'') new_output = b'\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( b'Directive: ' + tree.getpath(directive).encode('UTF-8'), ) if directive.is_multiline_directive: new_output.append( b'End: ' + tree.getpath(directive.get_end_directive()).encode('UTF-8'), ) else: new_output.append( b'Single-line: ' + directive.totext() ) new_output.append(b'') new_output = b'\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)