testwhitespace.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Copyright (C) 2007 Søren Roug, European Environment Agency
  4. #
  5. # This is free software. You may redistribute it under the terms
  6. # of the Apache license and the GNU General Public License Version
  7. # 2 or at your option any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public
  15. # License along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. #
  18. # Contributor(s):
  19. #
  20. import unittest, os
  21. import io
  22. import zipfile
  23. from odf import teletype
  24. from odf.opendocument import OpenDocumentText, load
  25. from odf import style, text
  26. from odf.text import P
  27. class TestWhite(unittest.TestCase):
  28. def test_create(self):
  29. """ Test that tabs and newlines are converted to elements """
  30. para = P()
  31. teletype.addTextToElement(para,
  32. u"The boy stood on the burning deck,\n" +
  33. u"\tHis feet\twere\t\tfull of blisters.\n" +
  34. u"The captain stood in\tthe public house\n" +
  35. u" With beer running down his whiskers. " );
  36. outfp = io.StringIO()
  37. para.toXml(1,outfp)
  38. self.assertEqual(u'''<text:p>The boy stood <text:s text:c="2"/>on the burning deck,<text:line-break/>''' +
  39. u'''<text:tab/>His feet<text:tab/>were<text:tab/><text:tab/>full of blisters.<text:line-break/>''' +
  40. u'''The captain <text:s text:c="1"/>stood in<text:tab/>the public house<text:line-break/>''' +
  41. u''' <text:s text:c="8"/>With beer running down his whiskers. <text:s text:c="2"/></text:p>''', outfp.getvalue())
  42. def test_extract(self):
  43. """ Convert a paragraph to plain text """
  44. poem_odt = os.path.join(
  45. os.path.dirname(__file__), u"examples", u"serious_poem.odt")
  46. d = load(poem_odt)
  47. allparas = d.getElementsByType(P)
  48. content = u"""<text:p text:style-name="Standard">The boy stood <text:s text:c="3"/>on the burning deck,<text:line-break/><text:tab/>Whence all<text:tab/>but<text:tab/><text:tab/>him had fled.<text:line-break/>The flames <text:s text:c="2"/>that lit<text:tab/>the battle's<text:tab/>wreck,<text:line-break/> <text:s text:c="11"/>Shone o'er him, round the dead. <text:s text:c="2"/></text:p>"""
  49. self.assertEqual(u"The boy stood on the burning deck,\n\tWhence all\tbut\t\thim had fled.\nThe flames that lit\tthe battle's\twreck,\n Shone o'er him, round the dead. ", teletype.extractText(allparas[0]))
  50. def test_extract_with_span(self):
  51. """ Extract a text with a bold/italic span """
  52. poem_odt = os.path.join(
  53. os.path.dirname(__file__), u"examples", u"simplestyles.odt")
  54. d = load(poem_odt)
  55. teletype.extractText(d.body)
  56. self.assertEqual(u'Plain textBoldItalicBold italicUnderlineUnderline italicUnderline bold italicKm2 - superscriptH2O - subscript', teletype.extractText(d.body))
  57. if __name__ == '__main__':
  58. unittest.main()