From c69b1736f208ff78c9483dfc13081c806f9374dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8ger=20Hanseg=C3=A5rd?= Date: Wed, 20 Mar 2024 18:17:53 +0100 Subject: 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 (cherry picked from commit 6af4c86b2004cf539d8344b09d38abf515e964e1) Reviewed-by: Qt Cherry-pick Bot (cherry picked from commit eb7e0047ecaff3ff2a8c329a442f3f5670663025) --- tests/auto/unit/multimedia/CMakeLists.txt | 1 + tests/auto/unit/multimedia/qmaybe/CMakeLists.txt | 9 ++ tests/auto/unit/multimedia/qmaybe/tst_qmaybe.cpp | 124 +++++++++++++++++++++++ 3 files changed, 134 insertions(+) create mode 100644 tests/auto/unit/multimedia/qmaybe/CMakeLists.txt create mode 100644 tests/auto/unit/multimedia/qmaybe/tst_qmaybe.cpp 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 +#include +#include +#ifdef Q_OS_WINDOWS +#include +#include +#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; + +#ifdef Q_OS_WINDOWS +struct DummyComObject : QComObject +{ +}; +#endif + +} // namespace + +//clang-format off + +class tst_QMaybe : public QObject +{ + Q_OBJECT + +private slots: + void operatorBool_returnsFalse_onlyWhenErrorSet() + { + { + const QMaybe error{ -1 }; // TOOD: Is it safe to deduce expected/unexpected only based on type? + QVERIFY(!static_cast(error)); + } + + { + const QMaybe success{ "It worked!"_L1 }; + QVERIFY(static_cast(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>); // And it is const + } + } + + void dereferenceOperator_returnsPointerToValue_whenValueTypeOverloadsAddressOfOperator() + { + { + QMaybe mutableValue{ DummyHandle{ 1 } }; + QCOMPARE_EQ(mutableValue->get(), 1); + QVERIFY(mutableValue->isValid()); // We did not accidentally call operator& that resets + // QUniqueHandle + } + + { + const QMaybe 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, HRESULT> mutableObject{ + makeComObject() + }; + QCOMPARE_NE(mutableObject->Get(), nullptr); + + const ComPtr unknownFromMutable = mutableObject.value(); + QVERIFY(unknownFromMutable); // We did not accidentally call operator& that resets + // QUniqueHandle + } + + { + QMaybe, HRESULT> immutableObject{ + makeComObject() + }; + QCOMPARE_NE(immutableObject->Get(), nullptr); + + const ComPtr 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 -- cgit v1.2.3