aboutsummaryrefslogtreecommitdiffstats
path: root/sources
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@qt.io>2025-12-02 11:01:19 +0100
committerFriedemann Kleint <Friedemann.Kleint@qt.io>2025-12-03 15:25:46 +0100
commit39c74e4317c1f4b34a2d0d66a721b44a5adc652f (patch)
tree5db77d05e47a74723fb8e944a012352cb8decf11 /sources
parent57d79452870c5d6358fcb519a58f76ca9b4edc61 (diff)
shiboken6: Split out the spaceship operator tests
On this occasion, add a test for a free operator<=>() behind guards. Task-number: PYSIDE-3245 Change-Id: Ife8f17add0a8f1c3bd1194a5dfabce4f97c78006 Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
Diffstat (limited to 'sources')
-rw-r--r--sources/shiboken6/tests/libsample/CMakeLists.txt1
-rw-r--r--sources/shiboken6/tests/libsample/oddbool.cpp4
-rw-r--r--sources/shiboken6/tests/libsample/oddbool.h21
-rw-r--r--sources/shiboken6/tests/libsample/spaceship.cpp27
-rw-r--r--sources/shiboken6/tests/libsample/spaceship.h47
-rw-r--r--sources/shiboken6/tests/samplebinding/CMakeLists.txt1
-rw-r--r--sources/shiboken6/tests/samplebinding/global.h1
-rw-r--r--sources/shiboken6/tests/samplebinding/oddbool_test.py15
-rw-r--r--sources/shiboken6/tests/samplebinding/samplebinding.pyproject1
-rw-r--r--sources/shiboken6/tests/samplebinding/spaceship_test.py44
-rw-r--r--sources/shiboken6/tests/samplebinding/typesystem_sample.xml1
11 files changed, 124 insertions, 39 deletions
diff --git a/sources/shiboken6/tests/libsample/CMakeLists.txt b/sources/shiboken6/tests/libsample/CMakeLists.txt
index fc98d404a..840a981af 100644
--- a/sources/shiboken6/tests/libsample/CMakeLists.txt
+++ b/sources/shiboken6/tests/libsample/CMakeLists.txt
@@ -70,6 +70,7 @@ simplefile.cpp simplefile.h
size.cpp size.h
snakecasetest.cpp snakecasetest.h
sometime.cpp sometime.h
+spaceship.cpp spaceship.h
str.cpp str.h
strlist.cpp strlist.h
templateptr.cpp templateptr.h
diff --git a/sources/shiboken6/tests/libsample/oddbool.cpp b/sources/shiboken6/tests/libsample/oddbool.cpp
index bc1ee833f..5fde578a1 100644
--- a/sources/shiboken6/tests/libsample/oddbool.cpp
+++ b/sources/shiboken6/tests/libsample/oddbool.cpp
@@ -21,7 +21,3 @@ int ComparisonTester::compare(const ComparisonTester &rhs) const
return 1;
return 0;
}
-
-SpaceshipComparisonTester::SpaceshipComparisonTester(int v) : m_value(v)
-{
-}
diff --git a/sources/shiboken6/tests/libsample/oddbool.h b/sources/shiboken6/tests/libsample/oddbool.h
index dd2d32604..852351dd4 100644
--- a/sources/shiboken6/tests/libsample/oddbool.h
+++ b/sources/shiboken6/tests/libsample/oddbool.h
@@ -8,10 +8,6 @@
#include <type_traits>
-#if __cplusplus >= 202002 || (defined(_MSVC_LANG) && _MSVC_LANG >= 202002)
-# include <compare>
-#endif
-
class OddBool
{
@@ -86,21 +82,4 @@ inline std::enable_if<std::is_assignable<ComparisonTester, int>::value, bool>::t
operator!=(const ComparisonTester &c1, const ComparisonTester &c2)
{ return c1.compare(c2) != 0; }
-class LIBSAMPLE_API SpaceshipComparisonTester
-{
-public:
- explicit SpaceshipComparisonTester(int v);
-
-#if __cplusplus >= 202002 || (defined(_MSVC_LANG) && _MSVC_LANG >= 202002)
- auto operator<=>(const SpaceshipComparisonTester &rhs) const = default;
-
- enum Enabled { HasSpaceshipOperator = 1 };
-#else
- enum Enabled { HasSpaceshipOperator = 0 };
-#endif // C++ 20
-
-private:
- int m_value;
-};
-
#endif // ODDBOOL_H
diff --git a/sources/shiboken6/tests/libsample/spaceship.cpp b/sources/shiboken6/tests/libsample/spaceship.cpp
new file mode 100644
index 000000000..b30f2f30f
--- /dev/null
+++ b/sources/shiboken6/tests/libsample/spaceship.cpp
@@ -0,0 +1,27 @@
+// Copyright (C) 2025 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
+
+#include "spaceship.h"
+
+SpaceshipComparisonTester::SpaceshipComparisonTester(int v) noexcept
+ : m_value(v)
+{
+}
+
+FreeSpaceshipComparisonTester::FreeSpaceshipComparisonTester(int v) noexcept
+ : m_value(v)
+{
+}
+
+int FreeSpaceshipComparisonTester::value() const
+{
+ return m_value;
+}
+
+#if __cplusplus >= 202002 || (defined(_MSVC_LANG) && _MSVC_LANG >= 202002)
+std::strong_ordering operator<=>(FreeSpaceshipComparisonTester lhs,
+ FreeSpaceshipComparisonTester rhs)
+{
+ return lhs.value() <=> rhs.value();
+}
+#endif // C++ 20
diff --git a/sources/shiboken6/tests/libsample/spaceship.h b/sources/shiboken6/tests/libsample/spaceship.h
new file mode 100644
index 000000000..0d0854fe6
--- /dev/null
+++ b/sources/shiboken6/tests/libsample/spaceship.h
@@ -0,0 +1,47 @@
+// Copyright (C) 2025 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
+
+#ifndef SPACESHIP_H
+#define SPACESHIP_H
+
+#include "libsamplemacros.h"
+
+#if __cplusplus >= 202002 || (defined(_MSVC_LANG) && _MSVC_LANG >= 202002)
+# include <compare>
+#endif
+
+class LIBSAMPLE_API SpaceshipComparisonTester
+{
+public:
+ explicit SpaceshipComparisonTester(int v) noexcept;
+
+#if __cplusplus >= 202002 || (defined(_MSVC_LANG) && _MSVC_LANG >= 202002)
+ auto operator<=>(const SpaceshipComparisonTester &rhs) const = default;
+
+ enum Enabled { HasSpaceshipOperator = 1 };
+#else
+ enum Enabled { HasSpaceshipOperator = 0 };
+#endif // C++ 20
+
+private:
+ int m_value;
+};
+
+class LIBSAMPLE_API FreeSpaceshipComparisonTester
+{
+public:
+ explicit FreeSpaceshipComparisonTester(int v) noexcept;
+
+ int value() const;
+
+private:
+ int m_value;
+};
+
+#if __cplusplus >= 202002 || (defined(_MSVC_LANG) && _MSVC_LANG >= 202002)
+// Does not provide equality as it is not defaulted.
+LIBSAMPLE_API std::strong_ordering operator<=>(FreeSpaceshipComparisonTester lhs,
+ FreeSpaceshipComparisonTester rhs);
+#endif // C++ 20
+
+#endif // SPACESHIP_H
diff --git a/sources/shiboken6/tests/samplebinding/CMakeLists.txt b/sources/shiboken6/tests/samplebinding/CMakeLists.txt
index 54bdc419c..c9e4f601f 100644
--- a/sources/shiboken6/tests/samplebinding/CMakeLists.txt
+++ b/sources/shiboken6/tests/samplebinding/CMakeLists.txt
@@ -38,6 +38,7 @@ ${CMAKE_CURRENT_BINARY_DIR}/sample/echo_wrapper.cpp
${CMAKE_CURRENT_BINARY_DIR}/sample/event_wrapper.cpp
${CMAKE_CURRENT_BINARY_DIR}/sample/expression_wrapper.cpp
${CMAKE_CURRENT_BINARY_DIR}/sample/exceptiontest_wrapper.cpp
+${CMAKE_CURRENT_BINARY_DIR}/sample/freespaceshipcomparisontester_wrapper.cpp
${CMAKE_CURRENT_BINARY_DIR}/sample/friendofonlycopy_wrapper.cpp
${CMAKE_CURRENT_BINARY_DIR}/sample/handleholder_wrapper.cpp
${CMAKE_CURRENT_BINARY_DIR}/sample/implicitconv_wrapper.cpp
diff --git a/sources/shiboken6/tests/samplebinding/global.h b/sources/shiboken6/tests/samplebinding/global.h
index 1de25224f..ec66687a6 100644
--- a/sources/shiboken6/tests/samplebinding/global.h
+++ b/sources/shiboken6/tests/samplebinding/global.h
@@ -61,6 +61,7 @@
#include "simplefile.h"
#include "size.h"
#include "snakecasetest.h"
+#include "spaceship.h"
#include "str.h"
#include "strlist.h"
#include "sometime.h"
diff --git a/sources/shiboken6/tests/samplebinding/oddbool_test.py b/sources/shiboken6/tests/samplebinding/oddbool_test.py
index 2d48556de..290d9de61 100644
--- a/sources/shiboken6/tests/samplebinding/oddbool_test.py
+++ b/sources/shiboken6/tests/samplebinding/oddbool_test.py
@@ -14,7 +14,7 @@ sys.path.append(os.fspath(Path(__file__).resolve().parents[1]))
from shiboken_paths import init_paths
init_paths()
-from sample import OddBoolUser, ComparisonTester, SpaceshipComparisonTester
+from sample import OddBoolUser, ComparisonTester
class DerivedOddBoolUser (OddBoolUser):
@@ -62,19 +62,6 @@ class OddBoolTest(unittest.TestCase):
t2 = ComparisonTester(42)
self.assertEqual(t1, t2)
- def testSpaceshipOperator(self):
- if not SpaceshipComparisonTester.Enabled.HasSpaceshipOperator:
- print("Skipping Spaceship Operator test")
- return
- t1 = SpaceshipComparisonTester(42)
- t2 = SpaceshipComparisonTester(42)
- self.assertEqual(t1, t2)
- self.assertTrue(t1 <= t2)
- self.assertTrue(t1 >= t2)
- t2 = SpaceshipComparisonTester(43)
- self.assertTrue(t1 < t2)
- self.assertFalse(t1 > t2)
-
if __name__ == '__main__':
unittest.main()
diff --git a/sources/shiboken6/tests/samplebinding/samplebinding.pyproject b/sources/shiboken6/tests/samplebinding/samplebinding.pyproject
index ba6ba6f8f..7e39631b4 100644
--- a/sources/shiboken6/tests/samplebinding/samplebinding.pyproject
+++ b/sources/shiboken6/tests/samplebinding/samplebinding.pyproject
@@ -109,6 +109,7 @@
"simplefile_test.py",
"size_test.py",
"snakecase_test.py",
+ "spaceship_test.py",
"static_nonstatic_methods_test.py",
"str_test.py",
"strlist_test.py",
diff --git a/sources/shiboken6/tests/samplebinding/spaceship_test.py b/sources/shiboken6/tests/samplebinding/spaceship_test.py
new file mode 100644
index 000000000..92d65d4ee
--- /dev/null
+++ b/sources/shiboken6/tests/samplebinding/spaceship_test.py
@@ -0,0 +1,44 @@
+#!/usr/bin/env python
+# Copyright (C) 2025 The Qt Company Ltd.
+# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
+from __future__ import annotations
+
+'''Test cases for C++ 20 spaceship operators.'''
+
+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 sample import FreeSpaceshipComparisonTester, SpaceshipComparisonTester
+
+
+class SpaceshipTest(unittest.TestCase):
+
+ @unittest.skipUnless(SpaceshipComparisonTester.Enabled.HasSpaceshipOperator, "< C++ 20")
+ def testSpaceshipOperator(self):
+ t1 = SpaceshipComparisonTester(42)
+ t2 = SpaceshipComparisonTester(42)
+ self.assertEqual(t1, t2)
+ self.assertTrue(t1 <= t2)
+ self.assertTrue(t1 >= t2)
+ t2 = SpaceshipComparisonTester(43)
+ self.assertTrue(t1 < t2)
+ self.assertFalse(t1 > t2)
+
+ @unittest.skipUnless(SpaceshipComparisonTester.Enabled.HasSpaceshipOperator, "< C++ 20")
+ def testFreeSpaceshipOperator(self):
+ """Test a free operator<=>(). It does not provide equality
+ as it is not defaulted."""
+ t1 = FreeSpaceshipComparisonTester(1)
+ t2 = FreeSpaceshipComparisonTester(2)
+ self.assertTrue(t1 < t2)
+ self.assertFalse(t1 > t2)
+
+
+if __name__ == '__main__':
+ unittest.main()
diff --git a/sources/shiboken6/tests/samplebinding/typesystem_sample.xml b/sources/shiboken6/tests/samplebinding/typesystem_sample.xml
index 711db9c4a..305448b93 100644
--- a/sources/shiboken6/tests/samplebinding/typesystem_sample.xml
+++ b/sources/shiboken6/tests/samplebinding/typesystem_sample.xml
@@ -159,6 +159,7 @@
<value-type name="SpaceshipComparisonTester">
<enum-type name="Enabled"/>
</value-type>
+ <value-type name="FreeSpaceshipComparisonTester"/>
<primitive-type name="PStr">
<include file-name="str.h" location="global"/>