def test_get_language_name_method(self): """ Test if the ``get_language_name`` return the value set at constructor. """ root_tree_node = RootTreeNode() tree_node = root_tree_node.new_child('test', generate_fixed_code_block_type_cls('foobar'), attrs={}, content='# Hello World!') language_name = tree_node.get_language_name() self.assertEqual('foobar', language_name)
def test_text_rendering_auto_upper(self): """ Test text rendering. """ root_tree_node = RootTreeNode() tree_node = root_tree_node.new_child('not', NotNotationTreeNode) rendered_output = tree_node.render_text('reset') expected_output = '/RESET' self.assertEqual(expected_output, rendered_output)
def test_render_text_with_no_src_link(self): """ Test the ``render_text`` method without a source link. """ root_tree_node = RootTreeNode() tree_node = root_tree_node.new_child('img', ImageTreeNode, attrs={}) output_result = tree_node.render_text('test') expected_result = 'test' self.assertEqual(expected_result, output_result)
def test_render_text_with_default_language(self): """ Test the ``render_text`` method with the default language set. """ root_tree_node = RootTreeNode() tree_node = root_tree_node.new_child('code', CodeBlockTreeNode, attrs={}, content='# Hello World!') output_result = tree_node.render_text('') expected_result = '1. # Hello World!\n' self.assertEqual(expected_result, output_result)
def test_get_figure_id_slugify(self): """ Test the ``get_figure_id`` method call the ``slugify`` function. """ root_tree_node = RootTreeNode() tree_node = root_tree_node.new_child('code', CodeBlockTreeNode, attrs={'id': 'test'}) with unittest.mock.patch('skcode.tags.codeblocks.slugify') as mock_slugify: tree_node.get_figure_id() mock_slugify.assert_called_once_with('test')
def test_render_text_trailing_whitespaces(self): """ Test the ``render_text`` method. """ root_tree_node = RootTreeNode() tree_node = root_tree_node.new_child('task', TodoTaskTreeNode) output_result = tree_node.render_text(' test\ntest2\n ') expected_result = '[ ] test\n test2\n' self.assertEqual(expected_result, output_result)
def test_render_text_is_important(self): """ Test the ``render_text`` method with a "important" flag. """ root_tree_node = RootTreeNode() tree_node = root_tree_node.new_child('notabene', NotaBeneTreeNode, attrs={'important': ''}) output_result = tree_node.render_text('test\ntest2\n') expected_result = 'N.B. test\ntest2\n\n' self.assertEqual(expected_result, output_result)
def test_render_text(self): """ Test the ``render_text`` method. """ root_tree_node = RootTreeNode() tree_node = root_tree_node.new_child('code', CodeBlockTreeNode, attrs={'code': 'python'}, content='# Hello World!') output_result = tree_node.render_text('') expected_result = '1. # Hello World!\n' self.assertEqual(expected_result, output_result)
def test_render_text_trailing_whitespaces(self): """ Test the ``render_text`` method. """ root_tree_node = RootTreeNode() tree_node = root_tree_node.new_child('notabene', NotaBeneTreeNode) output_result = tree_node.render_text(' test\ntest2\n ') expected_result = 'N.B. test\ntest2\n\n' self.assertEqual(expected_result, output_result)
def test_render_text_with_trailing_blank_lines(self): """ Test the ``render_text`` method with trailing blank lines. """ root_tree_node = RootTreeNode() tree_node = root_tree_node.new_child('code', CodeBlockTreeNode, attrs={'code': 'python'}, content='\n\n# Hello World!\n') output_result = tree_node.render_text('') expected_result = '1. # Hello World!\n' self.assertEqual(expected_result, output_result)
def test_render_text(self): """ Test the ``render_text`` method. """ root_tree_node = RootTreeNode() tree_node = root_tree_node.new_child('todolist', TodoListTreeNode) output_result = tree_node.render_text('test') expected_result = '-- TODO LIST --\ntest\n' self.assertEqual(expected_result, output_result)
def test_render_html_with_invalid_color(self): """ Test the ``render_html`` method with an invalid color. """ root_tree_node = RootTreeNode() tree_node = root_tree_node.new_child('color', ColorTextTreeNode, attrs={'color': 'foobar'}) output_result = tree_node.render_html('Hello World!') expected_result = 'Hello World!' self.assertEqual(expected_result, output_result)
def test_render_html(self): """ Test the ``render_html`` method with a valid color. """ root_tree_node = RootTreeNode() tree_node = root_tree_node.new_child('color', ColorTextTreeNode, attrs={'color': '#FFFFFF'}) output_result = tree_node.render_html('Hello World!') expected_result = '<span style="color: #ffffff">Hello World!</span>' self.assertEqual(expected_result, output_result)
def test_get_color_value_method(self): """ Test the ``get_color_value`` method. """ cls = generate_fixed_color_text_cls('customtype') root_tree_node = RootTreeNode() tree_node = root_tree_node.new_child('alert', cls, attrs={}) color_value = tree_node.get_color_value() self.assertEqual('customtype', color_value)
def test_render_text(self): """ Test the ``render_text`` method with a valid color. """ root_tree_node = RootTreeNode() tree_node = root_tree_node.new_child('color', ColorTextTreeNode, attrs={'color': '#FFFFFF'}) output_result = tree_node.render_text('Hello World!') expected_result = 'Hello World!' self.assertEqual(expected_result, output_result)
def test_text_rendering(self): """ Test text rendering. """ root_tree_node = RootTreeNode() tree_node = root_tree_node.new_child('noparse', NoParseTreeNode, content='[u]... some text[/u]') rendered_output = tree_node.render_text('') expected_output = '[u]... some text[/u]' self.assertEqual(expected_output, rendered_output)
def test_text_rendering_with_encoded_html_entities(self): """ Test text rendering with encoded HTML entities. """ root_tree_node = RootTreeNode() tree_node = root_tree_node.new_child('noparse', NoParseTreeNode, content='[u]... <some text>[/u]') rendered_output = tree_node.render_text('') expected_output = '[u]... <some text>[/u]' self.assertEqual(expected_output, rendered_output)
def test_html_rendering(self): """ Test HTML rendering. """ root_tree_node = RootTreeNode() tree_node = root_tree_node.new_child('not', NotNotationTreeNode) rendered_output = tree_node.render_html('RESET') expected_output = '<span style="text-decoration:overline; text-transform: uppercase;">RESET</span>' self.assertEqual(expected_output, rendered_output)
def test_render_text_is_done(self): """ Test the ``render_text`` method with a "done" task. """ root_tree_node = RootTreeNode() tree_node = root_tree_node.new_child('task', TodoTaskTreeNode, attrs={'done': ''}) output_result = tree_node.render_text('test\ntest2\n') expected_result = '[x] test\n test2\n' self.assertEqual(expected_result, output_result)
def test_get_source_link_url(self): """ Test the ``get_source_link_url`` method with the "src" attribute set. """ root_tree_node = RootTreeNode() tree_node = root_tree_node.new_child('code', CodeBlockTreeNode, attrs={'src': 'https://github.com/TamiaLab/PySkCode'}) src_link_url = tree_node.get_source_link_url() self.assertEqual('https://github.com/TamiaLab/PySkCode', src_link_url)
def test_text_rendering_trailing_whitespace(self): """ Test text rendering with trailing whitespaces. """ root_tree_node = RootTreeNode() tree_node = root_tree_node.new_child('not', NotNotationTreeNode) rendered_output = tree_node.render_text(' RESET ') expected_output = ' /RESET ' self.assertEqual(expected_output, rendered_output)
def test_render_html(self): """ Test the ``render_html`` method. """ root_tree_node = RootTreeNode() tree_node = root_tree_node.new_child('task', TodoTaskTreeNode) output_result = tree_node.render_html('test') expected_result = '<li class="task_pending">test</li>\n' self.assertEqual(expected_result, output_result)
def test_render_html_is_done(self): """ Test the ``render_html`` method with a "done" task. """ root_tree_node = RootTreeNode() tree_node = root_tree_node.new_child('task', TodoTaskTreeNode, attrs={'done': ''}) output_result = tree_node.render_html('test') expected_result = '<li class="task_done">test</li>\n' self.assertEqual(expected_result, output_result)
def test_render_text(self): """ Test the ``render_text`` method. """ root_tree_node = RootTreeNode() tree_node = root_tree_node.new_child('youtube', YoutubeTreeNode, content='https://www.youtube.com/watch?v=dQw4w9WgXcQ') output_result = tree_node.render_text('test') expected_result = 'https://youtu.be/dQw4w9WgXcQ' self.assertEqual(expected_result, output_result)
def test_render_text_without_video(self): """ Test the ``render_text`` method without a valid video ID. """ root_tree_node = RootTreeNode() tree_node = root_tree_node.new_child('youtube', YoutubeTreeNode) output_result = tree_node.render_text('test') expected_result = 'test' self.assertEqual(expected_result, output_result)
def test_render_html_with_no_src_link(self): """ Test the ``render_html`` method without a source link. """ root_tree_node = RootTreeNode() tree_node = root_tree_node.new_child('img', ImageTreeNode, attrs={}, content='') output_result = tree_node.render_html('test') expected_result = '<img src="" />' self.assertEqual(expected_result, output_result)
def test_get_youtube_video_id_path_2(self): """ Test the ``get_youtube_video_id`` method with a valid youtube link. """ root_tree_node = RootTreeNode() tree_node = root_tree_node.new_child('youtube', YoutubeTreeNode, content='https://youtu.be/something/dQw4w9WgXcQ') video_id = tree_node.get_youtube_video_id() self.assertEqual('dQw4w9WgXcQ', video_id)
def test_render_text(self): """ Test the ``render_text`` method. """ root_tree_node = RootTreeNode() tree_node = root_tree_node.new_child('img', ImageTreeNode, attrs={}, content='http://example.com/image.jpg') output_result = tree_node.render_text('test') expected_result = 'http://example.com/image.jpg' self.assertEqual(expected_result, output_result)
def test_text_rendering_only_whitespaces(self): """ Test text rendering with no text. """ root_tree_node = RootTreeNode() tree_node = root_tree_node.new_child('not', NotNotationTreeNode) rendered_output = tree_node.render_text(' ') expected_output = '' self.assertEqual(expected_output, rendered_output)
def test_render_html(self): """ Test the ``render_html`` method. """ root_tree_node = RootTreeNode() tree_node = root_tree_node.new_child('todolist', TodoListTreeNode) output_result = tree_node.render_html('test') expected_result = '<ul>test</ul>\n' self.assertEqual(expected_result, output_result)
def test_render_text_with_hl_lines(self): """ Test the ``render_text`` method with 'hl_lines" set. """ root_tree_node = RootTreeNode() tree_node = root_tree_node.new_child( 'code', CodeBlockTreeNode, attrs={ 'code': 'python', 'hl_lines': '1' }, content='# Hello World!\n# Bonjour le monde !') output_result = tree_node.render_text('') expected_result = '1> # Hello World!\n2. # Bonjour le monde !\n' self.assertEqual(expected_result, output_result)
def test_get_source_link_url_sanitize_with_relative_url_conversion(self): """ Test the ``get_source_link_url`` method call the ``sanitize_url`` function. """ root_tree_node = RootTreeNode() setup_relative_urls_conversion(root_tree_node, 'http://example.com/') tree_node = root_tree_node.new_child( 'code', CodeBlockTreeNode, attrs={'src': 'https://github.com/TamiaLab/PySkCode'}) with unittest.mock.patch( 'skcode.tags.codeblocks.sanitize_url') as mock_sanitize_url: tree_node.get_source_link_url() mock_sanitize_url.assert_called_once_with( 'https://github.com/TamiaLab/PySkCode', absolute_base_url='http://example.com/')
def test_render_html_with_default_language(self): """ Test the ``render_html`` method with the default language set. """ root_tree_node = RootTreeNode() tree_node = root_tree_node.new_child('code', CodeBlockTreeNode, attrs={}, content='# Hello World!') output_result = tree_node.render_html('') expected_result = """<div class="codetable"> <table class="highlighttable"><tr><td><div class="linenodiv" style="background-color: #f0f0f0; padding-right: 10px"><pre style="line-height: 125%">1</pre></div></td><td class="code"><div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"># Hello World! </pre></div> </td></tr></table> </div>""" self.assertEqual(expected_result, output_result)
def test_pre_process_node(self): """ Test the ``pre_process_node`` method. """ root_tree_node = RootTreeNode() tree_node = root_tree_node.new_child('footnote', FootnoteDeclarationTreeNode) tree_node.pre_process_node() self.assertEqual('', tree_node.error_message) self.assertEqual({'footnote-1'}, root_tree_node.known_ids) tree_node = root_tree_node.new_child('footnote', FootnoteDeclarationTreeNode, attrs={'id': 'footnote-1'}) tree_node.pre_process_node() self.assertEqual('ID already used previously', tree_node.error_message) self.assertEqual({'footnote-1'}, root_tree_node.known_ids)
def test_default_render_error_html_implementation_content(self): """ Test the default ``render_error_html`` method implementation. """ root_tree_node = RootTreeNode() tree_node = root_tree_node.new_child('child', DummyTreeNode, error_message='msg', source_open_tag='[test]', source_close_tag='[/test]', content='content') expected_html = '<error="msg">[test]</error>\ncontent\n<error="msg">[/test]</error>' self.assertEqual( expected_html, tree_node.render_error_html( '', '<error="{error_message}">{source}</error>'))
def test_default_render_error_html_implementation_xss(self): """ Test the default ``render_error_html`` method implementation. """ root_tree_node = RootTreeNode() tree_node = root_tree_node.new_child('child', DummyTreeNode, error_message='msg', source_open_tag='<test>', source_close_tag='</test>', content='') expected_html = '<error="msg"><test></error>\ninner HTML\n<error="msg"></test></error>' self.assertEqual( expected_html, tree_node.render_error_html( 'inner HTML', '<error="{error_message}">{source}</error>'))
def test_render_text_with_linenostart(self): """ Test the ``render_text`` method with 'linenostart" set. """ root_tree_node = RootTreeNode() tree_node = root_tree_node.new_child('code', CodeBlockTreeNode, attrs={ 'code': 'python', 'hl_lines': '1', 'linenostart': '5' }, content='# Hello World!') output_result = tree_node.render_text('') expected_result = '5. # Hello World!\n' self.assertEqual(expected_result, output_result)
def test_assertions_constructor(self): """ Test assertions at constructor """ root_tree_node = RootTreeNode() tree_node = root_tree_node.new_child('test', DummyTreeNode) with self.assertRaises(AssertionError) as e: TreeNode(None, tree_node, 'test') self.assertEqual('The root tree node instance is mandatory.', str(e.exception)) with self.assertRaises(AssertionError) as e: TreeNode(root_tree_node, None, 'test') self.assertEqual( 'The parent node instance is mandatory for non-root nodes.', str(e.exception))
def test_render_html(self): """ Test the ``render_html`` method. """ root_tree_node = RootTreeNode() tree_node = root_tree_node.new_child('code', CodeBlockTreeNode, attrs={'code': 'python'}, content='# Hello World!') output_result = tree_node.render_html('') expected_result = """<div class="codetable"> <table class="highlighttable"><tr><td><div class="linenodiv" style="background-color: #f0f0f0; padding-right: 10px"><pre style="line-height: 125%">1</pre></div></td><td class="code"><div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span style="color: #408080; font-style: italic"># Hello World!</span> </pre></div> </td></tr></table> </div>""" self.assertEqual(expected_result, output_result)
def test_render_text_with_src_filename_and_html_entities(self): """ Test the ``render_text`` method with 'filename" set containing HTML entities. """ root_tree_node = RootTreeNode() tree_node = root_tree_node.new_child('code', CodeBlockTreeNode, attrs={ 'code': 'python', 'hl_lines': '1', 'linenostart': '5', 'filename': '<test>.py' }, content='# Hello World!') output_result = tree_node.render_text('') expected_result = '5. # Hello World!\nSource : <test>.py\n' self.assertEqual(expected_result, output_result)
def test_get_figure_id_from_counter_increment(self): """ Test the ``get_figure_id_from_counter`` method by calling it multiple time with different figures. """ root_tree_node = RootTreeNode() tree_node = root_tree_node.new_child('figure', FigureDeclarationTreeNode) self.assertEqual('figure-1', tree_node.get_figure_id_from_counter()) self.assertEqual('figure-1', tree_node.get_figure_id_from_counter()) tree_node_2 = root_tree_node.new_child('figure', FigureDeclarationTreeNode) self.assertEqual('figure-2', tree_node_2.get_figure_id_from_counter()) self.assertEqual('figure-2', tree_node_2.get_figure_id_from_counter()) tree_node_3 = root_tree_node.new_child('figure', FigureDeclarationTreeNode) self.assertEqual('figure-3', tree_node_3.get_figure_id_from_counter()) self.assertEqual('figure-3', tree_node_3.get_figure_id_from_counter())
def test_render_html_with_src_date(self): """ Test the ``render_html`` method with a source date. """ root_tree_node = RootTreeNode() tree_node = root_tree_node.new_child('quote', QuoteTreeNode, attrs={ 'author': 'John Doe', 'link': 'http://example.com/', 'date': '1356048000' }) output_result = tree_node.render_html('Hello World!') expected_result = '<blockquote>Hello World!\n<footer><a href="http://example.com/" ' \ 'rel="nofollow"><cite>John Doe</cite></a> - <time datetime="2012-12-21T00:00:00">' \ '21/12/2012 00:00:00</time></footer></blockquote>\n' self.assertEqual(expected_result, output_result)
def test_render_text_with_src_link_only(self): """ Test the ``render_text`` method with 'src" set. """ root_tree_node = RootTreeNode() tree_node = root_tree_node.new_child( 'code', CodeBlockTreeNode, attrs={ 'code': 'python', 'hl_lines': '1', 'linenostart': '5', 'src': 'https://github.com/TamiaLab/PySkCode' }, content='# Hello World!') output_result = tree_node.render_text('') expected_result = '5. # Hello World!\nSource : https://github.com/TamiaLab/PySkCode\n' self.assertEqual(expected_result, output_result)
def test_setup_smileys_replacement(self): """ Test the ``setup_smileys_replacement`` helper. """ document_tree = RootTreeNode() self.assertNotIn(EMOTICONS_MAP_ATTR_NAME, document_tree.attrs) self.assertNotIn(EMOTICONS_REGEX_ATTR_NAME, document_tree.attrs) self.assertNotIn(EMOTICONS_BASE_URL_ATTR_NAME, document_tree.attrs) self.assertNotIn(EMOTICONS_HTML_CLASS_ATTR_NAME, document_tree.attrs) setup_smileys_replacement(document_tree, 'http://example.com/', ( (':)', 'smile.png'), (':<test?:', 'test.png'), ), 'custom_css') self.assertIn(EMOTICONS_MAP_ATTR_NAME, document_tree.attrs) self.assertEqual({ ':)': 'smile.png', ':<test?:': 'test.png' }, document_tree.attrs[EMOTICONS_MAP_ATTR_NAME]) self.assertIn(EMOTICONS_REGEX_ATTR_NAME, document_tree.attrs) self.assertEqual( r"re.compile('(^|\\s+)(?P<emoticon>\\:\\)|\\:\\<\\;test\\?\\:)(\\s+|$)')", str(document_tree.attrs[EMOTICONS_REGEX_ATTR_NAME])) self.assertIn(EMOTICONS_BASE_URL_ATTR_NAME, document_tree.attrs) self.assertEqual( 'http://example.com/test.png', document_tree.attrs[EMOTICONS_BASE_URL_ATTR_NAME]('test.png')) self.assertIn(EMOTICONS_HTML_CLASS_ATTR_NAME, document_tree.attrs) self.assertEqual('custom_css', document_tree.attrs[EMOTICONS_HTML_CLASS_ATTR_NAME])
def test_setup_smileys_replacement_trailing_slash_base_url(self): """ Test the ``setup_smileys_replacement`` helper. """ document_tree = RootTreeNode() setup_smileys_replacement(document_tree, 'http://example.com') self.assertEqual( 'http://example.com/test.png', document_tree.attrs[EMOTICONS_BASE_URL_ATTR_NAME]('test.png'))
def test_html_rendering_with_trailing_newline(self): """ Test HTML rendering with trailing newline. """ root_tree_node = RootTreeNode() for alert_type in AlertBoxTreeNode.accepted_types: tree_node = root_tree_node.new_child('alert', AlertBoxTreeNode, attrs={ 'title': 'Test', 'type': alert_type }) rendered_output = tree_node.render_html('\n\nHello world!\n') expected_output = AlertBoxTreeNode.html_template[ alert_type].format(type=alert_type, title='Test', inner_html='Hello world!') self.assertEqual(expected_output, rendered_output)
def test_html_rendering_with_html_entities_in_title(self): """ Test HTML rendering with HTML entities in title. """ root_tree_node = RootTreeNode() for alert_type in AlertBoxTreeNode.accepted_types: tree_node = root_tree_node.new_child('alert', AlertBoxTreeNode, attrs={ 'title': '<Test>', 'type': alert_type }) rendered_output = tree_node.render_html('Hello world!') expected_output = AlertBoxTreeNode.html_template[ alert_type].format(type=alert_type, title='<Test>', inner_html='Hello world!') self.assertEqual(expected_output, rendered_output)
def test_text_rendering_without_title_but_trailing_newline(self): """ Test text rendering without title. """ root_tree_node = RootTreeNode() for alert_type in AlertBoxTreeNode.accepted_types: tree_node = root_tree_node.new_child('alert', AlertBoxTreeNode, attrs={'type': alert_type}) rendered_output = tree_node.render_text( '\nHello world!\nBonjour le monde !\n\n') expected_output = """*** {title_line} * Hello world! * Bonjour le monde ! *** """.format(title_line=AlertBoxTreeNode.text_title_line_template[alert_type]. format(title='')) self.assertEqual(expected_output, rendered_output)
def test_setup_smileys_replacement_callable_base_url(self): """ Test the ``setup_smileys_replacement`` helper. """ document_tree = RootTreeNode() setup_smileys_replacement(document_tree, lambda x: 'http://example.com/lambda/' + x) self.assertEqual( 'http://example.com/lambda/test.png', document_tree.attrs[EMOTICONS_BASE_URL_ATTR_NAME]('test.png'))
def test_setup_relative_urls_conversion(self): """ Test the ``setup_relative_urls_conversion`` helper. """ document_tree = RootTreeNode() self.assertNotIn(RELATIVE_URL_BASE_ATTR_NAME, document_tree.attrs) setup_relative_urls_conversion(document_tree, 'http://example.com/') self.assertIn(RELATIVE_URL_BASE_ATTR_NAME, document_tree.attrs) self.assertEqual('http://example.com/', document_tree.attrs[RELATIVE_URL_BASE_ATTR_NAME])
def test_get_attribute_value_second_try(self): """ Test the ``get_attribute_value`` helper method """ document_tree = RootTreeNode() tree_node = document_tree.new_child('node', DummyTreeNode, attrs={ 'at1': '', 'at2': 'bar', 'at3': 'baz' }) self.assertEqual('bar', tree_node.get_attribute_value('at1', 'at2', 'at3')) tree_node = document_tree.new_child('node', DummyTreeNode, attrs={'at3': 'baz'}) self.assertEqual('baz', tree_node.get_attribute_value('at1', 'at2', 'at3'))
def test_setup_cosmetics_replacement(self): """ Test the ``setup_cosmetics_replacement`` helper. """ document_tree = RootTreeNode() self.assertNotIn(COSMETICS_MAP_ATTR_NAME, document_tree.attrs) setup_cosmetics_replacement(document_tree, DEFAULT_COSMETICS_MAP) self.assertIn(COSMETICS_MAP_ATTR_NAME, document_tree.attrs) self.assertEqual(DEFAULT_COSMETICS_MAP, document_tree.attrs[COSMETICS_MAP_ATTR_NAME])
def test_render_to_text(self): """ Test the ``render_to_text`` function """ root_tree_node = RootTreeNode() tree_node_l1 = root_tree_node.new_child('level1', get_test_node('level1-1')) root_tree_node.new_child('level1', get_test_node('level1-2')) tree_node_l2 = tree_node_l1.new_child('level2', get_test_node('level2-1')) tree_node_l1.new_child('level2', get_test_node('level2-2')) root_tree_node.new_child('level1', get_test_node('level1-3')) tree_node_l2.new_child('level3', get_test_node('level3-1')) tree_node_l2.new_child('level3', get_test_node('level3-2')) tree_node_l1.new_child('level2', get_test_node('level2-3')) output = render_to_text(root_tree_node, some_custom_kwarg='foobar') expected_output = '[TEXT+level1-1]' \ '[TEXT+level2-1]' \ '[TEXT+level3-1][/TEXT]' \ '[TEXT+level3-2][/TEXT]' \ '[/TEXT]' \ '[TEXT+level2-2][/TEXT]' \ '[TEXT+level2-3][/TEXT]' \ '[/TEXT]' \ '[TEXT+level1-2][/TEXT]' \ '[TEXT+level1-3][/TEXT]' self.assertEqual(expected_output, output)
def test_render_to_text_with_error(self): """ Test the ``render_to_text`` function with erroneous node """ root_tree_node = RootTreeNode() tree_node_l1 = root_tree_node.new_child('level1', get_test_node('level1-1')) root_tree_node.new_child('level1', get_test_node('level1-2')) tree_node_l2 = tree_node_l1.new_child('level2', get_test_node('level2-1')) tree_node_l1.new_child('level2', get_test_node('level2-2')) root_tree_node.new_child('level1', get_test_node('level1-3')) tree_node_l2.new_child('level3', get_test_node('level3-1')) tree_node_l2.new_child('level3', get_test_node('level3-2'), source_open_tag='[test]', source_close_tag='[/test]', error_message='foo') tree_node_l1.new_child('level2', get_test_node('level2-3')) output = render_to_text(root_tree_node, some_custom_kwarg='foobar') expected_output = '[TEXT+level1-1]' \ '[TEXT+level2-1]' \ '[TEXT+level3-1][/TEXT]' \ '[test][/test]' \ '[/TEXT]' \ '[TEXT+level2-2][/TEXT]' \ '[TEXT+level2-3][/TEXT]' \ '[/TEXT]' \ '[TEXT+level1-2][/TEXT]' \ '[TEXT+level1-3][/TEXT]' self.assertEqual(expected_output, output)
def test_sanitize_node_erroneous_line_numbers(self): """ Test the ``sanitize_node`` method with erroneous line numbers. """ root_tree_node = RootTreeNode() tree_node = root_tree_node.new_child('code', CodeBlockTreeNode, attrs={}) tree_node.sanitize_node([]) self.assertEqual('', tree_node.error_message) tree_node = root_tree_node.new_child('code', CodeBlockTreeNode, attrs={'hl_lines': 'abc'}) tree_node.sanitize_node([]) self.assertEqual('abc is not a number', tree_node.error_message) tree_node = root_tree_node.new_child('code', CodeBlockTreeNode, attrs={'linenostart': 'abc'}) tree_node.sanitize_node([]) self.assertEqual('abc is not a number', tree_node.error_message)
def test_sanitize_node(self): """ Test the ``sanitize_node`` method. """ root_tree_node = RootTreeNode() tree_node = root_tree_node.new_child('list', ListTreeNode, attrs={'start': '3'}) tree_node.sanitize_node([]) self.assertEqual('', tree_node.error_message) tree_node = root_tree_node.new_child('list', ListTreeNode, attrs={'start': '-3'}) tree_node.sanitize_node([]) self.assertEqual('First line number must be positive', tree_node.error_message) tree_node = root_tree_node.new_child('list', ListTreeNode, attrs={'start': 'abcd'}) tree_node.sanitize_node([]) self.assertEqual('abcd is not a number', tree_node.error_message)
def test_render_html_with_only_figure_id(self): """ Test the ``render_html`` method with 'src" set. """ root_tree_node = RootTreeNode() tree_node = root_tree_node.new_child('code', CodeBlockTreeNode, attrs={ 'code': 'python', 'hl_lines': '1', 'linenostart': '5', 'id': 'helloworld' }, content='# Hello World!') output_result = tree_node.render_html('') expected_result = """<a id="helloworld"></a>\n<div class="codetable"> <table class="highlighttable"><tr><td><div class="linenodiv" style="background-color: #f0f0f0; padding-right: 10px"><pre style="line-height: 125%"><a href="#helloworld-5">5</a></pre></div></td><td class="code"><div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><a name="helloworld-5"></a><span style="background-color: #ffffcc"><span style="color: #408080; font-style: italic"># Hello World!</span> </span></pre></div> </td></tr></table> </div>""" self.assertEqual(expected_result, output_result)
def test_text_rendering_with_html_entities_in_title(self): """ Test text rendering with HTML entities in title. """ root_tree_node = RootTreeNode() for alert_type in AlertBoxTreeNode.accepted_types: tree_node = root_tree_node.new_child('alert', AlertBoxTreeNode, attrs={ 'title': '<Test>', 'type': alert_type }) rendered_output = tree_node.render_text( 'Hello world!\nBonjour le monde !') expected_output = """*** {title_line} * Hello world! * Bonjour le monde ! *** """.format(title_line=AlertBoxTreeNode.text_title_line_template[alert_type]. format(title='<Test>')) self.assertEqual(expected_output, rendered_output)
def test_text_rendering_newlines(self): """ Test text rendering with various newline ending in content. """ root_tree_node = RootTreeNode() for alert_type in AlertBoxTreeNode.accepted_types: tree_node = root_tree_node.new_child('alert', AlertBoxTreeNode, attrs={ 'title': 'Test', 'type': alert_type }) rendered_output = tree_node.render_text( 'Hello world!\nBonjour le monde !\r\nYolo.') expected_output = """*** {title_line} * Hello world! * Bonjour le monde ! * Yolo. *** """.format(title_line=AlertBoxTreeNode.text_title_line_template[alert_type]. format(title='Test')) self.assertEqual(expected_output, rendered_output)
def test_sanitize_node(self): """ Test if the ``sanitize_node`` method mark the node as erroneous when title is missing """ root_tree_node = RootTreeNode() tree_node = root_tree_node.new_child('fnref', FootnoteReferenceTreeNode, content='') tree_node.sanitize_node([]) self.assertEqual('Missing footnote ID', tree_node.error_message) self.assertEqual(set(), root_tree_node.known_ids) tree_node = root_tree_node.new_child('fnref', FootnoteReferenceTreeNode, content='test') tree_node.sanitize_node([]) self.assertEqual('Unknown footnote ID', tree_node.error_message) root_tree_node.known_ids.add('test') tree_node = root_tree_node.new_child('fnref', FootnoteReferenceTreeNode, content='test') tree_node.sanitize_node([]) self.assertEqual('', tree_node.error_message)