diff options
Diffstat (limited to 'sources/shiboken6/tests/samplebinding/enum_test.py')
-rw-r--r-- | sources/shiboken6/tests/samplebinding/enum_test.py | 92 |
1 files changed, 19 insertions, 73 deletions
diff --git a/sources/shiboken6/tests/samplebinding/enum_test.py b/sources/shiboken6/tests/samplebinding/enum_test.py index 66d7daf5b..276b8d894 100644 --- a/sources/shiboken6/tests/samplebinding/enum_test.py +++ b/sources/shiboken6/tests/samplebinding/enum_test.py @@ -1,33 +1,6 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- -# -############################################################################# -## -## Copyright (C) 2021 The Qt Company Ltd. -## Contact: https://www.qt.io/licensing/ -## -## This file is part of the test suite of Qt for Python. -## -## $QT_BEGIN_LICENSE:GPL-EXCEPT$ -## Commercial License Usage -## Licensees holding valid commercial Qt licenses may use this file in -## accordance with the commercial license agreement provided with the -## Software or, alternatively, in accordance with the terms contained in -## a written agreement between you and The Qt Company. For licensing terms -## and conditions see https://www.qt.io/terms-conditions. For further -## information use the contact form at https://www.qt.io/contact-us. -## -## GNU General Public License Usage -## Alternatively, this file may be used under the terms of the GNU -## General Public License version 3 as published by the Free Software -## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -## included in the packaging of this file. Please review the following -## information to ensure the GNU General Public License requirements will -## be met: https://www.gnu.org/licenses/gpl-3.0.html. -## -## $QT_END_LICENSE$ -## -############################################################################# +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 '''Test cases for Python representation of C++ enums.''' @@ -40,33 +13,27 @@ sys.path.append(os.fspath(Path(__file__).resolve().parents[1])) from shiboken_paths import init_paths init_paths() -import shiboken6 # This is needed after the introduction of BUILD_DIR. import sample from sample import SampleNamespace, ObjectType, Event + def createTempFile(): import tempfile return tempfile.SpooledTemporaryFile(mode='rw') + class EnumTest(unittest.TestCase): '''Test case for Python representation of C++ enums.''' - def testEnumRepr(self): - enum = SampleNamespace.Option(1) - self.assertEqual(eval(repr(enum)), enum) - - enum = SampleNamespace.Option(999) - self.assertEqual(eval(repr(enum)), enum) - def testHashability(self): self.assertEqual(hash(SampleNamespace.TwoIn), hash(SampleNamespace.TwoOut)) self.assertNotEqual(hash(SampleNamespace.TwoIn), hash(SampleNamespace.OneIn)) def testEnumValuesInsideEnum(self): '''Enum values should be accessible inside the enum as well as outside.''' - for value_name in SampleNamespace.Option.values: + for value_name in SampleNamespace.Option.__members__: enum_item1 = getattr(SampleNamespace.Option, value_name) enum_item2 = getattr(SampleNamespace, value_name) self.assertEqual(enum_item1, enum_item2) @@ -92,16 +59,18 @@ class EnumTest(unittest.TestCase): def testEnumConstructorWithTooManyParameters(self): '''Calling the constructor of non-extensible enum with the wrong number of parameters.''' - self.assertRaises(TypeError, SampleNamespace.InValue, 13, 14) + self.assertRaises((TypeError, ValueError), SampleNamespace.InValue, 13, 14) def testEnumConstructorWithNonNumberParameter(self): '''Calling the constructor of non-extensible enum with a string.''' - self.assertRaises(TypeError, SampleNamespace.InValue, '1') + self.assertRaises((TypeError, ValueError), SampleNamespace.InValue, '1') def testEnumItemAsDefaultValueToIntArgument(self): '''Calls function with an enum item as default value to an int argument.''' - self.assertEqual(SampleNamespace.enumItemAsDefaultValueToIntArgument(), SampleNamespace.ZeroIn) - self.assertEqual(SampleNamespace.enumItemAsDefaultValueToIntArgument(SampleNamespace.ZeroOut), SampleNamespace.ZeroOut) + self.assertEqual(SampleNamespace.enumItemAsDefaultValueToIntArgument(), + SampleNamespace.ZeroIn) + self.assertEqual(SampleNamespace.enumItemAsDefaultValueToIntArgument(SampleNamespace.ZeroOut), # noqa E:501 + SampleNamespace.ZeroOut) self.assertEqual(SampleNamespace.enumItemAsDefaultValueToIntArgument(123), 123) def testAnonymousGlobalEnums(self): @@ -126,49 +95,26 @@ class EnumTest(unittest.TestCase): self.assertEqual(event.eventType(), Event.BASIC_EVENT) event.setEventTypeByConstRef(Event.SOME_EVENT) self.assertEqual(event.eventType(), Event.SOME_EVENT) - - def testEnumTpPrintImplementation(self): - '''Without SbkEnum.tp_print 'print' returns the enum represented as an int.''' - tmpfile = createTempFile() - print(Event.ANY_EVENT, file=tmpfile) - tmpfile.seek(0) - text = tmpfile.read().strip() - tmpfile.close() - self.assertEqual(text, str(Event.ANY_EVENT)) - self.assertEqual(text, repr(Event.ANY_EVENT)) + event.setEventTypeByConstPtr(Event.BASIC_EVENT) + self.assertEqual(event.eventType(), Event.BASIC_EVENT) def testEnumArgumentWithDefaultValue(self): '''Option enumArgumentWithDefaultValue(Option opt = UnixTime);''' self.assertEqual(SampleNamespace.enumArgumentWithDefaultValue(), SampleNamespace.UnixTime) - self.assertEqual(SampleNamespace.enumArgumentWithDefaultValue(SampleNamespace.RandomNumber), SampleNamespace.RandomNumber) - - def testSignature(self): - enum = SampleNamespace.Option(1) - types = type(enum).mro() - klass = types[0] - base = types[1] - # The class has an empty signature. - self.assertEqual(klass.__signature__, None) - # The base class must be Enum - self.assertNotEqual(base.__signature__, None) - # It contains an int annotation. - param = base.__signature__.parameters["itemValue"] - self.assertEqual(param.annotation, int) + self.assertEqual(SampleNamespace.enumArgumentWithDefaultValue(SampleNamespace.RandomNumber), # noqa E:501 + SampleNamespace.RandomNumber) class MyEvent(Event): def __init__(self): - Event.__init__(self, Event.EventType(999)) + Event.__init__(self, Event.EventType(3)) + class OutOfBoundsTest(unittest.TestCase): def testValue(self): e = MyEvent() - self.assertEqual(repr(e.eventType()), 'sample.Event.EventType(999)') + self.assertEqual(repr(e.eventType()), "<EventType.ANY_EVENT: 3>") - def testNoneName(self): - e = MyEvent() - t = e.eventType() - self.assertEqual(t.name, None) class EnumOverloadTest(unittest.TestCase): '''Test case for overloads involving enums''' @@ -180,6 +126,7 @@ class EnumOverloadTest(unittest.TestCase): self.assertEqual(o.callWithEnum('', Event.ANY_EVENT, 9), 81) self.assertEqual(o.callWithEnum('', 9), 9) + class EnumOperators(unittest.TestCase): '''Test case for operations on enums''' @@ -189,4 +136,3 @@ class EnumOperators(unittest.TestCase): if __name__ == '__main__': unittest.main() - |