1#
2# Some more tests for exception handling.
3# XXX: we should centralize all exception handling tests into this file, this
4#      is now mostly used to check general unicode support in exceptions.
5#
6from __future__ import unicode_literals
7from PyObjCTest.exceptions import *
8
9from PyObjCTools.TestSupport import *
10import objc, sys
11
12class TestExceptionsFromObjC (TestCase):
13    def testSimple(self):
14        o = PyObjCTestExceptions.alloc().init()
15
16        try:
17            o.raiseSimple()
18
19        except objc.error as e:
20            self.assertEqual(str(e), 'SimpleException - hello world')
21            self.assertEqual(e._pyobjc_info_['name'], 'SimpleException')
22            self.assertEqual(e._pyobjc_info_['reason'], 'hello world')
23            self.assertEqual(e._pyobjc_info_['userInfo'], None)
24
25    def testSimpleWithInfo(self):
26        o = PyObjCTestExceptions.alloc().init()
27
28        try:
29            o.raiseSimpleWithInfo()
30
31        except objc.error as e:
32            self.assertEqual(str(e), 'InfoException - Reason string')
33            self.assertEqual(e._pyobjc_info_['name'], 'InfoException')
34            self.assertEqual(e._pyobjc_info_['reason'], 'Reason string')
35            self.assertEqual(e._pyobjc_info_['userInfo'], {
36                'key1': 'value1',
37                'key2': 'value2',
38            })
39
40    def testUnicodeName(self):
41        o = PyObjCTestExceptions.alloc().init()
42
43        try:
44            o.raiseUnicodeName()
45
46        except objc.error as e:
47            if sys.version_info[0] == 2:
48                self.assertEqual(str(e), 'SimpleException\u1234\u2049 - hello world'.encode('utf-8'))
49            else:
50                self.assertEqual(str(e), 'SimpleException\u1234\u2049 - hello world')
51            self.assertEqual(e._pyobjc_info_['name'], 'SimpleException\u1234\u2049')
52            self.assertEqual(e._pyobjc_info_['reason'], 'hello world')
53            self.assertEqual(e._pyobjc_info_['userInfo'], None)
54
55    def testUnicodeReason(self):
56        o = PyObjCTestExceptions.alloc().init()
57
58        try:
59            o.raiseUnicodeReason()
60
61        except objc.error as e:
62            if sys.version_info[0] == 2:
63                self.assertEqual(str(e), 'SimpleException - hello world\u1234\u2049'.encode('utf-8'))
64            else:
65                self.assertEqual(str(e), 'SimpleException - hello world\u1234\u2049')
66            self.assertEqual(e._pyobjc_info_['name'], 'SimpleException')
67            self.assertEqual(e._pyobjc_info_['reason'], 'hello world\u1234\u2049')
68            self.assertEqual(e._pyobjc_info_['userInfo'], None)
69
70    def testUnicodeWithInfo(self):
71        o = PyObjCTestExceptions.alloc().init()
72
73        try:
74            o.raiseUnicodeWithInfo()
75
76        except objc.error as e:
77            if sys.version_info[0] == 2:
78                self.assertEqual(str(e), 'InfoException\u1234\u2049 - Reason string\u1234\u2049'.encode('utf-8'))
79            else:
80                self.assertEqual(str(e), 'InfoException\u1234\u2049 - Reason string\u1234\u2049')
81            self.assertEqual(e._pyobjc_info_['name'], 'InfoException\u1234\u2049')
82            self.assertEqual(e._pyobjc_info_['reason'], 'Reason string\u1234\u2049')
83            self.assertEqual(e._pyobjc_info_['userInfo'], {
84                'key1\u1234\u2049': 'value1\u1234\u2049',
85                'key2\u1234\u2049': 'value2\u1234\u2049',
86            })
87
88    def testRaisingStringsInObjectiveC(self):
89        # Bug #1741095, @throw anNSString
90
91        o = PyObjCTestExceptions.alloc().init()
92        try:
93            o.raiseAString()
94
95        except objc.error as e:
96            self.assertEqual(e._pyobjc_exc_, "thrown string")
97
98if __name__ == "__main__":
99    main()
100