File: TestStrings.py

package info (click to toggle)
vtk 5.8.0-13
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 130,524 kB
  • sloc: cpp: 1,129,256; ansic: 708,203; tcl: 48,526; python: 20,875; xml: 6,779; yacc: 4,208; perl: 3,121; java: 2,788; lex: 931; sh: 660; asm: 471; makefile: 299
file content (89 lines) | stat: -rw-r--r-- 2,601 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
"""Test string and unicode support in VTK-Python

The following string features have to be tested for string and unicode
- Pass a string arg by value
- Pass a string arg by reference
- Return a string arg by value
- Return a string arg by reference

The following features are not supported
- Pointers to strings, arrays of strings
- Passing a string arg by reference and returning a value in it

Created on May 12, 2010 by David Gobbi
"""

import sys
import exceptions
import vtk
from vtk.test import Testing

unicode_support = False
try:
    unicode('hello')
    unicode_support = True
except:
    print "unicode not supported on this python installation"


class TestString(Testing.vtkTest):
    def testPassByValue(self):
        """Pass string by value... hard to find examples of this,
        because "const char *" methods shadow "vtkStdString" methods.
        """
        self.assertEqual('y', 'y')

    def testReturnByValue(self):
        """Return a string by value."""
        a = vtk.vtkArray.CreateArray(1, vtk.VTK_INT)
        a.Resize(1,1)
        a.SetDimensionLabel(0, 'x')
        s = a.GetDimensionLabel(0)
        self.assertEqual(s, 'x')

    def testPassByReference(self):
        """Pass a string by reference."""
        a = vtk.vtkArray.CreateArray(0, vtk.VTK_STRING)
        a.SetName("myarray")
        s = a.GetName()
        self.assertEqual(s, "myarray")

    def testReturnByReference(self):
        """Return a string by reference."""
        a = vtk.vtkStringArray()
        s = "hello"
        a.InsertNextValue(s)
        t = a.GetValue(0)
        self.assertEqual(t, s)

    def testPassAndReturnUnicodeByReference(self):
        """Pass a unicode string by const reference"""
        if not unicode_support:
            return

        a = vtk.vtkUnicodeStringArray()
        a.InsertNextValue(u'Fran\xe7ois')
        u = a.GetValue(0)
        self.assertEqual(u, u'Fran\xe7ois')

    def testPassStringAsUnicode(self):
        """Pass a string when unicode is expected.  Should fail."""
        if not unicode_support:
            return

        a = vtk.vtkUnicodeStringArray()
        self.assertRaises(exceptions.TypeError,
                          a.InsertNextValue, ('Francois',))

    def testPassUnicodeAsString(self):
        """Pass a unicode where a string is expected.  Should succeed."""
        if not unicode_support:
            return

        a = vtk.vtkStringArray()
        a.InsertNextValue(u'Francois')
        s = a.GetValue(0)
        self.assertEqual(s, 'Francois')

if __name__ == "__main__":
    Testing.main([(TestString, 'test')])