Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
dyve committed Apr 24, 2024
1 parent 5f8ef9c commit 62e7053
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 69 deletions.
2 changes: 1 addition & 1 deletion src/bootstrap4/renderers.py
Expand Up @@ -31,7 +31,7 @@
render_label,
)
from .text import text_value
from .utils import DJANGO_VERSION, add_css_class, render_template_file
from .utils import add_css_class, render_template_file

try:
# If Django is set up without a database, importing this widget gives RuntimeError
Expand Down
3 changes: 0 additions & 3 deletions src/bootstrap4/utils.py
Expand Up @@ -2,7 +2,6 @@
from collections.abc import Mapping
from urllib.parse import parse_qs, urlparse, urlunparse

from django import get_version
from django.forms.utils import flatatt
from django.template.base import FilterExpression, TemplateSyntaxError, Variable, VariableDoesNotExist, kwarg_re
from django.template.loader import get_template
Expand All @@ -13,8 +12,6 @@

from .text import text_value

DJANGO_VERSION = int(get_version()[:1])

# RegEx for quoted string
QUOTED_STRING = re.compile(r'^["\'](?P<noquotes>.+)["\']$')

Expand Down
87 changes: 22 additions & 65 deletions tests/test_forms.py
Expand Up @@ -4,7 +4,6 @@
from django.utils.html import escape

from bootstrap4.exceptions import BootstrapError
from bootstrap4.utils import DJANGO_VERSION

from .forms import CharFieldTestForm, TestForm
from .utils import render_field, render_form_field, render_template_with_form
Expand All @@ -29,17 +28,13 @@ def test_show_help(self):
res = render_form_field("subject")
self.assertIn("my_help_text", res)
self.assertNotIn("<i>my_help_text</i>", res)
res = render_template_with_form(
"{% bootstrap_field form.subject show_help=0 %}"
)
res = render_template_with_form("{% bootstrap_field form.subject show_help=0 %}")
self.assertNotIn("my_help_text", res)

def test_help_with_quotes(self):
# Checkboxes get special handling, so test a checkbox and something else
res = render_form_field("sender")
self.assertIn(
'title="{}"'.format(escape(TestForm.base_fields["sender"].help_text)), res
)
self.assertIn('title="{}"'.format(escape(TestForm.base_fields["sender"].help_text)), res)
res = render_form_field("cc_myself")
self.assertIn(
'title="{}"'.format(escape(TestForm.base_fields["cc_myself"].help_text)),
Expand All @@ -62,9 +57,7 @@ def test_xss_field(self):
res,
)
self.assertIn(
(
'placeholder="XSS&quot; onmouseover=&quot;alert(&#x27;Hello, XSS&#x27;)&quot; foo=&quot;"'
),
('placeholder="XSS&quot; onmouseover=&quot;alert(&#x27;Hello, XSS&#x27;)&quot; foo=&quot;"'),
res,
)

Expand Down Expand Up @@ -106,39 +99,29 @@ def test_checkbox(self):
# strip out newlines and spaces around newlines
res = "".join(line.strip() for line in res.split("\n"))
res = BeautifulSoup(res, "html.parser")
form_group = self._select_one_element(
res, ".form-group", "Checkbox should be rendered inside a .form-group."
)
form_group = self._select_one_element(res, ".form-group", "Checkbox should be rendered inside a .form-group.")
form_check = self._select_one_element(
form_group,
".form-check",
"There should be a .form-check inside .form-group",
)
checkbox = self._select_one_element(
form_check, "input", "The checkbox should be inside the .form-check"
)
checkbox = self._select_one_element(form_check, "input", "The checkbox should be inside the .form-check")
self.assertIn(
"form-check-input",
checkbox["class"],
"The checkbox should have the class 'form-check-input'.",
)
label = checkbox.next_sibling
self.assertIsNotNone(label, "The label should be rendered after the checkbox.")
self.assertEqual(
label.name, "label", "After the checkbox there should be a label."
)
self.assertEqual(label.name, "label", "After the checkbox there should be a label.")
self.assertEqual(
label["for"],
checkbox["id"],
"The for attribute of the label should be the id of the checkbox.",
)
help_text = label.next_sibling
self.assertIsNotNone(
help_text, "The help text should be rendered after the label."
)
self.assertEqual(
help_text.name, "small", "The help text should be rendered as <small> tag."
)
self.assertIsNotNone(help_text, "The help text should be rendered after the label.")
self.assertEqual(help_text.name, "small", "The help text should be rendered as <small> tag.")
self.assertIn(
"form-text",
help_text["class"],
Expand Down Expand Up @@ -185,9 +168,7 @@ def test_required_field(self):
# Required settings in field
form_field = "form.subject"
rendered = render_template_with_form(
"{% bootstrap_field "
+ form_field
+ ' required_css_class="test-required" %}'
"{% bootstrap_field " + form_field + ' required_css_class="test-required" %}'
)
self.assertIn("test-required", rendered)

Expand All @@ -202,16 +183,10 @@ def test_empty_permitted(self):
self.assertNotIn(required_css_class, res)

def test_input_group(self):
res = render_template_with_form(
'{% bootstrap_field form.subject addon_before="$" addon_after=".00" %}'
)
res = render_template_with_form('{% bootstrap_field form.subject addon_before="$" addon_after=".00" %}')
self.assertIn('class="input-group"', res)
self.assertIn(
'class="input-group-prepend"><span class="input-group-text">$', res
)
self.assertIn(
'class="input-group-append"><span class="input-group-text">.00', res
)
self.assertIn('class="input-group-prepend"><span class="input-group-text">$', res)
self.assertIn('class="input-group-append"><span class="input-group-text">.00', res)

def test_input_group_addon_button(self):
res = render_template_with_form(
Expand All @@ -225,9 +200,7 @@ def test_input_group_addon_button(self):
self.assertIn('<div class="input-group-append">.00</div>', res)

def test_input_group_addon_empty(self):
res = render_template_with_form(
'{% bootstrap_field form.subject addon_before=None addon_after="after" %}'
) # noqa
res = render_template_with_form('{% bootstrap_field form.subject addon_before=None addon_after="after" %}') # noqa
self.assertIn('class="input-group"', res)
self.assertNotIn("input-group-prepend", res)
self.assertIn(
Expand Down Expand Up @@ -262,27 +235,21 @@ def test_input_group_addon_validation(self):
self.assertEqual(
len(res.select(".form-group > .invalid-feedback")),
0,
"The invalid-feedback message must be placed inside the "
"input-group and not inside the form-group",
"The invalid-feedback message must be placed inside the " "input-group and not inside the form-group",
)
self.assertEqual(
len(res.select(".input-group > .form-text")),
0,
"The form-text message must be placed inside the form-group and "
"not inside the input-group",
"The form-text message must be placed inside the form-group and " "not inside the input-group",
)

def test_size(self):
def _test_size(param, klass):
res = render_template_with_form(
'{% bootstrap_field form.subject size="' + param + '" %}'
)
res = render_template_with_form('{% bootstrap_field form.subject size="' + param + '" %}')
self.assertIn(klass, res)

def _test_size_medium(param):
res = render_template_with_form(
'{% bootstrap_field form.subject size="' + param + '" %}'
)
res = render_template_with_form('{% bootstrap_field form.subject size="' + param + '" %}')
self.assertNotIn("form-control-lg", res)
self.assertNotIn("form-control-sm", res)
self.assertNotIn("form-control-md", res)
Expand All @@ -307,9 +274,7 @@ def test_field_same_render(self):
self.assertEqual(rendered_a, rendered_b)

def test_label(self):
res = render_template_with_form(
'{% bootstrap_label "foobar" label_for="subject" %}'
)
res = render_template_with_form('{% bootstrap_label "foobar" label_for="subject" %}')
self.assertEqual('<label for="subject">foobar</label>', res)

def test_attributes_consistency(self):
Expand All @@ -320,9 +285,7 @@ def test_attributes_consistency(self):

class ComponentsTest(TestCase):
def test_bootstrap_alert(self):
res = render_template_with_form(
'{% bootstrap_alert "content" alert_type="danger" %}'
)
res = render_template_with_form('{% bootstrap_alert "content" alert_type="danger" %}')
self.assertEqual(
res.strip(),
'<div class="alert alert-danger alert-dismissible" role="alert">'
Expand All @@ -335,23 +298,17 @@ def test_bootstrap_alert(self):
class ShowLabelTest(TestCase):
def test_show_label_false(self):
form = CharFieldTestForm()
res = render_template_with_form(
"{% bootstrap_form form show_label=False %}", {"form": form}
)
res = render_template_with_form("{% bootstrap_form form show_label=False %}", {"form": form})
self.assertIn("sr-only", res)

def test_show_label_sr_only(self):
form = CharFieldTestForm()
res = render_template_with_form(
"{% bootstrap_form form show_label='sr-only' %}", {"form": form}
)
res = render_template_with_form("{% bootstrap_form form show_label='sr-only' %}", {"form": form})
self.assertIn("sr-only", res)

def test_show_label_skip(self):
form = CharFieldTestForm()
res = render_template_with_form(
"{% bootstrap_form form show_label='skip' %}", {"form": form}
)
res = render_template_with_form("{% bootstrap_form form show_label='skip' %}", {"form": form})
self.assertNotIn("<label>", res)

def test_for_formset(self):
Expand Down

0 comments on commit 62e7053

Please sign in to comment.