mirror of
https://github.com/simonbrunel/qtpromise.git
synced 2025-07-02 07:15:25 +08:00
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:
@ -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()
|
||||
{
|
||||
|
@ -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
|
||||
|
Reference in New Issue
Block a user