| 1 |
# Copyright (C) 2015 Christoph Reiter <[email protected]> |
| 2 |
# |
| 3 |
# This library is free software; you can redistribute it and/or |
| 4 |
# modify it under the terms of the GNU Lesser General Public |
| 5 |
# License as published by the Free Software Foundation; either |
| 6 |
# version 2.1 of the License, or (at your option) any later version. |
| 7 |
# |
| 8 |
# This library is distributed in the hope that it will be useful, |
| 9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 |
# Lesser General Public License for more details. |
| 12 |
# |
| 13 |
# You should have received a copy of the GNU Lesser General Public |
| 14 |
# License along with this library; if not, write to the Free Software |
| 15 |
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 |
| 16 |
# USA |
| 17 |
|
| 18 |
import unittest |
| 19 |
import pickle |
| 20 |
|
| 21 |
import gi |
| 22 |
from gi.repository import GIMarshallingTests |
| 23 |
from gi.repository import Regress |
| 24 |
|
| 25 |
|
| 26 |
ResultTuple = gi._gi.ResultTuple |
| 27 |
|
| 28 |
|
| 29 |
class TestResultTuple(unittest.TestCase): |
| 30 |
def test_base(self): |
| 31 |
self.assertTrue(issubclass(ResultTuple, tuple)) |
| 32 |
|
| 33 |
def test_create(self): |
| 34 |
names = [None, "foo", None, "bar"] |
| 35 |
for i in range(10): |
| 36 |
new = ResultTuple._new_type(names) |
| 37 |
self.assertTrue(issubclass(new, ResultTuple)) |
| 38 |
|
| 39 |
def test_repr_dir(self): |
| 40 |
new = ResultTuple._new_type([None, "foo", None, "bar"]) |
| 41 |
inst = new([1, 2, 3, "a"]) |
| 42 |
|
| 43 |
self.assertEqual(repr(inst), "(1, foo=2, 3, bar='a')") |
| 44 |
self.assertTrue("foo" in dir(inst)) |
| 45 |
|
| 46 |
def test_repr_dir_empty(self): |
| 47 |
new = ResultTuple._new_type([]) |
| 48 |
inst = new() |
| 49 |
self.assertEqual(repr(inst), "()") |
| 50 |
dir(inst) |
| 51 |
|
| 52 |
def test_getatttr(self): |
| 53 |
new = ResultTuple._new_type([None, "foo", None, "bar"]) |
| 54 |
inst = new([1, 2, 3, "a"]) |
| 55 |
|
| 56 |
self.assertTrue(hasattr(inst, "foo")) |
| 57 |
self.assertEqual(inst.foo, inst[1]) |
| 58 |
self.assertRaises(AttributeError, getattr, inst, "nope") |
| 59 |
|
| 60 |
def test_pickle(self): |
| 61 |
new = ResultTuple._new_type([None, "foo", None, "bar"]) |
| 62 |
inst = new([1, 2, 3, "a"]) |
| 63 |
|
| 64 |
inst2 = pickle.loads(pickle.dumps(inst)) |
| 65 |
self.assertEqual(inst2, inst) |
| 66 |
self.assertTrue(isinstance(inst2, tuple)) |
| 67 |
self.assertFalse(isinstance(inst2, new)) |
| 68 |
|
| 69 |
def test_gi(self): |
| 70 |
res = GIMarshallingTests.init_function([]) |
| 71 |
self.assertEqual(repr(res), "(True, argv=[])") |
| 72 |
|
| 73 |
res = GIMarshallingTests.array_return_etc(5, 9) |
| 74 |
self.assertEqual(repr(res), "([5, 0, 1, 9], sum=14)") |
| 75 |
|
| 76 |
res = GIMarshallingTests.array_out_etc(-5, 9) |
| 77 |
self.assertEqual(repr(res), "(ints=[-5, 0, 1, 9], sum=4)") |
| 78 |
|
| 79 |
def cb(): |
| 80 |
return 1, 2 |
| 81 |
|
| 82 |
res = GIMarshallingTests.callback_multiple_out_parameters(cb) |
| 83 |
self.assertEqual(repr(res), "(a=1.0, b=2.0)") |
| 84 |
|
| 85 |
def test_regress(self): |
| 86 |
res = Regress.TestObj().skip_return_val(50, 42.0, 60, 2, 3) |
| 87 |
self.assertEqual(repr(res), "(out_b=51, inout_d=61, out_sum=32)") |