diff options
author | Jøger Hansegård <joger.hansegard@qt.io> | 2024-03-20 18:17:53 +0100 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2024-03-25 16:44:44 +0000 |
commit | c69b1736f208ff78c9483dfc13081c806f9374dc (patch) | |
tree | 40a808333ef262af975f16881628f757e4fc031f | |
parent | bf8ffdd473ba3f25334c827d571a7cdb520258d1 (diff) |
Add some auto tests for QMaybe
Having tests helps keeping QMaybe maintainable and with documented
requirements.
Pick-to: 6.5
Change-Id: If5bcd320d811e7698f66523a7180a95b94759e02
Reviewed-by: Artem Dyomin <artem.dyomin@qt.io>
(cherry picked from commit 6af4c86b2004cf539d8344b09d38abf515e964e1)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
(cherry picked from commit eb7e0047ecaff3ff2a8c329a442f3f5670663025)
-rw-r--r-- | tests/auto/unit/multimedia/CMakeLists.txt | 1 | ||||
-rw-r--r-- | tests/auto/unit/multimedia/qmaybe/CMakeLists.txt | 9 | ||||
-rw-r--r-- | tests/auto/unit/multimedia/qmaybe/tst_qmaybe.cpp | 124 |
3 files changed, 134 insertions, 0 deletions
diff --git a/tests/auto/unit/multimedia/CMakeLists.txt b/tests/auto/unit/multimedia/CMakeLists.txt index dd43a2585..c6663a800 100644 --- a/tests/auto/unit/multimedia/CMakeLists.txt +++ b/tests/auto/unit/multimedia/CMakeLists.txt @@ -25,6 +25,7 @@ add_subdirectory(qaudiodecoder) add_subdirectory(qsamplecache) add_subdirectory(qscreencapture) add_subdirectory(qvideotexturehelper) +add_subdirectory(qmaybe) add_subdirectory(qmediadevices) add_subdirectory(qerrorinfo) add_subdirectory(qvideobuffers) diff --git a/tests/auto/unit/multimedia/qmaybe/CMakeLists.txt b/tests/auto/unit/multimedia/qmaybe/CMakeLists.txt new file mode 100644 index 000000000..a9053e87c --- /dev/null +++ b/tests/auto/unit/multimedia/qmaybe/CMakeLists.txt @@ -0,0 +1,9 @@ +# Copyright (C) 2024 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause + +qt_internal_add_test(tst_qmaybe + SOURCES + tst_qmaybe.cpp + LIBRARIES + Qt::MultimediaPrivate +) diff --git a/tests/auto/unit/multimedia/qmaybe/tst_qmaybe.cpp b/tests/auto/unit/multimedia/qmaybe/tst_qmaybe.cpp new file mode 100644 index 000000000..e195346ae --- /dev/null +++ b/tests/auto/unit/multimedia/qmaybe/tst_qmaybe.cpp @@ -0,0 +1,124 @@ +// Copyright (C) 2024 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 + +#include <QtTest/QtTest> +#include <private/qmaybe_p.h> +#include <QtCore/private/quniquehandle_p.h> +#ifdef Q_OS_WINDOWS +#include <private/qcomptr_p.h> +#include <private/qcomobject_p.h> +#endif + +QT_USE_NAMESPACE + +using namespace Qt::StringLiterals; + +namespace { + +// Helpers used to verify interop with QUniqueHandle and ComPtr which +// overloads operator& +struct DummyHandleTraits +{ + using Type = int; + static Type invalidValue() { return -1; } + static bool close(Type handle) { return true; } +}; + +using DummyHandle = QUniqueHandle<DummyHandleTraits>; + +#ifdef Q_OS_WINDOWS +struct DummyComObject : QComObject<IUnknown> +{ +}; +#endif + +} // namespace + +//clang-format off + +class tst_QMaybe : public QObject +{ + Q_OBJECT + +private slots: + void operatorBool_returnsFalse_onlyWhenErrorSet() + { + { + const QMaybe<QString, int> error{ -1 }; // TOOD: Is it safe to deduce expected/unexpected only based on type? + QVERIFY(!static_cast<bool>(error)); + } + + { + const QMaybe<QString, int> success{ "It worked!"_L1 }; + QVERIFY(static_cast<bool>(success)); + } + } + + void value_returnsReferenceToValue_whenValueSet() + { + { + QMaybe mutableVal{ 1 }; + mutableVal.value() = 2; + QCOMPARE_EQ(*mutableVal, 2); // value() must have returned a mutable reference + } + + { + const QMaybe immutableVal{ 2 }; + QCOMPARE_EQ(*std::addressof(immutableVal.value()), 2); // value() must have returned a reference + static_assert(std::is_const_v<std::remove_reference_t<decltype(immutableVal.value())>>); // And it is const + } + } + + void dereferenceOperator_returnsPointerToValue_whenValueTypeOverloadsAddressOfOperator() + { + { + QMaybe<DummyHandle, int> mutableValue{ DummyHandle{ 1 } }; + QCOMPARE_EQ(mutableValue->get(), 1); + QVERIFY(mutableValue->isValid()); // We did not accidentally call operator& that resets + // QUniqueHandle + } + + { + const QMaybe<DummyHandle, int> immutableValue{ DummyHandle{ 2 } }; + QCOMPARE_EQ(immutableValue->get(), 2); + QVERIFY(immutableValue->isValid()); // We did not accidentally call operator& that + // resets QUniqueHandle + } + } + +#ifdef Q_OS_WINDOWS + void dereferenceOperator_returnsPointerToValue_whenValueIsComPtr() + { + // Similar test as with QUniqueHandle, but with ComPtr that is used + // frequently on Windows and may behave slightly differently + + { + QMaybe<ComPtr<DummyComObject>, HRESULT> mutableObject{ + makeComObject<DummyComObject>() + }; + QCOMPARE_NE(mutableObject->Get(), nullptr); + + const ComPtr<IUnknown> unknownFromMutable = mutableObject.value(); + QVERIFY(unknownFromMutable); // We did not accidentally call operator& that resets + // QUniqueHandle + } + + { + QMaybe<ComPtr<DummyComObject>, HRESULT> immutableObject{ + makeComObject<DummyComObject>() + }; + QCOMPARE_NE(immutableObject->Get(), nullptr); + + const ComPtr<IUnknown> unknownFromImmutable = immutableObject.value(); + QVERIFY(unknownFromImmutable); // We did not accidentally call operator& that resets + // QUniqueHandle + } + } +#endif +}; + +QTEST_APPLESS_MAIN(tst_QMaybe) + +#include "tst_qmaybe.moc" + +//clang-format on |