Пример #1
0
 def test_get_future_imports(self) -> None:
     node = black.lib2to3_parse("\n")
     self.assertEqual(set(), black.get_future_imports(node))
     node = black.lib2to3_parse("from __future__ import black\n")
     self.assertEqual({"black"}, black.get_future_imports(node))
     node = black.lib2to3_parse(
         "from __future__ import multiple, imports\n")
     self.assertEqual({"multiple", "imports"},
                      black.get_future_imports(node))
     node = black.lib2to3_parse(
         "from __future__ import (parenthesized, imports)\n")
     self.assertEqual({"parenthesized", "imports"},
                      black.get_future_imports(node))
     node = black.lib2to3_parse(
         "from __future__ import multiple\nfrom __future__ import imports\n"
     )
     self.assertEqual({"multiple", "imports"},
                      black.get_future_imports(node))
     node = black.lib2to3_parse("# comment\nfrom __future__ import black\n")
     self.assertEqual({"black"}, black.get_future_imports(node))
     node = black.lib2to3_parse(
         '"""docstring"""\nfrom __future__ import black\n')
     self.assertEqual({"black"}, black.get_future_imports(node))
     node = black.lib2to3_parse(
         "some(other, code)\nfrom __future__ import black\n")
     self.assertEqual(set(), black.get_future_imports(node))
     node = black.lib2to3_parse("from some.module import black\n")
     self.assertEqual(set(), black.get_future_imports(node))
Пример #2
0
 def test_get_future_imports(self) -> None:
     node = black.lib2to3_parse("\n")
     self.assertEqual(set(), black.get_future_imports(node))
     node = black.lib2to3_parse("from __future__ import black\n")
     self.assertEqual({"black"}, black.get_future_imports(node))
     node = black.lib2to3_parse("from __future__ import multiple, imports\n")
     self.assertEqual({"multiple", "imports"}, black.get_future_imports(node))
     node = black.lib2to3_parse("from __future__ import (parenthesized, imports)\n")
     self.assertEqual({"parenthesized", "imports"}, black.get_future_imports(node))
     node = black.lib2to3_parse(
         "from __future__ import multiple\nfrom __future__ import imports\n"
     )
     self.assertEqual({"multiple", "imports"}, black.get_future_imports(node))
     node = black.lib2to3_parse("# comment\nfrom __future__ import black\n")
     self.assertEqual({"black"}, black.get_future_imports(node))
     node = black.lib2to3_parse('"""docstring"""\nfrom __future__ import black\n')
     self.assertEqual({"black"}, black.get_future_imports(node))
     node = black.lib2to3_parse("some(other, code)\nfrom __future__ import black\n")
     self.assertEqual(set(), black.get_future_imports(node))
     node = black.lib2to3_parse("from some.module import black\n")
     self.assertEqual(set(), black.get_future_imports(node))
Пример #3
0
def fuzz(buf):
    try:
        src_node = lib2to3_parse(buf.lstrip().decode('utf-8'))
    except Exception:
        # not interested in bad input here
        return

    future_imports = get_future_imports(src_node)

    normalize_strings = True
    lines = LineGenerator(
        remove_u_prefix="unicode_literals" in future_imports,
        normalize_strings=normalize_strings,
    )
    elt = EmptyLineTracker()

    for current_line in lines.visit(src_node):
        before, after = elt.maybe_empty_lines(current_line)