File indexing completed on 2024-05-26 05:13:50

0001 /*
0002     SPDX-FileCopyrightText: 2019 Daniel Vrátil <dvratil@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include <QObject>
0008 #include <QTest>
0009 
0010 #include "shared/akscopeguard.h"
0011 
0012 using namespace Akonadi;
0013 
0014 class AkScopeGuardTest : public QObject
0015 {
0016     Q_OBJECT
0017 
0018 private:
0019     static void staticMethod()
0020     {
0021         Q_ASSERT(!mCalled);
0022         mCalled = true;
0023     }
0024 
0025     void regularMethod()
0026     {
0027         Q_ASSERT(!mCalled);
0028         mCalled = true;
0029     }
0030 
0031 private Q_SLOTS:
0032 
0033     void testLambda()
0034     {
0035         mCalled = false;
0036         {
0037             AkScopeGuard guard([&]() {
0038                 Q_ASSERT(!mCalled);
0039                 mCalled = true;
0040             });
0041         }
0042         QVERIFY(mCalled);
0043     }
0044 
0045     void testStaticMethod()
0046     {
0047         mCalled = false;
0048         {
0049             AkScopeGuard guard(&AkScopeGuardTest::staticMethod);
0050         }
0051         QVERIFY(mCalled);
0052     }
0053 
0054     void testBindExpr()
0055     {
0056         mCalled = false;
0057         {
0058             AkScopeGuard guard(std::bind(&AkScopeGuardTest::regularMethod, this));
0059         }
0060         QVERIFY(mCalled);
0061     }
0062 
0063     void testStdFunction()
0064     {
0065         mCalled = false;
0066         std::function<void()> func = [&]() {
0067             Q_ASSERT(!mCalled);
0068             mCalled = true;
0069         };
0070         {
0071             AkScopeGuard guard(func);
0072         }
0073         QVERIFY(mCalled);
0074     }
0075 
0076 private:
0077     static bool mCalled;
0078 };
0079 
0080 bool AkScopeGuardTest::mCalled = false;
0081 
0082 QTEST_GUILESS_MAIN(AkScopeGuardTest)
0083 
0084 #include "akscopeguardtest.moc"