<commit_before>import _path_pyblosxom

import string
import os
import os.path

from Pyblosxom import tools, pyblosxom

class TestVAR_REGEXP:
    """tools.VAR_REGEXP

    This tests the various syntaxes for variables in PyBlosxom templates.
    """
    def _get_match(self, compiled_regexp, s):
        r = compiled_regexp.search(s)
        print repr(r)
        return r and r.group(1)

    def test_escaped_variables(self):
        VR = tools.VAR_REGEXP
        assert self._get_match(VR, "\\$test") == None
        # FIXME - this is bad behavior
        assert self._get_match(VR, "\\\\$test") == None

    def test_dollar_then_string(self):
        VR = tools.VAR_REGEXP
        assert self._get_match(VR, "$test") == "test"
        assert self._get_match(VR, "$test-test") == "test-test"
        assert self._get_match(VR, "$test_test") == "test_test"
        assert self._get_match(VR, " $test ") == "test"
        assert self._get_match(VR, "other stuff $test ") == "test"
        assert self._get_match(VR, "other $test stuff") == "test"
        assert self._get_match(VR, "other $test $test2 stuff") == "test"

    def test_delimiters(self):
        VR = tools.VAR_REGEXP
        for c in ('|', '=', '+', ' ', '$', '<', '>'):
            assert self._get_match(VR, "$test%s1" % c) == "test"

    def test_namespace(self):
        VR = tools.VAR_REGEXP
        assert self._get_match(VR, "$foo::bar") == "foo::bar"
        assert self._get_match(VR, " $foo::bar ") == "foo::bar"
        assert self._get_match(VR, "other $foo::bar stuff") == "foo::bar"

    def test_single_function(self):
        VR = tools.VAR_REGEXP
        assert self._get_match(VR, "$foo()") == "foo()"
        assert self._get_match(VR, " $foo() ") == "foo()"
        assert self._get_match(VR, "other $foo() stuff") == "foo()"
        assert self._get_match(VR, "other $foo::bar() stuff") == "foo::bar()"

    def test_function_with_arguments(self):
        VR = tools.VAR_REGEXP
        assert self._get_match(VR, '$foo("arg1")') == 'foo("arg1")'
        assert self._get_match(VR, '$foo("arg1", 1)') == 'foo("arg1", 1)'

    def test_parens(self):
        VR = tools.VAR_REGEXP
        assert self._get_match(VR, "$(foo)") == "(foo)"
        assert self._get_match(VR, "$(foo())") == "(foo())"
        assert self._get_match(VR, "$(foo::bar)") == "(foo::bar)"
        assert self._get_match(VR, "$(foo::bar())") == "(foo::bar())"
        assert self._get_match(VR, "$(foo::bar(1, 2, 3))") == "(foo::bar(1, 2, 3))"


class Testparse:
    """tools.parse"""
    def _get_req(self):
        return pyblosxom.Request( {}, {}, {} )

    def test_simple(self):
        def pt(d, t):
            return tools.parse(self._get_req(), "iso-8859-1", d, t)

        assert pt( { "foo": "FOO" }, "foo foo foo") == "foo foo foo"
        assert pt( { "foo": "FOO" }, "foo $foo foo") == "foo FOO foo"
        assert pt( { "foo": "FOO" }, "foo $foor foo") == "foo  foo"

    def test_delimited(self):
        def pt(d, t):
            return tools.parse(self._get_req(), "iso-8859-1", d, t)

        assert pt( { "foo": "FOO" }, "foo $(foo) foo") == "foo FOO foo"
        assert pt( { "foo": "FOO" }, "foo $(foor) foo") == "foo  foo"

    def test_functions(self):
        def pt(d, t):
            return tools.parse(self._get_req(), "iso-8859-1", d, t)

        assert pt( { "foo": lambda req, vd: "FOO" }, "foo foo foo") == "foo foo foo"
        assert pt( { "foo": lambda req, vd: "FOO" }, "foo $foo() foo") == "foo FOO foo"
        assert pt( { "foo": lambda req, vd, z: z }, "foo $foo('a') foo") == "foo a foo"
        assert pt( { "foo": lambda req, vd, z: z, "bar": "BAR" }, "foo $foo(bar) foo") == "foo BAR foo"

    def test_functions_old_behavior(self):
        def pt(d, t):
            return tools.parse(self._get_req(), "iso-8859-1", d, t)

        # test the old behavior that allowed for functions that have no
        # arguments--in this case we don't pass a request object in
        assert pt( { "foo": (lambda : "FOO") }, "foo $foo() foo") == "foo FOO foo"

    def test_functions_with_args_that_have_commas(self):
        def pt(d, t):
            return tools.parse(self._get_req(), "iso-8859-1", d, t)

        vd = { "foo": lambda req, vd, x: (x + "A"),
               "foo2": lambda req, vd, x, y: (y + x) }

        assert pt(vd, '$foo("ba,ar")') == "ba,arA"
        assert pt(vd, '$foo2("a,b", "c,d")') == "c,da,b"

    def test_functions_with_var_args(self):
        def pt(d, t):<commit_msg>Added test for $foo($bar) template variable syntax.<commit_after>import _path_pyblosxom

import string
import os
import os.path

from Pyblosxom import tools, pyblosxom

class TestVAR_REGEXP:
    """tools.VAR_REGEXP

    This tests the various syntaxes for variables in PyBlosxom templates.
    """
    def _get_match(self, compiled_regexp, s):
        r = compiled_regexp.search(s)
        print repr(r)
        return r and r.group(1)

    def test_escaped_variables(self):
        VR = tools.VAR_REGEXP
        assert self._get_match(VR, "\\$test") == None
        # FIXME - this is bad behavior
        assert self._get_match(VR, "\\\\$test") == None

    def test_dollar_then_string(self):
        VR = tools.VAR_REGEXP
        assert self._get_match(VR, "$test") == "test"
        assert self._get_match(VR, "$test-test") == "test-test"
        assert self._get_match(VR, "$test_test") == "test_test"
        assert self._get_match(VR, " $test ") == "test"
        assert self._get_match(VR, "other stuff $test ") == "test"
        assert self._get_match(VR, "other $test stuff") == "test"
        assert self._get_match(VR, "other $test $test2 stuff") == "test"

    def test_delimiters(self):
        VR = tools.VAR_REGEXP
        for c in ('|', '=', '+', ' ', '$', '<', '>'):
            assert self._get_match(VR, "$test%s1" % c) == "test"

    def test_namespace(self):
        VR = tools.VAR_REGEXP
        assert self._get_match(VR, "$foo::bar") == "foo::bar"
        assert self._get_match(VR, " $foo::bar ") == "foo::bar"
        assert self._get_match(VR, "other $foo::bar stuff") == "foo::bar"

    def test_single_function(self):
        VR = tools.VAR_REGEXP
        assert self._get_match(VR, "$foo()") == "foo()"
        assert self._get_match(VR, " $foo() ") == "foo()"
        assert self._get_match(VR, "other $foo() stuff") == "foo()"
        assert self._get_match(VR, "other $foo::bar() stuff") == "foo::bar()"

    def test_function_with_arguments(self):
        VR = tools.VAR_REGEXP
        assert self._get_match(VR, '$foo("arg1")') == 'foo("arg1")'
        assert self._get_match(VR, '$foo("arg1", 1)') == 'foo("arg1", 1)'

    def test_parens(self):
        VR = tools.VAR_REGEXP
        assert self._get_match(VR, "$(foo)") == "(foo)"
        assert self._get_match(VR, "$(foo())") == "(foo())"
        assert self._get_match(VR, "$(foo::bar)") == "(foo::bar)"
        assert self._get_match(VR, "$(foo::bar())") == "(foo::bar())"
        assert self._get_match(VR, "$(foo::bar(1, 2, 3))") == "(foo::bar(1, 2, 3))"


class Testparse:
    """tools.parse"""
    def _get_req(self):
        return pyblosxom.Request( {}, {}, {} )

    def test_simple(self):
        def pt(d, t):
            return tools.parse(self._get_req(), "iso-8859-1", d, t)

        assert pt( { "foo": "FOO" }, "foo foo foo") == "foo foo foo"
        assert pt( { "foo": "FOO" }, "foo $foo foo") == "foo FOO foo"
        assert pt( { "foo": "FOO" }, "foo $foor foo") == "foo  foo"

    def test_delimited(self):
        def pt(d, t):
            return tools.parse(self._get_req(), "iso-8859-1", d, t)

        assert pt( { "foo": "FOO" }, "foo $(foo) foo") == "foo FOO foo"
        assert pt( { "foo": "FOO" }, "foo $(foor) foo") == "foo  foo"

    def test_functions(self):
        def pt(d, t):
            return tools.parse(self._get_req(), "iso-8859-1", d, t)

        assert pt( { "foo": lambda req, vd: "FOO" }, "foo foo foo") == "foo foo foo"
        assert pt( { "foo": lambda req, vd: "FOO" }, "foo $foo() foo") == "foo FOO foo"
        assert pt( { "foo": lambda req, vd, z: z }, "foo $foo('a') foo") == "foo a foo"
        assert pt( { "foo": lambda req, vd, z: z, "bar": "BAR" }, "foo $foo(bar) foo") == "foo BAR foo"
        assert pt( { "foo": lambda req, vd, z: z, "bar": "BAR" }, "foo $foo($bar) foo") == "foo BAR foo"

    def test_functions_old_behavior(self):
        def pt(d, t):
            return tools.parse(self._get_req(), "iso-8859-1", d, t)

        # test the old behavior that allowed for functions that have no
        # arguments--in this case we don't pass a request object in
        assert pt( { "foo": (lambda : "FOO") }, "foo $foo() foo") == "foo FOO foo"

    def test_functions_with_args_that_have_commas(self):
        def pt(d, t):
            return tools.parse(self._get_req(), "iso-8859-1", d, t)

        vd = { "foo": lambda req, vd, x: (x + "A"),
               "foo2": lambda req, vd, x, y: (y + x) }

        assert pt(vd, '$foo("ba,ar")') == "ba,arA"
        assert pt(vd, '$foo2("a,b", "c,d")') == "c,da,b"

    def test_functions_with_var_args(self):
        def pt(d, t):
{'fraction_non_alphanumeric': 0.19124904046496327, 'fraction_numerical': 0.009211536352670248, 'mean_word_length': 2.678902783380395, 'pattern_counts': {'":': 28, '<': 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': '2194300', 'n_tokens_mistral': 3336, 'n_tokens_neox': 3195, 'n_words': 847}