summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib/tools/qscopeguard/tst_qscopeguard.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'tests/auto/corelib/tools/qscopeguard/tst_qscopeguard.cpp')
-rw-r--r--tests/auto/corelib/tools/qscopeguard/tst_qscopeguard.cpp23
1 files changed, 22 insertions, 1 deletions
diff --git a/tests/auto/corelib/tools/qscopeguard/tst_qscopeguard.cpp b/tests/auto/corelib/tools/qscopeguard/tst_qscopeguard.cpp
index 015cbcc0e7..b7c2b952e2 100644
--- a/tests/auto/corelib/tools/qscopeguard/tst_qscopeguard.cpp
+++ b/tests/auto/corelib/tools/qscopeguard/tst_qscopeguard.cpp
@@ -1,10 +1,12 @@
// Copyright (C) 2018 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Sérgio Martins <sergio.martins@kdab.com>
// Copyright (C) 2020 The Qt Company Ltd.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#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); });