def unequal_attributes_cases() -> List[NEA[ET.Element, ET.Element]]: cases_positive_direction = [ NEA( 'number of attributes / one empty', {}, {'a': 'A'} ), NEA( 'number of attributes / no one empty', {'a': 'A'}, {'a': 'A', 'b': 'A'}, ), NEA( 'non-empty set of attributes / diff key', {'a': 'value'}, {'b': 'value'}, ), NEA( 'non-empty set of attributes / diff value', {'a': 'value 1'}, {'a': 'value 2'}, ), ] tag = 'attr-test' return _with_positive_and_negative_direction([ NEA(c.name, element(tag, c.expected), element(tag, c.actual), ) for c in cases_positive_direction ])
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_not_equals__num_children(self): child_name = 'child-name' cases__pos_dir = [ NEA('expected: 0', [], [element(child_name)], ), NEA('expected: 1', [element(child_name)], [element(child_name), element(child_name)], ), NEA('expected: 2', [element(child_name), element(child_name)], [element(child_name), element(child_name), element(child_name), element(child_name)], ), ] for children_level_case in CHILDREN_LEVEL_CASES: cases = _with_positive_and_negative_direction([ NEA(c.name, children_level_case.value(c.expected), children_level_case.value(c.actual), ) for c in cases__pos_dir ]) for case in cases: with self.subTest(children_level_case=children_level_case.name, children_sequence=case.name): with self.assertRaises(TestException): sut.equals(case.expected).apply_without_message(self.put, case.actual)
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_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_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 unequal_text_cases() -> List[NEA[ET.Element, ET.Element]]: return [ NEA('unequal-text', element('expected'), element('actual'), ) ]
def unequal_tail_cases() -> List[NEA[ET.Element, ET.Element]]: tag = 'the-tag' return [ NEA('unequal-tail', element(tag, tail='a'), element(tag, tail='ab'), ) ]
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_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') 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 _equivalent_child_sequence_variants() -> List[NameAndValue[Callable[[ET.Element], Sequence[ET.Element]]]]: return [ NameAndValue( 'single child', lambda child: [child], ), NameAndValue( 'preceded by single element', lambda child: [element('preceding'), child], ), NameAndValue( 'followed by single element', lambda child: [child, element('following')], ), ]
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_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 equal_element_cases__wo_tail() -> List[NameAndValue[ET.Element]]: tag = 'the-tag' return [ NameAndValue( 'text={text}, attrs={attrs}'.format( text=repr(text), attrs=attributes, ), element(tag, text=text, attributes=attributes) ) for text in [None, 'some text'] for attributes in [{}, {'key': 'value'}] ]
def test_anchor(self): _cases(self, [ Case('anchor without tagged content', text=AnchorText(StringText('anchor text'), _CrossReferenceString('the-target')) , expected=element('span', attributes={'id': "the-target"}, text='anchor text'), ), Case('anchor with tagged string text', text=AnchorText(StringText('anchor text', tags={'1st', '2nd'}), _CrossReferenceString('the-target')) , expected=element('span', attributes={'id': 'the-target'}, children=[ element('span', attributes={'class': '1st 2nd'}, text='anchor text') ]), ), Case('anchor with tagged cross-ref text', text=AnchorText(CrossReferenceText(StringText('title text'), _CrossReferenceString('target-of-anchored'), tags={'tag1', 'tag2'}), _CrossReferenceString('target-of-anchor')) , expected=element('span', attributes={'id': "target-of-anchor"}, children=[element('a', attributes={'class': "tag1 tag2", 'href': "#target-of-anchored"}, text='title text', )]) , ), ])
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)
def test_plain_string_text__inside(self): # ARRANGE # root_tag_name = 'root' source_text = 'source text' root_element_to_mutate = Element(root_tag_name) renderer = sut.TextRenderer(TargetRenderer()) text = StringText(source_text) # EXPECTATION # expected_element = element(root_tag_name, text=source_text) assertion = asrt_xml.equals(expected_element) # ACT # actual = renderer.apply(root_element_to_mutate, root_element_to_mutate, sut.Position.INSIDE, text) # ASSERT # assertion.apply_with_message(self, actual, 'element structure') self.assertIs(root_element_to_mutate, actual, 'returned element object')
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)
class _StringTargetRenderer(TargetRenderer): def apply(self, target: core.CrossReferenceTarget) -> str: 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 #
def _header_w_h(h_text: str, h: str = 'h1') -> Element: return element('header', children=[element(h, text=h_text)])
def test_tail__none_and_empty_str_are_equivalent(self): for case in self.EQUIVALENT_STR_CASES: with self.subTest(case.name): expected = element('tag', tail=case.expected) actual = element(expected.tag, tail=case.actual) sut.equals(expected).apply_without_message(self, actual)
def _mk_children_at_lvl_2(children: Sequence[ET.Element]) -> ET.Element: return element('root', children=[element('level-1', children=children)])
def _mk_children_at_lvl_1(children: Sequence[ET.Element]) -> ET.Element: return element('root', children=children)
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)