testunicode.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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, sys
  21. from odf.opendocument import OpenDocumentText
  22. from odf import style, text
  23. from odf.text import P, H
  24. from odf.element import IllegalChild
  25. class TestUnicode(unittest.TestCase):
  26. def setUp(self):
  27. self.textdoc = OpenDocumentText()
  28. self.saved = False
  29. def tearDown(self):
  30. if self.saved:
  31. os.unlink("TEST.odt")
  32. def assertContains(self, stack, needle):
  33. self.assertNotEqual(-1, stack.find(needle))
  34. def assertNotContains(self, stack, needle):
  35. self.assertEqual(-1, stack.find(needle))
  36. @unittest.skipIf(sys.version_info[0] != 2,
  37. "For Python3, unicode strings are type 'str'.")
  38. def test_xstyle(self):
  39. self.assertRaises(UnicodeDecodeError, style.Style, name="X✗", family="paragraph")
  40. xstyle = style.Style(name=u"X✗", family=u"paragraph")
  41. pp = style.ParagraphProperties(padding=u"0.2cm")
  42. pp.setAttribute(u"backgroundcolor", u"rød")
  43. xstyle.addElement(pp)
  44. self.textdoc.styles.addElement(xstyle)
  45. self.textdoc.save(u"TEST.odt")
  46. self.saved = True
  47. def test_text(self):
  48. p = P(text=u"Æblegrød")
  49. p.addText(u' Blåbærgrød')
  50. self.textdoc.text.addElement(p)
  51. self.textdoc.save(u"TEST.odt")
  52. self.saved = True
  53. def test_contenttext(self):
  54. p = H(outlinelevel=1,text=u"Æblegrød")
  55. p.addText(u' Blåbærgrød')
  56. self.textdoc.text.addElement(p)
  57. c = self.textdoc.contentxml() # contentxml is supposed to yield a bytes
  58. self.assertContains(c, b'<office:body><office:text><text:h text:outline-level="1">\xc3\x86blegr\xc3\xb8d Bl\xc3\xa5b\xc3\xa6rgr\xc3\xb8d</text:h></office:text></office:body>')
  59. self.assertContains(c, b'xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0"')
  60. self.assertContains(c, b'<office:automatic-styles/>')
  61. def test_illegaltext(self):
  62. p = H(outlinelevel=1,text=u"Spot \u001e the")
  63. p.addText(u' d\u00a3libe\u0000rate \ud801 mistakes\U0002fffe')
  64. self.textdoc.text.addElement(p)
  65. c = self.textdoc.contentxml() # contentxml is supposed to yield a bytes
  66. # unicode replacement char \UFFFD === \xef\xbf\xbd in UTF-8
  67. self.assertContains(c, b'<office:body><office:text><text:h text:outline-level="1">Spot \xef\xbf\xbd the d\xc2\xa3libe\xef\xbf\xbdrate \xef\xbf\xbd mistakes\xef\xbf\xbd</text:h></office:text></office:body>')
  68. self.assertContains(c, b'xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0"')
  69. self.assertContains(c, b'<office:automatic-styles/>')
  70. if __name__ == '__main__':
  71. if sys.version_info[0]==2:
  72. unittest.main()
  73. else:
  74. sys.stderr.write("\n----------------------------------------------------------------------\nRan no test\n\n")
  75. sys.stderr.write("For Python3, unicode strings are type 'str'.\n")
  76. sys.stderr.write("OK\n")