tst_QScopeGuard: test if and how guard in optional<> works
It's a bit cumbersome, but works, in principle, using CTAD. Task-number: QTBUG-114200 Change-Id: Ib7354180e870a695a978edabf684aedfcf9d9ecc Reviewed-by: Jarek Kobus <jaroslaw.kobus@qt.io> (cherry picked from commit b08ddd2c4ecedccd0bc08e9f2390a7b86ed861f4) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
parent
a83d59b901
commit
f732833eaa
@ -5,6 +5,8 @@
|
|||||||
#include <QTest>
|
#include <QTest>
|
||||||
#include <QtCore/QScopeGuard>
|
#include <QtCore/QScopeGuard>
|
||||||
|
|
||||||
|
#include <optional>
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
\class tst_QScopeGuard
|
\class tst_QScopeGuard
|
||||||
\internal
|
\internal
|
||||||
@ -20,6 +22,7 @@ private Q_SLOTS:
|
|||||||
void construction();
|
void construction();
|
||||||
void constructionFromLvalue();
|
void constructionFromLvalue();
|
||||||
void constructionFromRvalue();
|
void constructionFromRvalue();
|
||||||
|
void optionalGuard();
|
||||||
void leavingScope();
|
void leavingScope();
|
||||||
void exceptions();
|
void exceptions();
|
||||||
};
|
};
|
||||||
@ -117,6 +120,24 @@ void tst_QScopeGuard::constructionFromRvalue()
|
|||||||
QCOMPARE(Callable::moved, 1);
|
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()
|
void tst_QScopeGuard::leavingScope()
|
||||||
{
|
{
|
||||||
auto cleanup = qScopeGuard([] { s_globalState++; QCOMPARE(s_globalState, 3); });
|
auto cleanup = qScopeGuard([] { s_globalState++; QCOMPARE(s_globalState, 3); });
|
||||||
|
Loading…
x
Reference in New Issue
Block a user