<commit_before># -*- coding: utf-8 -*-
"""
    jinja2.testsuite.filters
    ~~~~~~~~~~~~~~~~~~~~~~~~

    Tests for the jinja filters.

    :copyright: (c) 2017 by the Jinja Team.
    :license: BSD, see LICENSE for more details.
"""
import random
import pytest
from jinja2 import Markup, Environment
from jinja2._compat import text_type, implements_to_string


@implements_to_string
class Magic(object):
    def __init__(self, value):
        self.value = value

    def __str__(self):
        return text_type(self.value)


@pytest.mark.filter
class TestFilter(object):

    def test_filter_calling(self, env):
        rv = env.call_filter('sum', [1, 2, 3])
        assert rv == 6

    def test_capitalize(self, env):
        tmpl = env.from_string('{{ "foo bar"|capitalize }}')
        assert tmpl.render() == 'Foo bar'

    def test_center(self, env):
        tmpl = env.from_string('{{ "foo"|center(9) }}')
        assert tmpl.render() == '   foo   '

    def test_default(self, env):
        tmpl = env.from_string(
            "{{ missing|default('no') }}|{{ false|default('no') }}|"
            "{{ false|default('no', true) }}|{{ given|default('no') }}"
        )
        assert tmpl.render(given='yes') == 'no|False|no|yes'

    @pytest.mark.parametrize('args,expect', (
        ('', "[('aa', 0), ('AB', 3), ('b', 1), ('c', 2)]"),
        ('true',  "[('AB', 3), ('aa', 0), ('b', 1), ('c', 2)]"),
        ('by="value"',  "[('aa', 0), ('b', 1), ('c', 2), ('AB', 3)]"),
        ('reverse=true', "[('c', 2), ('b', 1), ('AB', 3), ('aa', 0)]")
    ))
    def test_dictsort(self, env, args, expect):
        t = env.from_string('{{{{ foo|dictsort({args}) }}}}'.format(args=args))
        out = t.render(foo={"aa": 0, "b": 1, "c": 2, "AB": 3})
        assert out == expect

    def test_batch(self, env):
        tmpl = env.from_string("{{ foo|batch(3)|list }}|"
                               "{{ foo|batch(3, 'X')|list }}")
        out = tmpl.render(foo=list(range(10)))
        assert out == ("[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]|"
                       "[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 'X', 'X']]")

    def test_slice(self, env):
        tmpl = env.from_string('{{ foo|slice(3)|list }}|'
                               '{{ foo|slice(3, "X")|list }}')
        out = tmpl.render(foo=list(range(10)))
        assert out == ("[[0, 1, 2, 3], [4, 5, 6], [7, 8, 9]]|"
                       "[[0, 1, 2, 3], [4, 5, 6, 'X'], [7, 8, 9, 'X']]")

    def test_escape(self, env):
        tmpl = env.from_string('''{{ '<">&'|escape }}''')
        out = tmpl.render()
        assert out == '&lt;&#34;&gt;&amp;'

    def test_striptags(self, env):
        tmpl = env.from_string('''{{ foo|striptags }}''')
        out = tmpl.render(foo='  <p>just a small   \n <a href="#">'
                          'example</a> link</p>\n<p>to a webpage</p> '
                          '<!-- <p>and some commented stuff</p> -->')
        assert out == 'just a small example link to a webpage'

    def test_filesizeformat(self, env):
        tmpl = env.from_string(
            '{{ 100|filesizeformat }}|'
            '{{ 1000|filesizeformat }}|'
            '{{ 1000000|filesizeformat }}|'
            '{{ 1000000000|filesizeformat }}|'
            '{{ 1000000000000|filesizeformat }}|'
            '{{ 100|filesizeformat(true) }}|'
            '{{ 1000|filesizeformat(true) }}|'
            '{{ 1000000|filesizeformat(true) }}|'
            '{{ 1000000000|filesizeformat(true) }}|'
            '{{ 1000000000000|filesizeformat(true) }}'
        )
        out = tmpl.render()
        assert out == (
            '100 Bytes|1.0 kB|1.0 MB|1.0 GB|1.0 TB|100 Bytes|'
            '1000 Bytes|976.6 KiB|953.7 MiB|931.3 GiB'
        )

    def test_filesizeformat_issue59(self, env):
        tmpl = env.from_string(
            '{{ 300|filesizeformat }}|'
            '{{ 3000|filesizeformat }}|'
            '{{ 3000000|filesizeformat }}|'
            '{{ 3000000000|filesizeformat }}|'
            '{{ 3000000000000|filesizeformat }}|'
            '{{ 300|filesizeformat(true) }}|'
            '{{ 3000|filesizeformat(true) }}|'
            '{{ 3000000|filesizeformat(true) }}'
        )
        out = tmpl.render()
        assert out == (
            '300 Bytes|3.0 kB|3.0 MB|3.0 GB|3.0 TB|300 Bytes|'
            '2.9 KiB|2.9 MiB'
        )

    def test_first(self, env):
        tmpl = env.from_string('{{ foo|first }}')
        out = tmpl.render(foo=list(range(10)))
        assert out == '0'

    def test_float(self, env):
        tmpl = env.from_string('{{ "42"|float }}|'
                               '{{ "ajsghasjgd"|float }}|'
                               '{{ "32.32"|float }}')
        out = tmpl.render()
        assert out == '42.0|0.0|32.32'

    def test_format(self, env):
        tmpl = env.from_string('''{{ "%s|%s"|format("a", "b") }}''')
        out = tmpl.render()
        assert out == 'a|b'

    @staticmethod
    def _test_indent_multiline_template(env, markup=False):
        text = '\n'.join(['', 'foo bar', '"baz"', ''])
        if markup:
            text = Markup(text)
        t = env.from_string('{{ foo|indent(2, false, false) }}')
        assert t.render(foo=text) == '\n  foo bar\n  "baz"\n'
        t = env.from_string('{{ foo|indent(2, false, true) }}')
        assert t.render(foo=text) == '\n  foo bar\n  "baz"\n  '
        t = env.from_string('{{ foo|indent(2, true, false) }}')
        assert t.render(foo=text) == '  \n  foo bar\n  "baz"\n'
        t = env.from_string('{{ foo|indent(2, true, true) }}')
        assert t.render(foo=text) == '  \n  foo bar\n  "baz"\n  '

    def test_indent(self, env):
        self._test_indent_multiline_template(env)
        t = env.from_string('{{ "jinja"|indent }}')
        assert t.render() == 'jinja'
        t = env.from_string('{{ "jinja"|indent(first=true) }}')
        assert t.render() == '    jinja'
        t = env.from_string('{{ "jinja"|indent(blank=true) }}')
        assert t.render() == 'jinja'

    def test_indent_markup_input(self, env):
        '''
        Tests casses where the filter input is a Markup type
        '''
        self._test_indent_multiline_template(env, markup=True)

    def test_indentfirst_deprecated(self, env):
        with pytest.warns(DeprecationWarning):
            env.from_string('{{ "jinja"|indent(indentfirst=true) }}').render()

    def test_int(self, env):<commit_msg>Fix spelling in docstring<commit_after># -*- coding: utf-8 -*-
"""
    jinja2.testsuite.filters
    ~~~~~~~~~~~~~~~~~~~~~~~~

    Tests for the jinja filters.

    :copyright: (c) 2017 by the Jinja Team.
    :license: BSD, see LICENSE for more details.
"""
import random
import pytest
from jinja2 import Markup, Environment
from jinja2._compat import text_type, implements_to_string


@implements_to_string
class Magic(object):
    def __init__(self, value):
        self.value = value

    def __str__(self):
        return text_type(self.value)


@pytest.mark.filter
class TestFilter(object):

    def test_filter_calling(self, env):
        rv = env.call_filter('sum', [1, 2, 3])
        assert rv == 6

    def test_capitalize(self, env):
        tmpl = env.from_string('{{ "foo bar"|capitalize }}')
        assert tmpl.render() == 'Foo bar'

    def test_center(self, env):
        tmpl = env.from_string('{{ "foo"|center(9) }}')
        assert tmpl.render() == '   foo   '

    def test_default(self, env):
        tmpl = env.from_string(
            "{{ missing|default('no') }}|{{ false|default('no') }}|"
            "{{ false|default('no', true) }}|{{ given|default('no') }}"
        )
        assert tmpl.render(given='yes') == 'no|False|no|yes'

    @pytest.mark.parametrize('args,expect', (
        ('', "[('aa', 0), ('AB', 3), ('b', 1), ('c', 2)]"),
        ('true',  "[('AB', 3), ('aa', 0), ('b', 1), ('c', 2)]"),
        ('by="value"',  "[('aa', 0), ('b', 1), ('c', 2), ('AB', 3)]"),
        ('reverse=true', "[('c', 2), ('b', 1), ('AB', 3), ('aa', 0)]")
    ))
    def test_dictsort(self, env, args, expect):
        t = env.from_string('{{{{ foo|dictsort({args}) }}}}'.format(args=args))
        out = t.render(foo={"aa": 0, "b": 1, "c": 2, "AB": 3})
        assert out == expect

    def test_batch(self, env):
        tmpl = env.from_string("{{ foo|batch(3)|list }}|"
                               "{{ foo|batch(3, 'X')|list }}")
        out = tmpl.render(foo=list(range(10)))
        assert out == ("[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]|"
                       "[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 'X', 'X']]")

    def test_slice(self, env):
        tmpl = env.from_string('{{ foo|slice(3)|list }}|'
                               '{{ foo|slice(3, "X")|list }}')
        out = tmpl.render(foo=list(range(10)))
        assert out == ("[[0, 1, 2, 3], [4, 5, 6], [7, 8, 9]]|"
                       "[[0, 1, 2, 3], [4, 5, 6, 'X'], [7, 8, 9, 'X']]")

    def test_escape(self, env):
        tmpl = env.from_string('''{{ '<">&'|escape }}''')
        out = tmpl.render()
        assert out == '&lt;&#34;&gt;&amp;'

    def test_striptags(self, env):
        tmpl = env.from_string('''{{ foo|striptags }}''')
        out = tmpl.render(foo='  <p>just a small   \n <a href="#">'
                          'example</a> link</p>\n<p>to a webpage</p> '
                          '<!-- <p>and some commented stuff</p> -->')
        assert out == 'just a small example link to a webpage'

    def test_filesizeformat(self, env):
        tmpl = env.from_string(
            '{{ 100|filesizeformat }}|'
            '{{ 1000|filesizeformat }}|'
            '{{ 1000000|filesizeformat }}|'
            '{{ 1000000000|filesizeformat }}|'
            '{{ 1000000000000|filesizeformat }}|'
            '{{ 100|filesizeformat(true) }}|'
            '{{ 1000|filesizeformat(true) }}|'
            '{{ 1000000|filesizeformat(true) }}|'
            '{{ 1000000000|filesizeformat(true) }}|'
            '{{ 1000000000000|filesizeformat(true) }}'
        )
        out = tmpl.render()
        assert out == (
            '100 Bytes|1.0 kB|1.0 MB|1.0 GB|1.0 TB|100 Bytes|'
            '1000 Bytes|976.6 KiB|953.7 MiB|931.3 GiB'
        )

    def test_filesizeformat_issue59(self, env):
        tmpl = env.from_string(
            '{{ 300|filesizeformat }}|'
            '{{ 3000|filesizeformat }}|'
            '{{ 3000000|filesizeformat }}|'
            '{{ 3000000000|filesizeformat }}|'
            '{{ 3000000000000|filesizeformat }}|'
            '{{ 300|filesizeformat(true) }}|'
            '{{ 3000|filesizeformat(true) }}|'
            '{{ 3000000|filesizeformat(true) }}'
        )
        out = tmpl.render()
        assert out == (
            '300 Bytes|3.0 kB|3.0 MB|3.0 GB|3.0 TB|300 Bytes|'
            '2.9 KiB|2.9 MiB'
        )

    def test_first(self, env):
        tmpl = env.from_string('{{ foo|first }}')
        out = tmpl.render(foo=list(range(10)))
        assert out == '0'

    def test_float(self, env):
        tmpl = env.from_string('{{ "42"|float }}|'
                               '{{ "ajsghasjgd"|float }}|'
                               '{{ "32.32"|float }}')
        out = tmpl.render()
        assert out == '42.0|0.0|32.32'

    def test_format(self, env):
        tmpl = env.from_string('''{{ "%s|%s"|format("a", "b") }}''')
        out = tmpl.render()
        assert out == 'a|b'

    @staticmethod
    def _test_indent_multiline_template(env, markup=False):
        text = '\n'.join(['', 'foo bar', '"baz"', ''])
        if markup:
            text = Markup(text)
        t = env.from_string('{{ foo|indent(2, false, false) }}')
        assert t.render(foo=text) == '\n  foo bar\n  "baz"\n'
        t = env.from_string('{{ foo|indent(2, false, true) }}')
        assert t.render(foo=text) == '\n  foo bar\n  "baz"\n  '
        t = env.from_string('{{ foo|indent(2, true, false) }}')
        assert t.render(foo=text) == '  \n  foo bar\n  "baz"\n'
        t = env.from_string('{{ foo|indent(2, true, true) }}')
        assert t.render(foo=text) == '  \n  foo bar\n  "baz"\n  '

    def test_indent(self, env):
        self._test_indent_multiline_template(env)
        t = env.from_string('{{ "jinja"|indent }}')
        assert t.render() == 'jinja'
        t = env.from_string('{{ "jinja"|indent(first=true) }}')
        assert t.render() == '    jinja'
        t = env.from_string('{{ "jinja"|indent(blank=true) }}')
        assert t.render() == 'jinja'

    def test_indent_markup_input(self, env):
        '''
        Tests cases where the filter input is a Markup type
        '''
        self._test_indent_multiline_template(env, markup=True)

    def test_indentfirst_deprecated(self, env):
        with pytest.warns(DeprecationWarning):
            env.from_string('{{ "jinja"|indent(indentfirst=true) }}').render()

    def test_int(self, env):
{'fraction_non_alphanumeric': 0.20514826695876692, 'fraction_numerical': 0.04381503794695251, 'mean_word_length': 2.218026183282981, 'pattern_counts': {'":': 8, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 12, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6113637', 'n_tokens_mistral': 4928, 'n_tokens_neox': 4409, 'n_words': 928}