summaryrefslogtreecommitdiffstats
path: root/async/autotests/asynctest.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'async/autotests/asynctest.cpp')
-rw-r--r--async/autotests/asynctest.cpp98
1 files changed, 98 insertions, 0 deletions
diff --git a/async/autotests/asynctest.cpp b/async/autotests/asynctest.cpp
new file mode 100644
index 0000000..f2a70a4
--- /dev/null
+++ b/async/autotests/asynctest.cpp
@@ -0,0 +1,98 @@
1/*
2 * Copyright 2014 Daniel Vrátil <dvratil@redhat.com>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of
7 * the License or (at your option) version 3 or any later version
8 * accepted by the membership of KDE e.V. (or its successor approved
9 * by the membership of KDE e.V.), which shall act as a proxy
10 * defined in Section 14 of version 3 of the license.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
22#include "../src/async.h"
23
24#include <QObject>
25#include <QString>
26#include <QTimer>
27#include <QtTest/QTest>
28
29class AsyncTest : public QObject
30{
31 Q_OBJECT
32
33public:
34 AsyncTest()
35 {}
36
37 ~AsyncTest()
38 {}
39
40private Q_SLOTS:
41 void testSyncPromises();
42 void testAsyncPromises();
43};
44
45void AsyncTest::testSyncPromises()
46{
47 auto baseJob = Async::start<int>(
48 []() -> Async::Future<int> {
49 auto f = Async::Future<int>(42);
50 f.setFinished();
51 return f;
52 })
53 .then<QString, int>(
54 [](int v) -> Async::Future<QString> {
55 auto f = Async::Future<QString>("Result is " + QString::number(v));
56 f.setFinished();
57 return f;
58 });
59
60 auto job = baseJob.then<QString, QString>(
61 [](const QString &v) -> Async::Future<QString> {
62 auto f = Async::Future<QString>(v.toUpper());
63 f.setFinished();
64 return f;
65 });
66
67 job.exec();
68 Async::Future<QString> future = job.result();
69
70 QCOMPARE(future.value(), QString::fromLatin1("RESULT IS 42"));
71}
72
73void AsyncTest::testAsyncPromises()
74{
75 auto job = Async::start<int>(
76 []() -> Async::Future<int> {
77 Async::Future<int> future(-1);
78 QTimer *timer = new QTimer();
79 QObject::connect(timer, &QTimer::timeout,
80 [&]() {
81 future.setValue(42);
82 future.setFinished();
83 timer->deleteLater();
84 });
85 timer->setSingleShot(true);
86 timer->start(200);
87 return future;
88 });
89
90 job.exec();
91 Async::Future<int> future = job.result();
92 QCOMPARE(future.value(), 42);
93}
94
95
96QTEST_MAIN(AsyncTest);
97
98#include "asynctest.moc"