-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuilder_test.py
More file actions
64 lines (48 loc) · 1.35 KB
/
builder_test.py
File metadata and controls
64 lines (48 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""Demonstrate using Builder design pattern."""
from typing import Final
from builder.builder import HtmlElement
# fmt: off
EXP_HELLO: Final[str] = """<p>
hello
</p>"""
EXP_HELLOS: Final[str] = """<ul>
<li>
hello
</li>
<li>
hola
</li>
</ul>"""
# fmt: on
def test_create_html_manual() -> None:
"""Create HTML elements manually."""
words = ["hello"]
parts = ["<p>"]
for word in words:
parts.append(f" {word}")
parts.append("</p>")
assert "\n".join(parts) == EXP_HELLO
words = ["hello", "hola"]
parts = ["<ul>"]
for word in words:
parts.append(" <li>")
parts.append(f" {word}")
parts.append(" </li>")
parts.append("</ul>")
assert "\n".join(parts) == EXP_HELLOS
def test_create_html_builder() -> None:
"""Create HTML elements using a builder."""
builder = HtmlElement.create("p", "hello")
assert str(builder) == EXP_HELLO
builder = HtmlElement.create("ul")
builder.add_child("li", "hello")
builder.add_child("li", "hola")
assert str(builder) == EXP_HELLOS
def test_create_html_builder_fluent() -> None:
"""Create HTML elements using a builder with a fluent API."""
builder = (
HtmlElement.create("ul")
.add_child_fluent("li", "hello")
.add_child_fluent("li", "hola")
)
assert str(builder) == EXP_HELLOS