<commit_before>from unittest import TestCase

from django.template import Context, Engine
from django.template.base import TextNode, VariableNode


class NodelistTest(TestCase):

    @classmethod
    def setUpClass(cls):
        cls.engine = Engine()
        super().setUpClass()

    def test_for(self):
        template = self.engine.from_string('{% for i in 1 %}{{ a }}{% endfor %}')
        vars = template.nodelist.get_nodes_by_type(VariableNode)
        self.assertEqual(len(vars), 1)

    def test_if(self):
        template = self.engine.from_string('{% if x %}{{ a }}{% endif %}')
        vars = template.nodelist.get_nodes_by_type(VariableNode)
        self.assertEqual(len(vars), 1)

    def test_ifequal(self):
        template = self.engine.from_string('{% ifequal x y %}{{ a }}{% endifequal %}')
        vars = template.nodelist.get_nodes_by_type(VariableNode)
        self.assertEqual(len(vars), 1)

    def test_ifchanged(self):
        template = self.engine.from_string('{% ifchanged x %}{{ a }}{% endifchanged %}')
        vars = template.nodelist.get_nodes_by_type(VariableNode)
        self.assertEqual(len(vars), 1)


class TextNodeTest(TestCase):

    def test_textnode_repr(self):
        engine = Engine()
        for temptext, reprtext in [
            ("Hello, world!", "<TextNode: 'Hello, world!'>"),
            ("One\ntwo.", "<TextNode: 'One\\ntwo.'>"),
        ]:
            template = engine.from_string(temptext)
            texts = template.nodelist.get_nodes_by_type(TextNode)
            self.assertEqual(repr(texts[0]), reprtext)


class ErrorIndexTest(TestCase):
    """
    Checks whether index of error is calculated correctly in
    template debugger in for loops. Refs ticket #5831
    """
    def test_correct_exception_index(self):
        tests = [
            ('{% load bad_tag %}{% for i in range %}{% badsimpletag %}{% endfor %}', (38, 56)),
            (
                '{% load bad_tag %}{% for i in range %}{% for j in range %}'
                '{% badsimpletag %}{% endfor %}{% endfor %}',
                (58, 76)
            ),
            (
                '{% load bad_tag %}{% for i in range %}{% badsimpletag %}'
                '{% for j in range %}Hello{% endfor %}{% endfor %}',
                (38, 56)
            ),
            (
                '{% load bad_tag %}{% for i in range %}{% for j in five %}'
                '{% badsimpletag %}{% endfor %}{% endfor %}',
                (38, 57)
            ),
            ('{% load bad_tag %}{% for j in five %}{% badsimpletag %}{% endfor %}', (18, 37)),
        ]
        context = Context({
            'range': range(5),
            'five': 5,
        })
        engine = Engine(debug=True, libraries={'bad_tag': 'template_tests.templatetags.bad_tag'})
        for source, expected_error_source_index in tests:
            template = engine.from_string(source)
            try:
                template.render(context)
            except (RuntimeError, TypeError) as e:
                debug = e.template_debug
                self.assertEqual((debug['start'], debug['end']), expected_error_source_index)
<commit_msg>Switched unittest.TestCase to SimpleTestCase in template_tests/test_nodelist.py.<commit_after>from django.template import Context, Engine
from django.template.base import TextNode, VariableNode
from django.test import SimpleTestCase


class NodelistTest(SimpleTestCase):

    @classmethod
    def setUpClass(cls):
        cls.engine = Engine()
        super().setUpClass()

    def test_for(self):
        template = self.engine.from_string('{% for i in 1 %}{{ a }}{% endfor %}')
        vars = template.nodelist.get_nodes_by_type(VariableNode)
        self.assertEqual(len(vars), 1)

    def test_if(self):
        template = self.engine.from_string('{% if x %}{{ a }}{% endif %}')
        vars = template.nodelist.get_nodes_by_type(VariableNode)
        self.assertEqual(len(vars), 1)

    def test_ifequal(self):
        template = self.engine.from_string('{% ifequal x y %}{{ a }}{% endifequal %}')
        vars = template.nodelist.get_nodes_by_type(VariableNode)
        self.assertEqual(len(vars), 1)

    def test_ifchanged(self):
        template = self.engine.from_string('{% ifchanged x %}{{ a }}{% endifchanged %}')
        vars = template.nodelist.get_nodes_by_type(VariableNode)
        self.assertEqual(len(vars), 1)


class TextNodeTest(SimpleTestCase):

    def test_textnode_repr(self):
        engine = Engine()
        for temptext, reprtext in [
            ("Hello, world!", "<TextNode: 'Hello, world!'>"),
            ("One\ntwo.", "<TextNode: 'One\\ntwo.'>"),
        ]:
            template = engine.from_string(temptext)
            texts = template.nodelist.get_nodes_by_type(TextNode)
            self.assertEqual(repr(texts[0]), reprtext)


class ErrorIndexTest(SimpleTestCase):
    """
    Checks whether index of error is calculated correctly in
    template debugger in for loops. Refs ticket #5831
    """
    def test_correct_exception_index(self):
        tests = [
            ('{% load bad_tag %}{% for i in range %}{% badsimpletag %}{% endfor %}', (38, 56)),
            (
                '{% load bad_tag %}{% for i in range %}{% for j in range %}'
                '{% badsimpletag %}{% endfor %}{% endfor %}',
                (58, 76)
            ),
            (
                '{% load bad_tag %}{% for i in range %}{% badsimpletag %}'
                '{% for j in range %}Hello{% endfor %}{% endfor %}',
                (38, 56)
            ),
            (
                '{% load bad_tag %}{% for i in range %}{% for j in five %}'
                '{% badsimpletag %}{% endfor %}{% endfor %}',
                (38, 57)
            ),
            ('{% load bad_tag %}{% for j in five %}{% badsimpletag %}{% endfor %}', (18, 37)),
        ]
        context = Context({
            'range': range(5),
            'five': 5,
        })
        engine = Engine(debug=True, libraries={'bad_tag': 'template_tests.templatetags.bad_tag'})
        for source, expected_error_source_index in tests:
            template = engine.from_string(source)
            try:
                template.render(context)
            except (RuntimeError, TypeError) as e:
                debug = e.template_debug
                self.assertEqual((debug['start'], debug['end']), expected_error_source_index)

{'fraction_non_alphanumeric': 0.14353089136928157, 'fraction_numerical': 0.010061311114604623, 'mean_word_length': 2.2196356275303644, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '6186097', 'n_tokens_mistral': 2002, 'n_tokens_neox': 1868, 'n_words': 433}