diff options
Diffstat (limited to 'cros_utils/tiny_render_test.py')
-rwxr-xr-x | cros_utils/tiny_render_test.py | 177 |
1 files changed, 177 insertions, 0 deletions
diff --git a/cros_utils/tiny_render_test.py b/cros_utils/tiny_render_test.py new file mode 100755 index 00000000..114a1796 --- /dev/null +++ b/cros_utils/tiny_render_test.py @@ -0,0 +1,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() |