from django.template.defaultfilters import linebreaks_filter from django.test import SimpleTestCase from django.utils.safestring import mark_safe from ..utils import setup class LinebreaksTests(SimpleTestCase): """ The contents in "linebreaks" are escaped according to the current autoescape setting. """ @setup({'linebreaks01': '{{ a|linebreaks }} {{ b|linebreaks }}'}) def test_linebreaks01(self): output = self.engine.render_to_string('linebreaks01', {"a": "x&\ny", "b": mark_safe("x&\ny")}) self.assertEqual(output, "

x&
y

x&
y

") @setup({'linebreaks02': '{% autoescape off %}{{ a|linebreaks }} {{ b|linebreaks }}{% endautoescape %}'}) def test_linebreaks02(self): output = self.engine.render_to_string('linebreaks02', {"a": "x&\ny", "b": mark_safe("x&\ny")}) self.assertEqual(output, "

x&
y

x&
y

") class FunctionTests(SimpleTestCase): def test_line(self): self.assertEqual(linebreaks_filter('line 1'), '

line 1

') def test_newline(self): self.assertEqual(linebreaks_filter('line 1\nline 2'), '

line 1
line 2

') def test_carriage(self): self.assertEqual(linebreaks_filter('line 1\rline 2'), '

line 1
line 2

') def test_carriage_newline(self): self.assertEqual(linebreaks_filter('line 1\r\nline 2'), '

line 1
line 2

') def test_non_string_input(self): self.assertEqual(linebreaks_filter(123), '

123

') def test_autoescape(self): self.assertEqual( linebreaks_filter('foo\nbar\nbuz'), '

foo
<a>bar</a>
buz

', ) def test_autoescape_off(self): self.assertEqual( linebreaks_filter('foo\nbar\nbuz', autoescape=False), '

foo
bar
buz

', )