xref: /aosp_15_r20/external/toolchain-utils/cros_utils/tiny_render_test.py (revision 760c253c1ed00ce9abd48f8546f08516e57485fe)
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3# Copyright 2020 The ChromiumOS Authors
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7"""Tests for tiny_render."""
8
9
10import unittest
11
12import tiny_render
13
14
15# Admittedly, the HTML generated by this isn't always _beautiful_ to read
16# (especially with e.g., ordered lists). Since the intent is for the HTML to be
17# shipped alongside the plain-text, the hope is that people won't have to
18# subject themselves to reading the HTML often. :)
19class Test(unittest.TestCase):
20    """Tests for tiny_render."""
21
22    def test_bold(self):
23        pieces = [
24            tiny_render.Bold("hello"),
25            ", ",
26            tiny_render.Bold(["world", "!"]),
27        ]
28
29        self.assertEqual(
30            tiny_render.render_text_pieces(pieces),
31            "**hello**, **world!**",
32        )
33
34        self.assertEqual(
35            tiny_render.render_html_pieces(pieces),
36            "<b>hello</b>, <b>world!</b>",
37        )
38
39    def test_line_break(self):
40        pieces = [
41            "hello",
42            tiny_render.line_break,
43            ["world", "!"],
44        ]
45
46        self.assertEqual(
47            tiny_render.render_text_pieces(pieces),
48            "hello\nworld!",
49        )
50
51        self.assertEqual(
52            tiny_render.render_html_pieces(pieces),
53            "hello<br />\nworld!",
54        )
55
56    def test_linkification(self):
57        pieces = [
58            "hello ",
59            tiny_render.Link(href="https://google.com", inner="world!"),
60        ]
61
62        self.assertEqual(
63            tiny_render.render_text_pieces(pieces),
64            "hello world!",
65        )
66
67        self.assertEqual(
68            tiny_render.render_html_pieces(pieces),
69            'hello <a href="https://google.com">world!</a>',
70        )
71
72    def test_unordered_list(self):
73        pieces = [
74            "hello:",
75            tiny_render.UnorderedList(
76                [
77                    "world",
78                    "w o r l d",
79                ]
80            ),
81        ]
82
83        self.assertEqual(
84            tiny_render.render_text_pieces(pieces),
85            "\n".join(
86                (
87                    "hello:",
88                    "  - world",
89                    "  - w o r l d",
90                )
91            ),
92        )
93
94        self.assertEqual(
95            tiny_render.render_html_pieces(pieces),
96            "\n".join(
97                (
98                    "hello:<ul>",
99                    "<li>world</li>",
100                    "<li>w o r l d</li>",
101                    "</ul>",
102                    "",
103                )
104            ),
105        )
106
107    def test_nested_unordered_list(self):
108        pieces = [
109            "hello:",
110            tiny_render.UnorderedList(
111                [
112                    "world",
113                    ["and more:", tiny_render.UnorderedList(["w o r l d"])],
114                    "world2",
115                ]
116            ),
117        ]
118
119        self.assertEqual(
120            tiny_render.render_text_pieces(pieces),
121            "\n".join(
122                (
123                    "hello:",
124                    "  - world",
125                    "  - and more:",
126                    "    - w o r l d",
127                    "  - world2",
128                )
129            ),
130        )
131
132        self.assertEqual(
133            tiny_render.render_html_pieces(pieces),
134            "\n".join(
135                (
136                    "hello:<ul>",
137                    "<li>world</li>",
138                    "<li>and more:<ul>",
139                    "<li>w o r l d</li>",
140                    "</ul>",
141                    "</li>",
142                    "<li>world2</li>",
143                    "</ul>",
144                    "",
145                )
146            ),
147        )
148
149    def test_switch(self):
150        pieces = ["hello ", tiny_render.Switch(text="text", html="html")]
151        self.assertEqual(tiny_render.render_text_pieces(pieces), "hello text")
152        self.assertEqual(tiny_render.render_html_pieces(pieces), "hello html")
153
154    def test_golden(self):
155        pieces = [
156            "hello",
157            tiny_render.UnorderedList(
158                [
159                    tiny_render.Switch(
160                        text="text", html=tiny_render.Bold("html")
161                    ),
162                    "the",
163                    tiny_render.Bold("sun"),
164                ]
165            ),
166            tiny_render.line_break,
167            ["is", " out!"],
168        ]
169
170        self.assertEqual(
171            tiny_render.render_text_pieces(pieces),
172            "\n".join(
173                (
174                    "hello",
175                    "  - text",
176                    "  - the",
177                    "  - **sun**",
178                    "is out!",
179                )
180            ),
181        )
182
183        self.assertEqual(
184            tiny_render.render_html_pieces(pieces),
185            "\n".join(
186                (
187                    "hello<ul>",
188                    "<li><b>html</b></li>",
189                    "<li>the</li>",
190                    "<li><b>sun</b></li>",
191                    "</ul>",
192                    "<br />",
193                    "is out!",
194                )
195            ),
196        )
197
198
199if __name__ == "__main__":
200    unittest.main()
201