tests: don't test for specific device labels
[pygobject.git] / tests / test_error.py
bloba15c7d338a39a94c93a6c51fd58af033fe758606
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
23 # USA
25 from __future__ import absolute_import
27 import unittest
28 import pickle
30 from gi.repository import GLib
31 from gi.repository import GIMarshallingTests
34 class TestType(unittest.TestCase):
35 def test_attributes(self):
36 e = GLib.Error('test message', 'mydomain', 42)
37 self.assertEqual(e.message, 'test message')
38 self.assertEqual(e.domain, 'mydomain')
39 self.assertEqual(e.code, 42)
41 def test_new_literal(self):
42 mydomain = GLib.quark_from_string('mydomain')
43 e = GLib.Error.new_literal(mydomain, 'test message', 42)
44 self.assertEqual(e.message, 'test message')
45 self.assertEqual(e.domain, 'mydomain')
46 self.assertEqual(e.code, 42)
48 def test_matches(self):
49 mydomain = GLib.quark_from_string('mydomain')
50 notmydomain = GLib.quark_from_string('notmydomain')
51 e = GLib.Error('test message', 'mydomain', 42)
52 self.assertTrue(e.matches(mydomain, 42))
53 self.assertFalse(e.matches(notmydomain, 42))
54 self.assertFalse(e.matches(mydomain, 40))
56 def test_str(self):
57 e = GLib.Error('test message', 'mydomain', 42)
58 self.assertEqual(str(e),
59 'mydomain: test message (42)')
61 def test_repr(self):
62 e = GLib.Error('test message', 'mydomain', 42)
63 self.assertEqual(repr(e),
64 "GLib.Error('test message', 'mydomain', 42)")
66 def test_inheritance(self):
67 self.assertTrue(issubclass(GLib.Error, RuntimeError))
69 def test_pickle(self):
71 def check_pickle(e):
72 assert isinstance(e, GLib.Error)
73 new_e = pickle.loads(pickle.dumps(e))
74 assert type(new_e) is type(e)
75 assert repr(e) == repr(new_e)
77 e = GLib.Error('test message', 'mydomain', 42)
78 check_pickle(e)
80 try:
81 GLib.file_get_contents("")
82 except Exception as e:
83 check_pickle(e)
86 class ObjectWithVFuncException(GIMarshallingTests.Object):
87 def do_vfunc_meth_with_err(self, x):
88 if x == 42:
89 return True
91 raise GLib.Error('unexpected value %d' % x, 'mydomain', 42)
94 class TestMarshalling(unittest.TestCase):
95 def test_array_in_crash(self):
96 # Previously there was a bug in invoke, in which C arrays were unwrapped
97 # from inside GArrays to be passed to the C function. But when a GError was
98 # set, invoke would attempt to free the C array as if it were a GArray.
99 # This crash is only for C arrays. It does not happen for C functions which
100 # take in GArrays. See https://bugzilla.gnome.org/show_bug.cgi?id=642708
101 self.assertRaises(GLib.Error, GIMarshallingTests.gerror_array_in, [1, 2, 3])
103 def test_out(self):
104 # See https://bugzilla.gnome.org/show_bug.cgi?id=666098
105 error, debug = GIMarshallingTests.gerror_out()
107 self.assertIsInstance(error, GLib.Error)
108 self.assertEqual(error.domain, GIMarshallingTests.CONSTANT_GERROR_DOMAIN)
109 self.assertEqual(error.code, GIMarshallingTests.CONSTANT_GERROR_CODE)
110 self.assertEqual(error.message, GIMarshallingTests.CONSTANT_GERROR_MESSAGE)
111 self.assertEqual(debug, GIMarshallingTests.CONSTANT_GERROR_DEBUG_MESSAGE)
113 def test_out_transfer_none(self):
114 # See https://bugzilla.gnome.org/show_bug.cgi?id=666098
115 error, debug = GIMarshallingTests.gerror_out_transfer_none()
117 self.assertIsInstance(error, GLib.Error)
118 self.assertEqual(error.domain, GIMarshallingTests.CONSTANT_GERROR_DOMAIN)
119 self.assertEqual(error.code, GIMarshallingTests.CONSTANT_GERROR_CODE)
120 self.assertEqual(error.message, GIMarshallingTests.CONSTANT_GERROR_MESSAGE)
121 self.assertEqual(GIMarshallingTests.CONSTANT_GERROR_DEBUG_MESSAGE, debug)
123 def test_return(self):
124 # See https://bugzilla.gnome.org/show_bug.cgi?id=666098
125 error = GIMarshallingTests.gerror_return()
127 self.assertIsInstance(error, GLib.Error)
128 self.assertEqual(error.domain, GIMarshallingTests.CONSTANT_GERROR_DOMAIN)
129 self.assertEqual(error.code, GIMarshallingTests.CONSTANT_GERROR_CODE)
130 self.assertEqual(error.message, GIMarshallingTests.CONSTANT_GERROR_MESSAGE)
132 def test_exception(self):
133 with self.assertRaises(GLib.Error) as context:
134 GIMarshallingTests.gerror()
136 e = context.exception
137 self.assertEqual(e.domain, GIMarshallingTests.CONSTANT_GERROR_DOMAIN)
138 self.assertEqual(e.code, GIMarshallingTests.CONSTANT_GERROR_CODE)
139 self.assertEqual(e.message, GIMarshallingTests.CONSTANT_GERROR_MESSAGE)
141 def test_vfunc_no_exception(self):
142 obj = ObjectWithVFuncException()
143 self.assertTrue(obj.vfunc_meth_with_error(42))
145 def test_vfunc_gerror_exception(self):
146 obj = ObjectWithVFuncException()
147 with self.assertRaises(GLib.Error) as context:
148 obj.vfunc_meth_with_error(-1)
150 e = context.exception
151 self.assertEqual(e.message, 'unexpected value -1')
152 self.assertEqual(e.domain, 'mydomain')
153 self.assertEqual(e.code, 42)
155 def tests_compare_two_gerrors_in_gvalue(self):
156 error = GLib.Error.new_literal(1, "error", 1)
157 error1 = GLib.Error.new_literal(1, "error", 1)
159 GIMarshallingTests.compare_two_gerrors_in_gvalue(error, error1)