From 3b81dbe844b750c05bda6ae1ad15c889d1dc39e5 Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Sat, 20 Jun 2015 07:37:43 -0400 Subject: [PATCH] Used %r in the TextNode repr to show newlines better. --- django/template/base.py | 4 ++-- tests/template_tests/test_nodelist.py | 18 +++++++++++++++++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/django/template/base.py b/django/template/base.py index 6579a41279..2f1b92851d 100644 --- a/django/template/base.py +++ b/django/template/base.py @@ -976,8 +976,8 @@ class TextNode(Node): self.s = s def __repr__(self): - return force_str("" % self.s[:25], 'ascii', - errors='replace') + rep = "<%s: %r>" % (self.__class__.__name__, self.s[:25]) + return force_str(rep, 'ascii', errors='replace') def render(self, context): return self.s diff --git a/tests/template_tests/test_nodelist.py b/tests/template_tests/test_nodelist.py index 3070c9d68a..46b48404dd 100644 --- a/tests/template_tests/test_nodelist.py +++ b/tests/template_tests/test_nodelist.py @@ -1,7 +1,8 @@ from unittest import TestCase from django.template import Context, Engine -from django.template.base import VariableNode +from django.template.base import TextNode, VariableNode +from django.utils import six class NodelistTest(TestCase): @@ -32,6 +33,21 @@ class NodelistTest(TestCase): self.assertEqual(len(vars), 1) +class TextNodeTest(TestCase): + + def test_textnode_repr(self): + engine = Engine() + for temptext, reprtext in [ + ("Hello, world!", ""), + ("One\ntwo.", ""), + ]: + template = engine.from_string(temptext) + texts = template.nodelist.get_nodes_by_type(TextNode) + if six.PY3: + reprtext = reprtext.replace("u'", "'") + self.assertEqual(repr(texts[0]), reprtext) + + class ErrorIndexTest(TestCase): """ Checks whether index of error is calculated correctly in