Skip to content
This repository was archived by the owner on Feb 11, 2020. It is now read-only.

Commit 20608d2

Browse files
committed
Covered template exceptions and added small test improvements
1 parent fe691ac commit 20608d2

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed

bootstrap_ui/templatetags/bootstrap_ui_tags.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def render_tag(self, context, *tag_args, **tag_kwargs):
111111
scope = context.render_context[self]
112112

113113
if scope['use_tag'] == 'a':
114-
if 'link' not in scope:
114+
if 'link' not in scope or not scope['link']:
115115
raise TemplateSyntaxError(
116116
'%r requires keyword argument %r when %r is given' % (self.tag_name, 'link', scope['use_tag'])
117117
)

tests/tests.py

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# -*- coding: utf-8 -*-
2-
from django.template import Context, Template
2+
from django.template import Context, Template, TemplateSyntaxError
33
from django.test import SimpleTestCase
44
from django.utils.safestring import mark_safe
55

@@ -9,6 +9,7 @@ class HtmlTagNodeTest(SimpleTestCase):
99
SAMPLE_CONTENT_UNICODE = 'Lörém ípsüm'
1010
SAMPLE_CSS = 'simple sample'
1111
SAMPLE_TAG = 'span'
12+
SAMPLE_INVALID_TAG = 'foo'
1213

1314
HTMLTAG_START = mark_safe('<div>')
1415
HTMLTAG_CSS_CLASSES_START = mark_safe('<div class="' + SAMPLE_CSS + '">')
@@ -44,7 +45,7 @@ def setUp(self):
4445

4546
def test_htmltag_is_rendered(self):
4647
rendered = self.TEMPLATE_SIMPLE.render(Context({}))
47-
self.assertIn(self.HTMLTAG_START + self.HTMLTAG_END, rendered)
48+
self.assertInHTML(self.HTMLTAG_START + self.HTMLTAG_END, rendered)
4849

4950
def test_htmltag_content_is_rendered(self):
5051
rendered = self.TEMPLATE_CONTENT.render(Context({'content': self.SAMPLE_CONTENT}))
@@ -56,11 +57,33 @@ def test_htmltag_content_unicode_is_rendered(self):
5657

5758
def test_htmltag_css_classes_are_rendered(self):
5859
rendered = self.TEMPLATE_CSS.render(Context({'css': self.SAMPLE_CSS}))
59-
self.assertIn(self.HTMLTAG_CSS_CLASSES_START + self.HTMLTAG_END, rendered)
60+
self.assertInHTML(self.HTMLTAG_CSS_CLASSES_START + self.HTMLTAG_END, rendered)
6061

6162
def test_htmltag_tag_is_rendered(self):
6263
rendered = self.TEMPLATE_TAG.render(Context({'tag': self.SAMPLE_TAG}))
63-
self.assertIn(self.HTMLTAG_TAG_START + self.HTMLTAG_TAG_END, rendered)
64+
self.assertInHTML(self.HTMLTAG_TAG_START + self.HTMLTAG_TAG_END, rendered)
65+
66+
def test_htmltag_invalid_tag_raises_exception(self):
67+
with self.assertRaises(TemplateSyntaxError):
68+
self.TEMPLATE_TAG.render(Context({'tag': self.SAMPLE_INVALID_TAG}))
69+
70+
71+
class BootstrapTagsTest(SimpleTestCase):
72+
HTMLTAG_START = mark_safe('<div class="bs">')
73+
HTMLTAG_END = mark_safe('</div>')
74+
75+
TEMPLATE_SIMPLE = Template(
76+
'{% load bootstrap_ui_tags %}'
77+
'{% bootstraptag %}'
78+
'{% endbootstraptag %}'
79+
)
80+
81+
def setUp(self):
82+
pass
83+
84+
def test_bootstraptag_is_rendered(self):
85+
rendered = self.TEMPLATE_SIMPLE.render(Context({}))
86+
self.assertInHTML(self.HTMLTAG_START + self.HTMLTAG_END, rendered)
6487

6588

6689
class ListGroupTagsTest(SimpleTestCase):
@@ -109,13 +132,16 @@ def test_list_group_item_in_list_group_is_rendered(self):
109132

110133
def test_list_group_item_default_tag_is_rendered(self):
111134
rendered = self.TEMPLATE_SIMPLE.render(Context({}))
112-
self.assertIn(self.LIST_GROUP_ITEM_START, rendered)
113-
self.assertIn(self.LIST_GROUP_ITEM_END, rendered)
135+
self.assertInHTML(self.LIST_GROUP_ITEM_START + self.LIST_GROUP_ITEM_END, rendered)
114136

115137
def test_list_group_item_link_is_rendered(self):
116138
rendered = self.TEMPLATE_LINK.render(Context({'sample_link': self.SAMPLE_LINK, 'sample_label': self.SAMPLE_LABEL}))
117139
self.assertInHTML(self.LIST_GROUP_ITEM_LINK_START + self.SAMPLE_LABEL + self.LIST_GROUP_ITEM_LINK_END, rendered)
118140

141+
def test_list_group_item_link_without_destination_raises_exception(self):
142+
with self.assertRaises(TemplateSyntaxError):
143+
self.TEMPLATE_LINK.render(Context({}))
144+
119145

120146
class PanelTagsTest(SimpleTestCase):
121147
SAMPLE_HEADING = 'Lorem ipsum heading'
@@ -169,7 +195,7 @@ def setUp(self):
169195

170196
def test_panel_is_rendered(self):
171197
rendered = self.TEMPLATE_SIMPLE.render(Context({}))
172-
self.assertIn(
198+
self.assertInHTML(
173199
self.PANEL_START
174200
+ self.PANEL_HEADING_START + self.PANEL_HEADING_END
175201
+ self.PANEL_BODY_START + self.PANEL_BODY_END

0 commit comments

Comments
 (0)