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]')
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)
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)
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)
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]")