File: test_string.py

package info (click to toggle)
wxpython4.0 4.2.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 232,540 kB
  • sloc: cpp: 958,937; python: 233,059; ansic: 150,441; makefile: 51,662; sh: 8,687; perl: 1,563; javascript: 584; php: 326; xml: 200
file content (91 lines) | stat: -rw-r--r-- 3,547 bytes parent folder | download | duplicates (3)
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
90
91
import unittest
import wx
import six
from unittests import wtc

#---------------------------------------------------------------------------



class String(unittest.TestCase):

    if hasattr(wx, 'testStringTypemap'):
        if not six.PY3:
            def test_StringTypemapsPy2(self):
                utf  = '\xc3\xa9l\xc3\xa9phant'     # utf-8 string
                uni  = utf.decode('utf-8')          # convert to unicode
                iso  = uni.encode('iso-8859-1')     # make a string with a different encoding


                # wx.testStringTypemap() will accept a parameter that
                # is a Unicode object or an 'ascii' or 'utf-8' string object,
                # which will then be converted to a wxString. The return
                # value is a Unicode object that has been converted from a
                # wxString that is a copy of the wxString created for the
                # parameter.

                # ascii
                result = wx.testStringTypemap('hello')
                self.assertTrue(type(result) == unicode)
                self.assertTrue(result == unicode('hello'))

                # unicode should pass through unmodified
                result = wx.testStringTypemap(uni)
                self.assertTrue(result == uni)

                # utf-8 is converted
                result = wx.testStringTypemap(utf)
                self.assertTrue(result == uni)

                # can't auto-convert this
                with self.assertRaises(UnicodeDecodeError):
                    result = wx.testStringTypemap(iso)

                # utf-16-be
                val = "\x00\xe9\x00l\x00\xe9\x00p\x00h\x00a\x00n\x00t"
                with self.assertRaises(UnicodeDecodeError):
                    result = wx.testStringTypemap(val)
                result = wx.testStringTypemap( val.decode('utf-16-be'))
                self.assertTrue(result == uni)

                # utf-32-be
                val = "\x00\x00\x00\xe9\x00\x00\x00l\x00\x00\x00\xe9\x00\x00\x00p\x00\x00\x00h\x00\x00\x00a\x00\x00\x00n\x00\x00\x00t"
                with self.assertRaises(UnicodeDecodeError):
                    result = wx.testStringTypemap(val)
                result = wx.testStringTypemap(val.decode('utf-32-be'))
                self.assertTrue(result == uni)

                # utf-8 with BOM
                #val = "\xef\xbb\xbfHello"
                #result = wx.testStringTypemap(val)
                #self.assertTrue(result == u'\ufeffHello')

        else:
            def test_StringTypemapsPy3(self):
                utf  = b'\xc3\xa9l\xc3\xa9phant'    # utf-8 bytes
                uni  = utf.decode('utf-8')          # convert to unicode
                iso  = uni.encode('iso-8859-1')     # make a string with a different encoding

                # ascii
                result = wx.testStringTypemap(b'hello')
                self.assertTrue(type(result) == str)
                self.assertTrue(result == 'hello')

                # unicode should pass through unmodified
                result = wx.testStringTypemap(uni)
                self.assertTrue(result == uni)

                # utf-8 is converted
                result = wx.testStringTypemap(utf)
                self.assertTrue(result == uni)

                # can't auto-convert this
                with self.assertRaises(UnicodeDecodeError):
                    result = wx.testStringTypemap(iso)


#---------------------------------------------------------------------------


if __name__ == '__main__':
    unittest.main()