def test_single_line_text_blocks(self): check(self, [Paragraph([StringText('text 1'), StringText('text 2')])], sut.parse(['text 1'] + sut.TEXT_SEPARATOR_LINES + ['text 2']))
def test_list_with_item_multiple_paragraphs_contents_before_other_item(self): expected = [ lists.HeaderContentList([ _list_item('item 1', [ Paragraph([StringText('item 1 contents paragraph 1')]), Paragraph([StringText('item 1 contents paragraph 2')]), ]), _list_item('item 2', [ Paragraph([StringText('item 2 contents paragraph 1')]), ]), ], self.EXPECTED_LIST_FORMAT), Paragraph([StringText('last paragraph')]), ] actual = sut.parse([' * item 1', '', ' item 1 contents paragraph 1', '', '', ' item 1 contents paragraph 2', '', ' * item 2', '', ' item 2 contents paragraph 1', '', 'last paragraph']) check(self, expected, actual)
def test(self): # ARRANGE # root_element_to_mutate = Element('root') section_header_text = 'section header' article_header_text = 'article header' sc = SectionContents([], [ Section(StringText(section_header_text), SectionContents([], [])), Article(StringText(article_header_text), ArticleContents([], SectionContents([], []))), ]) expected_element = element( root_element_to_mutate.tag, children=[ element('section', children=[ _header_w_h(section_header_text, 'h3'), ]), element('article', children=[ _header_w_h(article_header_text), ]), ]) # ACT # ret_val = TEST_RENDERER.render_section_contents( sut.Environment(2), root_element_to_mutate, sc) # ASSERT # assert_contents_and_that_last_child_is_returned( expected_element, root_element_to_mutate, ret_val, self)
def test_cross_reference(self): _cases(self, [ Case('target in same doc', text=CrossReferenceText(StringText('title text'), _CrossReferenceString('the-target')) , expected=element('a', attributes={'href': '#the-target'}, text='title text'), ), Case('target in other doc', text=CrossReferenceText(StringText('title text'), _CrossReferenceString('the-target'), target_is_id_in_same_document=False) , expected=element('a', attributes={'href': 'the-target'}, text='title text'), ), Case('with tags', text=CrossReferenceText(StringText('title text'), _CrossReferenceString('the-target'), tags={'1st', '2nd'}) , expected=element('a', attributes={'href': '#the-target', 'class': '1st 2nd'}, text='title text'), ), ])
def test_list_with_item_multiple_paragraphs_contents_in_middle_of_paragraphs(self): expected = [ Paragraph([StringText('first paragraph')]), lists.HeaderContentList([ _list_item('item 1', [ Paragraph([StringText('line 1 in contents paragraph 1'), StringText('line 2 in contents paragraph 1')]), Paragraph([StringText('line 1 in contents paragraph 2')]), ]), ], self.EXPECTED_LIST_FORMAT), Paragraph([StringText('last paragraph')]), ] actual = sut.parse(['first paragraph', '', '', ' * item 1', '', ' line 1 in contents paragraph 1', '', ' line 2 in contents paragraph 1', '', '', ' line 1 in contents paragraph 2', '', '', 'last paragraph']) check(self, expected, actual)
def test_single_line_text_blocks(self): check(self, [Paragraph([StringText('text in para 1')]), Paragraph([StringText('text in para 2')])], sut.parse(['text in para 1'] + sut.PARAGRAPH_SEPARATOR_LINES + ['text in para 2']))
def test(self): # ARRANGE # root_element_to_mutate = Element('root') header_1_text = 'header 1' header_2_text = 'header 2' sc = SectionContents([], [ Section(StringText(header_1_text), SectionContents([], [])), Section(StringText(header_2_text), SectionContents([], [])), ]) expected_element = element(root_element_to_mutate.tag, children=[ element('section', children=[ _header_w_h(header_1_text), ]), element('section', children=[ _header_w_h(header_2_text), ]), ]) # ACT # ret_val = TEST_RENDERER.render_section_contents( sut.Environment(0), root_element_to_mutate, sc) # ASSERT # assert_contents_and_that_last_child_is_returned( expected_element, root_element_to_mutate, ret_val, self)
def test_with_fixed_root_target(self): # ARRANGE # root_header = StringText('root header') sub_header = StringText('sub header') sub_local_target_name = 'should not be used' fixed_target = CustomCrossReferenceTargetTestImpl('the fixed root target') default_target = CustomCrossReferenceTargetTestImpl('default target component') target_factory = ConstantTargetInfoFactoryTestImpl(default_target) unadjusted_object = sut.hierarchy(root_header, paragraphs.empty(), [sut.child(sub_local_target_name, sut.leaf(sub_header.value, section_contents(doc.empty_section_contents()))) ] ) adjusted_object = sut.with_fixed_root_target(fixed_target, unadjusted_object) # EXPECTATION # expected_root_target_info = TargetInfo(root_header, fixed_target) expected_sub_target_info = TargetInfo(sub_header, default_target) target_info_node_assertion = equals_target_info_node( TargetInfoNode(expected_root_target_info, [target_info_leaf(expected_sub_target_info)]), mk_equals_cross_ref_id=equals_custom_cross_ref_test_impl) section_assertion = section_matches( target=equals_custom_cross_ref_test_impl(fixed_target), header=asrt_para.equals_text(expected_root_target_info.presentation_text), contents=section_contents_matches( initial_paragraphs=asrt.is_empty_sequence, sections=asrt.matches_sequence([ section_matches( target=equals_custom_cross_ref_test_impl(default_target), header=asrt_para.equals_text(sub_header), contents=section_contents_matches( initial_paragraphs=asrt.is_empty_sequence, sections=asrt.is_empty_sequence ) ) ]) )) # ACT & ASSERT # self._act_and_assert(adjusted_object, target_factory, target_info_node_assertion, section_assertion)
def test_larger_paragraph_separator(self): check(self, [Paragraph([StringText('text in para 1')]), Paragraph([StringText('text in para 2')])], sut.parse(['text in para 1'] + sut.PARAGRAPH_SEPARATOR_LINES + sut.PARAGRAPH_SEPARATOR_LINES + ['text in para 2']))
def test_complex_structure_with_reset_of_section_level(self): # ARRANGE # s = Article( StringText('article header'), ArticleContents( [para('para in abstract')], SectionContents([para('initial para in contents')], [ Section( StringText('header 1/1'), SectionContents([para('para 1/1')], [ Section(StringText('header 1/1/1'), SectionContents([para('para 1/1/1')], [])) ])) ]))) for section_level in range(3): with self.subTest('section level = ' + str(section_level)): root = Element('root') environment = sut.Environment(section_level) # ACT # ret_val = TEST_RENDERER.render_section_item( environment, root, s) # ASSERT # expected_element = element( 'root', children=[ element('article', children=[ element( 'header', children=[ element('h1', text='article header'), element('p', text='para in abstract'), ], ), element('p', text='initial para in contents'), element( 'section', children=[ _header_w_h('header 1/1'), element('p', text='para 1/1'), element( 'section', children=[ _header_w_h( 'header 1/1/1', 'h2'), element('p', text='para 1/1/1'), ]) ]) ]) ]) assert_contents_and_that_last_child_is_returned( expected_element, root, ret_val, self)
def test_multi_line_text_blocks(self): input_lines = ['text', '1'] + \ sut.TEXT_SEPARATOR_LINES + \ ['text', '2'] check(self, [Paragraph([StringText('text 1'), StringText('text 2')])], sut.parse(input_lines))
def test_multiple_text_blocks(self): check(self, [Paragraph([StringText('text 1 in para 1'), StringText('text 2 in para 1')]), Paragraph([StringText('text 1 in para 2'), StringText('text 2 in para 2')])], sut.parse(['text 1 in para 1'] + sut.TEXT_SEPARATOR_LINES + ['text 2 in para 1'] + sut.PARAGRAPH_SEPARATOR_LINES + ['text 1 in para 2'] + sut.TEXT_SEPARATOR_LINES + ['text 2 in para 2']))
def test_literal_block_between_paragraph_blocks(self): check(self, [ Paragraph([StringText('para 1')]), sut.LiteralLayout(lines_content(['literal line'])), Paragraph([StringText('para 2')]), ], sut.parse(['para 1'] + sut.PARAGRAPH_SEPARATOR_LINES + ['```', 'literal line', '```'] + sut.PARAGRAPH_SEPARATOR_LINES + ['para 2']))
def test_single_anchor_with_cross_reference_as_anchored_text(self): # ARRANGE # root = Element('root') para = Paragraph([ core.AnchorText( core.CrossReferenceText( StringText('cross ref title'), CrossReferenceTargetTestImpl('cross ref target'), target_is_id_in_same_document=True), CrossReferenceTargetTestImpl('anchor target')), ]) # ACT # ret_val = sut.render(TextRenderer(TARGET_RENDERER), root, para, skip_surrounding_p_element=True) # ASSERT # xml_string = as_unicode_str(root) self.assertEqual( '<root>' '<span id="anchor target">' '<a href="#cross ref target">cross ref title</a>' '</span>' '</root>', xml_string) self.assertIs(root, ret_val)
def test_plain_string_text__after(self): # ARRANGE # root_tag_name = 'root' source_text = 'source text' root_element_to_mutate = Element(root_tag_name) sub = SubElement(root_element_to_mutate, 'sub') renderer = sut.TextRenderer(TargetRenderer()) text = StringText(source_text) # EXPECTATION # expected_element = element(root_tag_name, children=[element(sub.tag, tail=source_text)]) assertion = asrt_xml.equals(expected_element) # ACT # actual = renderer.apply(root_element_to_mutate, sub, sut.Position.AFTER, text) # ASSERT # assertion.apply_with_message(self, actual, 'element structure') self.assertIs(root_element_to_mutate, actual, 'returned element object')
def test_simple_document(self): # ARRANGE # section_contents = SectionContents([para('para 0')], [ Section(StringText('header 1'), SectionContents([para('para 1'), para('')], [])) ]) document_setup = sut.DocumentSetup('page title') # ACT # output_file = io.StringIO() DOCUMENT_RENDERER.apply(output_file, document_setup, section_contents) actual = output_file.getvalue() # ASSERT # expected = (DOCTYPE_XHTML1_0 + '<html>' '<head>' '<title>page title</title>' '</head>' '<body>' '<p>para 0</p>' '<section>' '<header>' '<h1>header 1</h1>' '</header>' '<p>para 1</p>' '<p></p>' '</section>' '</body>' '</html>') self.assertEqual(expected, actual)
def test_hierarchy_with_sub_sections(self): # ARRANGE # target_factory = TargetInfoFactoryTestImpl(['target_component']) expected_section_contents_object1 = doc.empty_section_contents() expected_section_contents_object2 = docs.section_contents(docs.paras('testing testing')) expected_root_initial_para = docs.para('root initial paras') expected_root_initial_paras = [expected_root_initial_para] root_header = StringText('root header') sub_section_header_1 = 'sub1' sub_section_header_2 = 'sub2' sub_section_local_target_1 = 'sub-target1' sub_section_local_target_2 = 'sub-target2' object_to_test = sut.hierarchy( root_header, paragraphs.constant(expected_root_initial_paras), [sut.child(sub_section_local_target_1, sut.leaf(sub_section_header_1, section_contents(expected_section_contents_object1))), sut.child(sub_section_local_target_2, sut.leaf(sub_section_header_2, section_contents(expected_section_contents_object2))) ]) # EXPECTATION # expected_root_target_info = target_factory.root(root_header) sub1_target = target_factory.sub(sub_section_header_1, sub_section_local_target_1) sub2_target = target_factory.sub(sub_section_header_2, sub_section_local_target_2) target_info_node_assertion = equals_target_info_node( TargetInfoNode(expected_root_target_info, [ target_info_leaf(sub1_target), target_info_leaf(sub2_target), ]), mk_equals_cross_ref_id=equals_custom_cross_ref_test_impl) section_assertion2 = section_matches( target=equals_custom_cross_ref_test_impl(expected_root_target_info.target), header=asrt_para.equals_text(expected_root_target_info.presentation_text), contents=section_contents_matches( initial_paragraphs=asrt.matches_sequence([asrt.Is(expected_root_initial_para)]), sections=asrt.matches_sequence([ section_matches( target=equals_custom_cross_ref_test_impl(sub1_target.target), header=asrt_para.equals_text(sub1_target.presentation_text), contents=asrt.Is(expected_section_contents_object1)), section_matches( target=equals_custom_cross_ref_test_impl(sub2_target.target), header=asrt_para.equals_text(sub2_target.presentation_text), contents=asrt.Is(expected_section_contents_object2)), ]))) # ACT & ASSERT # self._act_and_assert(object_to_test, target_factory, target_info_node_assertion, section_assertion2)
def test_single_text_block_on_multiple_lines(self): input_lines = ['the', 'text', 'block' ] check(self, [Paragraph([StringText(' '.join(input_lines))])], sut.parse(input_lines))
def test_list_in_middle_of_paragraphs(self): expected = [ Paragraph([StringText('first paragraph')]), lists.HeaderContentList([_list_item('item 1'), _list_item('item 2')], self.EXPECTED_LIST_FORMAT), Paragraph([StringText('last paragraph')]), ] actual = sut.parse(['first paragraph', '', '', ' * item 1', ' * item 2', '', '', 'last paragraph']) check(self, expected, actual)
def test_visit_paragraph(self): # ARRANGE # item = Paragraph([StringText('string text')]) visitor = AVisitorThatRecordsVisitedMethods() # ACT # ret_val = visitor.visit(item) # ASSERT # self.assertEqual([Paragraph], visitor.visited_types) self.assertIs(item, ret_val)
def parse_list_item_from_item_line( self, first_item_header_line: str, item_line_prefix: str) -> lists.HeaderContentListItem: item_line_prefix_len = len(item_line_prefix) header = first_item_header_line[item_line_prefix_len:].strip() content_paragraph_items = self.parse_list_content_paragraph_items( item_line_prefix_len) return docs.list_item(StringText(header), content_paragraph_items)
def test_section(self): # ARRANGE # section = sut.Section(StringText('header'), sut.empty_section_contents()) visitor = SectionItemVisitorThatRecordsExpectedClassAndReturnsArg() # ACT # ret_val = section.accept(visitor) # ASSERT # self.assertEqual([sut.Section], visitor.visited_classes) self.assertIs(section, ret_val)
def test_string_with_tags(self): _cases(self, [ Case('tags', text=StringText('the text', tags={'1st', '2nd'}), expected=element('span', attributes={'class': '1st 2nd'}, text='the text') ), ])
def test_single_list_with_with_contents(self): expected = [lists.HeaderContentList([ _list_item('item 1', [ Paragraph([StringText('contents 1')]), ]), _list_item('item 2', [ Paragraph([StringText('contents 2')]), ]), ], self.EXPECTED_LIST_FORMAT)] actual = sut.parse([' * item 1', ' contents 1', ' * item 2', ' contents 2', ]) check(self, expected, actual)
def test_article(self): # ARRANGE # article = sut.Article( StringText('header'), ArticleContents([], sut.empty_section_contents())) visitor = SectionItemVisitorThatRecordsExpectedClassAndReturnsArg() # ACT # ret_val = article.accept(visitor) # ASSERT # self.assertEqual([sut.Article], visitor.visited_classes) self.assertIs(article, ret_val)
def test_single_list_with_single_item_with_contents_on_next_line(self): expected = [lists.HeaderContentList([ _list_item('item', [ Paragraph([StringText('contents')]), ])], self.EXPECTED_LIST_FORMAT)] actual = sut.parse([' 1. item', ' contents', ]) check(self, expected, actual)
def test_level_greater_than_highest_h_level(self): # ARRANGE # root = Element('root') header_text = 'text' expected_element = element(root.tag, children=[element('h6', text=header_text)]) # ACT # ret_val = self.HN_SECTION_HEADER_RENDERER.apply( sut.Environment(6), root, StringText(header_text), {}) # ASSERT # assert_contents_and_that_last_child_is_returned( expected_element, root, ret_val, self)
def test_two_cross_reference(self): # ARRANGE # root = Element('root') para = Paragraph([ core.CrossReferenceText(StringText('title 1'), CrossReferenceTargetTestImpl('target 1')), core.CrossReferenceText(StringText('title 2'), CrossReferenceTargetTestImpl('target 2')) ]) # ACT # ret_val = sut.render(TextRenderer(TARGET_RENDERER), root, para) # ASSERT # xml_string = as_unicode_str(root) self.assertEqual( '<root>' '<p>' '<a href="#target 1">' 'title 1</a><br /><a href="#target 2">title 2' '</a>' '</p>' '</root>', xml_string) self._assert_first_child__of_actual_is_same_object_as(root, ret_val)
def test_misc(self): lines = ['', ' ', 'para 1 text 1', ' ', '', ' ', ' para 2 text 1', ' ', 'para 2 text 2 ', ' ', '\tpara 2 text 3 ', '', ' '] indented_lines = [' ' + line for line in lines] actual = sut.normalize_and_parse(lines_content(indented_lines)) check(self, [Paragraph([StringText('para 1 text 1')]), Paragraph([StringText('para 2 text 1'), StringText('para 2 text 2'), StringText('para 2 text 3')])], actual)
def test_single_sub_section_with_initial_paras_everywhere(self): # ARRANGE # root_element_to_mutate = Element('root') para_0_text = 'para 0' header_1_text = 'header 1' para_1_text = 'para 1' header_1_1_text = 'header 1/1' para_2_text = 'para 2' sc = SectionContents([para(para_0_text)], [ Section( StringText(header_1_text), SectionContents([para(para_1_text)], [ Section(StringText(header_1_1_text), SectionContents([para(para_2_text)], [])) ])) ]) expected_element = element( root_element_to_mutate.tag, children=[ element('p', text=para_0_text), element('section', children=[ _header_w_h(header_1_text), element('p', text=para_1_text), element('section', children=[ _header_w_h(header_1_1_text, 'h2'), element('p', text=para_2_text), ]) ]), ]) # ACT # ret_val = TEST_RENDERER.render_section_contents( sut.Environment(0), root_element_to_mutate, sc) # ASSERT # assert_contents_and_that_last_child_is_returned( expected_element, root_element_to_mutate, ret_val, self)