Repository: django/django
File: django/test/testcases.py (Python)

Code before:
        )


class _AssertTemplateUsedContext:
    def __init__(self, test_case, template_name, msg_prefix="", count=None):
        self.test_case = test_case
        self.template_name = template_name
        self.msg_prefix = msg_prefix
        self.count = count

        self.rendered_templates = []
        self.context = ContextList()

    def on_template_render(self, sender, signal, template, context, **kwargs):
        self.rendered_templates.append(template)
        self.context.append(copy(context))

    @property
    def rendered_template_names(self):
        rendered_names = []
        for t in self.rendered_templates:
            if t.name is None:
                continue
            if isinstance(t, PartialTemplate):
                rendered_names.append(f"{t.origin.template_name}#{t.name}")
            rendered_names.append(t.name)
        return rendered_names

    def test(self):
        self.test_case._assert_template_used(
            self.template_name,
            self.rendered_template_names,
            self.msg_prefix,
            self.count,
        )

    def __enter__(self):
        template_rendered.connect(self.on_template_render)
        return self

    def __exit__(self, exc_type, exc_value, traceback):
        template_rendered.disconnect(self.on_template_render)
        if exc_type is not None:
            return
        self.test()


class _AssertTemplateNotUsedContext(_AssertTemplateUsedContext):
    def test(self):
        self.test_case.assertFalse(
            self.template_name in self.rendered_template_names,

Review comment (bug):
I think this also has a bug where the name is appended twice if the if guard evaluates to True. I'll try to provide a test that shows (or denies) this.

Code after:
        )


class _AssertTemplateUsedContext:
    def __init__(self, test_case, template_name, msg_prefix="", count=None):
        self.test_case = test_case
        self.template_name = template_name
        self.msg_prefix = msg_prefix
        self.count = count

        self.rendered_templates = []
        self.context = ContextList()

    def on_template_render(self, sender, signal, template, context, **kwargs):
        self.rendered_templates.append(template)
        self.context.append(copy(context))

    @property
    def rendered_template_names(self):
        return [
            (
                f"{t.origin.template_name}#{t.name}"
                if isinstance(t, PartialTemplate)
                else t.name
            )
            for t in self.rendered_templates
            if t.name is not None
        ]

    def test(self):
        self.test_case._assert_template_used(
            self.template_name,
            self.rendered_template_names,
            self.msg_prefix,
            self.count,
        )

    def __enter__(self):
        template_rendered.connect(self.on_template_render)
        return self

    def __exit__(self, exc_type, exc_value, traceback):
        template_rendered.disconnect(self.on_template_render)
        if exc_type is not None:
            return
        self.test()


class _AssertTemplateNotUsedContext(_AssertTemplateUsedContext):
    def test(self):
        self.test_case.assertFalse(