Skip to content

Commit 4e69c92

Browse files
move normalize_whitespace from fixture to utils (#155)
1 parent fd828c8 commit 4e69c92

File tree

7 files changed

+102
-113
lines changed

7 files changed

+102
-113
lines changed

tests/conftest.py

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import contextlib
44
import logging
5-
import re
65
from dataclasses import dataclass
76
from pathlib import Path
87
from typing import TYPE_CHECKING
@@ -243,28 +242,6 @@ def example_template(templates_dir):
243242
)
244243

245244

246-
@pytest.fixture
247-
def normalize_whitespace():
248-
def func(text: str) -> str:
249-
"""Normalize whitespace in rendered template output"""
250-
# multiple whitespace characters
251-
text = re.sub(r"\s+", " ", text)
252-
# after opening tag, including when there are attributes
253-
text = re.sub(r"<(\w+)(\s+[^>]*)?\s*>", r"<\1\2>", text)
254-
# before closing tag
255-
text = re.sub(r"\s+>", ">", text)
256-
# after opening tag and before closing tag
257-
text = re.sub(r">\s+<", "><", text)
258-
# immediately after opening tag (including attributes) or before closing tag
259-
text = re.sub(r"(<\w+(?:\s+[^>]*)?>)\s+|\s+(<\/\w+>)", r"\1\2", text)
260-
# between tags and text content
261-
text = re.sub(r">\s+([^<])", r">\1", text)
262-
text = re.sub(r"([^>])\s+<", r"\1<", text)
263-
return text.strip()
264-
265-
return func
266-
267-
268245
@pytest.fixture(autouse=True)
269246
def registry():
270247
from django_bird.components import components

tests/templatetags/test_bird.py

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from django_bird.templatetags.tags.bird import do_bird
2121
from tests.utils import TestComponent
2222
from tests.utils import TestComponentCase
23+
from tests.utils import normalize_whitespace
2324

2425

2526
class TestTagParsing:
@@ -153,9 +154,7 @@ def test_attrs_do_bird(self, params, expected_attrs):
153154
],
154155
ids=lambda x: x.description,
155156
)
156-
def test_basic_name_templatetag(
157-
self, test_case, templates_dir, normalize_whitespace
158-
):
157+
def test_basic_name_templatetag(self, test_case, templates_dir):
159158
test_case.component.create(templates_dir)
160159

161160
template = Template(
@@ -165,7 +164,7 @@ def test_basic_name_templatetag(
165164

166165
assert normalize_whitespace(rendered) == test_case.expected
167166

168-
def test_nested_name_templatetag(self, templates_dir, normalize_whitespace):
167+
def test_nested_name_templatetag(self, templates_dir):
169168
test_case = TestComponentCase(
170169
description="Nested component name",
171170
component=TestComponent(
@@ -254,9 +253,7 @@ def test_nested_name_templatetag(self, templates_dir, normalize_whitespace):
254253
],
255254
ids=lambda x: x.description,
256255
)
257-
def test_dynamic_name_template_context_templatetag(
258-
self, test_case, templates_dir, normalize_whitespace
259-
):
256+
def test_dynamic_name_template_context_templatetag(self, test_case, templates_dir):
260257
test_case.component.create(templates_dir)
261258

262259
template = Template(
@@ -266,7 +263,7 @@ def test_dynamic_name_template_context_templatetag(
266263

267264
assert normalize_whitespace(rendered) == test_case.expected
268265

269-
def test_dynamic_name_with_string(self, templates_dir, normalize_whitespace):
266+
def test_dynamic_name_with_string(self, templates_dir):
270267
button = TestComponent(
271268
name="button",
272269
content="""
@@ -299,7 +296,7 @@ def test_nonexistent_name_templatetag(self, templates_dir):
299296
template.render(Context({}))
300297

301298

302-
def test_self_closing_tag(templates_dir, normalize_whitespace):
299+
def test_self_closing_tag(templates_dir):
303300
test_case = TestComponentCase(
304301
component=TestComponent(
305302
name="image",
@@ -359,7 +356,7 @@ class TestAttributes:
359356
],
360357
ids=lambda x: x.description,
361358
)
362-
def test_basic(self, test_case, templates_dir, normalize_whitespace):
359+
def test_basic(self, test_case, templates_dir):
363360
test_case.component.create(templates_dir)
364361

365362
template = Template(test_case.template_content)
@@ -424,7 +421,7 @@ def test_basic(self, test_case, templates_dir, normalize_whitespace):
424421
],
425422
ids=lambda x: x.description,
426423
)
427-
def test_boolean(self, test_case, templates_dir, normalize_whitespace):
424+
def test_boolean(self, test_case, templates_dir):
428425
test_case.component.create(templates_dir)
429426

430427
template = Template(test_case.template_content)
@@ -493,9 +490,7 @@ def test_boolean(self, test_case, templates_dir, normalize_whitespace):
493490
],
494491
ids=lambda x: x.description,
495492
)
496-
def test_dynamic_template_context(
497-
self, test_case, templates_dir, normalize_whitespace
498-
):
493+
def test_dynamic_template_context(self, test_case, templates_dir):
499494
test_case.component.create(templates_dir)
500495

501496
template = Template(test_case.template_content)
@@ -553,9 +548,7 @@ def test_dynamic_template_context(
553548
],
554549
ids=lambda x: x.description,
555550
)
556-
def test_nested_template_context(
557-
self, test_case, templates_dir, normalize_whitespace
558-
):
551+
def test_nested_template_context(self, test_case, templates_dir):
559552
test_case.component.create(templates_dir)
560553

561554
template = Template(test_case.template_content)
@@ -625,7 +618,7 @@ def test_nested_template_context(
625618
],
626619
ids=lambda x: x.description,
627620
)
628-
def test_error_handling(self, test_case, templates_dir, normalize_whitespace):
621+
def test_error_handling(self, test_case, templates_dir):
629622
test_case.component.create(templates_dir)
630623

631624
template = Template(test_case.template_content)
@@ -646,7 +639,6 @@ def test_data_bird_attributes(
646639
expected,
647640
override_app_settings,
648641
templates_dir,
649-
normalize_whitespace,
650642
):
651643
button = TestComponent(
652644
name="button",
@@ -729,7 +721,7 @@ class TestProperties:
729721
],
730722
ids=lambda x: x.description,
731723
)
732-
def test_basic(self, test_case, templates_dir, normalize_whitespace):
724+
def test_basic(self, test_case, templates_dir):
733725
test_case.component.create(templates_dir)
734726

735727
template = Template(test_case.template_content)
@@ -805,7 +797,7 @@ def test_basic(self, test_case, templates_dir, normalize_whitespace):
805797
],
806798
ids=lambda x: x.description,
807799
)
808-
def test_dynamic(self, test_case, templates_dir, normalize_whitespace):
800+
def test_dynamic(self, test_case, templates_dir):
809801
test_case.component.create(templates_dir)
810802

811803
template = Template(test_case.template_content)
@@ -874,7 +866,7 @@ def test_dynamic(self, test_case, templates_dir, normalize_whitespace):
874866
],
875867
ids=lambda x: x.description,
876868
)
877-
def test_error_handling(self, test_case, templates_dir, normalize_whitespace):
869+
def test_error_handling(self, test_case, templates_dir):
878870
test_case.component.create(templates_dir)
879871

880872
template = Template(test_case.template_content)
@@ -883,7 +875,7 @@ def test_error_handling(self, test_case, templates_dir, normalize_whitespace):
883875
assert normalize_whitespace(rendered) == test_case.expected
884876

885877

886-
def test_attrs_and_props(templates_dir, normalize_whitespace):
878+
def test_attrs_and_props(templates_dir):
887879
test_case = TestComponentCase(
888880
component=TestComponent(
889881
name="button",
@@ -969,7 +961,7 @@ class TestSlots:
969961
],
970962
ids=lambda x: x.description,
971963
)
972-
def test_default(self, test_case, templates_dir, normalize_whitespace):
964+
def test_default(self, test_case, templates_dir):
973965
test_case.component.create(templates_dir)
974966

975967
template = Template(test_case.template_content)
@@ -1065,7 +1057,7 @@ def test_default(self, test_case, templates_dir, normalize_whitespace):
10651057
],
10661058
ids=lambda x: x.description,
10671059
)
1068-
def test_named(self, test_case, templates_dir, normalize_whitespace):
1060+
def test_named(self, test_case, templates_dir):
10691061
test_case.component.create(templates_dir)
10701062

10711063
template = Template(test_case.template_content)
@@ -1159,9 +1151,7 @@ def make_fancy(value):
11591151
],
11601152
ids=lambda x: x.description,
11611153
)
1162-
def test_with_outside_templatetag(
1163-
self, test_case, fancy_filter, templates_dir, normalize_whitespace
1164-
):
1154+
def test_with_outside_templatetag(self, test_case, fancy_filter, templates_dir):
11651155
test_case.component.create(templates_dir)
11661156

11671157
template = Template(test_case.template_content)
@@ -1170,7 +1160,7 @@ def test_with_outside_templatetag(
11701160
assert normalize_whitespace(rendered) == test_case.expected
11711161

11721162

1173-
def test_nested_components_with_loops(templates_dir, normalize_whitespace):
1163+
def test_nested_components_with_loops(templates_dir):
11741164
nav = TestComponent(
11751165
name="nav",
11761166
content="""
@@ -1302,7 +1292,7 @@ def test_nested_components_with_loops(templates_dir, normalize_whitespace):
13021292
],
13031293
ids=lambda x: x.description,
13041294
)
1305-
def test_parent_context_access(test_case, templates_dir, normalize_whitespace):
1295+
def test_parent_context_access(test_case, templates_dir):
13061296
test_case.component.create(templates_dir)
13071297

13081298
template = Template(test_case.template_content)
@@ -1394,7 +1384,7 @@ def test_parent_context_access(test_case, templates_dir, normalize_whitespace):
13941384
],
13951385
ids=lambda x: x.description,
13961386
)
1397-
def test_only_flag(test_case, templates_dir, normalize_whitespace):
1387+
def test_only_flag(test_case, templates_dir):
13981388
test_case.component.create(templates_dir)
13991389

14001390
template = Template(test_case.template_content)

tests/templatetags/test_slot.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from django_bird.templatetags.tags.slot import parse_slot_name
99
from tests.utils import TestComponent
1010
from tests.utils import TestComponentCase
11+
from tests.utils import normalize_whitespace
1112

1213

1314
@pytest.mark.parametrize(
@@ -44,7 +45,7 @@ class TestTemplateTag:
4445
'{% bird:slot name="default" %}{% endbird:slot %}',
4546
],
4647
)
47-
def test_default_slot(self, component_content, templates_dir, normalize_whitespace):
48+
def test_default_slot(self, component_content, templates_dir):
4849
test_case = TestComponentCase(
4950
component=TestComponent(
5051
name="test",
@@ -61,7 +62,7 @@ def test_default_slot(self, component_content, templates_dir, normalize_whitespa
6162

6263
assert normalize_whitespace(rendered) == test_case.expected
6364

64-
def test_default_content(self, templates_dir, normalize_whitespace):
65+
def test_default_content(self, templates_dir):
6566
test_case = TestComponentCase(
6667
component=TestComponent(
6768
name="test",
@@ -87,7 +88,7 @@ def test_default_content(self, templates_dir, normalize_whitespace):
8788

8889
assert normalize_whitespace(rendered) == test_case.expected
8990

90-
def test_default_content_override(self, templates_dir, normalize_whitespace):
91+
def test_default_content_override(self, templates_dir):
9192
test_case = TestComponentCase(
9293
component=TestComponent(
9394
name="test",
@@ -126,7 +127,7 @@ def test_default_content_override(self, templates_dir, normalize_whitespace):
126127
"{% bird:slot name=named_slot %}{% endbird:slot %}",
127128
],
128129
)
129-
def test_named_slot(self, component_content, templates_dir, normalize_whitespace):
130+
def test_named_slot(self, component_content, templates_dir):
130131
test_case = TestComponentCase(
131132
component=TestComponent(
132133
name="test",
@@ -210,7 +211,7 @@ def test_named_slot(self, component_content, templates_dir, normalize_whitespace
210211
],
211212
ids=lambda x: x.description,
212213
)
213-
def test_nested_slots(self, test_case, templates_dir, normalize_whitespace):
214+
def test_nested_slots(self, test_case, templates_dir):
214215
test_case.component.create(templates_dir)
215216

216217
template = Template(test_case.template_content)
@@ -254,7 +255,7 @@ def test_nested_slots(self, test_case, templates_dir, normalize_whitespace):
254255
],
255256
ids=lambda x: x.description,
256257
)
257-
def test_template_content(self, test_case, templates_dir, normalize_whitespace):
258+
def test_template_content(self, test_case, templates_dir):
258259
test_case.component.create(templates_dir)
259260

260261
template = Template(test_case.template_content)

tests/test_components.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
from .utils import TestAsset
2525
from .utils import TestComponent
26+
from .utils import normalize_whitespace
2627

2728

2829
class TestComponentClass:
@@ -208,7 +209,6 @@ def test_id_sequence(
208209
expected,
209210
override_app_settings,
210211
templates_dir,
211-
normalize_whitespace,
212212
):
213213
button = TestComponent(
214214
name="button", content="<button {{ attrs }}>{{ slot }}</button>"

tests/test_fixtures.py

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from __future__ import annotations
22

3-
import pytest
43
from django.template.loader import get_template
54

65
from .utils import TestComponent
@@ -16,21 +15,3 @@ def test_test_component(templates_dir):
1615

1716
assert template
1817
assert template.render({}) == content
19-
20-
21-
@pytest.mark.parametrize(
22-
"contents,expected",
23-
[
24-
("<button> Click me </button>", "<button>Click me</button>"),
25-
("<button> Click me</button>", "<button>Click me</button>"),
26-
("<button>Click me </button>", "<button>Click me</button>"),
27-
("<button >Click me</button>", "<button>Click me</button>"),
28-
(
29-
"<button class='btn'> Click me </button>",
30-
"<button class='btn'>Click me</button>",
31-
),
32-
("\n<button>\n Click me\n</button>\n", "<button>Click me</button>"),
33-
],
34-
)
35-
def test_normalize_whitespace(contents, expected, normalize_whitespace):
36-
assert normalize_whitespace(contents) == expected

0 commit comments

Comments
 (0)