def test_section_with_single_initial_para(self): # ARRANGE # root = Element('root') sc = SectionContents([], [Section(StringText('section header'), SectionContents([para('initial para')], []))]) # ACT # ret_val = TEST_RENDERER.render_section_contents(sut.Environment(0), root, sc) # ASSERT # assert_contents_and_that_last_child_is_returned( '<root>' '<section>' '<header>' '<h1>section header</h1>' '</header>' '<p>initial para</p>' '</section>' '</root>' , root, ret_val, self)
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(self): # ARRANGE # root = Element('root') sc = SectionContents([], [Section(StringText('header 1'), SectionContents([], [])), Section(StringText('header 2'), SectionContents([], [])), ]) # ACT # ret_val = TEST_RENDERER.render_section_contents(sut.Environment(0), root, sc) # ASSERT # assert_contents_and_that_last_child_is_returned( '<root>' '<section>' '<header>' '<h1>header 1</h1>' '</header>' '</section>' '<section>' '<header>' '<h1>header 2</h1>' '</header>' '</section>' '</root>' , root, ret_val, self)
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(self): # ARRANGE # root = Element('root') sc = SectionContents([], [Section(StringText('section header'), SectionContents([], [])), Article(StringText('article header'), ArticleContents([], SectionContents([], []))), ]) # ACT # ret_val = TEST_RENDERER.render_section_contents(sut.Environment(2), root, sc) # ASSERT # assert_contents_and_that_last_child_is_returned( '<root>' '<section>' '<header>' '<h3>section header</h3>' '</header>' '</section>' '<article>' '<header>' '<h1>article header</h1>' '</header>' '</article>' '</root>' , root, ret_val, self)
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_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 # assert_contents_and_that_last_child_is_returned( '<root>' '<article>' '<header>' '<h1>article header</h1>' '<p>para in abstract</p>' '</header>' '<p>initial para in contents</p>' '<section>' '<header>' '<h1>header 1/1</h1>' '</header>' '<p>para 1/1</p>' '<section>' '<header>' '<h2>header 1/1/1</h2>' '</header>' '<p>para 1/1/1</p>' '</section>' '</section>' '</article>' '</root>' , root, ret_val, self)
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_single_paragraph_item(self): # ARRANGE # root = Element('root') sc = SectionContents([para('the only para')], []) # ACT # ret_val = TEST_RENDERER.render_section_contents(sut.Environment(0), root, sc) # ASSERT # assert_contents_and_that_last_child_is_returned( '<root>' '<p>the only para</p>' '</root>', root, ret_val, self)
def test_level_greater_than_highest_h_level(self): # ARRANGE # root = Element('root') # ACT # ret_val = self.HN_SECTION_HEADER_RENDERER.apply(sut.Environment(6), root, StringText('text'), {}) # ASSERT # assert_contents_and_that_last_child_is_returned( '<root>' '<h6>text</h6>' '</root>', root, ret_val, self)
def test_single_paragraph_item(self): # ARRANGE # root = Element('root') paragraph_text = 'the only para' sc = SectionContents([para(paragraph_text)], []) expected_element = element( root.tag, children=[element('p', text=paragraph_text)]) # ACT # ret_val = TEST_RENDERER.render_section_contents( sut.Environment(0), root, sc) # ASSERT # assert_contents_and_that_last_child_is_returned( expected_element, root, ret_val, self)
def test_with_attributes(self): # ARRANGE # root = Element('root') # ACT # ret_val = self.HN_SECTION_HEADER_RENDERER.apply(sut.Environment(3), root, StringText('text'), {'attr1': 'attr1-value', 'attr2': 'attr2-value'}) # ASSERT # assert_contents_and_that_last_child_is_returned( '<root>' '<h4 attr1="attr1-value" attr2="attr2-value">text</h4>' '</root>', root, ret_val, self)
def test_multiple_paragraph_items(self): # ARRANGE # root_element_to_mutate = Element('root') paragraph_1_text = 'para 1' paragraph_2_text = 'para 2' sc = SectionContents([para(paragraph_1_text), para(paragraph_2_text)], []) expected_element = element(root_element_to_mutate.tag, children=[ element('p', text=paragraph_1_text), element('p', text=paragraph_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_attributes(self): # ARRANGE # root_to_mutate = Element('root') # ACT # ret_val = self.HN_SECTION_HEADER_RENDERER.apply( sut.Environment(3), root_to_mutate, StringText('text'), { 'attr1': 'attr1-value', 'attr2': 'attr2-value' }) expected_element = element(root_to_mutate.tag, children=[ element('h4', attributes={ 'attr1': 'attr1-value', 'attr2': 'attr2-value' }, text='text') ]) # ASSERT # assert_contents_and_that_last_child_is_returned( expected_element, root_to_mutate, ret_val, self)
def test_single_sub_section_with_initial_paras_everywhere(self): # ARRANGE # root = Element('root') sc = SectionContents( [para('para 0')], [Section(StringText('header 1'), SectionContents([para('para 1')], [Section(StringText('header 1/1'), SectionContents([para('para 2')], []))]))]) # ACT # ret_val = TEST_RENDERER.render_section_contents(sut.Environment(0), root, sc) # ASSERT # assert_contents_and_that_last_child_is_returned( '<root>' '<p>para 0</p>' '<section>' '<header>' '<h1>header 1</h1>' '</header>' '<p>para 1</p>' '<section>' '<header>' '<h2>header 1/1</h2>' '</header>' '<p>para 2</p>' '</section>' '</section>' '</root>' , root, ret_val, self)
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)
def test_section_with_single_initial_para(self): # ARRANGE # root_element_to_mutate = Element('root') section_header_text = 'section header' initial_para_text = 'initial para' sc = SectionContents([], [ Section(StringText(section_header_text), SectionContents([para(initial_para_text)], [])) ]) expected_element = element( root_element_to_mutate.tag, children=[ element('section', children=[ _header_w_h(section_header_text), element('p', text=initial_para_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)
assert isinstance(target, _CrossReferenceString) return target.string def _test_inside(put: unittest.TestCase, case: Case): # ARRANGE # root_element_to_mutate = Element('root') renderer = sut.TextRenderer(_StringTargetRenderer()) # ACT # actual = renderer.apply(root_element_to_mutate, root_element_to_mutate, sut.Position.INSIDE, case.text) # ASSERT # expected_xml = element('root', children=[case.expected]) assert_contents_and_that_last_child_is_returned( expected_xml, root_element_to_mutate, actual, put) def _test_after(put: unittest.TestCase, case: Case): root_element_to_mutate = Element('root') sub = SubElement(root_element_to_mutate, 'sub') renderer = sut.TextRenderer(_StringTargetRenderer()) # ACT # actual = renderer.apply(root_element_to_mutate, sub, sut.Position.AFTER, case.text) # ASSERT # expected_xml = element('root', children=[element('sub'), case.expected]) assert_contents_and_that_last_child_is_returned(
def test_simple(self): cases = [ ('empty', Article(StringText('header'), ArticleContents([], empty_section_contents())), '<root>' '<article>' '<header>' '<h1>header</h1>' '</header>' '</article>' '</root>' ), ('empty with target', Article(StringText('header'), ArticleContents([], empty_section_contents()), target=CrossReferenceTargetTestImpl('target-name')), '<root>' '<article id="target-name">' '<header>' '<h1>header</h1>' '</header>' '</article>' '</root>' ), ('empty with tags', Article(StringText('header'), ArticleContents([], empty_section_contents()), tags={'label1', 'label2'}), '<root>' '<article class="label1 label2">' '<header>' '<h1>header</h1>' '</header>' '</article>' '</root>' ), ('empty with target and tags', Article(StringText('header'), ArticleContents([], empty_section_contents()), target=CrossReferenceTargetTestImpl('article-target'), tags={'label1', 'label2'}), '<root>' '<article class="label1 label2" id="article-target">' '<header>' '<h1>header</h1>' '</header>' '</article>' '</root>' ), ('single abstract paragraph', Article(StringText('header'), ArticleContents([para('abstract paragraph')], empty_section_contents())), '<root>' '<article>' '<header>' '<h1>header</h1>' '<p>abstract paragraph</p>' '</header>' '</article>' '</root>' ) ] for test_case_name, article, expected in cases: with self.subTest(test_case_name): root = Element('root') environment = sut.Environment(0) # ACT # ret_val = TEST_RENDERER.render_section_item(environment, root, article) # ASSERT # assert_contents_and_that_last_child_is_returned( expected, root, ret_val, self)
def test(self): # ARRANGE # cases = [ ('empty', Section( StringText('header 1'), empty_section_contents()), '<root>' '<section>' '<header>' '<h1>header 1</h1>' '</header>' '</section>' '</root>' ), ('empty with target', Section( StringText('header 1'), empty_section_contents(), target=CrossReferenceTargetTestImpl('section-target-name')), '<root>' '<section id="section-target-name">' '<header>' '<h1>header 1</h1>' '</header>' '</section>' '</root>' ), ('empty with tags', Section( StringText('header 1'), empty_section_contents(), tags={'first-label', 'second-label'}), '<root>' '<section class="first-label second-label">' '<header>' '<h1>header 1</h1>' '</header>' '</section>' '</root>' ), ('empty with target and tags', Section( StringText('header 1'), empty_section_contents(), target=CrossReferenceTargetTestImpl('t'), tags={'l1', 'l2'}), '<root>' '<section class="l1 l2" id="t">' '<header>' '<h1>header 1</h1>' '</header>' '</section>' '</root>' ), ('with contents', Section( StringText('header 1'), SectionContents( [para('para 1')], [Section( StringText('header 1/1'), SectionContents( [para('para 1/1')], []))]) ), '<root>' '<section>' '<header>' '<h1>header 1</h1>' '</header>' '<p>para 1</p>' '<section>' '<header>' '<h2>header 1/1</h2>' '</header>' '<p>para 1/1</p>' '</section>' '</section>' '</root>' ), ] for test_case_name, section, expected in cases: with self.subTest(test_case_name): root = Element('root') # ACT # ret_val = TEST_RENDERER.render_section_item(sut.Environment(0), root, section) # ASSERT # assert_contents_and_that_last_child_is_returned( expected, root, ret_val, self)
def test(self): # ARRANGE # cases = [ ('empty', Section(StringText('header 1'), empty_section_contents()), element('root', children=[ element('section', children=[ _header_w_h('header 1'), ]) ])), ('empty with target', Section( StringText('header 1'), empty_section_contents(), target=CrossReferenceTargetTestImpl('section-target-name')), element('root', children=[ element('section', attributes={'id': 'section-target-name'}, children=[_header_w_h('header 1')]) ])), ('empty with tags', Section(StringText('header 1'), empty_section_contents(), tags={'first-label', 'second-label'}), element('root', children=[ element( 'section', attributes={'class': 'first-label second-label'}, children=[_header_w_h('header 1')]) ])), ('empty with target and tags', Section(StringText('header 1'), empty_section_contents(), target=CrossReferenceTargetTestImpl('t'), tags={'l1', 'l2'}), element('root', children=[ element('section', attributes={ 'class': 'l1 l2', 'id': 't' }, children=[_header_w_h('header 1')]) ])), ('with contents', Section( StringText('header 1'), SectionContents([para('para 1')], [ Section(StringText('header 1/1'), SectionContents([para('para 1/1')], [])) ])), element('root', children=[ element('section', children=[ _header_w_h('header 1'), element('p', text='para 1'), element('section', children=[ _header_w_h( 'header 1/1', 'h2'), element('p', text='para 1/1'), ]) ]) ])), ] for test_case_name, section, expected in cases: with self.subTest(test_case_name): root_element_to_mutate = Element('root') # ACT # ret_val = TEST_RENDERER.render_section_item( sut.Environment(0), root_element_to_mutate, section) # ASSERT # assert_contents_and_that_last_child_is_returned( expected, root_element_to_mutate, ret_val, self)
def test_simple(self): cases = [ ('empty', Article(StringText('header'), ArticleContents([], empty_section_contents())), element('root', children=[ element('article', children=[ _header_w_h('header'), ]) ])), ('empty with target', Article(StringText('header'), ArticleContents([], empty_section_contents()), target=CrossReferenceTargetTestImpl('target-name')), element('root', children=[ element('article', attributes={'id': 'target-name'}, children=[_header_w_h('header')]) ])), ('empty with tags', Article(StringText('header'), ArticleContents([], empty_section_contents()), tags={'label1', 'label2'}), element('root', children=[ element('article', attributes={'class': 'label1 label2'}, children=[_header_w_h('header')]) ])), ('empty with target and tags', Article(StringText('header'), ArticleContents([], empty_section_contents()), target=CrossReferenceTargetTestImpl('article-target'), tags={'label1', 'label2'}), element('root', children=[ element('article', attributes={ 'class': 'label1 label2', 'id': 'article-target' }, children=[_header_w_h('header')]) ])), ('single abstract paragraph', Article( StringText('header'), ArticleContents([para('abstract paragraph')], empty_section_contents())), element('root', children=[ element('article', children=[ element( 'header', children=[ element('h1', text='header'), element( 'p', text='abstract paragraph'), ], ) ]) ])) ] for test_case_name, article, expected in cases: with self.subTest(test_case_name): root = Element('root') environment = sut.Environment(0) # ACT # ret_val = TEST_RENDERER.render_section_item( environment, root, article) # ASSERT # assert_contents_and_that_last_child_is_returned( expected, root, ret_val, self)