Debian Codegraph @

tests/test_error.py

raw
1 # test_error.py: Tests for GError wrapper implementation
2 #
3 # Copyright (C) 2012 Will Thompson
4 # Copyright (C) 2013 Martin Pitt
5 # Copyright (C) 2014 Simon Feltman <[email protected]>
6 #
7 # This library is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public
9 # License as published by the Free Software Foundation; either
10 # version 2.1 of the License, or (at your option) any later version.
11 #
12 # This library is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # Lesser General Public License for more details.
16 #
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with this library; if not, write to the Free Software
19 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
20 # USA
21
22 import unittest
23 import pickle
24
25 from gi.repository import GLib
26 from gi.repository import GIMarshallingTests
27
28
29 class TestType(unittest.TestCase):
30 def test_attributes(self):
31 e = GLib.Error("test message", "mydomain", 42)
32 self.assertEqual(e.message, "test message")
33 self.assertEqual(e.domain, "mydomain")
34 self.assertEqual(e.code, 42)
35
36 def test_new_literal(self):
37 mydomain = GLib.quark_from_string("mydomain")
38 e = GLib.Error.new_literal(mydomain, "test message", 42)
39 self.assertEqual(e.message, "test message")
40 self.assertEqual(e.domain, "mydomain")
41 self.assertEqual(e.code, 42)
42
43 def test_matches(self):
44 mydomain = GLib.quark_from_string("mydomain")
45 notmydomain = GLib.quark_from_string("notmydomain")
46 e = GLib.Error("test message", "mydomain", 42)
47 self.assertTrue(e.matches(mydomain, 42))
48 self.assertFalse(e.matches(notmydomain, 42))
49 self.assertFalse(e.matches(mydomain, 40))
50
51 def test_str(self):
52 e = GLib.Error("test message", "mydomain", 42)
53 self.assertEqual(str(e), "mydomain: test message (42)")
54
55 def test_repr(self):
56 e = GLib.Error("test message", "mydomain", 42)
57 self.assertEqual(repr(e), "GLib.Error('test message', 'mydomain', 42)")
58
59 def test_inheritance(self):
60 self.assertTrue(issubclass(GLib.Error, RuntimeError))
61
62 def test_pickle(self):
63 def check_pickle(e):
64 assert isinstance(e, GLib.Error)
65 new_e = pickle.loads(pickle.dumps(e))
66 assert type(new_e) is type(e)
67 assert repr(e) == repr(new_e)
68
69 e = GLib.Error("test message", "mydomain", 42)
70 check_pickle(e)
71
72 try:
73 GLib.file_get_contents("")
74 except Exception as e:
75 check_pickle(e)
76
77
78 class ObjectWithVFuncException(GIMarshallingTests.Object):
79 def do_vfunc_meth_with_err(self, x):
80 if x == 42:
81 return True
82
83 msg = f"unexpected value {x:d}"
84 raise GLib.Error(msg, "mydomain", 42)
85
86
87 class TestMarshalling(unittest.TestCase):
88 def test_array_in_crash(self):
89 # Previously there was a bug in invoke, in which C arrays were unwrapped
90 # from inside GArrays to be passed to the C function. But when a GError was
91 # set, invoke would attempt to free the C array as if it were a GArray.
92 # This crash is only for C arrays. It does not happen for C functions which
93 # take in GArrays. See https://bugzilla.gnome.org/show_bug.cgi?id=642708
94 self.assertRaises(GLib.Error, GIMarshallingTests.gerror_array_in, [1, 2, 3])
95
96 def test_out(self):
97 # See https://bugzilla.gnome.org/show_bug.cgi?id=666098
98 error, debug = GIMarshallingTests.gerror_out()
99
100 self.assertIsInstance(error, GLib.Error)
101 self.assertEqual(error.domain, GIMarshallingTests.CONSTANT_GERROR_DOMAIN)
102 self.assertEqual(error.code, GIMarshallingTests.CONSTANT_GERROR_CODE)
103 self.assertEqual(error.message, GIMarshallingTests.CONSTANT_GERROR_MESSAGE)
104 self.assertEqual(debug, GIMarshallingTests.CONSTANT_GERROR_DEBUG_MESSAGE)
105
106 def test_out_transfer_none(self):
107 # See https://bugzilla.gnome.org/show_bug.cgi?id=666098
108 error, debug = GIMarshallingTests.gerror_out_transfer_none()
109
110 self.assertIsInstance(error, GLib.Error)
111 self.assertEqual(error.domain, GIMarshallingTests.CONSTANT_GERROR_DOMAIN)
112 self.assertEqual(error.code, GIMarshallingTests.CONSTANT_GERROR_CODE)
113 self.assertEqual(error.message, GIMarshallingTests.CONSTANT_GERROR_MESSAGE)
114 self.assertEqual(GIMarshallingTests.CONSTANT_GERROR_DEBUG_MESSAGE, debug)
115
116 def test_return(self):
117 # See https://bugzilla.gnome.org/show_bug.cgi?id=666098
118 error = GIMarshallingTests.gerror_return()
119
120 self.assertIsInstance(error, GLib.Error)
121 self.assertEqual(error.domain, GIMarshallingTests.CONSTANT_GERROR_DOMAIN)
122 self.assertEqual(error.code, GIMarshallingTests.CONSTANT_GERROR_CODE)
123 self.assertEqual(error.message, GIMarshallingTests.CONSTANT_GERROR_MESSAGE)
124
125 def test_exception(self):
126 with self.assertRaises(GLib.Error) as context:
127 GIMarshallingTests.gerror()
128
129 e = context.exception
130 self.assertEqual(e.domain, GIMarshallingTests.CONSTANT_GERROR_DOMAIN)
131 self.assertEqual(e.code, GIMarshallingTests.CONSTANT_GERROR_CODE)
132 self.assertEqual(e.message, GIMarshallingTests.CONSTANT_GERROR_MESSAGE)
133
134 def test_vfunc_no_exception(self):
135 obj = ObjectWithVFuncException()
136 self.assertTrue(obj.vfunc_meth_with_error(42))
137
138 def test_vfunc_gerror_exception(self):
139 obj = ObjectWithVFuncException()
140 with self.assertRaises(GLib.Error) as context:
141 obj.vfunc_meth_with_error(-1)
142
143 e = context.exception
144 self.assertEqual(e.message, "unexpected value -1")
145 self.assertEqual(e.domain, "mydomain")
146 self.assertEqual(e.code, 42)
147
148 def tests_compare_two_gerrors_in_gvalue(self):
149 error = GLib.Error.new_literal(1, "error", 1)
150 error1 = GLib.Error.new_literal(1, "error", 1)
151
152 GIMarshallingTests.compare_two_gerrors_in_gvalue(error, error1)
153
154
155 def test_nullable_error():
156 error = GLib.Error.new_literal(1, "error", 1)
157
158 assert GIMarshallingTests.nullable_gerror(error) == 1
159 assert GIMarshallingTests.nullable_gerror(None) == 0