def test_separation_between_header_and_content__with_both_initial_paragraphs_and_sub_sections( self): paragraph_item_formatter = paragraph_item.Formatter( CROSS_REF_TITLE_ONLY_TEXT_FORMATTER, paragraph_item.Wrapper(page_width=100), num_item_separator_lines=0) formatter = sut.Formatter( paragraph_item_formatter, sut.Separation(between_sections=1, between_header_and_content=2, between_initial_paragraphs_and_sub_sections=3)) header_string = 'Section Header' contents = SectionContents([single_text_para('initial paragraph')], [empty_section('Content Section Header')]) cases = [ ('section', Section(text(header_string), contents)), ('article', Article(text(header_string), ArticleContents([], contents))), ] for test_case_name, section_item in cases: with self.subTest(test_case_name): # ACT # actual = formatter.format_section_item(section_item) # ASSERT # self.assertEqual([ header_string, BLANK_LINE, BLANK_LINE, 'initial paragraph', BLANK_LINE, BLANK_LINE, BLANK_LINE, 'Content Section Header', ], actual)
def test_empty_section_item(self): # ARRANGE # paragraph_item_formatter = paragraph_item.Formatter( CROSS_REF_TITLE_ONLY_TEXT_FORMATTER, paragraph_item.Wrapper(page_width=100), num_item_separator_lines=0) formatter = sut.Formatter( paragraph_item_formatter, sut.Separation(between_sections=1, between_header_and_content=2, between_initial_paragraphs_and_sub_sections=3)) header_string = 'Section Header' cases = [ ('section', empty_section(header_string)), ('section with target', Section(text(header_string), empty_section_contents(), target=CrossReferenceTarget())), ('article', empty_article(header_string)), ('article with target', Article(text(header_string), empty_article_contents(), target=CrossReferenceTarget())), ] for test_case_name, section_item in cases: with self.subTest(test_case_name): # ACT # actual = formatter.format_section_item(section_item) # ASSERT # self.assertEqual([header_string], actual)
def test_multiple_and_nested_sub_sections(self): paragraph_item_formatter = paragraph_item.Formatter( CROSS_REF_TITLE_ONLY_TEXT_FORMATTER, paragraph_item.Wrapper(page_width=100), num_item_separator_lines=0) formatter = sut.Formatter( paragraph_item_formatter, sut.Separation(between_sections=1, between_header_and_content=2, between_initial_paragraphs_and_sub_sections=3)) section_contents = SectionContents([], [ empty_section('Section 1'), sut.Section( text('Section 2'), sut.SectionContents([], [empty_article('Section 2.1')])) ]) actual = formatter.format_section_contents(section_contents) self.assertEqual([ 'Section 1', BLANK_LINE, 'Section 2', BLANK_LINE, BLANK_LINE, 'Section 2.1', ], actual)
def test_article_separation_between_header_and_content(self): # ARRANGE # paragraph_item_formatter = paragraph_item.Formatter( CROSS_REF_TITLE_ONLY_TEXT_FORMATTER, paragraph_item.Wrapper(page_width=100), num_item_separator_lines=0) formatter = sut.Formatter( paragraph_item_formatter, sut.Separation(between_sections=1, between_header_and_content=2, between_initial_paragraphs_and_sub_sections=3)) header = text('Article Header') cases = [ ('single abstract para / no contents para', Article( header, ArticleContents([single_text_para('abstract paragraph')], empty_section_contents())), [ 'Article Header', BLANK_LINE, BLANK_LINE, 'abstract paragraph', ]), ('single abstract para / single contents para', Article( header, ArticleContents([single_text_para('abstract paragraph')], single_para_contents('contents paragraph'))), [ 'Article Header', BLANK_LINE, BLANK_LINE, 'abstract paragraph', 'contents paragraph', ]), ('single abstract para / single contents para', Article( header, ArticleContents( [single_text_para('abstract paragraph')], SectionContents([], [empty_section('Sub Section Header')]))), [ 'Article Header', BLANK_LINE, BLANK_LINE, 'abstract paragraph', BLANK_LINE, BLANK_LINE, BLANK_LINE, 'Sub Section Header', ]), ] for test_case_name, article, expected_lines in cases: with self.subTest(test_case_name): # ACT # actual = formatter.format_section_item(article) # ASSERT # self.assertEqual(expected_lines, actual)
def custom_indent(self): formatter = sut.Formatter(CROSS_REF_TITLE_ONLY_TEXT_FORMATTER, sut.Wrapper(page_width=100), list_formats=self.LIST_FORMATS) actual = formatter.resolve_list_format( lists.Format(lists.ListType.VARIABLE_LIST, custom_indent_spaces=10)) self.assertEqual(10 * ' ', actual.indent_str)
def formatter_with_page_width(page_width: int, literal_layout_indent: str = '' ) -> sut.Formatter: return sut.Formatter( CROSS_REF_TITLE_ONLY_TEXT_FORMATTER, sut.Wrapper(page_width=page_width), literal_layout_indent=literal_layout_indent, )
def custom_separation(self): custom_separations = lists.Separations(10, 20) formatter = sut.Formatter(CROSS_REF_TITLE_ONLY_TEXT_FORMATTER, sut.Wrapper(page_width=100), list_formats=self.LIST_FORMATS) actual = formatter.resolve_list_format( lists.Format(lists.ListType.VARIABLE_LIST, custom_separations=custom_separations)) self.assertIs(custom_separations, actual.separations)
def test_ordered_list(self): items = [header_only_item('header')] formatter = sut.Formatter(CROSS_REF_TITLE_ONLY_TEXT_FORMATTER, sut.Wrapper(page_width=100), list_formats=self.LIST_FORMATS) actual = formatter.format_header_content_list( lists.HeaderContentList(items, lists.Format(lists.ListType.ORDERED_LIST))) self.assertEqual(['1. header'], actual)
def test_section_content_indent__for_nested_sections(self): paragraph_item_formatter = paragraph_item.Formatter( CROSS_REF_TITLE_ONLY_TEXT_FORMATTER, paragraph_item.Wrapper(page_width=100), num_item_separator_lines=0) content_indent = ' ' formatter = sut.Formatter( paragraph_item_formatter, section_content_indent_str=content_indent, separation=sut.Separation( between_sections=1, between_header_and_content=2, between_initial_paragraphs_and_sub_sections=3)) header = text('Section 1') contents = SectionContents([], [ Section( text('Section 1.1'), SectionContents( [single_text_para('paragraph in section 1.1')], [ Section( text('Section 1.1.1'), SectionContents([ single_text_para('paragraph in section 1.1.1') ], [])) ])) ]) cases = [ ('section', Section(header, contents)), ('article', Article(header, ArticleContents([], contents))), ] for test_case_name, section_item in cases: with self.subTest(test_case_name): # ACT # actual = formatter.format_section_item(section_item) # ASSERT # self.assertEqual([ 'Section 1', BLANK_LINE, BLANK_LINE, (1 * content_indent) + 'Section 1.1', BLANK_LINE, BLANK_LINE, (2 * content_indent) + 'paragraph in section 1.1', BLANK_LINE, BLANK_LINE, BLANK_LINE, (2 * content_indent) + 'Section 1.1.1', BLANK_LINE, BLANK_LINE, (3 * content_indent) + 'paragraph in section 1.1.1', ], actual)
def test_with_resolved_indent(self): format_indent_spaces = 2 indent_str = format_indent_spaces * ' ' list_format = lf.ListFormat( lf.HeaderAndIndentFormatPlain(contents_indent_spaces=4), NO_SEPARATIONS, indent_str=indent_str) items = [item('header', [single_text_para('contents')])] the_list = sut.HeaderContentList( items, lists.Format(lists.ListType.VARIABLE_LIST)) formatter = sut.Formatter( CROSS_REF_TITLE_ONLY_TEXT_FORMATTER, sut.Wrapper(page_width=100), list_formats=lf.ListFormats(variable_list_format=list_format)) actual = formatter.format_header_content_list(the_list) self.assertEqual( [indent_str + 'header', indent_str + (4 * ' ') + 'contents'], actual)
def test_section_content_indent_w_literal_layout_indent(self): ll_indent = 'LLI ' content_indent = 'CI ' paragraph_item_formatter = paragraph_item.Formatter( CROSS_REF_TITLE_ONLY_TEXT_FORMATTER, paragraph_item.Wrapper(page_width=100), num_item_separator_lines=0, literal_layout_indent=ll_indent) formatter = sut.Formatter( paragraph_item_formatter, section_content_indent_str=content_indent, separation=sut.Separation( between_sections=1, between_header_and_content=2, between_initial_paragraphs_and_sub_sections=3)) header = text('Section Header') contents = SectionContents([LiteralLayout('literal layout 1')], [ Section(text('Sub Section Header'), SectionContents([LiteralLayout('literal layout 2')])) ]) cases = [ ('section', Section(header, contents)), ('article', Article(header, ArticleContents([], contents))), ] for test_case_name, section_item in cases: with self.subTest(test_case_name): # ACT # actual = formatter.format_section_item(section_item) # ASSERT # self.assertEqual([ 'Section Header', BLANK_LINE, BLANK_LINE, content_indent + ll_indent + 'literal layout 1', BLANK_LINE, BLANK_LINE, BLANK_LINE, content_indent + 'Sub Section Header', BLANK_LINE, BLANK_LINE, content_indent * 2 + ll_indent + 'literal layout 2', ], actual)
def test_only_initial_paragraphs(self): paragraph_item_formatter = paragraph_item.Formatter( CROSS_REF_TITLE_ONLY_TEXT_FORMATTER, paragraph_item.Wrapper(page_width=5), num_item_separator_lines=1, list_formats=lf.ListFormats(variable_list_format=lf.ListFormat( lf.HeaderAndIndentFormatPlain(), lists.Separations(0, 0), indent_str=''))) formatter = sut.Formatter(paragraph_item_formatter) section_contents = SectionContents([ single_text_para('12345 123 5'), lists.HeaderContentList( [header_only_item('12345 123')], lists.Format(lists.ListType.VARIABLE_LIST), ) ], []) actual = formatter.format_section_contents(section_contents) self.assertEqual(['12345', '123 5', BLANK_LINE, '12345', '123'], actual)
def test_initial_paragraph_and_single_sub_section(self): paragraph_item_formatter = paragraph_item.Formatter( CROSS_REF_TITLE_ONLY_TEXT_FORMATTER, paragraph_item.Wrapper(page_width=100), num_item_separator_lines=0) formatter = sut.Formatter( paragraph_item_formatter, sut.Separation(between_sections=1, between_header_and_content=2, between_initial_paragraphs_and_sub_sections=3)) section_contents = SectionContents( [single_text_para('initial paragraph')], [empty_section('Section Header')]) actual = formatter.format_section_contents(section_contents) self.assertEqual([ 'initial paragraph', BLANK_LINE, BLANK_LINE, BLANK_LINE, 'Section Header', ], actual)
def test_with_custom_indent(self): custom_indent_spaces = 5 custom_indent_str = custom_indent_spaces * ' ' content_indent_spaces = 4 content_indent_str = content_indent_spaces * ' ' list_format = lf.ListFormat( lf.HeaderAndIndentFormatPlain( contents_indent_spaces=content_indent_spaces), NO_SEPARATIONS, indent_str='should not be used since overridden by custom indent') items = [item('header', [single_text_para('contents')])] the_list = sut.HeaderContentList( items, lists.Format(lists.ListType.VARIABLE_LIST, custom_indent_spaces=custom_indent_spaces)) formatter = sut.Formatter( CROSS_REF_TITLE_ONLY_TEXT_FORMATTER, sut.Wrapper(page_width=100), list_formats=lf.ListFormats(variable_list_format=list_format)) actual = formatter.format_header_content_list(the_list) self.assertEqual([ custom_indent_str + 'header', custom_indent_str + content_indent_str + 'contents' ], actual)
def formatter_with_page_width(page_width: int) -> sut.Formatter: return sut.Formatter(CROSS_REF_TITLE_ONLY_TEXT_FORMATTER, sut.Wrapper(page_width=page_width), list_formats=list_formats_with(indent_str=''))