Manfred Moitzi avatar Manfred Moitzi committed 342e031

better Python 2&3 'none us ascii' testing routines

Comments (0)

Files changed (4)

         return element
 
     def tostring(self):
-        """ Get the XML representation as `string`.
+        """ Get the XML representation as unicode `string`.
 
-        :return: ``utf-8`` encoded XML string of this object and all its subelements
+        :return: unicode XML string of this object and all its subelements
 
         """
         xml = self.get_xml()
         xml_utf8_str = etree.tostring(xml, encoding='utf-8')
-        if PYTHON3: # return an unicode string
-            return xml_utf8_str.decode()
-        else: #return the utf8 encoded string
-            return xml_utf8_str
+        return xml_utf8_str.decode('utf-8')
 
     def get_xml(self):
         """ Get the XML representation as `ElementTree` object.

svgwrite/drawing.py

         self._stylesheets.append( (href, title, alternate, media) )
 
     def write(self, fileobj):
-        """ Write the ``utf-8`` encoded XML string to *fileobj*.
+        """ Write XML string to **fileobj**.
 
         :param fileobj: a *file-like* object
 
         fileobj.write(self.tostring())
 
     def save(self):
-        """ Write the ``utf-8`` encoded XML string to :attr:`filename`. """
+        """ Write the XML string to **filename**. """
         if PYTHON3:
             fileobj = open(self.filename, mode='w', encoding='utf-8')
         else:
         fileobj.close()
 
     def saveas(self, filename):
-        """ Write the ``utf-8`` encoded XML string to *filename*.
+        """ Write the XML string to **filename**.
 
         :param string filename: filesystem filename valid for :func:`open`
         """

tests/test_drawing.py

 import os
 
 import unittest
-from svgwrite.utils import PYTHON3
+from svgwrite.utils import PYTHON3, to_unicode
 
 if PYTHON3:
     from io import StringIO
         self.assertTrue(os.path.exists(fn))
         os.remove(fn)
 
-    def test_non_us_ascii_cahrs(self):
+    def test_non_us_ascii_chars(self):
         dwg = Drawing()
         dwg.set_desc('öäü')
         f = StringIO()
         dwg.write(f)
         result = f.getvalue()
         f.close()
-        if PYTHON3: # result is an unicode string!
-            self.assertEqual(result, '<?xml version="1.0" encoding="utf-8" ?>\n' \
-                '<svg baseProfile="full" height="100%" version="1.1" width="100%" '\
-                'xmlns="http://www.w3.org/2000/svg" xmlns:ev="http://www.w3.org/2001/xml-events" '\
-                'xmlns:xlink="http://www.w3.org/1999/xlink">'
-                '<title>öäü</title><defs /></svg>')
-        else:
-            self.assertEqual(result, '<?xml version="1.0" encoding="utf-8" ?>\n' \
-                '<svg baseProfile="full" height="100%" version="1.1" width="100%" '\
-                'xmlns="http://www.w3.org/2000/svg" xmlns:ev="http://www.w3.org/2001/xml-events" '\
-                'xmlns:xlink="http://www.w3.org/1999/xlink">'
-                '<title>\xc3\xb6\xc3\xa4\xc3\xbc</title><defs /></svg>')
+        self.assertEqual(result, to_unicode(
+            '<?xml version="1.0" encoding="utf-8" ?>\n' \
+            '<svg baseProfile="full" height="100%" version="1.1" width="100%" '\
+            'xmlns="http://www.w3.org/2000/svg" xmlns:ev="http://www.w3.org/2001/xml-events" '\
+            'xmlns:xlink="http://www.w3.org/1999/xlink">'
+            '<title>öäü</title><defs /></svg>'))
 
 class TestDrawingTinyProfile(unittest.TestCase):
     def test_empty_drawing(self):

tests/test_text.py

 import sys
 import unittest
 
-from svgwrite.utils import PYTHON3
+from svgwrite.utils import PYTHON3, to_unicode
 from svgwrite.text import TSpan, TRef, TextPath
 
+
 class TestTSpan(unittest.TestCase):
     def test_constructor(self):
         txt = TSpan('testtext')
 
     def test_non_us_ascii_chars(self):
         txt = TSpan('öäü')
-        if PYTHON3:
-            expected_result = '<tspan>öäü</tspan>'
-        else:
-            expected_result = '<tspan>\xc3\xb6\xc3\xa4\xc3\xbc</tspan>'
-        self.assertEqual(txt.tostring(), expected_result)
+        self.assertEqual(txt.tostring(), to_unicode('<tspan>öäü</tspan>'))
 
     def test_errors(self):
         # None for x, y, dx, dy, rotate is valid - willl be ignored
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.