aboutsummaryrefslogtreecommitdiff
path: root/cros_utils/tiny_render_test.py
blob: 114a179665a0898394f30ba93c01f99b73c92595 (plain)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright 2020 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Tests for tiny_render."""

from __future__ import print_function

import unittest

import tiny_render


# Admittedly, the HTML generated by this isn't always _beautiful_ to read
# (especially with e.g., ordered lists). Since the intent is for the HTML to be
# shipped alongside the plain-text, the hope is that people won't have to
# subject themselves to reading the HTML often. :)
class Test(unittest.TestCase):
  """Tests for tiny_render."""

  def test_bold(self):
    pieces = [
        tiny_render.Bold('hello'),
        ', ',
        tiny_render.Bold(['world', '!']),
    ]

    self.assertEqual(
        tiny_render.render_text_pieces(pieces),
        '**hello**, **world!**',
    )

    self.assertEqual(
        tiny_render.render_html_pieces(pieces),
        '<b>hello</b>, <b>world!</b>',
    )

  def test_line_break(self):
    pieces = [
        'hello',
        tiny_render.line_break,
        ['world', '!'],
    ]

    self.assertEqual(
        tiny_render.render_text_pieces(pieces),
        'hello\nworld!',
    )

    self.assertEqual(
        tiny_render.render_html_pieces(pieces),
        'hello<br />\nworld!',
    )

  def test_linkification(self):
    pieces = [
        'hello ',
        tiny_render.Link(href='https://google.com', inner='world!'),
    ]

    self.assertEqual(
        tiny_render.render_text_pieces(pieces),
        'hello world!',
    )

    self.assertEqual(
        tiny_render.render_html_pieces(pieces),
        'hello <a href="https://google.com">world!</a>',
    )

  def test_unordered_list(self):
    pieces = [
        'hello:',
        tiny_render.UnorderedList([
            'world',
            'w o r l d',
        ]),
    ]

    self.assertEqual(
        tiny_render.render_text_pieces(pieces),
        '\n'.join((
            'hello:',
            '  - world',
            '  - w o r l d',
        )),
    )

    self.assertEqual(
        tiny_render.render_html_pieces(pieces),
        '\n'.join((
            'hello:<ul>',
            '<li>world</li>',
            '<li>w o r l d</li>',
            '</ul>',
            '',
        )),
    )

  def test_nested_unordered_list(self):
    pieces = [
        'hello:',
        tiny_render.UnorderedList([
            'world',
            ['and more:', tiny_render.UnorderedList(['w o r l d'])],
            'world2',
        ])
    ]

    self.assertEqual(
        tiny_render.render_text_pieces(pieces),
        '\n'.join((
            'hello:',
            '  - world',
            '  - and more:',
            '    - w o r l d',
            '  - world2',
        )),
    )

    self.assertEqual(
        tiny_render.render_html_pieces(pieces),
        '\n'.join((
            'hello:<ul>',
            '<li>world</li>',
            '<li>and more:<ul>',
            '<li>w o r l d</li>',
            '</ul>',
            '</li>',
            '<li>world2</li>',
            '</ul>',
            '',
        )),
    )

  def test_switch(self):
    pieces = ['hello ', tiny_render.Switch(text='text', html='html')]
    self.assertEqual(tiny_render.render_text_pieces(pieces), 'hello text')
    self.assertEqual(tiny_render.render_html_pieces(pieces), 'hello html')

  def test_golden(self):
    pieces = [
        'hello',
        tiny_render.UnorderedList([
            tiny_render.Switch(text='text', html=tiny_render.Bold('html')),
            'the',
            tiny_render.Bold('sun'),
        ]),
        tiny_render.line_break,
        ['is', ' out!'],
    ]

    self.assertEqual(
        tiny_render.render_text_pieces(pieces), '\n'.join((
            'hello',
            '  - text',
            '  - the',
            '  - **sun**',
            'is out!',
        )))

    self.assertEqual(
        tiny_render.render_html_pieces(pieces), '\n'.join((
            'hello<ul>',
            '<li><b>html</b></li>',
            '<li>the</li>',
            '<li><b>sun</b></li>',
            '</ul>',
            '<br />',
            'is out!',
        )))


if __name__ == '__main__':
  unittest.main()