mirror of
https://github.com/simonbrunel/qtpromise.git
synced 2025-07-03 07:45:26 +08:00
Allow QSharedPointer as rejection reason
Embed the promise fulfillment value and rejection reason in respectively PromiseValue and PromiseError private wrappers, both storing the data in a shared pointer (QPromiseError is now deprecated).
This commit is contained in:
@ -6,6 +6,7 @@ SUBDIRS += \
|
||||
fail \
|
||||
finally \
|
||||
operators \
|
||||
reject \
|
||||
resolve \
|
||||
tap \
|
||||
tapfail \
|
||||
|
4
tests/auto/qtpromise/qpromise/reject/reject.pro
Normal file
4
tests/auto/qtpromise/qpromise/reject/reject.pro
Normal file
@ -0,0 +1,4 @@
|
||||
TARGET = tst_qpromise_reject
|
||||
SOURCES += $$PWD/tst_reject.cpp
|
||||
|
||||
include(../../qtpromise.pri)
|
74
tests/auto/qtpromise/qpromise/reject/tst_reject.cpp
Normal file
74
tests/auto/qtpromise/qpromise/reject/tst_reject.cpp
Normal file
@ -0,0 +1,74 @@
|
||||
// Tests
|
||||
#include "../../shared/utils.h"
|
||||
|
||||
// QtPromise
|
||||
#include <QtPromise>
|
||||
|
||||
// Qt
|
||||
#include <QtTest>
|
||||
|
||||
// STL
|
||||
#include <memory>
|
||||
|
||||
using namespace QtPromise;
|
||||
|
||||
class tst_qpromise_reject : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
private Q_SLOTS:
|
||||
void rejectWithValue();
|
||||
void rejectWithQSharedPtr();
|
||||
void rejectWithStdSharedPtr();
|
||||
};
|
||||
|
||||
QTEST_MAIN(tst_qpromise_reject)
|
||||
#include "tst_reject.moc"
|
||||
|
||||
void tst_qpromise_reject::rejectWithValue()
|
||||
{
|
||||
auto p = QPromise<int>::reject(42);
|
||||
|
||||
QCOMPARE(p.isRejected(), true);
|
||||
QCOMPARE(waitForError(p, -1), 42);
|
||||
}
|
||||
|
||||
// https://github.com/simonbrunel/qtpromise/issues/6
|
||||
void tst_qpromise_reject::rejectWithQSharedPtr()
|
||||
{
|
||||
QWeakPointer<int> wptr;
|
||||
|
||||
{
|
||||
QSharedPointer<int> sptr(new int(42));
|
||||
auto p = QPromise<int>::reject(sptr);
|
||||
|
||||
QCOMPARE(waitForError(p, QSharedPointer<int>()), sptr);
|
||||
|
||||
wptr = sptr;
|
||||
sptr.reset();
|
||||
|
||||
QCOMPARE(wptr.isNull(), false); // "p" still holds a reference
|
||||
}
|
||||
|
||||
QCOMPARE(wptr.isNull(), true);
|
||||
}
|
||||
|
||||
// https://github.com/simonbrunel/qtpromise/issues/6
|
||||
void tst_qpromise_reject::rejectWithStdSharedPtr()
|
||||
{
|
||||
std::weak_ptr<int> wptr;
|
||||
|
||||
{
|
||||
std::shared_ptr<int> sptr(new int(42));
|
||||
auto p = QPromise<int>::reject(sptr);
|
||||
|
||||
QCOMPARE(waitForError(p, std::shared_ptr<int>()), sptr);
|
||||
|
||||
wptr = sptr;
|
||||
sptr.reset();
|
||||
|
||||
QCOMPARE(wptr.use_count(), 1l); // "p" still holds a reference
|
||||
}
|
||||
|
||||
QCOMPARE(wptr.use_count(), 0l);
|
||||
}
|
@ -7,6 +7,9 @@
|
||||
// Qt
|
||||
#include <QtTest>
|
||||
|
||||
// STL
|
||||
#include <memory>
|
||||
|
||||
using namespace QtPromise;
|
||||
|
||||
class tst_qpromise_resolve : public QObject
|
||||
@ -14,14 +17,16 @@ class tst_qpromise_resolve : public QObject
|
||||
Q_OBJECT
|
||||
|
||||
private Q_SLOTS:
|
||||
void value();
|
||||
void empty_void();
|
||||
void resolveWithValue();
|
||||
void resolveWithNoValue();
|
||||
void resolveWithQSharedPtr();
|
||||
void resolveWithStdSharedPtr();
|
||||
};
|
||||
|
||||
QTEST_MAIN(tst_qpromise_resolve)
|
||||
#include "tst_resolve.moc"
|
||||
|
||||
void tst_qpromise_resolve::value()
|
||||
void tst_qpromise_resolve::resolveWithValue()
|
||||
{
|
||||
const int value = 42;
|
||||
auto p0 = QPromise<int>::resolve(value);
|
||||
@ -33,9 +38,49 @@ void tst_qpromise_resolve::value()
|
||||
QCOMPARE(waitForValue(p1, -1), 43);
|
||||
}
|
||||
|
||||
void tst_qpromise_resolve::empty_void()
|
||||
void tst_qpromise_resolve::resolveWithNoValue()
|
||||
{
|
||||
auto p = QPromise<void>::resolve();
|
||||
|
||||
QCOMPARE(p.isFulfilled(), true);
|
||||
}
|
||||
|
||||
// https://github.com/simonbrunel/qtpromise/issues/6
|
||||
void tst_qpromise_resolve::resolveWithQSharedPtr()
|
||||
{
|
||||
QWeakPointer<int> wptr;
|
||||
|
||||
{
|
||||
QSharedPointer<int> sptr(new int(42));
|
||||
auto p = QPromise<QSharedPointer<int>>::resolve(sptr);
|
||||
|
||||
QCOMPARE(waitForValue(p, QSharedPointer<int>()), sptr);
|
||||
|
||||
wptr = sptr;
|
||||
sptr.reset();
|
||||
|
||||
QCOMPARE(wptr.isNull(), false); // "p" still holds a reference
|
||||
}
|
||||
|
||||
QCOMPARE(wptr.isNull(), true);
|
||||
}
|
||||
|
||||
// https://github.com/simonbrunel/qtpromise/issues/6
|
||||
void tst_qpromise_resolve::resolveWithStdSharedPtr()
|
||||
{
|
||||
std::weak_ptr<int> wptr;
|
||||
|
||||
{
|
||||
std::shared_ptr<int> sptr(new int(42));
|
||||
auto p = QPromise<std::shared_ptr<int>>::resolve(sptr);
|
||||
|
||||
QCOMPARE(waitForValue(p, std::shared_ptr<int>()), sptr);
|
||||
|
||||
wptr = sptr;
|
||||
sptr.reset();
|
||||
|
||||
QCOMPARE(wptr.use_count(), 1l); // "p" still holds a reference
|
||||
}
|
||||
|
||||
QCOMPARE(wptr.use_count(), 0l);
|
||||
}
|
||||
|
Reference in New Issue
Block a user