def test_render_to_html_with_error(self): """ Test the ``render_to_html`` function with erroneous node """ root_tree_node = RootTreeNode() tree_node_l1 = root_tree_node.new_child('level1', get_test_node('level1-1'), source_open_tag='[level1-1]', source_close_tag='[/level1-1]', error_message='foo') tree_node_l1.error_message = 'foo' 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_l3 = tree_node_l2.new_child('level3', get_test_node('level3-2'), source_open_tag='[level3-2]', source_close_tag='[/level3-2]', error_message='bar') tree_node_l3.error_message = 'bar' tree_node_l1.new_child('level2', get_test_node('level2-3')) output = render_to_html(root_tree_node, html_error_template='<error={error_message}>{source}</error>', some_custom_kwarg='foobar') expected_output = '<error=foo>[level1-1]</error>\n' \ '[HTML+level2-1]' \ '[HTML+level3-1][/HTML]' \ '<error=bar>[level3-2]</error>\n<error=bar>[/level3-2]</error>' \ '[/HTML]' \ '[HTML+level2-2][/HTML]' \ '[HTML+level2-3][/HTML]\n' \ '<error=foo>[/level1-1]</error>' \ '[HTML+level1-2][/HTML]' \ '[HTML+level1-3][/HTML]' self.assertEqual(expected_output, output)
def test_render_to_html_with_error_suppressed(self): """ Test the ``render_to_html`` function with erroneous node (error suppressed) """ root_tree_node = RootTreeNode() tree_node_l1 = root_tree_node.new_child('level1', get_test_node('level1-1'), source_open_tag='[level1-1]', source_close_tag='[/level1-1]', error_message='foo') tree_node_l1.error_message = 'foo' 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_l3 = tree_node_l2.new_child('level3', get_test_node('level3-2'), source_open_tag='[level3-2]', source_close_tag='[/level3-2]', error_message='bar') tree_node_l3.error_message = 'bar' tree_node_l1.new_child('level2', get_test_node('level2-3')) output = render_to_html(root_tree_node, html_error_template=SUPPRESS_ERROR_HTML_TEMPLATE, some_custom_kwarg='foobar') expected_output = '<!-- foo --> [level1-1]\n' \ '[HTML+level2-1]' \ '[HTML+level3-1][/HTML]' \ '<!-- bar --> [level3-2]\n<!-- bar --> [/level3-2]' \ '[/HTML]' \ '[HTML+level2-2][/HTML]' \ '[HTML+level2-3][/HTML]\n' \ '<!-- foo --> [/level1-1]' \ '[HTML+level1-2][/HTML]' \ '[HTML+level1-3][/HTML]' self.assertEqual(expected_output, output)
def test_debug_print_ast_with_erroneous_parent(self): """ Test if the ``debug_print_ast`` work as expected when an erroneous aprent is found. """ # RootTreeNode # - level 1 child 1 # - level 2 child 1 # - level 3 child 1 # - level 1 child 2 root_tree_node = RootTreeNode() l1_tree_node = root_tree_node.new_child('level_1_child_1', DummyTreeNode, attrs={'foo': 'bar'}) root_tree_node.new_child('level_1_child_2', DummyTreeNode) l2_tree_node = l1_tree_node.new_child('level_2_child_1', DummyTreeNode) l3_tree_node = l2_tree_node.new_child('level_3_child_1', DummyTreeNode, content='Hello world!') # Add some oops l3_tree_node.parent = root_tree_node output_returned = [] def _print_to_str(*args): output_returned.append(' '.join(args)) debug_print_ast(root_tree_node, print_fnct=_print_to_str) expected_output = [ "RootTreeNode(name=\"None\", attrs={}, content='', len(children)=2) ", " DummyTreeNode(name=\"level_1_child_1\", attrs={'foo': 'bar'}, content='', len(children)=1) ", " DummyTreeNode(name=\"level_2_child_1\", attrs={}, content='', len(children)=1) ", " DummyTreeNode(name=\"level_3_child_1\", attrs={}, content='Hello world!', len(children)=0) !! Parent mismatch !!", " DummyTreeNode(name=\"level_1_child_2\", attrs={}, content='', len(children)=0) " ] self.maxDiff = None self.assertEqual(expected_output, output_returned)
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_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('email', EmailLinkTreeNode, attrs={}) tree_node.sanitize_node([]) self.assertEqual('Missing or erroneous target email address', tree_node.error_message) tree_node = root_tree_node.new_child('email', EmailLinkTreeNode, attrs={'email': '*****@*****.**'}) tree_node.sanitize_node([]) self.assertEqual('', tree_node.error_message)
def test_sanitize_node_erroneous_language_name(self): """ Test the ``sanitize_node`` method with erroneous language name. """ 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={'code': 'foobar'}) tree_node.sanitize_node([]) self.assertEqual('Unknown language', tree_node.error_message)
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('color', ColorTextTreeNode, attrs={'color': 'blue'}) tree_node.sanitize_node([]) self.assertEqual('', tree_node.error_message) tree_node = root_tree_node.new_child('color', ColorTextTreeNode, attrs={'color': ''}) tree_node.sanitize_node([]) self.assertEqual('Missing color value', tree_node.error_message)
def test_has_errors_true(self): """ Test the ``has_errors`` helper. """ document_tree = RootTreeNode() document_tree.new_child('node', DummyTreeNode) l1_tree_node = document_tree.new_child('test_l1', OtherDummyTreeNode) l1_tree_node.new_child('node', DummyTreeNode) l2_tree_node = l1_tree_node.new_child('test_l2', OtherDummyTreeNode, error_message='foobar') l2_tree_node.new_child('node', DummyTreeNode) self.assertTrue(document_tree.has_errors())
def test_has_errors_false(self): """ Test the ``has_errors`` helper. """ document_tree = RootTreeNode() document_tree.new_child('node', DummyTreeNode) l1_tree_node = document_tree.new_child('test_l1', OtherDummyTreeNode) l1_tree_node.new_child('node', DummyTreeNode) l2_tree_node = l1_tree_node.new_child('test_l2', OtherDummyTreeNode) l2_tree_node.new_child('node', DummyTreeNode) self.assertFalse(document_tree.has_errors())
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('url', UrlLinkTreeNode, attrs={}) tree_node.sanitize_node([]) self.assertEqual('Missing or erroneous target URL', tree_node.error_message) tree_node = root_tree_node.new_child('url', UrlLinkTreeNode, attrs={'url': 'http://example.com'}) tree_node.sanitize_node([]) self.assertEqual('', tree_node.error_message)
def test_search_in_tree(self): """ Test the ``search_in_tree`` helper. """ document_tree = RootTreeNode() node1 = document_tree.new_child('node', DummyTreeNode) l1_tree_node = document_tree.new_child('test_l1', OtherDummyTreeNode) node2 = l1_tree_node.new_child('node', DummyTreeNode) l2_tree_node = l1_tree_node.new_child('test_l2', OtherDummyTreeNode) node3 = l2_tree_node.new_child('node', DummyTreeNode) nodes = list(document_tree.search_in_tree(DummyTreeNode)) self.assertEqual([node1, node2, node3], nodes)
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('youtube', YoutubeTreeNode, content='https://www.youtube.com/watch?v=dQw4w9WgXcQ') tree_node.sanitize_node([]) self.assertEqual('', tree_node.error_message) tree_node = root_tree_node.new_child('youtube', YoutubeTreeNode, content='') tree_node.sanitize_node([]) self.assertEqual('Missing or erroneous video URL', tree_node.error_message)
def test_make_paragraphs_erroneous_nodes(self): """ Test the ``make_paragraphs`` paragraph utility. """ root_tree_node = RootTreeNode() a = root_tree_node.new_child(None, TextTreeNode, content='Text 1') b = root_tree_node.new_child(None, TextTreeNode, content='', error_message='Foobar') c = root_tree_node.new_child(None, NewlineTreeNode) make_paragraphs(root_tree_node) self.assertEqual(len(root_tree_node.children), 1) self.assertIsInstance(root_tree_node.children[0], ParagraphTreeNode) self.assertEqual([a, b, c], root_tree_node.children[0].children)
def test_make_paragraphs_trailing_newline(self): """ Test the ``make_paragraphs`` paragraph utility. """ root_tree_node = RootTreeNode() z = root_tree_node.new_child(None, NewlineTreeNode) a = root_tree_node.new_child(None, TextTreeNode, content='Text 1') b = root_tree_node.new_child(None, NewlineTreeNode) make_paragraphs(root_tree_node) self.assertEqual(len(root_tree_node.children), 2) self.assertEqual(root_tree_node.children[0], z) self.assertIsInstance(root_tree_node.children[1], ParagraphTreeNode) self.assertEqual([a, b], root_tree_node.children[1].children)
def test_pre_process_node(self): """ Test the ``pre_process_node`` method. """ root_tree_node = RootTreeNode() tree_node = root_tree_node.new_child('figure', FigureDeclarationTreeNode) tree_node.pre_process_node() self.assertEqual('', tree_node.error_message) self.assertEqual({'figure-1'}, root_tree_node.known_ids) tree_node = root_tree_node.new_child('figure', FigureDeclarationTreeNode, attrs={'id': 'figure-1'}) tree_node.pre_process_node() self.assertEqual('ID already used previously', tree_node.error_message) self.assertEqual({'figure-1'}, root_tree_node.known_ids)
def test_make_paragraphs_custom_class(self): """ Test the ``make_paragraphs`` paragraph utility. """ root_tree_node = RootTreeNode() a = root_tree_node.new_child(None, TextTreeNode, content='Text 1') b = root_tree_node.new_child(None, NewlineTreeNode) c = root_tree_node.new_child(None, TextTreeNode, content='Text 1') d = root_tree_node.new_child(None, NewlineTreeNode) make_paragraphs(root_tree_node, paragraph_node_cls=CustomParagraphTreeNode) self.assertEqual(len(root_tree_node.children), 1) self.assertIsInstance(root_tree_node.children[0], CustomParagraphTreeNode) self.assertEqual([a, b, c, d], root_tree_node.children[0].children)
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_sanitize_node_negative_line_numbers(self): """ Test the ``sanitize_node`` method with negative 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': '-1'}) tree_node.sanitize_node([]) self.assertEqual('Line number must be positive', tree_node.error_message) tree_node = root_tree_node.new_child('code', CodeBlockTreeNode, attrs={'linenostart': '-1'}) tree_node.sanitize_node([]) self.assertEqual('Line number must be positive', tree_node.error_message)
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_get_raw_content_method_no_recursion(self): """ Test if the ``get_raw_content`` method work as expected. """ # RootTreeNode # - level 1 child 1 # - level 2 child # - level 3 child # - level 1 child 2 root_tree_node = RootTreeNode() l1_tree_node = root_tree_node.new_child('level 1 child 1', DummyTreeNode, content='Level 1-1') root_tree_node.new_child('level 1 child 2', DummyTreeNode, content='Level 1-2') l2_tree_node = l1_tree_node.new_child('level 2 child', DummyTreeNode, content='Level 2') l2_tree_node.new_child('level 3 child', DummyTreeNode, content='Level 3') raw_content = l1_tree_node.get_raw_content(recursive=False) self.assertEqual('Level 1-1', raw_content)
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('goto', GoToAnchorTreeNode, attrs={'id': ''}) tree_node.sanitize_node([]) self.assertEqual('Missing anchor ID', tree_node.error_message) self.assertEqual(set(), root_tree_node.known_ids) tree_node = root_tree_node.new_child('goto', GoToAnchorTreeNode, attrs={'id': 'test'}) tree_node.sanitize_node([]) self.assertEqual('Unknown anchor ID', tree_node.error_message) root_tree_node.known_ids.add('test') tree_node = root_tree_node.new_child('goto', GoToAnchorTreeNode, attrs={'id': 'test'}) tree_node.sanitize_node([]) self.assertEqual('', tree_node.error_message)
def test_pre_process_node(self): """ Test if the ``pre_process_node`` method mark the node as erroneous when anchor ID is missing """ root_tree_node = RootTreeNode() tree_node = root_tree_node.new_child('anchor', AnchorTreeNode, content='') tree_node.pre_process_node() self.assertEqual('Missing anchor ID', tree_node.error_message) tree_node = root_tree_node.new_child('anchor', AnchorTreeNode, content='test') tree_node.pre_process_node() self.assertEqual('', tree_node.error_message) self.assertEqual({'test'}, root_tree_node.known_ids) tree_node = root_tree_node.new_child('anchor', AnchorTreeNode, content='test') tree_node.pre_process_node() self.assertEqual('ID already used previously', tree_node.error_message) self.assertEqual({'test'}, root_tree_node.known_ids)
def test_make_paragraphs_mixed_inline_block(self): """ Test the ``make_paragraphs`` paragraph utility. """ root_tree_node = RootTreeNode() a = root_tree_node.new_child(None, TextTreeNode, content='Text 1') b = root_tree_node.new_child(None, NewlineTreeNode) z = root_tree_node.new_child('block', CustomBlockTreeNode) c = root_tree_node.new_child(None, TextTreeNode, content='Text 1') d = root_tree_node.new_child(None, NewlineTreeNode) make_paragraphs(root_tree_node) self.assertEqual(len(root_tree_node.children), 3) self.assertIsInstance(root_tree_node.children[0], ParagraphTreeNode) self.assertEqual([a, b], root_tree_node.children[0].children) self.assertEqual(root_tree_node.children[1], z) self.assertIsInstance(root_tree_node.children[2], ParagraphTreeNode) self.assertEqual([c, d], root_tree_node.children[2].children)
def test_render_inner_html(self): """ Test the ``render_inner_html`` 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_inner_html(tree_node_l2, some_custom_kwarg='foobar') expected_output = '[HTML+level3-1][/HTML]' \ '[HTML+level3-2][/HTML]' self.assertEqual(expected_output, output)
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_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_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_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_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_get_alert_type_uppercase(self): """ Test the ``get_alert_type`` method with a valid alert type but in uppercase. """ root_tree_node = RootTreeNode() tree_node = root_tree_node.new_child('alert', AlertBoxTreeNode, attrs={'type': 'ERRor'}) alert_type = tree_node.get_alert_type() self.assertEqual('error', alert_type)
def test_text_rendering_with_extra_whitespaces(self): """ Test text rendering with extra whitespaces around the inner text. """ root_tree_node = RootTreeNode() tree_node = root_tree_node.new_child( 'abbr', AcronymTreeNode, attrs={'title': 'As Soon As Possible'}) rendered_output = tree_node.render_text(' ASAP ') expected_output = ' ASAP (As Soon As Possible) ' self.assertEqual(expected_output, rendered_output)
def test_text_rendering(self): """ Test text rendering. """ root_tree_node = RootTreeNode() tree_node = root_tree_node.new_child( 'abbr', AcronymTreeNode, attrs={'title': 'As Soon As Possible'}) rendered_output = tree_node.render_text('ASAP') expected_output = 'ASAP (As Soon As Possible)' self.assertEqual(expected_output, rendered_output)
def test_get_alert_type_with_invalid_type(self): """ Test the ``get_alert_type`` method with an invalid alert type. """ root_tree_node = RootTreeNode() tree_node = root_tree_node.new_child('alert', AlertBoxTreeNode, attrs={'type': 'notatype'}) alert_type = tree_node.get_alert_type() self.assertEqual(tree_node.default_type, alert_type)
def test_html_rendering_custom_css(self): """ Test HTML rendering. """ root_tree_node = RootTreeNode() tree_node = root_tree_node.new_child('figcaption', CustomFigureCaptionTreeNode) rendered_output = tree_node.render_html('test') expected_output = '<figcaption class="custom_css">test</figcaption>\n' self.assertEqual(expected_output, rendered_output)
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_render_text_with_alt_text(self): """ Test the ``render_text`` method with an alternative text. """ root_tree_node = RootTreeNode() tree_node = root_tree_node.new_child('img', ImageTreeNode, attrs={'alt': 'Example image.'}, content='http://example.com/image.jpg') output_result = tree_node.render_text('test') expected_result = 'http://example.com/image.jpg (Example image.)' self.assertEqual(expected_result, output_result)
def test_render_html_with_img_width(self): """ Test the ``render_html`` method with an image width. """ root_tree_node = RootTreeNode() tree_node = root_tree_node.new_child('img', ImageTreeNode, attrs={'alt': 'Example image.', 'width': '120'}, content='http://example.com/image.jpg') output_result = tree_node.render_html('test') expected_result = '<img src="http://example.com/image.jpg" alt="Example image." width="120" />' self.assertEqual(expected_result, output_result)
def test_render_html_with_alt_text_containing_html_entities(self): """ Test the ``render_html`` method with an alternative text containing HTML entities. """ root_tree_node = RootTreeNode() tree_node = root_tree_node.new_child('img', ImageTreeNode, attrs={'alt': '<Example image.>'}, content='http://example.com/image.jpg') output_result = tree_node.render_html('test') expected_result = '<img src="http://example.com/image.jpg" alt="<Example image.>" />' self.assertEqual(expected_result, output_result)
def test_default_render_text_implementation(self): """ Test the default ``render_text`` method implementation. """ root_tree_node = RootTreeNode() tree_node = root_tree_node.new_child('child', DummyTreeNode) with self.assertRaises(NotImplementedError) as e: tree_node.render_text('', custom_value='foobar') self.assertEqual('render_text() need to be implemented in subclass', str(e.exception))
def test_text_rendering_with_no_id(self): """ Test text rendering with no ID set. """ root_tree_node = RootTreeNode() tree_node = root_tree_node.new_child('fnref', FootnoteReferenceTreeNode, content='') rendered_output = tree_node.render_html('Hello world!') self.assertEqual('Hello world!', rendered_output)
def test_get_footnote_ref_id(self): """ Test the ``get_footnote_ref_id`` method. """ root_tree_node = RootTreeNode() tree_node = root_tree_node.new_child('fnref', FootnoteReferenceTreeNode, content='test') output = tree_node.get_footnote_ref_id('test') self.assertEqual('footnote-test', output)
def test_get_footnote_id_with_no_id_set(self): """ Test the ``get_footnote_id`` method with no ID set. """ root_tree_node = RootTreeNode() tree_node = root_tree_node.new_child('fnref', FootnoteReferenceTreeNode, content='') output = tree_node.get_footnote_id() self.assertEqual('', output)
def test_html_rendering(self): """ Test HTML rendering. """ root_tree_node = RootTreeNode() tree_node = root_tree_node.new_child( 'dd', DefinitionListTermDefinitionTreeNode) rendered_output = tree_node.render_html('test') expected_output = '<dd>test</dd>\n' self.assertEqual(expected_output, rendered_output)
def test_text_rendering(self): """ Test text rendering. """ root_tree_node = RootTreeNode() tree_node = root_tree_node.new_child('figcaption', FigureCaptionTreeNode) rendered_output = tree_node.render_text('test') expected_output = '' self.assertEqual(expected_output, rendered_output)
def test_text_rendering(self): """ Test text rendering. """ root_tree_node = RootTreeNode() tree_node = root_tree_node.new_child( 'dd', DefinitionListTermDefinitionTreeNode) rendered_output = tree_node.render_text('test') expected_output = 'test' 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() tree_node = root_tree_node.new_child( 'abbr', AcronymTreeNode, attrs={'title': '<As Soon As Possible>'}) rendered_output = tree_node.render_html('ASAP') expected_output = '<abbr title="<As Soon As Possible>">ASAP</abbr>' self.assertEqual(expected_output, rendered_output)
def test_text_rendering_trailing_whitespaces(self): """ Test text rendering with trailing whitespaces. """ root_tree_node = RootTreeNode() tree_node = root_tree_node.new_child( 'dd', DefinitionListTermDefinitionTreeNode) rendered_output = tree_node.render_text(' foo bar ') expected_output = 'foo bar' self.assertEqual(expected_output, rendered_output)
def test_text_rendering_with_html_entities_in_title(self): """ Test text rendering with HTML entities in title. """ root_tree_node = RootTreeNode() tree_node = root_tree_node.new_child( 'abbr', AcronymTreeNode, attrs={'title': '<As Soon As Possible>'}) rendered_output = tree_node.render_text('ASAP') expected_output = 'ASAP (<As Soon As Possible>)' self.assertEqual(expected_output, rendered_output)
def test_get_acronym_title_with_title_set(self): """ Test the ``get_acronym_title`` method when the "title" attribute is set. """ root_tree_node = RootTreeNode() tree_node = root_tree_node.new_child('abbr', AcronymTreeNode, attrs={'title': 'test'}) title = tree_node.get_acronym_title() self.assertEqual('test', title)
def test_get_alert_title_with_html_entities(self): """ Test the ``get_alert_title`` when the title contain HTML entities. """ root_tree_node = RootTreeNode() tree_node = root_tree_node.new_child('alert', AlertBoxTreeNode, attrs={'title': '<test>'}) title = tree_node.get_alert_title() self.assertEqual('<test>', title)
def test_get_alert_type_without_type(self): """ Test the ``get_alert_type`` method without an alert type. """ root_tree_node = RootTreeNode() tree_node = root_tree_node.new_child('alert', AlertBoxTreeNode, attrs={}) alert_type = tree_node.get_alert_type() self.assertEqual(tree_node.default_type, alert_type)
def test_get_alert_title_with_default_value(self): """ Test the ``get_alert_title`` with no title set. """ root_tree_node = RootTreeNode() tree_node = root_tree_node.new_child('alert', AlertBoxTreeNode, attrs={}) title = tree_node.get_alert_title() self.assertEqual('', title)
def test_get_alert_type(self): """ Test the ``get_alert_type`` method with a valid alert type. """ root_tree_node = RootTreeNode() for known_alert_type in AlertBoxTreeNode.accepted_types: tree_node = root_tree_node.new_child( 'alert', AlertBoxTreeNode, attrs={'type': known_alert_type}) alert_type = tree_node.get_alert_type() self.assertEqual(known_alert_type, alert_type)
def test_get_alert_title_with_title_set(self): """ Test the ``get_alert_title`` with the "title" attribute set. """ root_tree_node = RootTreeNode() tree_node = root_tree_node.new_child('alert', AlertBoxTreeNode, attrs={'title': 'test'}) title = tree_node.get_alert_title() self.assertEqual('test', title)
def test_html_rendering(self): """ Test HTML rendering. """ root_tree_node = RootTreeNode() tree_node = root_tree_node.new_child('figure', FigureDeclarationTreeNode) rendered_output = tree_node.render_html('test') expected_output = '<figure class="thumbnail" id="figure-1">test</figure>\n' self.assertEqual(expected_output, rendered_output)
def test_text_rendering_trailing_whitespaces(self): """ Test text rendering. """ root_tree_node = RootTreeNode() tree_node = root_tree_node.new_child('figure', FigureDeclarationTreeNode) rendered_output = tree_node.render_text(' foo bar ') expected_output = "+----------\n| foo bar\n+----------\n" self.assertEqual(expected_output, rendered_output)
def test_get_footnote_id_with_tagname_set(self): """ Test the ``get_footnote_id`` method with the tag name set. """ root_tree_node = RootTreeNode() tree_node = root_tree_node.new_child('footnote', FootnoteDeclarationTreeNode, attrs={'footnote': 'test'}) output = tree_node.get_footnote_id() self.assertEqual('test', output)
def test_get_footnote_id_with_no_id_set_and_counter_enabled(self): """ Test the ``get_footnote_id`` method with no ID set and auto-counter enabled. """ root_tree_node = RootTreeNode() tree_node = root_tree_node.new_child('footnote', FootnoteDeclarationTreeNode, attrs={}) output = tree_node.get_footnote_id() self.assertEqual('footnote-1', output)