Used %r in the TextNode repr to show newlines better.

This commit is contained in:
Ned Batchelder 2015-06-20 07:37:43 -04:00 committed by Tim Graham
parent c31bf8cb54
commit 3b81dbe844
2 changed files with 19 additions and 3 deletions

View File

@ -976,8 +976,8 @@ class TextNode(Node):
self.s = s self.s = s
def __repr__(self): def __repr__(self):
return force_str("<Text Node: '%s'>" % self.s[:25], 'ascii', rep = "<%s: %r>" % (self.__class__.__name__, self.s[:25])
errors='replace') return force_str(rep, 'ascii', errors='replace')
def render(self, context): def render(self, context):
return self.s return self.s

View File

@ -1,7 +1,8 @@
from unittest import TestCase from unittest import TestCase
from django.template import Context, Engine 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): class NodelistTest(TestCase):
@ -32,6 +33,21 @@ class NodelistTest(TestCase):
self.assertEqual(len(vars), 1) self.assertEqual(len(vars), 1)
class TextNodeTest(TestCase):
def test_textnode_repr(self):
engine = Engine()
for temptext, reprtext in [
("Hello, world!", "<TextNode: u'Hello, world!'>"),
("One\ntwo.", "<TextNode: u'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): class ErrorIndexTest(TestCase):
""" """
Checks whether index of error is calculated correctly in Checks whether index of error is calculated correctly in