def test_append_plain_text_guess_utf8(self): txt = 'A test,\n \twith \n\n some é and \t and 5 spaces.' para = Paragraph() para.append_plain_text(txt) expected = ('<text:p>A test,<text:line-break/> <text:s text:c="2"/>' '<text:tab/>with <text:line-break/><text:line-break/> ' 'some é and <text:tab/> and <text:s text:c="4"/>' '5 spaces.</text:p>') self.assertEqual(para.serialize(), expected)
def test_clone_different_unchanged_2(self): document = self.document s_orig = document.body.serialize() c = document.clone c.body.append(Paragraph("some text")) s_clone1 = c.body.serialize() document.body.append(Paragraph("new text")) s_clone2 = c.body.serialize() self.assertEqual(s_clone1, s_clone2)
def test_append_plain_text_guess_iso(self): txt = "A test,\n \twith \n\n some \xe9 and \t and 5 spaces." para = Paragraph() para.append_plain_text(txt) expected = ('<text:p>A test,<text:line-break/> <text:s text:c="2"/>' "<text:tab/>with <text:line-break/><text:line-break/> " 'some é and <text:tab/> and <text:s text:c="4"/>' "5 spaces.</text:p>") self.assertEqual(para.serialize(), expected)
def test_text(self): text = "Le Père Noël a une moustache rouge." paragraph = Paragraph(text) paragraph.set_span("highlight", regex="rouge") expected = ("<text:p>Le Père Noël a une moustache " "<text:span " 'text:style-name="highlight">rouge</text:span>.' "</text:p>") self.assertEqual(paragraph.serialize(), expected)
def test_offset(self): text = "Le Père Noël a une moustache rouge." paragraph = Paragraph(text) paragraph.set_span("highlight", offset=text.index("moustache")) expected = ("<text:p>Le Père Noël a une " '<text:span text:style-name="highlight">moustache ' "rouge.</text:span>" "</text:p>") self.assertEqual(paragraph.serialize(), expected)
def test_offset_length(self): text = "Le Père Noël a une moustache rouge." paragraph = Paragraph(text) paragraph.set_span("highlight", offset=text.index("moustache"), length=len("moustache")) expected = ('<text:p>Le Père Noël a une ' '<text:span text:style-name="highlight">moustache' '</text:span> rouge.' '</text:p>') self.assertEqual(paragraph.serialize(), expected)
def test_set_bookmark_with_limits(self): paragraph = Paragraph("aa bb bb aa") paragraph.set_bookmark("bookmark", position=(6, 8)) expected = ( "<text:p>aa bb " '<text:bookmark-start text:name="bookmark"/>' "bb" '<text:bookmark-end text:name="bookmark"/>' " aa" "</text:p>" ) self.assertEqual(paragraph.serialize(), expected)
def test_text_several(self): text = "Le Père rouge a une moustache rouge." paragraph = Paragraph(text) paragraph.set_span("highlight", regex="rouge") expected = ('<text:p>Le Père ' '<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(self): note = Note(note_id='note1', citation="1", body="élément pertubateur") paragraph = Paragraph("Un paragraphe") paragraph.insert_note(note, after="para") expected = ('<text:p>Un para' '<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>' 'graphe</text:p>') self.assertEqual(paragraph.serialize(), expected)
def test_insert_annotation(self): text = "It's like you're in a cave." creator = "Plato" date = datetime(2009, 8, 19) annotation = Annotation(text, creator=creator, date=date) paragraph = Paragraph("Un paragraphe") paragraph.insert_annotation(annotation, after="para") expected = ('<text:p>Un para' '<office:annotation office:name="__Fieldmark__lpod_1">' '<text:p>It\'s like you\'re in a cave.</text:p>' '<dc:creator>Plato</dc:creator>' '<dc:date>2009-08-19T00:00:00</dc:date>' '</office:annotation>' 'graphe</text:p>') self.assertEqual(paragraph.serialize(), expected)
def test_create_note1(self): # With an odf_element note_body = Paragraph('a footnote', style='Standard') note = Note(note_id='note1', citation='1', body=note_body) expected = self.expected.replace( '<text:p>', '<text:p text:style-name="Standard">') self.assertEqual(note.serialize(), expected)
def test_clone_different_unchanged_1(self): document = self.document s_orig = document.body.serialize() c = document.clone c.body.append(Paragraph("some text")) s_after = document.body.serialize() self.assertEqual(s_after, s_orig)
def test_set_bookmark_with_after_without_position(self): paragraph = Paragraph("aa bb aa aa cc aa dd") paragraph.set_span(style="style", regex="bb aa aa") paragraph.set_span(style="style", regex="dd") paragraph.set_bookmark("bookmark", after="aa") expected = ( '<text:p>aa<text:bookmark text:name="bookmark"/> ' '<text:span text:style-name="style">bb aa aa' "</text:span>" ' cc aa <text:span text:style-name="style">dd</text:span>' "</text:p>" ) self.assertEqual(paragraph.serialize(), expected)
def test_create_note1(self): # With an odf_element note_body = Paragraph("a footnote", style="Standard") note = Note(note_id="note1", citation="1", body=note_body) expected = self.expected.replace( "<text:p>", '<text:p text:style-name="Standard">' ) self.assertEqual(note.serialize(), expected)
def test_get_draw_orphans_connector(self): body = self.content.body page = body.get_draw_page() orphan_connector = ConnectorShape() orphan_connector.append(Paragraph('Orphan c')) body.append(orphan_connector) connectors = body.get_orphan_draw_connectors() self.assertEqual(len(connectors), 1)
def test_set_bookmark_with_role(self): paragraph = Paragraph("aa") paragraph.set_bookmark("bookmark", role="start") paragraph.set_bookmark("bookmark", role="end", position=-1) expected = ( "<text:p>" '<text:bookmark-start text:name="bookmark"/>' "aa" '<text:bookmark-end text:name="bookmark"/>' "</text:p>" ) self.assertEqual(paragraph.serialize(), expected)
def test_insert_paragraph(self): body = self.body.clone paragraph = Paragraph("An inserted test", style="Text_20_body") body.append(paragraph) last_paragraph = body.get_paragraphs()[-1] self.assertEqual(last_paragraph.text, "An inserted test")
def test_set_bookmark_with_position(self): paragraph = Paragraph("aa bb aa aa cc aa dd") paragraph.set_span(style="style", regex="bb aa aa") paragraph.set_span(style="style", regex="dd") paragraph.set_bookmark("bookmark1", position=0) paragraph.set_bookmark("bookmark2", position=2) paragraph.set_bookmark("bookmark3", position=len("aa bb aa aa cc aa dd")) expected = ( '<text:p><text:bookmark text:name="bookmark1"/>aa' '<text:bookmark text:name="bookmark2"/> ' '<text:span text:style-name="style">bb aa aa</text:span>' ' cc aa <text:span text:style-name="style">dd' '<text:bookmark text:name="bookmark3"/></text:span>' "</text:p>" ) self.assertEqual(paragraph.serialize(), expected)
def test_insert_paragraph(self): body = self.body.clone paragraph = Paragraph('An inserted test', style='Text_20_body') body.append(paragraph) last_paragraph = body.get_paragraphs()[-1] self.assertEqual(last_paragraph.text, 'An inserted test')
def test_set_bookmark_with_end(self): paragraph = Paragraph("aa bb aa aa cc aa dd") paragraph.set_span(style="style", regex="bb aa aa") paragraph.set_span(style="style", regex="dd") paragraph.set_bookmark("bookmark1", after="cc", position=-1) paragraph.set_bookmark("bookmark2", position=-1) expected = ( "<text:p>aa " '<text:span text:style-name="style">' "bb aa aa" "</text:span>" ' cc<text:bookmark text:name="bookmark1"/> aa ' '<text:span text:style-name="style">dd</text:span>' '<text:bookmark text:name="bookmark2"/>' "</text:p>" ) self.assertEqual(paragraph.serialize(), expected)