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