Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
VTK
VTK
Commits
8aead085
Commit
8aead085
authored
Aug 24, 2015
by
David Gobbi
Browse files
Add tests for encoded strings in python.
parent
4f450d14
Changes
1
Hide whitespace changes
Inline
Side-by-side
Common/Core/Testing/Python/TestStrings.py
View file @
8aead085
...
...
@@ -17,13 +17,14 @@ import sys
import
vtk
from
vtk.test
import
Testing
if
sys
.
hexversion
>
0x03000000
:
if
sys
.
hexversion
>
=
0x03000000
:
cedilla
=
'Fran
\xe7
ois'
nocedilla
=
'Francois'
eightbit
=
'Francois'
.
encode
(
'ascii'
)
else
:
cedilla
=
unicode
(
'Fran
\xe7
ois'
,
'latin1'
)
nocedilla
=
unicode
(
'Francois'
)
eightbit
=
'Francois'
class
TestString
(
Testing
.
vtkTest
):
def
testPassByValue
(
self
):
...
...
@@ -62,18 +63,47 @@ class TestString(Testing.vtkTest):
u
=
a
.
GetValue
(
0
)
self
.
assertEqual
(
u
,
cedilla
)
def
testPass
String
AsUnicode
(
self
):
"""Pass
a
string when unicode is expected. Should fail."""
def
testPass
Bytes
AsUnicode
(
self
):
"""Pass
8-bit
string when unicode is expected. Should fail."""
a
=
vtk
.
vtkUnicodeStringArray
()
self
.
assertRaises
(
TypeError
,
a
.
InsertNextValue
,
(
'Francois'
,)
)
a
.
InsertNextValue
,
eightbit
)
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
.
InsertNextValue
(
nocedilla
)
s
=
a
.
GetValue
(
0
)
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__"
:
Testing
.
main
([(
TestString
,
'test'
)])
David Gobbi
@dgobbi
mentioned in commit
dd9311c8
·
Aug 25, 2015
mentioned in commit
dd9311c8
mentioned in commit dd9311c873abd0b2ffdf4c8921e9deb45d73993f
Toggle commit list
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment