aboutsummaryrefslogtreecommitdiffstats
path: root/sources/shiboken6/tests/smartbinding/std_optional_test.py
blob: be969d0f5afee2ad22755beca6d3401ac763a0ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python
# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

import gc
import os
import sys
import unittest

from pathlib import Path
sys.path.append(os.fspath(Path(__file__).resolve().parents[1]))
from shiboken_paths import init_paths
init_paths()
from smart import Integer, StdOptionalTestBench, std


def call_func_on_optional(o):
    o.printInteger()


def integer_from_value(v):
    result = Integer()
    result.setValue(v)
    return result


class StdOptionalTests(unittest.TestCase):

    def testCInt(self):
        b = StdOptionalTestBench()
        ci = b.optionalInt()
        self.assertFalse(ci.has_value())
        b.setOptionalIntValue(42)
        ci = b.optionalInt()
        self.assertTrue(ci.has_value())
        self.assertEqual(ci.value(), 42)
        b.setOptionalInt(ci)
        ci = b.optionalInt()
        self.assertTrue(ci.has_value())
        self.assertEqual(ci.value(), 42)

        ci = std.optional_int(43)
        self.assertEqual(ci.value(), 43)

    def testInteger(self):
        b = StdOptionalTestBench()
        i = b.optionalInteger()
        self.assertFalse(i.has_value())
        self.assertFalse(i)
        # Must not throw a C++ exception
        self.assertRaises(AttributeError, call_func_on_optional, i)

        b.setOptionalIntegerValue(integer_from_value(42))
        i = b.optionalInteger()
        self.assertTrue(i.has_value())
        self.assertEqual(i.value().value(), 42)
        i.printInteger()
        print(i)
        b.setOptionalInteger(i)
        i = b.optionalInteger()
        self.assertTrue(i.has_value())
        self.assertEqual(i.value().value(), 42)
        call_func_on_optional(i)

        i = std.optional_Integer(integer_from_value(43))
        self.assertEqual(i.value().value(), 43)


if __name__ == '__main__':
    unittest.main()