Allow undefined rejection reason

While not recommended because it makes tracking errors more difficult, it's now possible to reject a promise without explicit reason, in which case, a built-in `QPromiseUndefinedException` is thrown. This is done in anticipation of handling rejection signals without argument.
This commit is contained in:
Simon Brunel
2019-01-31 17:59:39 +01:00
parent 16229fc2c9
commit fa5a4192ff
5 changed files with 96 additions and 0 deletions

View File

@ -33,6 +33,8 @@ private Q_SLOTS:
void rejectSync_void();
void rejectAsync();
void rejectAsync_void();
void rejectUndefined();
void rejectUndefined_void();
void connectAndResolve();
void connectAndReject();
};
@ -234,6 +236,30 @@ void tst_qpromise_construct::rejectThrowTwoArgs_void()
QCOMPARE(waitForError(p, QString()), QString("foo"));
}
void tst_qpromise_construct::rejectUndefined()
{
QPromise<int> p([](const QPromiseResolve<int>&, const QPromiseReject<int>& reject) {
QtPromisePrivate::qtpromise_defer([=]() {
reject();
});
});
QCOMPARE(p.isPending(), true);
QCOMPARE(waitForRejected<QPromiseUndefinedException>(p), true);
}
void tst_qpromise_construct::rejectUndefined_void()
{
QPromise<void> p([](const QPromiseResolve<void>&, const QPromiseReject<void>& reject) {
QtPromisePrivate::qtpromise_defer([=]() {
reject();
});
});
QCOMPARE(p.isPending(), true);
QCOMPARE(waitForRejected<QPromiseUndefinedException>(p), true);
}
// https://github.com/simonbrunel/qtpromise/issues/6
void tst_qpromise_construct::connectAndResolve()
{

View File

@ -44,4 +44,14 @@ static inline E waitForError(const QtPromise::QPromise<void>& promise, const E&
return error;
}
template <typename E, typename T>
static inline bool waitForRejected(const T& promise)
{
bool result = false;
promise.tapFail([&](const E&) {
result = true;
}).wait();
return result;
}
#endif // QTPROMISE_TESTS_AUTO_SHARED_UTILS_H