Changeset View
Changeset View
Standalone View
Standalone View
autotests/shared/akscopeguardtest.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | Copyright (C) 2019 Daniel Vrátil <dvratil@kde.org> | ||||
3 | | ||||
4 | This library is free software; you can redistribute it and/or modify it | ||||
5 | under the terms of the GNU Library General Public License as published by | ||||
6 | the Free Software Foundation; either version 2 of the License, or (at your | ||||
7 | option) any later version. | ||||
8 | | ||||
9 | This library is distributed in the hope that it will be useful, but WITHOUT | ||||
10 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||||
11 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public | ||||
12 | License for more details. | ||||
13 | | ||||
14 | You should have received a copy of the GNU Library General Public License | ||||
15 | along with this library; see the file COPYING.LIB. If not, write to the | ||||
16 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||||
17 | 02110-1301, USA. | ||||
18 | */ | ||||
19 | | ||||
20 | #include <QTest> | ||||
21 | #include <QObject> | ||||
22 | | ||||
23 | #include "shared/akscopeguard.h" | ||||
24 | | ||||
25 | using namespace Akonadi; | ||||
26 | | ||||
27 | class AkScopeGuardTest : public QObject | ||||
28 | { | ||||
29 | Q_OBJECT | ||||
30 | | ||||
31 | private: | ||||
32 | static void staticMethod() noexcept | ||||
33 | { | ||||
34 | Q_ASSERT(!mCalled); | ||||
35 | mCalled = true; | ||||
36 | } | ||||
37 | | ||||
38 | void regularMethod() noexcept | ||||
39 | { | ||||
40 | Q_ASSERT(!mCalled); | ||||
41 | mCalled = true; | ||||
42 | } | ||||
43 | | ||||
44 | private Q_SLOTS: | ||||
45 | | ||||
46 | void testLambda() | ||||
47 | { | ||||
48 | mCalled = false; | ||||
49 | { | ||||
50 | AkScopeGuard guard([&]() noexcept { | ||||
51 | Q_ASSERT(!mCalled); | ||||
52 | mCalled = true; | ||||
53 | }); | ||||
54 | } | ||||
55 | QVERIFY(mCalled); | ||||
56 | } | ||||
57 | | ||||
58 | void testStaticMethod() | ||||
59 | { | ||||
60 | mCalled = false; | ||||
61 | { | ||||
62 | AkScopeGuard guard(&AkScopeGuardTest::staticMethod); | ||||
63 | } | ||||
64 | QVERIFY(mCalled); | ||||
65 | } | ||||
66 | | ||||
67 | void testBindExpr() | ||||
68 | { | ||||
69 | mCalled = false; | ||||
70 | { | ||||
71 | AkScopeGuard guard(std::bind(&AkScopeGuardTest::regularMethod, this)); | ||||
72 | } | ||||
73 | QVERIFY(mCalled); | ||||
74 | } | ||||
75 | | ||||
76 | void testStdFunction() | ||||
77 | { | ||||
78 | mCalled = false; | ||||
79 | std::function<void() noexcept> func = [&]() noexcept { | ||||
80 | Q_ASSERT(!mCalled); | ||||
81 | mCalled = true; | ||||
82 | }; | ||||
83 | { | ||||
84 | AkScopeGuard guard(func); | ||||
85 | } | ||||
86 | QVERIFY(mCalled); | ||||
87 | } | ||||
88 | | ||||
89 | private: | ||||
90 | static bool mCalled; | ||||
91 | }; | ||||
92 | | ||||
93 | bool AkScopeGuardTest::mCalled = false; | ||||
94 | | ||||
95 | QTEST_GUILESS_MAIN(AkScopeGuardTest) | ||||
96 | | ||||
97 | #include "akscopeguardtest.moc" | ||||
98 | |