1#############################################################################
2##
3## Copyright (C) 2019 The Qt Company Ltd.
4## Contact: https://www.qt.io/licensing/
5##
6## This file is part of the test suite of Qt for Python.
7##
8## $QT_BEGIN_LICENSE:GPL-EXCEPT$
9## Commercial License Usage
10## Licensees holding valid commercial Qt licenses may use this file in
11## accordance with the commercial license agreement provided with the
12## Software or, alternatively, in accordance with the terms contained in
13## a written agreement between you and The Qt Company. For licensing terms
14## and conditions see https://www.qt.io/terms-conditions. For further
15## information use the contact form at https://www.qt.io/contact-us.
16##
17## GNU General Public License Usage
18## Alternatively, this file may be used under the terms of the GNU
19## General Public License version 3 as published by the Free Software
20## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21## included in the packaging of this file. Please review the following
22## information to ensure the GNU General Public License requirements will
23## be met: https://www.gnu.org/licenses/gpl-3.0.html.
24##
25## $QT_END_LICENSE$
26##
27#############################################################################
28
29"""
30iterable_test.py
31
32This test checks that the Iterable protocol is implemented correctly.
33"""
34
35import os
36import sys
37import unittest
38
39sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
40from init_paths import init_test_paths
41init_test_paths(False)
42
43import PySide2
44from PySide2 import QtCore, QtGui
45
46try:
47    import numpy as np
48    have_numpy = True
49except ImportError:
50    have_numpy = False
51
52class PySequenceTest(unittest.TestCase):
53
54    def test_iterable(self):
55        def gen(lis):
56            for item in lis:
57                if item == "crash":
58                    raise IndexError
59                yield item
60        # testing "pyseq_to_cpplist_conversion"
61        testfunc = QtCore.QUrl.fromStringList
62        # use a generator (iterable)
63        self.assertEqual(testfunc(gen(["asd", "ghj"])),
64            [PySide2.QtCore.QUrl('asd'), PySide2.QtCore.QUrl('ghj')])
65        # use an iterator
66        self.assertEqual(testfunc(iter(["asd", "ghj"])),
67            [PySide2.QtCore.QUrl('asd'), PySide2.QtCore.QUrl('ghj')])
68        self.assertRaises(IndexError, testfunc, gen(["asd", "crash", "ghj"]))
69        # testing QMatrix4x4
70        testfunc = QtGui.QMatrix4x4
71        self.assertEqual(testfunc(gen(range(16))), testfunc(range(16)))
72        # Note: The errormessage needs to be improved!
73        # We should better get a ValueError
74        self.assertRaises((TypeError, ValueError), testfunc, gen(range(15)))
75        # All other matrix sizes:
76        testfunc = QtGui.QMatrix2x2
77        self.assertEqual(testfunc(gen(range(4))), testfunc(range(4)))
78        testfunc = QtGui.QMatrix2x3
79        self.assertEqual(testfunc(gen(range(6))), testfunc(range(6)))
80
81    @unittest.skipUnless(have_numpy, "requires numpy")
82    def test_iterable_numpy(self):
83        # Demo for numpy: We create a unit matrix.
84        num_mat = np.eye(4)
85        num_mat.shape = 16
86        unit = QtGui.QMatrix4x4(num_mat)
87        self.assertEqual(unit, QtGui.QMatrix4x4())
88
89
90if __name__ == '__main__':
91    unittest.main()
92