From 4554f9a783665d64b59c35b53ae71178e56ac783 Mon Sep 17 00:00:00 2001 From: Mads Jensen Date: Sat, 24 Mar 2018 21:38:20 +0100 Subject: [PATCH] Increased test coverage for various template tags. --- .../template_tests/syntax_tests/test_cycle.py | 28 +++++++++++++------ .../syntax_tests/test_extends.py | 22 ++++++++++++++- .../syntax_tests/test_if_equal.py | 6 ++++ .../syntax_tests/test_include.py | 9 ++++++ .../template_tests/syntax_tests/test_lorem.py | 9 ++++++ tests/template_tests/syntax_tests/test_now.py | 6 ++++ 6 files changed, 70 insertions(+), 10 deletions(-) diff --git a/tests/template_tests/syntax_tests/test_cycle.py b/tests/template_tests/syntax_tests/test_cycle.py index b5712b54bb..12bf66e117 100644 --- a/tests/template_tests/syntax_tests/test_cycle.py +++ b/tests/template_tests/syntax_tests/test_cycle.py @@ -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') diff --git a/tests/template_tests/syntax_tests/test_extends.py b/tests/template_tests/syntax_tests/test_extends.py index 5b0b8d1811..0689838ae8 100644 --- a/tests/template_tests/syntax_tests/test_extends.py +++ b/tests/template_tests/syntax_tests/test_extends.py @@ -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): diff --git a/tests/template_tests/syntax_tests/test_if_equal.py b/tests/template_tests/syntax_tests/test_if_equal.py index 82ddf557db..f416b95523 100644 --- a/tests/template_tests/syntax_tests/test_if_equal.py +++ b/tests/template_tests/syntax_tests/test_if_equal.py @@ -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): diff --git a/tests/template_tests/syntax_tests/test_include.py b/tests/template_tests/syntax_tests/test_include.py index ca98e09771..8587639674 100644 --- a/tests/template_tests/syntax_tests/test_include.py +++ b/tests/template_tests/syntax_tests/test_include.py @@ -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): diff --git a/tests/template_tests/syntax_tests/test_lorem.py b/tests/template_tests/syntax_tests/test_lorem.py index 9963bbd026..631bc3d067 100644 --- a/tests/template_tests/syntax_tests/test_lorem.py +++ b/tests/template_tests/syntax_tests/test_lorem.py @@ -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) diff --git a/tests/template_tests/syntax_tests/test_now.py b/tests/template_tests/syntax_tests/test_now.py index 54cd4ccd2e..ac2a167b4f 100644 --- a/tests/template_tests/syntax_tests/test_now.py +++ b/tests/template_tests/syntax_tests/test_now.py @@ -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')