qtpromise/tests/auto/qpromise/tst_qpromise.cpp

183 lines
4.4 KiB
C++
Raw Normal View History

2017-05-15 01:03:01 +08:00
// QtPromise
#include <qtpromise/qpromise.h>
// Qt
#include <QtTest>
using namespace QtPromise;
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()
{
{ // fulfilled
2017-05-15 01:03:01 +08:00
QVector<int> values;
auto next = QPromise<int>::resolve(42).finally([&]() {
2017-05-15 01:03:01 +08:00
values << 8;
return 16; // ignored!
2017-05-15 01:03:01 +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}));
}
{ // 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
});
next.fail([&](const QString& err) {
error = err;
return 42;
2017-05-15 01:03:01 +08:00
}).wait();
QVERIFY(next.isRejected());
QCOMPARE(error, QString("foo"));
QCOMPARE(value, 8);
2017-05-15 01:03:01 +08:00
}
}
void tst_qpromise::finallyThrows()
{
{ // fulfilled
2017-05-15 01:03:01 +08:00
QString error;
auto next = QPromise<int>::resolve(42).finally([&]() {
2017-05-15 01:03:01 +08:00
throw QString("bar");
});
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"));
}
{ // rejected
2017-05-15 01:03:01 +08:00
QString error;
auto next = QPromise<int>::reject(QString("foo")).finally([&]() {
2017-05-15 01:03:01 +08:00
throw QString("bar");
});
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()
{
{ // fulfilled
2017-05-15 01:03:01 +08:00
QVector<int> values;
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;
return p;
2017-05-15 01:03:01 +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}));
}
{ // rejected
QString error;
2017-05-15 01:03:01 +08:00
QVector<int> values;
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;
return p;
2017-05-15 01:03:01 +08:00
});
next.then([&](int r) {
2017-05-15 01:03:01 +08:00
values << r;
}, [&](const QString& err) {
error = err;
2017-05-15 01:03:01 +08:00
}).wait();
QVERIFY(next.isRejected());
QCOMPARE(error, QString("foo"));
2017-05-15 01:03:01 +08:00
QCOMPARE(values, QVector<int>({8, 64}));
}
}
void tst_qpromise::finallyDelayedRejected()
{
{ // fulfilled
2017-05-15 01:03:01 +08:00
QString error;
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
});
});
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"));
}
{ // rejected
2017-05-15 01:03:01 +08:00
QString error;
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
});
});
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"));
}
}