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
21import unittest
22import pickle
23
24import gi
25from gi.repository import GIMarshallingTests
26from gi.repository import Regress
27
28
29ResultTuple = gi._gi.ResultTuple
30
31
32class TestResultTuple(unittest.TestCase):
33
34    def test_base(self):
35        self.assertTrue(issubclass(ResultTuple, tuple))
36
37    def test_create(self):
38        names = [None, "foo", None, "bar"]
39        for i in range(10):
40            new = ResultTuple._new_type(names)
41            self.assertTrue(issubclass(new, ResultTuple))
42
43    def test_repr_dir(self):
44        new = ResultTuple._new_type([None, "foo", None, "bar"])
45        inst = new([1, 2, 3, "a"])
46
47        self.assertEqual(repr(inst), "(1, foo=2, 3, bar='a')")
48        self.assertTrue("foo" in dir(inst))
49
50    def test_repr_dir_empty(self):
51        new = ResultTuple._new_type([])
52        inst = new()
53        self.assertEqual(repr(inst), "()")
54        dir(inst)
55
56    def test_getatttr(self):
57        new = ResultTuple._new_type([None, "foo", None, "bar"])
58        inst = new([1, 2, 3, "a"])
59
60        self.assertTrue(hasattr(inst, "foo"))
61        self.assertEqual(inst.foo, inst[1])
62        self.assertRaises(AttributeError, getattr, inst, "nope")
63
64    def test_pickle(self):
65        new = ResultTuple._new_type([None, "foo", None, "bar"])
66        inst = new([1, 2, 3, "a"])
67
68        inst2 = pickle.loads(pickle.dumps(inst))
69        self.assertEqual(inst2, inst)
70        self.assertTrue(isinstance(inst2, tuple))
71        self.assertFalse(isinstance(inst2, new))
72
73    def test_gi(self):
74        res = GIMarshallingTests.init_function([])
75        self.assertEqual(repr(res), "(True, argv=[])")
76
77        res = GIMarshallingTests.array_return_etc(5, 9)
78        self.assertEqual(repr(res), "([5, 0, 1, 9], sum=14)")
79
80        res = GIMarshallingTests.array_out_etc(-5, 9)
81        self.assertEqual(repr(res), "(ints=[-5, 0, 1, 9], sum=4)")
82
83        cb = lambda: (1, 2)
84        res = GIMarshallingTests.callback_multiple_out_parameters(cb)
85        self.assertEqual(repr(res), "(a=1.0, b=2.0)")
86
87    def test_regress(self):
88        res = Regress.TestObj().skip_return_val(50, 42.0, 60, 2, 3)
89        self.assertEqual(repr(res), "(out_b=51, inout_d=61, out_sum=32)")
90