Increased test coverage for various template tags.

This commit is contained in:
Mads Jensen 2018-03-24 21:38:20 +01:00 committed by Tim Graham
parent 623117d1f1
commit 4554f9a783
6 changed files with 70 additions and 10 deletions

View File

@ -8,22 +8,20 @@ class CycleTagTests(SimpleTestCase):
@setup({'cycle01': '{% cycle a %}'})
def test_cycle01(self):
with self.assertRaises(TemplateSyntaxError):
msg = "No named cycles in template. 'a' is not defined"
with self.assertRaisesMessage(TemplateSyntaxError, msg):
self.engine.get_template('cycle01')
@setup({'cycle05': '{% cycle %}'})
def test_cycle05(self):
with self.assertRaises(TemplateSyntaxError):
msg = "'cycle' tag requires at least two arguments"
with self.assertRaisesMessage(TemplateSyntaxError, msg):
self.engine.get_template('cycle05')
@setup({'cycle06': '{% cycle a %}'})
def test_cycle06(self):
with self.assertRaises(TemplateSyntaxError):
self.engine.get_template('cycle06')
@setup({'cycle07': '{% cycle a,b,c as foo %}{% cycle bar %}'})
def test_cycle07(self):
with self.assertRaises(TemplateSyntaxError):
msg = "Could not parse the remainder: ',b,c' from 'a,b,c'"
with self.assertRaisesMessage(TemplateSyntaxError, msg):
self.engine.get_template('cycle07')
@setup({'cycle10': "{% cycle 'a' 'b' 'c' as abc %}{% cycle abc %}"})
@ -69,7 +67,8 @@ class CycleTagTests(SimpleTestCase):
@setup({'cycle18': "{% cycle 'a' 'b' 'c' as foo invalid_flag %}"})
def test_cycle18(self):
with self.assertRaises(TemplateSyntaxError):
msg = "Only 'silent' flag is allowed after cycle's name, not 'invalid_flag'."
with self.assertRaisesMessage(TemplateSyntaxError, msg):
self.engine.get_template('cycle18')
@setup({'cycle19': "{% cycle 'a' 'b' as silent %}{% cycle silent %}"})
@ -167,3 +166,14 @@ class CycleTagTests(SimpleTestCase):
'values': [1, 2, 3, 4, 5, 6, 7, 8, 8, 8, 9, 9]
})
self.assertEqual(output, 'bcabcabcccaa')
@setup({
'undefined_cycle':
"{% cycle 'a' 'b' 'c' as cycler silent %}"
"{% for x in values %}"
"{% cycle undefined %}{{ cycler }}"
"{% endfor %}"
})
def test_cycle_undefined(self):
with self.assertRaisesMessage(TemplateSyntaxError, "Named cycle 'undefined' does not exist"):
self.engine.render_to_string('undefined_cycle')

View File

@ -1,4 +1,4 @@
from django.template import NodeList
from django.template import NodeList, TemplateSyntaxError
from django.template.base import Node
from django.template.loader_tags import ExtendsNode
from django.test import SimpleTestCase
@ -55,6 +55,9 @@ inheritance_templates = {
'inheritance40': "{% extends 'inheritance33' %}{% block opt %}new{{ block.super }}{% endblock %}",
'inheritance41': "{% extends 'inheritance36' %}{% block opt %}new{{ block.super }}{% endblock %}",
'inheritance42': "{% extends 'inheritance02'|cut:' ' %}",
'inheritance_empty': "{% extends %}",
'extends_duplicate': "{% extends 'base.html' %}{% extends 'base.html' %}",
'duplicate_block': "{% extends 'base.html' %}{% block content %}2{% endblock %}{% block content %}4{% endblock %}",
}
@ -400,6 +403,23 @@ class InheritanceTests(SimpleTestCase):
output = self.engine.render_to_string('inheritance42')
self.assertEqual(output, '1234')
@setup(inheritance_templates)
def test_inheritance_empty(self):
with self.assertRaisesMessage(TemplateSyntaxError, "'extends' takes one argument"):
self.engine.render_to_string('inheritance_empty')
@setup(inheritance_templates)
def test_extends_duplicate(self):
msg = "'extends' cannot appear more than once in the same template"
with self.assertRaisesMessage(TemplateSyntaxError, msg):
self.engine.render_to_string('extends_duplicate')
@setup(inheritance_templates)
def test_duplicate_block(self):
msg = "'block' tag with name 'content' appears more than once"
with self.assertRaisesMessage(TemplateSyntaxError, msg):
self.engine.render_to_string('duplicate_block')
class ExtendsNodeTests(SimpleTestCase):
def test_extends_node_repr(self):

View File

@ -1,3 +1,4 @@
from django.template import TemplateSyntaxError
from django.template.defaulttags import IfEqualNode
from django.test import SimpleTestCase
@ -217,6 +218,11 @@ class IfNotEqualTagTests(SimpleTestCase):
output = self.engine.render_to_string('ifnotequal04', {'a': 1, 'b': 1})
self.assertEqual(output, 'no')
@setup({'one_var': '{% ifnotequal a %}yes{% endifnotequal %}'})
def test_one_var(self):
with self.assertRaisesMessage(TemplateSyntaxError, "'ifnotequal' takes two arguments"):
self.engine.render_to_string('one_var', {'a': 1})
class IfEqualTests(SimpleTestCase):
def test_repr(self):

View File

@ -182,6 +182,15 @@ class IncludeTagTests(SimpleTestCase):
with self.assertRaises(TemplateSyntaxError):
template.render(context)
@setup({'include_empty': '{% include %}'})
def test_include_empty(self):
msg = (
"'include' tag takes at least one argument: the name of the "
"template to be included."
)
with self.assertRaisesMessage(TemplateSyntaxError, msg):
self.engine.get_template('include_empty')
class IncludeTests(SimpleTestCase):

View File

@ -1,4 +1,5 @@
from django.test import SimpleTestCase
from django.utils.lorem_ipsum import WORDS
from ..utils import setup
@ -9,3 +10,11 @@ class LoremTagTests(SimpleTestCase):
def test_lorem1(self):
output = self.engine.render_to_string('lorem1')
self.assertEqual(output, 'lorem ipsum dolor')
@setup({'lorem_random': '{% lorem 3 w random %}'})
def test_lorem_random(self):
output = self.engine.render_to_string('lorem_random')
words = output.split(' ')
self.assertEqual(len(words), 3)
for word in words:
self.assertIn(word, WORDS)

View File

@ -1,5 +1,6 @@
from datetime import datetime
from django.template import TemplateSyntaxError
from django.test import SimpleTestCase
from django.utils.formats import date_format
@ -59,3 +60,8 @@ class NowTagTests(SimpleTestCase):
self.assertEqual(output, '-%d %d %d-' % (
datetime.now().day, datetime.now().month, datetime.now().year,
))
@setup({'no_args': '{% now %}'})
def test_now_args(self):
with self.assertRaisesMessage(TemplateSyntaxError, "'now' statement takes one argument"):
self.engine.render_to_string('no_args')