import pytest

from potojson import pofile_to_json
from test import POFILE_START


@pytest.mark.parametrize(
    (
        'content', 'output', 'fallback_to_msgid', 'fuzzy',
        'pretty', 'indent', 'language', 'plural_forms', 'as_dict',
    ),
    (
        (
            POFILE_START + 'msgid "Hello"\nmsgstr "Hola"\n',
            '{"Hello": "Hola"}',
            False, False, False, None, None, None, False,
        ),

        # msgctxt
        (
            POFILE_START + 'msgctxt "Month"\nmsgid "May"\nmsgstr "Mayo"',
            '{"Month": {"May": "Mayo"}}',
            False, False, False, None, None, None, False,
        ),

        # obsolete
        (
            POFILE_START + '#~ msgid "May"\n#~ msgstr "Mayo"',
            '{}',
            False, False, False, None, None, None, False,
        ),

        # fallback_to_msgid
        #   True
        (
            POFILE_START + 'msgid "Hello"\nmsgstr ""\n',
            '{"Hello": "Hello"}',
            True, False, False, None, None, None, False,
        ),
        #   False
        (
            POFILE_START + 'msgid "Hello"\nmsgstr ""\n',
            '{"Hello": ""}',
            False, False, False, None, None, None, False,
        ),

        # msgid_plural
        (
            (POFILE_START + 'msgid "$n word"\nmsgid_plural "$n words"\n'
             'msgstr[0] "$n palabra"\nmsgstr[1] "$n palabras"\n'),
            '{"$n word": ["$n palabra", "$n palabras"]}',
            False, False, False, None, None, None, False,
        ),

        # msgid_plural + msgctxt
        (
            (POFILE_START + 'msgctxt "a context"\nmsgid "$n word"\n'
             'msgid_plural "$n words"\nmsgstr[0] "$n palabra"\n'
             'msgstr[1] "$n palabras"\n'),
            ('{"a context": {"$n word": ["$n palabra", "$n palabras"]}}'),
            False, False, False, None, None, None, False,
        ),

        # fallback_to_msgid + msgid_plural
        #   True
        (
            (POFILE_START + 'msgid "$n word"\nmsgid_plural "$n words"\n'
             'msgstr[0] ""\nmsgstr[1] ""\n'),
            '{"$n word": ["$n word", "$n words"]}',
            True, False, False, None, None, None, False,
        ),
        #   False
        (
            (POFILE_START + 'msgid "$n word"\nmsgid_plural "$n words"\n'
             'msgstr[0] ""\nmsgstr[1] ""\n'),
            '{"$n word": ["", ""]}',
            False, False, False, None, None, None, False,
        ),

        # fallback_to_msgid + msgid_plural + msgctxt
        #   True
        (
            (POFILE_START + 'msgctxt "a context"\nmsgid "$n word"\n'
             'msgid_plural "$n words"\nmsgstr[0] ""\nmsgstr[1] ""\n'),
            ('{"a context": {"$n word": ["$n word", "$n words"]}}'),
            True, False, False, None, None, None, False,
        ),
        #   False
        (
            (POFILE_START + 'msgctxt "a context"\nmsgid "$n word"\n'
             'msgid_plural "$n words"\nmsgstr[0] ""\nmsgstr[1] ""\n'),
            ('{"a context": {"$n word": ["", ""]}}'),
            False, False, False, None, None, None, False,
        ),

        # fuzzy
        #   True
        (
            POFILE_START + '#, fuzzy\nmsgid "Hello"\nmsgstr "Hola"\n',
            '{"Hello": "Hola"}',
            False, True, False, None, None, None, False,
        ),
        #   False
        (
            POFILE_START + '#, fuzzy\nmsgid "Hello"\nmsgstr "Hola"\n',
            '{}',
            False, False, False, None, None, None, False,
        ),

        # pretty
        (
            POFILE_START + 'msgid "Hello"\nmsgstr "Hola"\n',
            '{\n  "Hello": "Hola"\n}',
            False, False, True, None, None, None, False,
        ),
        # pretty with custom indent
        (
            POFILE_START + 'msgid "Hello"\nmsgstr "Hola"\n',
            '{\n   "Hello": "Hola"\n}',
            False, False, True, 3, None, None, False,
        ),

        # language
        #   discover from pofile
        (
            POFILE_START + '"Language: es\\n"\n\n',
            '{"": {"language": "es"}}',
            False, False, False, None, None, None, False,
        ),
        #   specified in keyword argument
        (
            POFILE_START,
            '{"": {"language": "es"}}',
            False, False, False, None, 'es', None, False,
        ),

        # plural_forms
        #   discover from pofile
        (
            POFILE_START + '"Plural-Forms: nplurals=2; plural=n != 1;\\n"\n\n',
            '{"": {"plural-forms": "nplurals=2; plural=n != 1;"}}',
            False, False, False, None, None, None, False,
        ),
        #   specified in keyword argument
        (
            POFILE_START,
            '{"": {"plural-forms": "nplurals=2; plural=n != 1;"}}',
            False, False, False, None, None, 'nplurals=2; plural=n != 1;',
            False
        ),

        # as dict
        (
            POFILE_START + 'msgid "Hello"\nmsgstr "Hola"\n',
            {'Hello': 'Hola'},
            False, False, False, None, None, None, True,
        )
    )
)
def test_pofile_content_to_json(content, output, fallback_to_msgid, fuzzy,
                                pretty, indent, language, plural_forms,
                                as_dict):
    assert pofile_to_json(
        content,
        fallback_to_msgid=fallback_to_msgid,
        fuzzy=fuzzy,
        pretty=pretty,
        indent=indent,
        language=language,
        plural_forms=plural_forms,
        as_dict=as_dict
    ) == output
