summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib/tools
diff options
context:
space:
mode:
authorMarc Mutz <marc.mutz@qt.io>2023-06-05 07:38:55 +0200
committerMarc Mutz <marc.mutz@qt.io>2023-07-10 19:47:08 +0000
commitb08ddd2c4ecedccd0bc08e9f2390a7b86ed861f4 (patch)
treebeca07d761c7ff3be6cd866b36ebadfc7669561a /tests/auto/corelib/tools
parent8b4dbce54e5e1bf53a1610ac0010e236fc9b2be9 (diff)
tst_QScopeGuard: test if and how guard in optional<> works
It's a bit cumbersome, but works, in principle, using CTAD. Pick-to: 6.6 6.5 6.2 Task-number: QTBUG-114200 Change-Id: Ib7354180e870a695a978edabf684aedfcf9d9ecc Reviewed-by: Jarek Kobus <jaroslaw.kobus@qt.io>
Diffstat (limited to 'tests/auto/corelib/tools')
-rw-r--r--tests/auto/corelib/tools/qscopeguard/tst_qscopeguard.cpp21
1 files changed, 21 insertions, 0 deletions
diff --git a/tests/auto/corelib/tools/qscopeguard/tst_qscopeguard.cpp b/tests/auto/corelib/tools/qscopeguard/tst_qscopeguard.cpp
index 015cbcc0e7..ac6551645f 100644
--- a/tests/auto/corelib/tools/qscopeguard/tst_qscopeguard.cpp
+++ b/tests/auto/corelib/tools/qscopeguard/tst_qscopeguard.cpp
@@ -5,6 +5,8 @@
#include <QTest>
#include <QtCore/QScopeGuard>
+#include <optional>
+
/*!
\class tst_QScopeGuard
\internal
@@ -20,6 +22,7 @@ private Q_SLOTS:
void construction();
void constructionFromLvalue();
void constructionFromRvalue();
+ void optionalGuard();
void leavingScope();
void exceptions();
};
@@ -117,6 +120,24 @@ void tst_QScopeGuard::constructionFromRvalue()
QCOMPARE(Callable::moved, 1);
}
+void tst_QScopeGuard::optionalGuard()
+{
+ int i = 0;
+ auto lambda = [&] { ++i; };
+ std::optional sg = false ? std::optional{qScopeGuard(lambda)} : std::nullopt;
+ QVERIFY(!sg);
+ QCOMPARE(i, 0);
+ sg.emplace(qScopeGuard(lambda));
+ QVERIFY(sg);
+ sg->dismiss();
+ sg.reset();
+ QCOMPARE(i, 0);
+ sg.emplace(qScopeGuard(lambda));
+ QCOMPARE(i, 0);
+ sg.reset();
+ QCOMPARE(i, 1);
+}
+
void tst_QScopeGuard::leavingScope()
{
auto cleanup = qScopeGuard([] { s_globalState++; QCOMPARE(s_globalState, 3); });