<commit_before>import ast
from textwrap import dedent

from eingefuhrt.formatters import hacking
from eingefuhrt.parser import get_imports


def format_(code, formatter):
    return formatter(get_imports(ast.parse(code, filename='<test>')))


def test_hacking():
    bad_code = dedent("""\
        from __future__ import absolute_import

        import functools
        from collections import namedtuple

        from mymodule.submodule import a as b, foo, bar, baz
        from .curry import wurst""")

    expected = dedent("""\
        from __future__ import absolute_import

        from collections import namedtuple
        import functools

        from .curry import wurst
        import mymodule.submodule.a as b
        from mymodule.submodule import bar
        from mymodule.submodule import baz
        from mymodule.submodule import foo
        """)

    assert format_(bad_code, formatter=hacking) == expected


def test_hacking_standard_lib_matching_regression():
    """Test how hacking is matching standard libraries

    This is a regression test, do not remove any import from the bad_code, but
    you can add as many new line as you want.
    """

    bad_code = dedent("""\
        import osterreich
        import os
        import request
        import re
        """)

    expected = dedent("""\
        import os
        import re

        import osterreich
        import request
        """)

    assert format_(bad_code, formatter=hacking) == expected


def test_hacking_support_python2():
    """Test support for python2 standard library"""
    code = dedent("""\
        import ConfigParser
        import re
        import StringIO

        import thirdparty
        """)

    assert format_(code, formatter=hacking) == code<commit_msg>We're expected to support inline comments in the future<commit_after>import pytest
import ast
from textwrap import dedent

from eingefuhrt.formatters import hacking
from eingefuhrt.parser import get_imports


def format_(code, formatter):
    return formatter(get_imports(ast.parse(code, filename='<test>')))


def test_hacking():
    bad_code = dedent("""\
        from __future__ import absolute_import

        import functools
        from collections import namedtuple

        from mymodule.submodule import a as b, foo, bar, baz
        from .curry import wurst""")

    expected = dedent("""\
        from __future__ import absolute_import

        from collections import namedtuple
        import functools

        from .curry import wurst
        import mymodule.submodule.a as b
        from mymodule.submodule import bar
        from mymodule.submodule import baz
        from mymodule.submodule import foo
        """)

    assert format_(bad_code, formatter=hacking) == expected


def test_hacking_standard_lib_matching_regression():
    """Test how hacking is matching standard libraries

    This is a regression test, do not remove any import from the bad_code, but
    you can add as many new line as you want.
    """

    bad_code = dedent("""\
        import osterreich
        import os
        import request
        import re
        """)

    expected = dedent("""\
        import os
        import re

        import osterreich
        import request
        """)

    assert format_(bad_code, formatter=hacking) == expected


def test_hacking_support_python2():
    """Test support for python2 standard library"""
    code = dedent("""\
        import ConfigParser
        import re
        import StringIO

        import thirdparty
        """)

    assert format_(code, formatter=hacking) == code


@pytest.mark.xfail(reason="Comments are not parsed yet")
def test_hacking_preserve_inline_comments():
    bad_code = dedent("""\
        import thirdparty # Third party library

        import os""")

    expected = dedent("""\
        import os

        import thirdparty # Third party library""")

    assert format_(bad_code, formatter=hacking) == expected
{'fraction_non_alphanumeric': 0.07319535588086824, 'fraction_numerical': 0.0010095911155981827, 'mean_word_length': 2.5994550408719346, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '629658', 'n_tokens_mistral': 1097, 'n_tokens_neox': 1052, 'n_words': 362}