Skip to content

Commit 15a8d80

Browse files
refactor bird templatetag tests (#89)
1 parent b640f87 commit 15a8d80

File tree

2 files changed

+1059
-405
lines changed

2 files changed

+1059
-405
lines changed

tests/conftest.py

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
import contextlib
44
import logging
55
import re
6+
from dataclasses import dataclass
7+
from dataclasses import field
68
from pathlib import Path
9+
from typing import Any
710

811
import pytest
912
from django.conf import settings
@@ -84,6 +87,37 @@ def _override_app_settings(**kwargs):
8487
return _override_app_settings
8588

8689

90+
@dataclass
91+
class TestComponent:
92+
name: str
93+
content: str
94+
parent_dir: str = "bird"
95+
sub_dir: str | None = None
96+
97+
def create(self, base_dir: Path) -> Path:
98+
parent = base_dir / self.parent_dir
99+
parent.mkdir(exist_ok=True)
100+
101+
if self.sub_dir is not None:
102+
dir = parent / self.sub_dir
103+
dir.mkdir(exist_ok=True)
104+
else:
105+
dir = parent
106+
107+
template = dir / f"{self.name}.html"
108+
template.write_text(self.content)
109+
return template
110+
111+
112+
@dataclass
113+
class TestComponentCase:
114+
component: TestComponent
115+
template_content: str
116+
expected: str
117+
description: str = ""
118+
template_context: dict[str, Any] = field(default_factory=dict)
119+
120+
87121
@pytest.fixture
88122
def create_bird_dir(templates_dir):
89123
def func(name):
@@ -152,25 +186,21 @@ def _create_template(template_file: Path) -> DjangoTemplate:
152186

153187
@pytest.fixture
154188
def normalize_whitespace():
155-
def func(text):
156-
# this makes writing tests much easier, as it gets rid of any
157-
# existing whitespace that may be present in the template file
158-
189+
def func(text: str) -> str:
190+
"""Normalize whitespace in rendered template output"""
159191
# multiple whitespace characters
160192
text = re.sub(r"\s+", " ", text)
161-
162193
# after opening tag, including when there are attributes
163194
text = re.sub(r"<(\w+)(\s+[^>]*)?\s*>", r"<\1\2>", text)
164-
165195
# before closing tag
166196
text = re.sub(r"\s+>", ">", text)
167-
168197
# after opening tag and before closing tag
169198
text = re.sub(r">\s+<", "><", text)
170-
171199
# immediately after opening tag (including attributes) or before closing tag
172200
text = re.sub(r"(<\w+(?:\s+[^>]*)?>)\s+|\s+(<\/\w+>)", r"\1\2", text)
173-
201+
# between tags and text content
202+
text = re.sub(r">\s+([^<])", r">\1", text)
203+
text = re.sub(r"([^>])\s+<", r"\1<", text)
174204
return text.strip()
175205

176206
return func

0 commit comments

Comments
 (0)