Exemple #1
0
 def setUp(self):
     container = Container('samples/example.odt')
     self.container = container
     content_part = XmlPart(ODF_CONTENT, container)
     self.annotation_element = (
         content_part.get_element('//office:annotation[1]'))
     self.paragraph_element = content_part.get_element('//text:p[1]')
Exemple #2
0
 def test_serialize(self):
     container = self.container
     content_bytes = container.get_part(ODF_CONTENT)
     content_part = XmlPart(ODF_CONTENT, container)
     # differences with lxml
     serialized = content_part.serialize().replace(b"'", b"'")
     self.assertEqual(content_bytes, serialized)
Exemple #3
0
class ReplaceTestCase(TestCase):
    def setUp(self):
        self.container = Container('samples/span_style.odt')
        self.content = XmlPart(ODF_CONTENT, self.container)
        self.paragraph = self.content.get_element('//text:p')
        self.span = self.paragraph.get_element('//text:span')

    def test_count(self):
        paragraph = self.paragraph
        expected = paragraph.serialize()
        count = paragraph.replace("ou")
        self.assertEqual(count, 2)
        # Ensure the orignal was not altered
        self.assertEqual(paragraph.serialize(), expected)

    def test_replace(self):
        paragraph = self.paragraph
        clone = paragraph.clone
        count = clone.replace("moustache", "barbe")
        self.assertEqual(count, 1)
        expected = "Le Père Noël a une barbe rouge."
        self.assertEqual(clone.text_recursive, expected)
        # Ensure the orignal was not altered
        self.assertNotEqual(clone.serialize(), paragraph.serialize())

    def test_across_span(self):
        paragraph = self.paragraph
        count = paragraph.replace("moustache rouge")
        self.assertEqual(count, 0)

    def test_missing(self):
        paragraph = self.paragraph
        count = paragraph.replace("barbe")
        self.assertEqual(count, 0)
Exemple #4
0
class MatchTestCase(TestCase):
    def setUp(self):
        self.container = Container('samples/span_style.odt')
        self.content = XmlPart(ODF_CONTENT, self.container)
        self.paragraph = self.content.get_element('//text:p')
        self.span = self.paragraph.get_element('//text:span')

    def test_match_paragraph(self):
        """Match text in a paragraph.
        """
        match = self.paragraph.match('ère')
        return self.assertTrue(match)

    def test_match_span(self):
        """Match text in a span.
        """
        match = self.span.match('moust')
        return self.assertTrue(match)

    def test_match_inner_span(self):
        """Match text in a span from the parent paragraph.
        """
        match = self.paragraph.match('roug')
        return self.assertTrue(match)

    def test_simple_regex(self):
        """Match a simple regex.
        """
        match = self.paragraph.match('che roug')
        return self.assertTrue(match)

    def test_intermediate_regex(self):
        """Match an intermediate regex.
        """
        match = self.paragraph.match('moustache (blanche|rouge)')
        return self.assertTrue(match)

    def test_complex_regex(self):
        """Match a complex regex.
        """
        # The (?<=...) part is pointless as we don't try to get groups from
        # a MatchObject. However, it's a valid regex expression.
        match = self.paragraph.match(r'(?<=m)(ou)\w+(che) (blan\2|r\1ge)')
        return self.assertTrue(match)

    def test_compiled_regex(self):
        """Match with a compiled pattern.
        """
        pattern = compile(r'moustache')
        match = self.paragraph.match(pattern)
        return self.assertTrue(match)

    def test_failing_match(self):
        """Test a regex that doesn't match.
        """
        match = self.paragraph.match('Le Père moustache')
        return self.assertFalse(match)
Exemple #5
0
 def test_delete(self):
     container = self.container
     content = XmlPart(ODF_CONTENT, container)
     paragraphs = content.get_elements("//text:p")
     for paragraph in paragraphs:
         content.delete_element(paragraph)
     serialized = content.serialize()
     self.assertEqual(serialized.count(b"<text:p"), 0)
Exemple #6
0
class SearchTestCase(TestCase):
    def setUp(self):
        self.container = Container("samples/span_style.odt")
        self.content = XmlPart(ODF_CONTENT, self.container)
        self.paragraph = self.content.get_element("//text:p")
        self.span = self.paragraph.get_element("//text:span")

    def test_search_paragraph(self):
        """Search text in a paragraph."""
        pos = self.paragraph.search("ère")
        return self.assertEqual(pos, 4)

    def test_match_span(self):
        """Search text in a span."""
        pos = self.span.search("moust")
        return self.assertEqual(pos, 0)

    def test_match_inner_span(self):
        """Search text in a span from the parent paragraph."""
        pos = self.paragraph.search("roug")
        return self.assertEqual(pos, 29)

    def test_simple_regex(self):
        """Search a simple regex."""
        pos = self.paragraph.search("che roug")
        return self.assertEqual(pos, 25)

    def test_intermediate_regex(self):
        """Search an intermediate regex."""
        pos = self.paragraph.search("moustache (blanche|rouge)")
        return self.assertEqual(pos, 19)

    def test_complex_regex(self):
        """Search a complex regex."""
        # The (?<=...) part is pointless as we don't try to get groups from
        # a MatchObject. However, it's a valid regex expression.
        pos = self.paragraph.search(r"(?<=m)(ou)\w+(che) (blan\2|r\1ge)")
        return self.assertEqual(pos, 20)

    def test_compiled_regex(self):
        """Search with a compiled pattern."""
        pattern = compile(r"moustache")
        pos = self.paragraph.search(pattern)
        return self.assertEqual(pos, 19)

    def test_failing_match(self):
        """Test a regex that doesn't match."""
        pos = self.paragraph.search("Le Père moustache")
        return self.assertTrue(pos is None)
Exemple #7
0
 def test_root(self):
     content = XmlPart(ODF_CONTENT, self.container)
     root = content.root
     self.assertTrue(isinstance(root, Element))
     self.assertEqual(root.tag, "office:document-content")
     self.assertNotEqual(content._XmlPart__root, None)
Exemple #8
0
 def test_tree(self):
     # Testing a private but important method
     content = XmlPart(ODF_CONTENT, self.container)
     tree = content._XmlPart__get_tree()
     self.assertTrue(isinstance(tree, _ElementTree))
     self.assertNotEqual(content._XmlPart__tree, None)
Exemple #9
0
 def test_get_element_list(self):
     content_part = XmlPart(ODF_CONTENT, self.container)
     elements = content_part.get_elements("//text:p")
     # The annotation paragraph is counted
     self.assertEqual(len(elements), 8)
Exemple #10
0
 def setUp(self):
     self.container = container = Container('samples/example.odt')
     self.content_part = content_part = XmlPart(ODF_CONTENT, container)
     self.paragraph_element = content_part.get_element('//text:p[1]')
     query = '//style:style[@style:family="paragraph"][1]'
     self.style_element = content_part.get_element(query)
Exemple #11
0
 def setUp(self):
     self.container = Container('samples/span_style.odt')
     self.content = XmlPart(ODF_CONTENT, self.container)
     self.paragraph = self.content.get_element('//text:p')
     self.span = self.paragraph.get_element('//text:span')
Exemple #12
0
 def setUp(self):
     container = Container("samples/example.odt")
     self.container = container
     content_part = XmlPart(ODF_CONTENT, container)
     self.paragraph_element = content_part.get_element("//text:p[1]")