1 # -*- Mode: Python; py-indent-offset: 4 -*-
2 # vim: tabstop=4 shiftwidth=4 expandtab
4 # test_error.py: Tests for GError wrapper implementation
6 # Copyright (C) 2012 Will Thompson
7 # Copyright (C) 2013 Martin Pitt
8 # Copyright (C) 2014 Simon Feltman <sfeltman@gnome.org>
10 # This library is free software; you can redistribute it and/or
11 # modify it under the terms of the GNU Lesser General Public
12 # License as published by the Free Software Foundation; either
13 # version 2.1 of the License, or (at your option) any later version.
15 # This library is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 # Lesser General Public License for more details.
20 # You should have received a copy of the GNU Lesser General Public
21 # License along with this library; if not, write to the Free Software
22 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
27 from gi.repository import GLib
28 from gi.repository import GIMarshallingTests
31 class TestType(unittest.TestCase):
32 def test_attributes(self):
33 e = GLib.Error('test message', 'mydomain', 42)
34 self.assertEqual(e.message, 'test message')
35 self.assertEqual(e.domain, 'mydomain')
36 self.assertEqual(e.code, 42)
38 def test_new_literal(self):
39 mydomain = GLib.quark_from_string('mydomain')
40 e = GLib.Error.new_literal(mydomain, 'test message', 42)
41 self.assertEqual(e.message, 'test message')
42 self.assertEqual(e.domain, 'mydomain')
43 self.assertEqual(e.code, 42)
45 def test_matches(self):
46 mydomain = GLib.quark_from_string('mydomain')
47 notmydomain = GLib.quark_from_string('notmydomain')
48 e = GLib.Error('test message', 'mydomain', 42)
49 self.assertTrue(e.matches(mydomain, 42))
50 self.assertFalse(e.matches(notmydomain, 42))
51 self.assertFalse(e.matches(mydomain, 40))
54 e = GLib.Error('test message', 'mydomain', 42)
55 self.assertEqual(str(e),
56 'mydomain: test message (42)')
59 e = GLib.Error('test message', 'mydomain', 42)
60 self.assertEqual(repr(e),
61 "GLib.Error('test message', 'mydomain', 42)")
63 def test_inheritance(self):
64 self.assertTrue(issubclass(GLib.Error, RuntimeError))
67 class ObjectWithVFuncException(GIMarshallingTests.Object):
68 def do_vfunc_meth_with_err(self, x):
72 raise GLib.Error('unexpected value %d' % x, 'mydomain', 42)
75 class TestMarshalling(unittest.TestCase):
76 def test_array_in_crash(self):
77 # Previously there was a bug in invoke, in which C arrays were unwrapped
78 # from inside GArrays to be passed to the C function. But when a GError was
79 # set, invoke would attempt to free the C array as if it were a GArray.
80 # This crash is only for C arrays. It does not happen for C functions which
81 # take in GArrays. See https://bugzilla.gnome.org/show_bug.cgi?id=642708
82 self.assertRaises(GLib.Error, GIMarshallingTests.gerror_array_in, [1, 2, 3])
85 # See https://bugzilla.gnome.org/show_bug.cgi?id=666098
86 error, debug = GIMarshallingTests.gerror_out()
88 self.assertIsInstance(error, GLib.Error)
89 self.assertEqual(error.domain, GIMarshallingTests.CONSTANT_GERROR_DOMAIN)
90 self.assertEqual(error.code, GIMarshallingTests.CONSTANT_GERROR_CODE)
91 self.assertEqual(error.message, GIMarshallingTests.CONSTANT_GERROR_MESSAGE)
92 self.assertEqual(debug, GIMarshallingTests.CONSTANT_GERROR_DEBUG_MESSAGE)
94 def test_out_transfer_none(self):
95 # See https://bugzilla.gnome.org/show_bug.cgi?id=666098
96 error, debug = GIMarshallingTests.gerror_out_transfer_none()
98 self.assertIsInstance(error, GLib.Error)
99 self.assertEqual(error.domain, GIMarshallingTests.CONSTANT_GERROR_DOMAIN)
100 self.assertEqual(error.code, GIMarshallingTests.CONSTANT_GERROR_CODE)
101 self.assertEqual(error.message, GIMarshallingTests.CONSTANT_GERROR_MESSAGE)
102 self.assertEqual(GIMarshallingTests.CONSTANT_GERROR_DEBUG_MESSAGE, debug)
104 def test_return(self):
105 # See https://bugzilla.gnome.org/show_bug.cgi?id=666098
106 error = GIMarshallingTests.gerror_return()
108 self.assertIsInstance(error, GLib.Error)
109 self.assertEqual(error.domain, GIMarshallingTests.CONSTANT_GERROR_DOMAIN)
110 self.assertEqual(error.code, GIMarshallingTests.CONSTANT_GERROR_CODE)
111 self.assertEqual(error.message, GIMarshallingTests.CONSTANT_GERROR_MESSAGE)
113 def test_exception(self):
114 with self.assertRaises(GLib.Error) as context:
115 GIMarshallingTests.gerror()
117 e = context.exception
118 self.assertEqual(e.domain, GIMarshallingTests.CONSTANT_GERROR_DOMAIN)
119 self.assertEqual(e.code, GIMarshallingTests.CONSTANT_GERROR_CODE)
120 self.assertEqual(e.message, GIMarshallingTests.CONSTANT_GERROR_MESSAGE)
122 def test_vfunc_no_exception(self):
123 obj = ObjectWithVFuncException()
124 self.assertTrue(obj.vfunc_meth_with_error(42))
126 def test_vfunc_gerror_exception(self):
127 obj = ObjectWithVFuncException()
128 with self.assertRaises(GLib.Error) as context:
129 obj.vfunc_meth_with_error(-1)
131 e = context.exception
132 self.assertEqual(e.message, 'unexpected value -1')
133 self.assertEqual(e.domain, 'mydomain')
134 self.assertEqual(e.code, 42)
136 def tests_compare_two_gerrors_in_gvalue(self):
137 error = GLib.Error.new_literal(1, "error", 1)
138 error1 = GLib.Error.new_literal(1, "error", 1)
140 GIMarshallingTests.compare_two_gerrors_in_gvalue(error, error1)
143 if __name__ == '__main__':