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_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_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_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_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(self): # ARRANGE # const_pi = Paragraph([]) cases = [ NEA('empty sequence of paragraphs', expected=asrt.matches_sequence([]), actual=[] ), NEA('single paragraph', expected=asrt.matches_sequence([ asrt.is__any(const_pi) ]), actual=[const_pi] ), ] for case in cases: with self.subTest(case.name): # ACT # actual = sut.constant(case.actual).apply(CONSTRUCTION_ENVIRONMENT) # ASSERT # case.expected.apply_without_message(self, actual)
def parse_paragraph(self) -> Paragraph: texts = [] while self.has_more_lines() and not self.is_at_paragraph_separator(): self.consume_separator_lines() texts.append(self.parse_text()) self.consume_separator_lines() return Paragraph(texts)
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_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_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 transform_list_to_table(l: lists.HeaderContentList) -> table.Table: rows = [] for item in l.items: assert isinstance(item, lists.HeaderContentListItem) header_cell = cell([Paragraph([item.header])]) value_cell = cell(item.content_paragraph_items) rows.append([header_cell, value_cell]) return table.Table(table.TableFormat(), rows)
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 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_empty(self): # ARRANGE # root = Element('root') para = Paragraph([]) # ACT # ret_val = sut.render(TextRenderer(TARGET_RENDERER), root, para) # ASSERT # xml_string = as_unicode_str(root) self.assertEqual('<root />', xml_string) self.assertIs(root, ret_val)
def test_two_string_text(self): # ARRANGE # root = Element('root') para = Paragraph([core.StringText('_1_'), core.StringText('_2_')]) # ACT # ret_val = sut.render(TextRenderer(TARGET_RENDERER), root, para) # ASSERT # xml_string = as_unicode_str(root) self.assertEqual('<root>' '<p>_1_<br />_2_</p>' '</root>', xml_string) self._assert_first_child__of_actual_is_same_object_as(root, ret_val)
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_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_two_string_text(self): # ARRANGE # root = Element('root') para = Paragraph([core.StringText('_1_'), core.StringText('_2_')]) # 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>' '_1_<br />_2_' '</root>', xml_string) self.assertIs(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(self): # ARRANGE # const_pi_1 = Paragraph([]) const_pi_2 = Paragraph([StringText('const pi 2')]) const_pi_3 = Paragraph([StringText('const pi 3')]) cases = [ NEA('empty sequence of paragraphs', expected=asrt.matches_sequence([]), actual=[] ), NEA('single constructor', expected=asrt.matches_sequence([ asrt.is__any(const_pi_1) ]), actual=[sut.constant([const_pi_1])] ), NEA('multiple constructors', expected=asrt.matches_sequence([ asrt.is__any(const_pi_1), asrt.is__any(const_pi_2), asrt.is__any(const_pi_3), ]), actual=[sut.constant([const_pi_1, const_pi_2]), sut.constant([const_pi_3])] ), ] for case in cases: with self.subTest(case.name): # ACT # actual = sut.sequence(case.actual).apply(CONSTRUCTION_ENVIRONMENT) # ASSERT # case.expected.apply_without_message(self, actual)
def test_nested_lists(self): expected = [ lists.HeaderContentList([ _list_item('itemized item 1', [ Paragraph([StringText('item 1 contents paragraph')]), lists.HeaderContentList( [ _list_item('ordered item 1/1', [ Paragraph([StringText('item 1/1 contents paragraph')]), ]), _list_item('ordered item 1/2', []), ], self.EXPECTED_ORDERED_LIST_FORMAT), ]), _list_item('itemized item 2', []), ], self.EXPECTED_ITEMIZED_LIST_FORMAT), ] actual = sut.parse([' * itemized item 1', '', ' item 1 contents paragraph', '', '', ' 1. ordered item 1/1', '', ' item 1/1 contents paragraph', '', ' 1. ordered item 1/2', '', ' * itemized item 2', ]) check(self, expected, actual)
def test_single_anchor_with_string_as_anchored_text(self): # ARRANGE # root = Element('root') para = Paragraph([ core.AnchorText(core.StringText('concrete string'), CrossReferenceTargetTestImpl('target')), ]) # ACT # ret_val = sut.render(TextRenderer(TARGET_RENDERER), root, para) # ASSERT # xml_string = as_unicode_str(root) self.assertEqual( '<root>' '<p>' '<span id="target">concrete string</span>' '</p>' '</root>', xml_string) self._assert_first_child__of_actual_is_same_object_as(root, ret_val)
def test_single_cross_reference_to_not_id_in_same_document(self): # ARRANGE # root = Element('root') para = Paragraph([ core.CrossReferenceText(StringText('title'), CrossReferenceTargetTestImpl('target'), target_is_id_in_same_document=False), ]) # ACT # ret_val = sut.render(TextRenderer(TARGET_RENDERER), root, para) # ASSERT # xml_string = as_unicode_str(root) self.assertEqual( '<root>' '<p>' '<a href="target">title</a>' '</p>' '</root>', xml_string) self._assert_first_child__of_actual_is_same_object_as(root, ret_val)
def test_single_anchor_with_string_as_anchored_text(self): # ARRANGE # root = Element('root') para = Paragraph([ core.AnchorText(core.StringText('concrete string'), CrossReferenceTargetTestImpl('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="target">concrete string</span>' '</root>', xml_string) self.assertIs(root, ret_val)
def test_cross_reference_and_string(self): # ARRANGE # root = Element('root') para = Paragraph([ core.CrossReferenceText(StringText('title'), CrossReferenceTargetTestImpl('target'), target_is_id_in_same_document=True), core.StringText('string') ]) # 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>' '<a href="#target">title</a><br />string' '</root>', xml_string) self.assertIs(root, ret_val)
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_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, skip_surrounding_p_element=True) # ASSERT # xml_string = as_unicode_str(root) self.assertEqual( '<root>' '<a href="#target 1">' 'title 1</a><br /><a href="#target 2">title 2' '</a>' '</root>', xml_string) self.assertIs(root, ret_val)
def para(str_or_text: StrOrText) -> ParagraphItem: return Paragraph([text_from_unknown(str_or_text)])