示例#1
0
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)
示例#2
0
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')
示例#3
0
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)
示例#4
0
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')
示例#5
0
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)
示例#6
0
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)
示例#7
0
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)
示例#8
0
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)
示例#10
0
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)
示例#11
0
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)
示例#12
0
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)
示例#13
0
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)
示例#14
0
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)
示例#15
0
def test_matches_known_good_parsing(example, output):
    example = parse(example).tostring()
    assert_same_content(output, example)
示例#16
0
def test_matches_known_good_parsing(example, output):
    example = parse(example).tostring()
    assert_same_content(output, example)