2017-05-15 01:03:01 +08:00
|
|
|
// QtPromise
|
|
|
|
#include <qtpromise/qpromise.h>
|
|
|
|
|
|
|
|
// Qt
|
|
|
|
#include <QtTest>
|
|
|
|
|
|
|
|
using namespace QtPromise;
|
2017-05-20 15:40:42 +08:00
|
|
|
using namespace QtPromisePrivate;
|
2017-05-15 01:03:01 +08:00
|
|
|
|
|
|
|
class tst_qpromise: public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
private Q_SLOTS:
|
|
|
|
void finallyReturns();
|
|
|
|
void finallyThrows();
|
|
|
|
void finallyDelayedFulfilled();
|
|
|
|
void finallyDelayedRejected();
|
|
|
|
|
|
|
|
}; // class tst_qpromise
|
|
|
|
|
|
|
|
QTEST_MAIN(tst_qpromise)
|
|
|
|
#include "tst_qpromise.moc"
|
|
|
|
|
|
|
|
void tst_qpromise::finallyReturns()
|
|
|
|
{
|
2017-05-20 15:40:42 +08:00
|
|
|
{ // fulfilled
|
2017-05-15 01:03:01 +08:00
|
|
|
QVector<int> values;
|
2017-05-20 15:40:42 +08:00
|
|
|
auto next = QPromise<int>::resolve(42).finally([&]() {
|
2017-05-15 01:03:01 +08:00
|
|
|
values << 8;
|
2017-05-20 15:40:42 +08:00
|
|
|
return 16; // ignored!
|
2017-05-15 01:03:01 +08:00
|
|
|
});
|
|
|
|
|
2017-05-20 15:40:42 +08:00
|
|
|
next.then([&](int r) {
|
2017-05-15 01:03:01 +08:00
|
|
|
values << r;
|
|
|
|
}).wait();
|
|
|
|
|
|
|
|
QVERIFY(next.isFulfilled());
|
|
|
|
QCOMPARE(values, QVector<int>({8, 42}));
|
|
|
|
}
|
2017-05-20 15:40:42 +08:00
|
|
|
{ // rejected
|
|
|
|
QString error;
|
|
|
|
int value = -1;
|
|
|
|
auto next = QPromise<int>([](const QPromiseResolve<int>) {
|
|
|
|
throw QString("foo");
|
|
|
|
}).finally([&]() {
|
|
|
|
value = 8;
|
|
|
|
return 16; // ignored!
|
2017-05-15 01:03:01 +08:00
|
|
|
});
|
|
|
|
|
2017-05-20 15:40:42 +08:00
|
|
|
next.fail([&](const QString& err) {
|
|
|
|
error = err;
|
|
|
|
return 42;
|
2017-05-15 01:03:01 +08:00
|
|
|
}).wait();
|
|
|
|
|
|
|
|
QVERIFY(next.isRejected());
|
2017-05-20 15:40:42 +08:00
|
|
|
QCOMPARE(error, QString("foo"));
|
|
|
|
QCOMPARE(value, 8);
|
2017-05-15 01:03:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void tst_qpromise::finallyThrows()
|
|
|
|
{
|
2017-05-20 15:40:42 +08:00
|
|
|
{ // fulfilled
|
2017-05-15 01:03:01 +08:00
|
|
|
QString error;
|
2017-05-20 15:40:42 +08:00
|
|
|
auto next = QPromise<int>::resolve(42).finally([&]() {
|
2017-05-15 01:03:01 +08:00
|
|
|
throw QString("bar");
|
|
|
|
});
|
|
|
|
|
2017-05-20 15:40:42 +08:00
|
|
|
next.fail([&](const QString& err) {
|
2017-05-15 01:03:01 +08:00
|
|
|
error = err;
|
|
|
|
return 0;
|
|
|
|
}).wait();
|
|
|
|
|
|
|
|
QVERIFY(next.isRejected());
|
|
|
|
QCOMPARE(error, QString("bar"));
|
|
|
|
}
|
2017-05-20 15:40:42 +08:00
|
|
|
{ // rejected
|
2017-05-15 01:03:01 +08:00
|
|
|
QString error;
|
2017-05-20 15:40:42 +08:00
|
|
|
auto next = QPromise<int>::reject(QString("foo")).finally([&]() {
|
2017-05-15 01:03:01 +08:00
|
|
|
throw QString("bar");
|
|
|
|
});
|
|
|
|
|
2017-05-20 15:40:42 +08:00
|
|
|
next.fail([&](const QString& err) {
|
2017-05-15 01:03:01 +08:00
|
|
|
error = err;
|
|
|
|
return 0;
|
|
|
|
}).wait();
|
|
|
|
|
|
|
|
QVERIFY(next.isRejected());
|
|
|
|
QCOMPARE(error, QString("bar"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void tst_qpromise::finallyDelayedFulfilled()
|
|
|
|
{
|
2017-05-20 15:40:42 +08:00
|
|
|
{ // fulfilled
|
2017-05-15 01:03:01 +08:00
|
|
|
QVector<int> values;
|
2017-05-20 15:40:42 +08:00
|
|
|
auto next = QPromise<int>::resolve(42).finally([&]() {
|
|
|
|
QPromise<int> p([&](const QPromiseResolve<int>& resolve) {
|
|
|
|
qtpromise_defer([=, &values]() {
|
|
|
|
values << 64;
|
|
|
|
resolve(16); // ignored!
|
|
|
|
});
|
2017-05-15 01:03:01 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
values << 8;
|
2017-05-20 15:40:42 +08:00
|
|
|
return p;
|
2017-05-15 01:03:01 +08:00
|
|
|
});
|
|
|
|
|
2017-05-20 15:40:42 +08:00
|
|
|
next.then([&](int r) {
|
2017-05-15 01:03:01 +08:00
|
|
|
values << r;
|
|
|
|
}).wait();
|
|
|
|
|
|
|
|
QVERIFY(next.isFulfilled());
|
|
|
|
QCOMPARE(values, QVector<int>({8, 64, 42}));
|
|
|
|
}
|
2017-05-20 15:40:42 +08:00
|
|
|
{ // rejected
|
|
|
|
QString error;
|
2017-05-15 01:03:01 +08:00
|
|
|
QVector<int> values;
|
2017-05-20 15:40:42 +08:00
|
|
|
auto next = QPromise<int>::reject(QString("foo")).finally([&]() {
|
|
|
|
QPromise<int> p([&](const QPromiseResolve<int>& resolve) {
|
|
|
|
qtpromise_defer([=, &values]() {
|
|
|
|
values << 64;
|
|
|
|
resolve(16); // ignored!
|
|
|
|
});
|
2017-05-15 01:03:01 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
values << 8;
|
2017-05-20 15:40:42 +08:00
|
|
|
return p;
|
2017-05-15 01:03:01 +08:00
|
|
|
});
|
|
|
|
|
2017-05-20 15:40:42 +08:00
|
|
|
next.then([&](int r) {
|
2017-05-15 01:03:01 +08:00
|
|
|
values << r;
|
2017-05-20 15:40:42 +08:00
|
|
|
}, [&](const QString& err) {
|
|
|
|
error = err;
|
2017-05-15 01:03:01 +08:00
|
|
|
}).wait();
|
|
|
|
|
|
|
|
QVERIFY(next.isRejected());
|
2017-05-20 15:40:42 +08:00
|
|
|
QCOMPARE(error, QString("foo"));
|
2017-05-15 01:03:01 +08:00
|
|
|
QCOMPARE(values, QVector<int>({8, 64}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void tst_qpromise::finallyDelayedRejected()
|
|
|
|
{
|
2017-05-20 15:40:42 +08:00
|
|
|
{ // fulfilled
|
2017-05-15 01:03:01 +08:00
|
|
|
QString error;
|
2017-05-20 15:40:42 +08:00
|
|
|
auto next = QPromise<int>::resolve(42).finally([]() {
|
|
|
|
return QPromise<int>([](const QPromiseResolve<int>&, const QPromiseReject<int>& reject) {
|
|
|
|
qtpromise_defer([=]() {
|
|
|
|
reject(QString("bar"));
|
|
|
|
});
|
2017-05-15 01:03:01 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-05-20 15:40:42 +08:00
|
|
|
next.fail([&](const QString& err) {
|
2017-05-15 01:03:01 +08:00
|
|
|
error = err;
|
|
|
|
return 0;
|
|
|
|
}).wait();
|
|
|
|
|
|
|
|
QVERIFY(next.isRejected());
|
|
|
|
QCOMPARE(error, QString("bar"));
|
|
|
|
}
|
2017-05-20 15:40:42 +08:00
|
|
|
{ // rejected
|
2017-05-15 01:03:01 +08:00
|
|
|
QString error;
|
2017-05-20 15:40:42 +08:00
|
|
|
auto next = QPromise<int>::reject(QString("foo")).finally([]() {
|
|
|
|
return QPromise<int>([](const QPromiseResolve<int>&, const QPromiseReject<int>& reject) {
|
|
|
|
qtpromise_defer([=]() {
|
|
|
|
reject(QString("bar"));
|
|
|
|
});
|
2017-05-15 01:03:01 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-05-20 15:40:42 +08:00
|
|
|
next.fail([&](const QString& err) {
|
2017-05-15 01:03:01 +08:00
|
|
|
error = err;
|
|
|
|
return 0;
|
|
|
|
}).wait();
|
|
|
|
|
|
|
|
QVERIFY(next.isRejected());
|
|
|
|
QCOMPARE(error, QString("bar"));
|
|
|
|
}
|
|
|
|
}
|