Commit 8aead085 authored by David Gobbi's avatar David Gobbi
Browse files

Add tests for encoded strings in python.

parent 4f450d14
...@@ -17,13 +17,14 @@ import sys ...@@ -17,13 +17,14 @@ import sys
import vtk import vtk
from vtk.test import Testing from vtk.test import Testing
if sys.hexversion > 0x03000000: if sys.hexversion >= 0x03000000:
cedilla = 'Fran\xe7ois' cedilla = 'Fran\xe7ois'
nocedilla = 'Francois' nocedilla = 'Francois'
eightbit = 'Francois'.encode('ascii')
else: else:
cedilla = unicode('Fran\xe7ois', 'latin1') cedilla = unicode('Fran\xe7ois', 'latin1')
nocedilla = unicode('Francois') nocedilla = unicode('Francois')
eightbit = 'Francois'
class TestString(Testing.vtkTest): class TestString(Testing.vtkTest):
def testPassByValue(self): def testPassByValue(self):
...@@ -62,18 +63,47 @@ class TestString(Testing.vtkTest): ...@@ -62,18 +63,47 @@ class TestString(Testing.vtkTest):
u = a.GetValue(0) u = a.GetValue(0)
self.assertEqual(u, cedilla) self.assertEqual(u, cedilla)
def testPassStringAsUnicode(self): def testPassBytesAsUnicode(self):
"""Pass a string when unicode is expected. Should fail.""" """Pass 8-bit string when unicode is expected. Should fail."""
a = vtk.vtkUnicodeStringArray() a = vtk.vtkUnicodeStringArray()
self.assertRaises(TypeError, self.assertRaises(TypeError,
a.InsertNextValue, ('Francois',)) a.InsertNextValue, eightbit)
def testPassUnicodeAsString(self): def testPassUnicodeAsString(self):
"""Pass a unicode where a string is expected. Should succeed.""" """Pass unicode where string is expected. Should succeed."""
a = vtk.vtkStringArray() a = vtk.vtkStringArray()
a.InsertNextValue(nocedilla) a.InsertNextValue(nocedilla)
s = a.GetValue(0) s = a.GetValue(0)
self.assertEqual(s, 'Francois') self.assertEqual(s, 'Francois')
def testPassBytesAsString(self):
"""Pass 8-bit string where string is expected. Should succeed."""
a = vtk.vtkStringArray()
a.InsertNextValue(eightbit)
s = a.GetValue(0)
self.assertEqual(s, 'Francois')
def testPassEncodedString(self):
"""Pass encoded 8-bit strings."""
a = vtk.vtkStringArray()
# latin1 encoded string will be returned as "bytes", which is
# just a normal str object in Python 2
encoded = cedilla.encode('latin1')
a.InsertNextValue(encoded)
result = a.GetValue(0)
self.assertEqual(type(result), bytes)
self.assertEqual(result, encoded)
# utf-8 encoded string will be returned as "str", which is
# actually unicode in Python 3
a = vtk.vtkStringArray()
encoded = cedilla.encode('utf-8')
a.InsertNextValue(encoded)
result = a.GetValue(0)
self.assertEqual(type(result), str)
if sys.hexversion >= 0x03000000:
self.assertEqual(result.encode('utf-8'), encoded)
else:
self.assertEqual(result, encoded)
if __name__ == "__main__": if __name__ == "__main__":
Testing.main([(TestString, 'test')]) Testing.main([(TestString, 'test')])
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment