import six from testing.util import parametrize, get_output, assert_same_content from . import xfailif_no_cheetah @xfailif_no_cheetah @parametrize(get_output('txt')) 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(
xfailif_no_js = pytest.mark.xfail( reason='nodejs not found') # pragma: no cover else: xfailif_no_js = pytest.mark.noop @xfailif_no_js @parametrize(get_examples) def test_can_make_round_trip(example): text = open(example).read() example = parse(example) assert text == example.totext().decode('UTF-8') @xfailif_no_js @parametrize(get_output('xml')) def test_matches_known_good_parsing(example, output): example = parse(example).tostring() assert_same_content(output, example) @xfailif_no_js @parametrize(get_output('xml', func=test_matches_known_good_parsing)) 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')
from testing.util import parametrize, get_output, assert_same_content from . import xfailif_no_cheetah @xfailif_no_cheetah @parametrize(get_output('txt')) 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) @xfailif_no_cheetah @parametrize(get_output)
assert text == example.totext() @parametrize(get_examples) def test_encoding_detection(example): from refactorlib.python.parse import detect_encoding text = open(example, 'rb').read() example = parse(example) detected_encoding = detect_encoding(text) assert ( example.encoding == detected_encoding or (example.encoding, detected_encoding) == ('UTF-8', None) ) @parametrize(get_output('xml')) def test_matches_known_good_parsing(example, output): example = parse(example).tostring() assert_same_content(output, example) @parametrize(get_output('xml', func=test_matches_known_good_parsing)) 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')