<commit_before># coding=utf-8
""""""
from __future__ import absolute_import, division, print_function, \
    unicode_literals

import pytest
from babel import Locale
from flask import _app_ctx_stack, _request_ctx_stack
from flask_babel import force_locale, get_locale
from jinja2 import DictLoader

from abilian import i18n
from abilian.testing import BaseTestCase

skip = pytest.mark.skip


class I18NTestCase(BaseTestCase):

    @skip
    def test_ensure_request_context(self):
        en = Locale(b'en')
        fr = Locale(b'fr')

        # By default, a request context is present is test case setup.
        # First let's show no new request context is set up and that
        # set_locale works as advertised.
        app_ctx = _app_ctx_stack.top
        current_ctx = _request_ctx_stack.top
        assert current_ctx is not None
        assert get_locale() == en

        with i18n.ensure_request_context():
            assert current_ctx == _request_ctx_stack.top

            with force_locale(fr):
                assert get_locale() == fr

        self._ctx.pop()
        self._ctx = None
        assert _app_ctx_stack.top is None
        assert _request_ctx_stack.top is None
        assert get_locale() is None

        app_ctx.push()

        # # ensure set_locale() doesn't set up a request context
        # with force_locale(fr):
        #     assert _request_ctx_stack.top is None
        #     assert get_locale() is None

        with i18n.ensure_request_context():
            assert _request_ctx_stack.top is not None
            assert current_ctx != _request_ctx_stack.top
            assert get_locale() == en

            with force_locale(fr):
                assert get_locale() == fr

        assert _request_ctx_stack.top is None
        assert get_locale() is None

        # test chaining
        with i18n.ensure_request_context(), force_locale(fr):
            assert get_locale() == fr

    def test_get_template_i18n(self):
        template_path = '/myfile.txt'
        en = Locale('en')
        result = i18n.get_template_i18n(template_path, locale=en)
        assert '/myfile.en.txt' in result
        assert '/myfile.txt' in result

        en = Locale('en_US')
        result = i18n.get_template_i18n(template_path, locale=en)
        assert '/myfile.en_US.txt' in result
        assert '/myfile.txt' in result

        with force_locale('fr'):
            result = i18n.get_template_i18n(template_path, get_locale())
            assert '/myfile.fr.txt' in result
            assert '/myfile.txt' in result

    def test_render_template_i18n(self):
        loader = DictLoader({
            'tmpl.txt': 'default ({{ locale }})',
            'tmpl.en.txt': 'en locale ({{ locale }})',
            'tmpl.fr.txt': 'fr locale ({{ locale }})',
        })
        app_loader = self.app.jinja_loader
        self.app.jinja_loader = loader
        render = i18n.render_template_i18n
        try:
            assert render('tmpl.txt', locale='fr') == 'fr locale (fr)'
            assert render('tmpl.txt', locale='en') == 'en locale (en)'
            assert render('tmpl.txt', locale='de') == 'default (de)'
        finally:
            self.app.jinja_loader = app_loader

    def test_default_country(self):
        assert 'DEFAULT_COUNTRY' in self.app.config
        assert self.app.config['DEFAULT_COUNTRY'] is None
        assert i18n.default_country() is None
        assert i18n.country_choices()[0][0] == 'AF'

        self.app.config['DEFAULT_COUNTRY'] = 'FR'
        assert i18n.default_country() == 'FR'
        assert i18n.country_choices()[0][0] == 'FR'
        assert i18n.country_choices(default_country_first=False)[0][0] == 'AF'<commit_msg>refactor: test.<commit_after># coding=utf-8
""""""
from __future__ import absolute_import, division, print_function, \
    unicode_literals

import pytest
from babel import Locale
from flask import _app_ctx_stack, _request_ctx_stack
from flask_babel import force_locale, get_locale
from jinja2 import DictLoader
from pytest import yield_fixture

from abilian import i18n
from abilian.testing import BaseTestCase

skip = pytest.mark.skip


@yield_fixture
def ctx(app):
    with app.test_request_context() as ctx:
        yield ctx


def test_get_template_i18n(app, ctx):
    template_path = '/myfile.txt'
    en = Locale('en')
    result = i18n.get_template_i18n(template_path, locale=en)
    assert '/myfile.en.txt' in result
    assert '/myfile.txt' in result

    en = Locale('en_US')
    result = i18n.get_template_i18n(template_path, locale=en)
    assert '/myfile.en_US.txt' in result
    assert '/myfile.txt' in result

    with force_locale('fr'):
        result = i18n.get_template_i18n(template_path, get_locale())
        assert '/myfile.fr.txt' in result
        assert '/myfile.txt' in result


def test_render_template_i18n(app, ctx):
    loader = DictLoader({
        'tmpl.txt': 'default ({{ locale }})',
        'tmpl.en.txt': 'en locale ({{ locale }})',
        'tmpl.fr.txt': 'fr locale ({{ locale }})',
    })
    app_loader = app.jinja_loader
    app.jinja_loader = loader
    render = i18n.render_template_i18n
    try:
        assert render('tmpl.txt', locale='fr') == 'fr locale (fr)'
        assert render('tmpl.txt', locale='en') == 'en locale (en)'
        assert render('tmpl.txt', locale='de') == 'default (de)'
    finally:
        app.jinja_loader = app_loader


def test_default_country(app, ctx):
    assert 'DEFAULT_COUNTRY' in app.config
    assert app.config['DEFAULT_COUNTRY'] is None
    assert i18n.default_country() is None
    assert i18n.country_choices()[0][0] == 'AF'

    app.config['DEFAULT_COUNTRY'] = 'FR'
    assert i18n.default_country() == 'FR'
    assert i18n.country_choices()[0][0] == 'FR'
    assert i18n.country_choices(default_country_first=False)[0][0] == 'AF'


# No idea what this test was supposed to do...
# @skip
# def test_ensure_request_context(app, ctx):
#     en = Locale(b'en')
#     fr = Locale(b'fr')
#
#     # By default, a request context is present is test case setup.
#     # First let's show no new request context is set up and that
#     # set_locale works as advertised.
#     app_ctx = _app_ctx_stack.top
#     current_ctx = _request_ctx_stack.top
#     assert current_ctx is not None
#     assert get_locale() == en
#
#     with i18n.ensure_request_context():
#         assert current_ctx == _request_ctx_stack.top
#
#         with force_locale(fr):
#             assert get_locale() == fr
#
#     self._ctx.pop()
#     self._ctx = None
#     assert _app_ctx_stack.top is None
#     assert _request_ctx_stack.top is None
#     assert get_locale() is None
#
#     app_ctx.push()
#
#     # # ensure set_locale() doesn't set up a request context
#     # with force_locale(fr):
#     #     assert _request_ctx_stack.top is None
#     #     assert get_locale() is None
#
#     with i18n.ensure_request_context():
#         assert _request_ctx_stack.top is not None
#         assert current_ctx != _request_ctx_stack.top
#         assert get_locale() == en
#
#         with force_locale(fr):
#             assert get_locale() == fr
#
#     assert _request_ctx_stack.top is None
#     assert get_locale() is None
#
#     # test chaining
#     with i18n.ensure_request_context(), force_locale(fr):
#         assert get_locale() == fr
{'fraction_non_alphanumeric': 0.10928287398875634, 'fraction_numerical': 0.012889071712601124, 'mean_word_length': 2.7328556806550663, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6581429', 'n_tokens_mistral': 2398, 'n_tokens_neox': 2304, 'n_words': 580}