def test_insert_element_last_child(self): element = Element.from_tag( '<office:text><text:p/><text:p/></office:text>') child = Element.from_tag('<text:h/>') element.append(child) self.assertEqual( element.serialize(), '<office:text><text:p/><text:p/><text:h/></office:text>')
def test_insert_element_first_child(self): element = Element.from_tag( '<office:text><text:p/><text:p/></office:text>') child = Element.from_tag('<text:h/>') element.insert(child, FIRST_CHILD) self.assertEqual( element.serialize(), '<office:text><text:h/><text:p/><text:p/></office:text>')
def test_insert_element_prev_sibling(self): root = Element.from_tag( '<office:text><text:p/><text:p/></office:text>') element = root.get_elements('//text:p')[0] sibling = Element.from_tag('<text:h/>') element.insert(sibling, PREV_SIBLING) self.assertEqual( root.serialize(), '<office:text><text:h/><text:p/><text:p/></office:text>')
def test_append_element(self): element = Element.from_tag("text:p") element.append("f") element.append("oo1") element.append(Element.from_tag("text:line-break")) element.append("f") element.append("oo2") self.assertEqual(element.serialize(), "<text:p>foo1<text:line-break/>foo2</text:p>")
def test_insert_element_next_sibling(self): root = Element.from_tag( "<office:text><text:p/><text:p/></office:text>") element = root.get_elements("//text:p")[0] sibling = Element.from_tag("<text:h/>") element.insert(sibling, NEXT_SIBLING) self.assertEqual( root.serialize(), "<office:text><text:p/><text:h/><text:p/></office:text>")
def test_delete_self_5(self): element = Element.from_tag( '<text:p><tag>x</tag><text:span/>keep</text:p>') child = element.get_element('//text:span') child.delete() self.assertEqual(element.serialize(), '<text:p><tag>x</tag>keep</text:p>')
def test_insert_link_simple(self): paragraph = Element.from_tag('<text:p>toto tata titi</text:p>') paragraph.insert_link("http://", from_="tata", to="tata") expected = ('<text:p>toto ' '<text:a xlink:href="http://">tata</text:a> ' 'titi</text:p>') self.assertEqual(paragraph.serialize(), expected)
def test_delete_self_4(self): element = Element.from_tag( "<text:p><tag>x</tag>before<text:span/>keep</text:p>") child = element.get_element("//text:span") child.delete() self.assertEqual(element.serialize(), "<text:p><tag>x</tag>beforekeep</text:p>")
def test_tail(self): data = ("<text:p>Le Père Noël a une " "<text:span>moustache</text:span> rouge.</text:p>") paragraph = Element.from_tag(data) paragraph.set_span("highlight", regex="rouge") expected = ("<text:p>Le Père Noël a une " "<text:span>moustache</text:span> " "<text:span " 'text:style-name="highlight">rouge</text:span>.' "</text:p>") self.assertEqual(paragraph.serialize(), expected)
def test_insert_link_medium(self): paragraph = Element.from_tag( "<text:p><text:span>toto</text:span> " "tata titi</text:p>" ) paragraph.insert_link("http://", from_="tata", to="tata") expected = ( "<text:p><text:span>toto</text:span> " '<text:a xlink:href="http://">tata</text:a> ' "titi</text:p>" ) self.assertEqual(paragraph.serialize(), expected)
def test_insert_link_horrible(self): paragraph = Element.from_tag('<text:p>toto ' '<text:span>tata titi</text:span>' ' tutu </text:p>') paragraph.insert_link("http://", from_="titi", to="tutu") expected = ('<text:p>toto <text:span>tata </text:span>' '<text:a xlink:href="http://">' '<text:span>titi</text:span> tutu' '</text:a> ' '</text:p>') self.assertEqual(paragraph.serialize(), expected)
def test_tail_several(self): data = ("<text:p>Le <text:span>Père</text:span> rouge a une " "moustache rouge.</text:p>") paragraph = Element.from_tag(data) paragraph.set_span("highlight", regex="rouge") expected = ('<text:p>Le <text:span>Père</text:span> ' '<text:span ' 'text:style-name="highlight">rouge</text:span> ' 'a une moustache ' '<text:span ' 'text:style-name="highlight">rouge</text:span>.' '</text:p>') self.assertEqual(paragraph.serialize(), expected)
def test_insert_note_after_span(self): note = Note(note_id='note1', citation="1", body="élément pertubateur") data = "<text:p>Un <text:span>para</text:span>graphe.</text:p>" paragraph = Element.from_tag(data) paragraph.insert_note(note, after="graphe") expected = ('<text:p>Un <text:span>para</text:span>graphe' '<text:note text:note-class="footnote" ' 'text:id="note1">' '<text:note-citation>1</text:note-citation>' '<text:note-body>' '<text:p>élément pertubateur</text:p>' '</text:note-body>' '</text:note>' '.</text:p>') self.assertEqual(paragraph.serialize(), expected)
def test_insert_link_hideous(self): paragraph = Element.from_tag( "<text:p>toto " "<text:span>tata titi</text:span>" " <text:span>tutu tyty</text:span>" "</text:p>" ) paragraph.insert_link("http://", from_="titi", to="tutu") expected = ( "<text:p>toto <text:span>tata </text:span>" '<text:a xlink:href="http://">' "<text:span>titi</text:span> " "<text:span>tutu</text:span>" "</text:a>" "<text:span> tyty</text:span>" "</text:p>" ) self.assertEqual(paragraph.serialize(), expected)
def test_qname(self): element = Element.from_tag("text:p") self.assertEqual(element.serialize(), "<text:p/>")
def test_delete_child(self): element = Element.from_tag('<text:p><text:span/></text:p>') child = element.get_element('//text:span') element.delete(child) self.assertEqual(element.serialize(), '<text:p/>')
def test_get_root(self): element = Element.from_tag('<text:p><text:span/></text:p>') root = element.root self.assertTrue(root.parent is None)
def test_pretty_serialize(self): # With pretty = True element = Element.from_tag("<root><a>spam</a><b/></root>") serialized = element.serialize(pretty=True) expected = "<root>\n" " <a>spam</a>\n" " <b/>\n" "</root>\n" self.assertEqual(serialized, expected)
def test_qname(self): element = Element.from_tag('text:p') self.assertEqual(element.serialize(), '<text:p/>')
def test_get_parent(self): element = Element.from_tag("<text:p><text:span/></text:p>") child = element.get_element("//text:span") parent = child.parent self.assertEqual(parent.tag, "text:p")
def test_simple(self): data = '<p>Template Element</p>' element = Element.from_tag(data) self.assertEqual(element.serialize(), data)
def test_delete_child(self): element = Element.from_tag("<text:p><text:span/></text:p>") child = element.get_element("//text:span") element.delete(child) self.assertEqual(element.serialize(), "<text:p/>")
def test_insert_element_bad_position(self): element = Element.from_tag('text:p') child = Element.from_tag('text:span') self.assertRaises(ValueError, element.insert, child, 999)
def test_insert_element_bad_element(self): element = Element.from_tag('text:p') self.assertRaises(AttributeError, element.insert, '<text:span/>', FIRST_CHILD)
def setUp(self): element = Element.from_tag("<text:p>text<text:span/>tail</text:p>") self.results = element.xpath("descendant::text()")
def test_register(self): register_element_class(self.dummy_element) element = Element.from_tag('office:dummy1') self.assertTrue(type(element) is self.dummy_element)
def test_unregistered(self): element = Element.from_tag('office:dummy2') self.assertTrue(type(element) is Element) self.assertFalse(isinstance(element, self.dummy_element))
def test_get_parent(self): element = Element.from_tag('<text:p><text:span/></text:p>') child = element.get_element('//text:span') parent = child.parent self.assertEqual(parent.tag, 'text:p')
def test_namespace(self): data = '<text:p>Template Element</text:p>' element = Element.from_tag(data) self.assertEqual(element.serialize(), data)
def test_delete_root(self): element = Element.from_tag('<text:p><text:span/></text:p>') root = element.root self.assertRaises(ValueError, root.delete)