mirror of
https://github.com/simonbrunel/qtpromise.git
synced 2024-11-21 18:24:29 +08:00
Rename qPromiseAll() helper to QtPromise::all()
For consistency with other helpers, deprecate `qPromiseAll()` in favor of `QtPromise::all()`.
This commit is contained in:
parent
963ec621e1
commit
cbf4cc7867
@ -38,7 +38,6 @@ module.exports = {
|
||||
'qtpromise/qpromise/then',
|
||||
'qtpromise/qpromise/timeout',
|
||||
'qtpromise/qpromise/wait',
|
||||
'qtpromise/qpromise/all.md',
|
||||
'qtpromise/qpromise/reject.md',
|
||||
'qtpromise/qpromise/resolve.md'
|
||||
]
|
||||
@ -46,13 +45,13 @@ module.exports = {
|
||||
{
|
||||
title: 'Helpers',
|
||||
children: [
|
||||
'qtpromise/helpers/all',
|
||||
'qtpromise/helpers/attempt',
|
||||
'qtpromise/helpers/connect',
|
||||
'qtpromise/helpers/each',
|
||||
'qtpromise/helpers/filter',
|
||||
'qtpromise/helpers/map',
|
||||
'qtpromise/helpers/resolve',
|
||||
'qtpromise/helpers/qpromiseall'
|
||||
'qtpromise/helpers/resolve'
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -20,19 +20,18 @@
|
||||
|
||||
## Static Functions
|
||||
|
||||
* [`[static] QPromise<T>::all`](qpromise/all.md)
|
||||
* [`[static] QPromise<T>::reject`](qpromise/reject.md)
|
||||
* [`[static] QPromise<T>::resolve`](qpromise/resolve.md)
|
||||
|
||||
## Helpers
|
||||
|
||||
* [`QtPromise::all`](helpers/all.md)
|
||||
* [`QtPromise::attempt`](helpers/attempt.md)
|
||||
* [`QtPromise::connect`](helpers/connect.md)
|
||||
* [`QtPromise::each`](helpers/each.md)
|
||||
* [`QtPromise::filter`](helpers/filter.md)
|
||||
* [`QtPromise::map`](helpers/map.md)
|
||||
* [`QtPromise::resolve`](helpers/resolve.md)
|
||||
* [`qPromiseAll`](helpers/qpromiseall.md)
|
||||
|
||||
## Exceptions
|
||||
|
||||
@ -42,4 +41,6 @@
|
||||
|
||||
## Deprecations
|
||||
|
||||
* `[static] QPromise<T>::all`: use [`QtPromise::all`](helpers/all.md) instead (since 0.5.0)
|
||||
* `QtPromise::qPromise`: use [`QtPromise::resolve`](helpers/resolve.md) instead (since 0.5.0)
|
||||
* `QtPromise::qPromiseAll`: use [`QtPromise::all`](helpers/all.md) instead (since 0.5.0)
|
||||
|
33
docs/qtpromise/helpers/all.md
Normal file
33
docs/qtpromise/helpers/all.md
Normal file
@ -0,0 +1,33 @@
|
||||
---
|
||||
title: all
|
||||
---
|
||||
|
||||
# QtPromise::all
|
||||
|
||||
*Since: 0.5.0*
|
||||
|
||||
```
|
||||
QtPromise::all(Sequence<QPromise<T>> promises) -> QPromise<QVector<T>>
|
||||
QtPromise::all(Sequence<QPromise<void>> promises) -> QPromise<void>
|
||||
```
|
||||
|
||||
Returns a `QPromise<QVector<T>>` (or `QPromise<void>`) that fulfills when **all** `promises` of (the same) type `T` have been fulfilled. The `output` value is a vector containing all the values of `promises`, in the same order, i.e., at the respective positions to the original sequence, regardless of completion order.
|
||||
|
||||
If any of the given `promises` fail, `output` immediately rejects with the error of the promise that rejected, whether or not the other promises are resolved.
|
||||
|
||||
`Sequence` is any STL compatible container (eg. `QVector`, `QList`, `std::vector`, etc.)
|
||||
|
||||
```cpp
|
||||
QVector<QPromise<QByteArray> > promises{
|
||||
download(QUrl("http://a...")),
|
||||
download(QUrl("http://b...")),
|
||||
download(QUrl("http://c..."))
|
||||
};
|
||||
|
||||
auto output = QtPromise::all(promises);
|
||||
|
||||
// output type: QPromise<QVector<QByteArray>>
|
||||
output.then([](const QVector<QByteArray>& res) {
|
||||
// {...}
|
||||
});
|
||||
```
|
@ -1,22 +0,0 @@
|
||||
---
|
||||
title: qPromiseAll
|
||||
---
|
||||
|
||||
# qPromiseAll
|
||||
|
||||
*Since: 0.1.0*
|
||||
|
||||
```
|
||||
qPromiseAll(Sequence<QPromise<T>> promises) -> QPromise<QVector<T>>
|
||||
qPromiseAll(Sequence<QPromise<void>> promises) -> QPromise<void>
|
||||
```
|
||||
|
||||
This method simply calls the appropriated [`QPromise<T>::all`](../qpromise/all.md) static method based on the given `QVector` type. In some cases, this method is more convenient than the static one since it avoid some extra typing:
|
||||
|
||||
```cpp
|
||||
QVector<QPromise<QByteArray> > promises{...}
|
||||
|
||||
auto output = qPromiseAll(promises);
|
||||
// eq. QPromise<QByteArray>::all(promises)
|
||||
```
|
||||
|
@ -31,4 +31,4 @@ output.then([](const QVector<QByteArray>& res) {
|
||||
});
|
||||
```
|
||||
|
||||
See also: [`qPromiseAll`](../helpers/qpromiseall.md)
|
||||
See also: [`QtPromise::all`](../helpers/all.md)
|
||||
|
@ -98,8 +98,11 @@ public:
|
||||
map(Functor fn);
|
||||
|
||||
public: // STATIC
|
||||
|
||||
// DEPRECATED (remove at version 1)
|
||||
template <template <typename, typename...> class Sequence = QVector, typename ...Args>
|
||||
inline static QPromise<QVector<T>> all(const Sequence<QPromise<T>, Args...>& promises);
|
||||
Q_DECL_DEPRECATED_X("Use QtPromise::all instead") static inline QPromise<QVector<T>>
|
||||
all(const Sequence<QPromise<T>, Args...>& promises);
|
||||
|
||||
inline static QPromise<T> resolve(const T& value);
|
||||
inline static QPromise<T> resolve(T&& value);
|
||||
@ -116,8 +119,11 @@ public:
|
||||
QPromise(F&& resolver): QPromiseBase<void>(std::forward<F>(resolver)) { }
|
||||
|
||||
public: // STATIC
|
||||
|
||||
// DEPRECATED (remove at version 1)
|
||||
template <template <typename, typename...> class Sequence = QVector, typename ...Args>
|
||||
inline static QPromise<void> all(const Sequence<QPromise<void>, Args...>& promises);
|
||||
Q_DECL_DEPRECATED_X("Use QtPromise::all instead") static inline QPromise<void>
|
||||
all(const Sequence<QPromise<void>, Args...>& promises);
|
||||
|
||||
inline static QPromise<void> resolve();
|
||||
|
||||
|
@ -174,7 +174,7 @@ inline QPromise<T> QPromise<T>::each(Functor fn)
|
||||
i++;
|
||||
}
|
||||
|
||||
return QPromise<void>::all(promises);
|
||||
return QtPromise::all(promises);
|
||||
});
|
||||
}
|
||||
|
||||
@ -201,35 +201,7 @@ template <typename T>
|
||||
template <template <typename, typename...> class Sequence, typename ...Args>
|
||||
inline QPromise<QVector<T>> QPromise<T>::all(const Sequence<QPromise<T>, Args...>& promises)
|
||||
{
|
||||
const int count = static_cast<int>(promises.size());
|
||||
if (count == 0) {
|
||||
return QPromise<QVector<T>>::resolve({});
|
||||
}
|
||||
|
||||
return QPromise<QVector<T>>([=](
|
||||
const QPromiseResolve<QVector<T>>& resolve,
|
||||
const QPromiseReject<QVector<T>>& reject) {
|
||||
|
||||
QSharedPointer<int> remaining(new int(count));
|
||||
QSharedPointer<QVector<T>> results(new QVector<T>(count));
|
||||
|
||||
int i = 0;
|
||||
for (const auto& promise: promises) {
|
||||
promise.then([=](const T& res) mutable {
|
||||
(*results)[i] = res;
|
||||
if (--(*remaining) == 0) {
|
||||
resolve(*results);
|
||||
}
|
||||
}, [=]() mutable {
|
||||
if (*remaining != -1) {
|
||||
*remaining = -1;
|
||||
reject(std::current_exception());
|
||||
}
|
||||
});
|
||||
|
||||
i++;
|
||||
}
|
||||
});
|
||||
return QtPromise::all(promises);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
@ -251,30 +223,7 @@ inline QPromise<T> QPromise<T>::resolve(T&& value)
|
||||
template <template <typename, typename...> class Sequence, typename ...Args>
|
||||
inline QPromise<void> QPromise<void>::all(const Sequence<QPromise<void>, Args...>& promises)
|
||||
{
|
||||
const int count = static_cast<int>(promises.size());
|
||||
if (count == 0) {
|
||||
return QPromise<void>::resolve();
|
||||
}
|
||||
|
||||
return QPromise<void>([=](
|
||||
const QPromiseResolve<void>& resolve,
|
||||
const QPromiseReject<void>& reject) {
|
||||
|
||||
QSharedPointer<int> remaining(new int(count));
|
||||
|
||||
for (const auto& promise: promises) {
|
||||
promise.then([=]() {
|
||||
if (--(*remaining) == 0) {
|
||||
resolve();
|
||||
}
|
||||
}, [=]() {
|
||||
if (*remaining != -1) {
|
||||
*remaining = -1;
|
||||
reject(std::current_exception());
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
return QtPromise::all(promises);
|
||||
}
|
||||
|
||||
inline QPromise<void> QPromise<void>::resolve()
|
||||
|
@ -40,15 +40,68 @@ resolve()
|
||||
}
|
||||
|
||||
template <typename T, template <typename, typename...> class Sequence = QVector, typename ...Args>
|
||||
static inline QPromise<QVector<T>> qPromiseAll(const Sequence<QPromise<T>, Args...>& promises)
|
||||
static inline QPromise<QVector<T>>
|
||||
all(const Sequence<QPromise<T>, Args...>& promises)
|
||||
{
|
||||
return QPromise<T>::all(promises);
|
||||
const int count = static_cast<int>(promises.size());
|
||||
if (count == 0) {
|
||||
return QtPromise::resolve(QVector<T>{});
|
||||
}
|
||||
|
||||
return QPromise<QVector<T>>([=](
|
||||
const QPromiseResolve<QVector<T>>& resolve,
|
||||
const QPromiseReject<QVector<T>>& reject) {
|
||||
|
||||
QSharedPointer<int> remaining(new int(count));
|
||||
QSharedPointer<QVector<T>> results(new QVector<T>(count));
|
||||
|
||||
int i = 0;
|
||||
for (const auto& promise: promises) {
|
||||
promise.then([=](const T& res) mutable {
|
||||
(*results)[i] = res;
|
||||
if (--(*remaining) == 0) {
|
||||
resolve(*results);
|
||||
}
|
||||
}, [=]() mutable {
|
||||
if (*remaining != -1) {
|
||||
*remaining = -1;
|
||||
reject(std::current_exception());
|
||||
}
|
||||
});
|
||||
|
||||
i++;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
template <template <typename, typename...> class Sequence = QVector, typename ...Args>
|
||||
static inline QPromise<void> qPromiseAll(const Sequence<QPromise<void>, Args...>& promises)
|
||||
static inline QPromise<void>
|
||||
all(const Sequence<QPromise<void>, Args...>& promises)
|
||||
{
|
||||
return QPromise<void>::all(promises);
|
||||
const int count = static_cast<int>(promises.size());
|
||||
if (count == 0) {
|
||||
return QtPromise::resolve();
|
||||
}
|
||||
|
||||
return QPromise<void>([=](
|
||||
const QPromiseResolve<void>& resolve,
|
||||
const QPromiseReject<void>& reject) {
|
||||
|
||||
QSharedPointer<int> remaining(new int(count));
|
||||
|
||||
for (const auto& promise: promises) {
|
||||
promise.then([=]() {
|
||||
if (--(*remaining) == 0) {
|
||||
resolve();
|
||||
}
|
||||
}, [=]() {
|
||||
if (*remaining != -1) {
|
||||
*remaining = -1;
|
||||
reject(std::current_exception());
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
template <typename Functor, typename... Args>
|
||||
@ -143,7 +196,7 @@ map(const Sequence& values, Functor fn)
|
||||
i++;
|
||||
}
|
||||
|
||||
return QPromise<ResType>::all(promises);
|
||||
return QtPromise::all(promises);
|
||||
}
|
||||
|
||||
template <typename Sequence, typename Functor>
|
||||
@ -177,6 +230,15 @@ qPromise(Args&&... args)
|
||||
return QtPromise::resolve(std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
template <typename... Args>
|
||||
Q_DECL_DEPRECATED_X("Use QtPromise::all instead")
|
||||
static inline auto
|
||||
qPromiseAll(Args&&... args)
|
||||
-> decltype(QtPromise::all(std::forward<Args>(args)...))
|
||||
{
|
||||
return QtPromise::all(std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
} // namespace QtPromise
|
||||
|
||||
#endif // QTPROMISE_QPROMISEHELPERS_H
|
||||
|
@ -1,3 +1,4 @@
|
||||
TEMPLATE = subdirs
|
||||
SUBDIRS += \
|
||||
qpromise
|
||||
qpromise \
|
||||
qpromiseall
|
||||
|
@ -0,0 +1,4 @@
|
||||
TARGET = tst_deprecations_helpers_qpromiseall
|
||||
SOURCES += $$PWD/tst_qpromiseall.cpp
|
||||
|
||||
include(../../deprecations.pri)
|
@ -0,0 +1,220 @@
|
||||
#include "../../../shared/utils.h"
|
||||
|
||||
// QtPromise
|
||||
#include <QtPromise>
|
||||
|
||||
// Qt
|
||||
#include <QtTest>
|
||||
|
||||
using namespace QtPromise;
|
||||
|
||||
class tst_deprecations_helpers_qpromiseall : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
private Q_SLOTS:
|
||||
void emptySequence();
|
||||
void emptySequence_void();
|
||||
void allPromisesSucceed();
|
||||
void allPromisesSucceed_void();
|
||||
void atLeastOnePromiseReject();
|
||||
void atLeastOnePromiseReject_void();
|
||||
void preserveOrder();
|
||||
void sequenceTypes();
|
||||
void sequenceTypes_void();
|
||||
};
|
||||
|
||||
QTEST_MAIN(tst_deprecations_helpers_qpromiseall)
|
||||
#include "tst_qpromiseall.moc"
|
||||
|
||||
namespace {
|
||||
|
||||
template <class Sequence>
|
||||
struct SequenceTester
|
||||
{
|
||||
Q_STATIC_ASSERT((std::is_same<typename Sequence::value_type, QPromise<int>>::value));
|
||||
|
||||
static void exec()
|
||||
{
|
||||
Sequence promises{
|
||||
QtPromise::resolve(42),
|
||||
QtPromise::resolve(43),
|
||||
QtPromise::resolve(44)
|
||||
};
|
||||
|
||||
promises.push_back(QtPromise::resolve(45));
|
||||
promises.insert(++promises.begin(), QtPromise::resolve(46));
|
||||
promises.pop_back();
|
||||
|
||||
auto p = qPromiseAll(promises);
|
||||
|
||||
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value));
|
||||
QCOMPARE(p.isPending(), true);
|
||||
QCOMPARE(waitForValue(p, QVector<int>()), QVector<int>({42, 46, 43, 44}));
|
||||
}
|
||||
};
|
||||
|
||||
template <template <typename, typename...> class Sequence, typename ...Args>
|
||||
struct SequenceTester<Sequence<QPromise<void>, Args...>>
|
||||
{
|
||||
static void exec()
|
||||
{
|
||||
Sequence<QPromise<void>, Args...> promises{
|
||||
QtPromise::resolve(),
|
||||
QtPromise::resolve(),
|
||||
QtPromise::resolve()
|
||||
};
|
||||
|
||||
promises.push_back(QtPromise::resolve());
|
||||
promises.insert(++promises.begin(), QtPromise::resolve());
|
||||
promises.pop_back();
|
||||
|
||||
auto p = qPromiseAll(promises);
|
||||
|
||||
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<void>>::value));
|
||||
QCOMPARE(p.isPending(), true);
|
||||
QCOMPARE(waitForValue(p, -1, 42), 42);
|
||||
}
|
||||
};
|
||||
|
||||
} // anonymous namespace
|
||||
|
||||
void tst_deprecations_helpers_qpromiseall::emptySequence()
|
||||
{
|
||||
auto p = qPromiseAll(QVector<QPromise<int>>());
|
||||
|
||||
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value));
|
||||
QCOMPARE(p.isFulfilled(), true);
|
||||
QCOMPARE(waitForValue(p, QVector<int>()), QVector<int>({}));
|
||||
}
|
||||
|
||||
void tst_deprecations_helpers_qpromiseall::emptySequence_void()
|
||||
{
|
||||
auto p = qPromiseAll(QVector<QPromise<void>>());
|
||||
|
||||
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<void>>::value));
|
||||
QCOMPARE(p.isFulfilled(), true);
|
||||
QCOMPARE(waitForValue(p, -1, 42), 42);
|
||||
}
|
||||
|
||||
void tst_deprecations_helpers_qpromiseall::allPromisesSucceed()
|
||||
{
|
||||
auto p0 = QtPromise::resolve(42);
|
||||
auto p1 = QtPromise::resolve(44);
|
||||
auto p2 = QPromise<int>([](const QPromiseResolve<int>& resolve) {
|
||||
QtPromisePrivate::qtpromise_defer([=](){
|
||||
resolve(43);
|
||||
});
|
||||
});
|
||||
|
||||
auto p = qPromiseAll(QVector<QPromise<int>>{p0, p2, p1});
|
||||
|
||||
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value));
|
||||
QCOMPARE(p0.isFulfilled(), true);
|
||||
QCOMPARE(p1.isFulfilled(), true);
|
||||
QCOMPARE(p2.isPending(), true);
|
||||
QCOMPARE(p.isPending(), true);
|
||||
QCOMPARE(waitForValue(p, QVector<int>()), QVector<int>({42, 43, 44}));
|
||||
QCOMPARE(p2.isFulfilled(), true);
|
||||
}
|
||||
|
||||
void tst_deprecations_helpers_qpromiseall::allPromisesSucceed_void()
|
||||
{
|
||||
auto p0 = QtPromise::resolve();
|
||||
auto p1 = QtPromise::resolve();
|
||||
auto p2 = QPromise<void>([](const QPromiseResolve<void>& resolve) {
|
||||
QtPromisePrivate::qtpromise_defer([=](){
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
|
||||
auto p = qPromiseAll(QVector<QPromise<void>>{p0, p2, p1});
|
||||
|
||||
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<void>>::value));
|
||||
QCOMPARE(p0.isFulfilled(), true);
|
||||
QCOMPARE(p1.isFulfilled(), true);
|
||||
QCOMPARE(p2.isPending(), true);
|
||||
QCOMPARE(p.isPending(), true);
|
||||
QCOMPARE(waitForValue(p, -1, 42), 42);
|
||||
QCOMPARE(p2.isFulfilled(), true);
|
||||
}
|
||||
|
||||
void tst_deprecations_helpers_qpromiseall::atLeastOnePromiseReject()
|
||||
{
|
||||
auto p0 = QtPromise::resolve(42);
|
||||
auto p1 = QtPromise::resolve(44);
|
||||
auto p2 = QPromise<int>([](const QPromiseResolve<int>&, const QPromiseReject<int>& reject) {
|
||||
QtPromisePrivate::qtpromise_defer([=](){
|
||||
reject(QString("foo"));
|
||||
});
|
||||
});
|
||||
|
||||
auto p = qPromiseAll(QVector<QPromise<int>>{p0, p2, p1});
|
||||
|
||||
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value));
|
||||
QCOMPARE(p0.isFulfilled(), true);
|
||||
QCOMPARE(p1.isFulfilled(), true);
|
||||
QCOMPARE(p2.isPending(), true);
|
||||
QCOMPARE(p.isPending(), true);
|
||||
QCOMPARE(waitForError(p, QString()), QString("foo"));
|
||||
QCOMPARE(p2.isRejected(), true);
|
||||
}
|
||||
|
||||
void tst_deprecations_helpers_qpromiseall::atLeastOnePromiseReject_void()
|
||||
{
|
||||
auto p0 = QtPromise::resolve();
|
||||
auto p1 = QtPromise::resolve();
|
||||
auto p2 = QPromise<void>([](const QPromiseResolve<void>&, const QPromiseReject<void>& reject) {
|
||||
QtPromisePrivate::qtpromise_defer([=](){
|
||||
reject(QString("foo"));
|
||||
});
|
||||
});
|
||||
|
||||
auto p = qPromiseAll(QVector<QPromise<void>>{p0, p2, p1});
|
||||
|
||||
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<void>>::value));
|
||||
QCOMPARE(p0.isFulfilled(), true);
|
||||
QCOMPARE(p1.isFulfilled(), true);
|
||||
QCOMPARE(p2.isPending(), true);
|
||||
QCOMPARE(p.isPending(), true);
|
||||
QCOMPARE(waitForError(p, QString()), QString("foo"));
|
||||
QCOMPARE(p2.isRejected(), true);
|
||||
}
|
||||
|
||||
void tst_deprecations_helpers_qpromiseall::preserveOrder()
|
||||
{
|
||||
auto p0 = QtPromise::resolve(42).delay(500);
|
||||
auto p1 = QtPromise::resolve(43).delay(100);
|
||||
auto p2 = QtPromise::resolve(44).delay(250);
|
||||
|
||||
auto p = qPromiseAll(QVector<QPromise<int>>{p0, p1, p2});
|
||||
|
||||
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value));
|
||||
QCOMPARE(p0.isPending(), true);
|
||||
QCOMPARE(p1.isPending(), true);
|
||||
QCOMPARE(p2.isPending(), true);
|
||||
QCOMPARE(p.isPending(), true);
|
||||
QCOMPARE(waitForValue(p, QVector<int>()), QVector<int>({42, 43, 44}));
|
||||
QCOMPARE(p0.isFulfilled(), true);
|
||||
QCOMPARE(p1.isFulfilled(), true);
|
||||
QCOMPARE(p2.isFulfilled(), true);
|
||||
}
|
||||
|
||||
// QVector::push_back/append isn't supported since it requires a default
|
||||
// constructor (see https://github.com/simonbrunel/qtpromise/issues/3)
|
||||
|
||||
void tst_deprecations_helpers_qpromiseall::sequenceTypes()
|
||||
{
|
||||
SequenceTester<QList<QPromise<int>>>::exec();
|
||||
//SequenceTester<QVector<QPromise<int>>>::exec();
|
||||
SequenceTester<std::list<QPromise<int>>>::exec();
|
||||
SequenceTester<std::vector<QPromise<int>>>::exec();
|
||||
}
|
||||
|
||||
void tst_deprecations_helpers_qpromiseall::sequenceTypes_void()
|
||||
{
|
||||
SequenceTester<QList<QPromise<void>>>::exec();
|
||||
//SequenceTester<QVector<QPromise<void>>>::exec();
|
||||
SequenceTester<std::list<QPromise<void>>>::exec();
|
||||
SequenceTester<std::vector<QPromise<void>>>::exec();
|
||||
}
|
4
tests/auto/qtpromise/deprecations/qpromise/all/all.pro
Normal file
4
tests/auto/qtpromise/deprecations/qpromise/all/all.pro
Normal file
@ -0,0 +1,4 @@
|
||||
TARGET = tst_deprecations_qpromise_all
|
||||
SOURCES += $$PWD/tst_all.cpp
|
||||
|
||||
include(../../deprecations.pri)
|
229
tests/auto/qtpromise/deprecations/qpromise/all/tst_all.cpp
Normal file
229
tests/auto/qtpromise/deprecations/qpromise/all/tst_all.cpp
Normal file
@ -0,0 +1,229 @@
|
||||
#include "../../../shared/utils.h"
|
||||
|
||||
// QtPromise
|
||||
#include <QtPromise>
|
||||
|
||||
// Qt
|
||||
#include <QtTest>
|
||||
|
||||
using namespace QtPromise;
|
||||
|
||||
class tst_deprecations_qpromise_all : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
private Q_SLOTS:
|
||||
void emptySequence();
|
||||
void emptySequence_void();
|
||||
void allPromisesSucceed();
|
||||
void allPromisesSucceed_void();
|
||||
void atLeastOnePromiseReject();
|
||||
void atLeastOnePromiseReject_void();
|
||||
void preserveOrder();
|
||||
void sequenceTypes();
|
||||
void sequenceTypes_void();
|
||||
};
|
||||
|
||||
QTEST_MAIN(tst_deprecations_qpromise_all)
|
||||
#include "tst_all.moc"
|
||||
|
||||
namespace {
|
||||
|
||||
template <class Sequence>
|
||||
struct SequenceTester
|
||||
{
|
||||
Q_STATIC_ASSERT((std::is_same<typename Sequence::value_type, QPromise<int>>::value));
|
||||
|
||||
static void exec()
|
||||
{
|
||||
Sequence promises{
|
||||
QtPromise::resolve(42),
|
||||
QtPromise::resolve(43),
|
||||
QtPromise::resolve(44)
|
||||
};
|
||||
|
||||
promises.push_back(QtPromise::resolve(45));
|
||||
promises.insert(++promises.begin(), QtPromise::resolve(46));
|
||||
promises.pop_back();
|
||||
|
||||
auto p = QPromise<int>::all(promises);
|
||||
|
||||
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value));
|
||||
|
||||
QCOMPARE(p.isPending(), true);
|
||||
QCOMPARE(waitForValue(p, QVector<int>()), QVector<int>({42, 46, 43, 44}));
|
||||
}
|
||||
};
|
||||
|
||||
template <template <typename, typename...> class Sequence, typename ...Args>
|
||||
struct SequenceTester<Sequence<QPromise<void>, Args...>>
|
||||
{
|
||||
static void exec()
|
||||
{
|
||||
Sequence<QPromise<void>, Args...> promises{
|
||||
QtPromise::resolve(),
|
||||
QtPromise::resolve(),
|
||||
QtPromise::resolve()
|
||||
};
|
||||
|
||||
promises.push_back(QtPromise::resolve());
|
||||
promises.insert(++promises.begin(), QtPromise::resolve());
|
||||
promises.pop_back();
|
||||
|
||||
auto p = QPromise<void>::all(promises);
|
||||
|
||||
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<void>>::value));
|
||||
|
||||
QCOMPARE(p.isPending(), true);
|
||||
QCOMPARE(waitForValue(p, -1, 42), 42);
|
||||
}
|
||||
};
|
||||
|
||||
} // anonymous namespace
|
||||
|
||||
void tst_deprecations_qpromise_all::emptySequence()
|
||||
{
|
||||
auto p = QPromise<int>::all({});
|
||||
|
||||
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value));
|
||||
|
||||
QCOMPARE(p.isFulfilled(), true);
|
||||
QCOMPARE(waitForValue(p, QVector<int>()), QVector<int>{});
|
||||
}
|
||||
|
||||
void tst_deprecations_qpromise_all::emptySequence_void()
|
||||
{
|
||||
auto p = QPromise<void>::all({});
|
||||
|
||||
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<void>>::value));
|
||||
|
||||
QCOMPARE(p.isFulfilled(), true);
|
||||
QCOMPARE(waitForValue(p, -1, 42), 42);
|
||||
}
|
||||
|
||||
void tst_deprecations_qpromise_all::allPromisesSucceed()
|
||||
{
|
||||
auto p0 = QtPromise::resolve(42);
|
||||
auto p1 = QtPromise::resolve(44);
|
||||
auto p2 = QPromise<int>([](const QPromiseResolve<int>& resolve) {
|
||||
QtPromisePrivate::qtpromise_defer([=](){
|
||||
resolve(43);
|
||||
});
|
||||
});
|
||||
|
||||
auto p = QPromise<int>::all({p0, p2, p1});
|
||||
|
||||
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value));
|
||||
|
||||
QCOMPARE(p0.isFulfilled(), true);
|
||||
QCOMPARE(p1.isFulfilled(), true);
|
||||
QCOMPARE(p2.isPending(), true);
|
||||
QCOMPARE(p.isPending(), true);
|
||||
QCOMPARE(waitForValue(p, QVector<int>()), QVector<int>({42, 43, 44}));
|
||||
QCOMPARE(p2.isFulfilled(), true);
|
||||
}
|
||||
|
||||
void tst_deprecations_qpromise_all::allPromisesSucceed_void()
|
||||
{
|
||||
auto p0 = QtPromise::resolve();
|
||||
auto p1 = QtPromise::resolve();
|
||||
auto p2 = QPromise<void>([](const QPromiseResolve<void>& resolve) {
|
||||
QtPromisePrivate::qtpromise_defer([=](){
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
|
||||
auto p = QPromise<void>::all({p0, p2, p1});
|
||||
|
||||
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<void>>::value));
|
||||
|
||||
QCOMPARE(p0.isFulfilled(), true);
|
||||
QCOMPARE(p1.isFulfilled(), true);
|
||||
QCOMPARE(p2.isPending(), true);
|
||||
QCOMPARE(p.isPending(), true);
|
||||
QCOMPARE(waitForValue(p, -1, 42), 42);
|
||||
QCOMPARE(p2.isFulfilled(), true);
|
||||
}
|
||||
|
||||
void tst_deprecations_qpromise_all::atLeastOnePromiseReject()
|
||||
{
|
||||
auto p0 = QtPromise::resolve(42);
|
||||
auto p1 = QtPromise::resolve(44);
|
||||
auto p2 = QPromise<int>([](const QPromiseResolve<int>&, const QPromiseReject<int>& reject) {
|
||||
QtPromisePrivate::qtpromise_defer([=](){
|
||||
reject(QString("foo"));
|
||||
});
|
||||
});
|
||||
|
||||
auto p = QPromise<int>::all({p0, p2, p1});
|
||||
|
||||
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value));
|
||||
|
||||
QCOMPARE(p0.isFulfilled(), true);
|
||||
QCOMPARE(p1.isFulfilled(), true);
|
||||
QCOMPARE(p2.isPending(), true);
|
||||
QCOMPARE(p.isPending(), true);
|
||||
QCOMPARE(waitForError(p, QString()), QString("foo"));
|
||||
QCOMPARE(p2.isRejected(), true);
|
||||
}
|
||||
|
||||
void tst_deprecations_qpromise_all::atLeastOnePromiseReject_void()
|
||||
{
|
||||
auto p0 = QtPromise::resolve();
|
||||
auto p1 = QtPromise::resolve();
|
||||
auto p2 = QPromise<void>([](const QPromiseResolve<void>&, const QPromiseReject<void>& reject) {
|
||||
QtPromisePrivate::qtpromise_defer([=](){
|
||||
reject(QString("foo"));
|
||||
});
|
||||
});
|
||||
|
||||
auto p = QPromise<void>::all({p0, p2, p1});
|
||||
|
||||
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<void>>::value));
|
||||
|
||||
QCOMPARE(p0.isFulfilled(), true);
|
||||
QCOMPARE(p1.isFulfilled(), true);
|
||||
QCOMPARE(p2.isPending(), true);
|
||||
QCOMPARE(p.isPending(), true);
|
||||
QCOMPARE(waitForError(p, QString()), QString("foo"));
|
||||
QCOMPARE(p2.isRejected(), true);
|
||||
}
|
||||
|
||||
void tst_deprecations_qpromise_all::preserveOrder()
|
||||
{
|
||||
auto p0 = QtPromise::resolve(42).delay(500);
|
||||
auto p1 = QtPromise::resolve(43).delay(100);
|
||||
auto p2 = QtPromise::resolve(44).delay(250);
|
||||
|
||||
auto p = QPromise<int>::all({p0, p1, p2});
|
||||
|
||||
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value));
|
||||
|
||||
QCOMPARE(p0.isPending(), true);
|
||||
QCOMPARE(p1.isPending(), true);
|
||||
QCOMPARE(p2.isPending(), true);
|
||||
QCOMPARE(p.isPending(), true);
|
||||
QCOMPARE(waitForValue(p, QVector<int>()), QVector<int>({42, 43, 44}));
|
||||
QCOMPARE(p0.isFulfilled(), true);
|
||||
QCOMPARE(p1.isFulfilled(), true);
|
||||
QCOMPARE(p2.isFulfilled(), true);
|
||||
}
|
||||
|
||||
// QVector::push_back/append isn't supported since it requires a default
|
||||
// constructor (see https://github.com/simonbrunel/qtpromise/issues/3)
|
||||
|
||||
void tst_deprecations_qpromise_all::sequenceTypes()
|
||||
{
|
||||
SequenceTester<QList<QPromise<int>>>::exec();
|
||||
//SequenceTester<QVector<QPromise<int>>>::exec();
|
||||
SequenceTester<std::list<QPromise<int>>>::exec();
|
||||
SequenceTester<std::vector<QPromise<int>>>::exec();
|
||||
}
|
||||
|
||||
void tst_deprecations_qpromise_all::sequenceTypes_void()
|
||||
{
|
||||
SequenceTester<QList<QPromise<void>>>::exec();
|
||||
//SequenceTester<QVector<QPromise<void>>>::exec();
|
||||
SequenceTester<std::list<QPromise<void>>>::exec();
|
||||
SequenceTester<std::vector<QPromise<void>>>::exec();
|
||||
}
|
3
tests/auto/qtpromise/deprecations/qpromise/qpromise.pro
Normal file
3
tests/auto/qtpromise/deprecations/qpromise/qpromise.pro
Normal file
@ -0,0 +1,3 @@
|
||||
TEMPLATE = subdirs
|
||||
SUBDIRS += \
|
||||
all
|
@ -46,7 +46,7 @@ struct SequenceTester
|
||||
promises.insert(++promises.begin(), QtPromise::resolve(46));
|
||||
promises.pop_back();
|
||||
|
||||
auto p = QtPromise::qPromiseAll(promises);
|
||||
auto p = QtPromise::all(promises);
|
||||
|
||||
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value));
|
||||
QCOMPARE(p.isPending(), true);
|
||||
@ -69,7 +69,7 @@ struct SequenceTester<Sequence<QPromise<void>, Args...>>
|
||||
promises.insert(++promises.begin(), QtPromise::resolve());
|
||||
promises.pop_back();
|
||||
|
||||
auto p = QtPromise::qPromiseAll(promises);
|
||||
auto p = QtPromise::all(promises);
|
||||
|
||||
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<void>>::value));
|
||||
QCOMPARE(p.isPending(), true);
|
||||
@ -81,7 +81,7 @@ struct SequenceTester<Sequence<QPromise<void>, Args...>>
|
||||
|
||||
void tst_helpers_all::emptySequence()
|
||||
{
|
||||
auto p = QtPromise::qPromiseAll(QVector<QPromise<int>>());
|
||||
auto p = QtPromise::all(QVector<QPromise<int>>());
|
||||
|
||||
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value));
|
||||
QCOMPARE(p.isFulfilled(), true);
|
||||
@ -90,7 +90,7 @@ void tst_helpers_all::emptySequence()
|
||||
|
||||
void tst_helpers_all::emptySequence_void()
|
||||
{
|
||||
auto p = QtPromise::qPromiseAll(QVector<QPromise<void>>());
|
||||
auto p = QtPromise::all(QVector<QPromise<void>>());
|
||||
|
||||
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<void>>::value));
|
||||
QCOMPARE(p.isFulfilled(), true);
|
||||
@ -107,7 +107,7 @@ void tst_helpers_all::allPromisesSucceed()
|
||||
});
|
||||
});
|
||||
|
||||
auto p = QtPromise::qPromiseAll(QVector<QPromise<int>>{p0, p2, p1});
|
||||
auto p = QtPromise::all(QVector<QPromise<int>>{p0, p2, p1});
|
||||
|
||||
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value));
|
||||
QCOMPARE(p0.isFulfilled(), true);
|
||||
@ -128,7 +128,7 @@ void tst_helpers_all::allPromisesSucceed_void()
|
||||
});
|
||||
});
|
||||
|
||||
auto p = QtPromise::qPromiseAll(QVector<QPromise<void>>{p0, p2, p1});
|
||||
auto p = QtPromise::all(QVector<QPromise<void>>{p0, p2, p1});
|
||||
|
||||
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<void>>::value));
|
||||
QCOMPARE(p0.isFulfilled(), true);
|
||||
@ -149,7 +149,7 @@ void tst_helpers_all::atLeastOnePromiseReject()
|
||||
});
|
||||
});
|
||||
|
||||
auto p = QtPromise::qPromiseAll(QVector<QPromise<int>>{p0, p2, p1});
|
||||
auto p = QtPromise::all(QVector<QPromise<int>>{p0, p2, p1});
|
||||
|
||||
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value));
|
||||
QCOMPARE(p0.isFulfilled(), true);
|
||||
@ -170,7 +170,7 @@ void tst_helpers_all::atLeastOnePromiseReject_void()
|
||||
});
|
||||
});
|
||||
|
||||
auto p = QtPromise::qPromiseAll(QVector<QPromise<void>>{p0, p2, p1});
|
||||
auto p = QtPromise::all(QVector<QPromise<void>>{p0, p2, p1});
|
||||
|
||||
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<void>>::value));
|
||||
QCOMPARE(p0.isFulfilled(), true);
|
||||
@ -187,7 +187,7 @@ void tst_helpers_all::preserveOrder()
|
||||
auto p1 = QtPromise::resolve(43).delay(100);
|
||||
auto p2 = QtPromise::resolve(44).delay(250);
|
||||
|
||||
auto p = QtPromise::qPromiseAll(QVector<QPromise<int>>{p0, p1, p2});
|
||||
auto p = QtPromise::all(QVector<QPromise<int>>{p0, p1, p2});
|
||||
|
||||
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value));
|
||||
QCOMPARE(p0.isPending(), true);
|
||||
|
@ -279,7 +279,7 @@ void tst_requirements::thenMultipleCalls()
|
||||
});
|
||||
});
|
||||
|
||||
qPromiseAll(QVector<QPromise<void>>{
|
||||
QtPromise::all(QVector<QPromise<void>>{
|
||||
p.then([&](int r) { values << r + 1; }),
|
||||
p.then([&](int r) { values << r + 2; }),
|
||||
p.then([&](int r) { values << r + 3; })
|
||||
@ -298,7 +298,7 @@ void tst_requirements::thenMultipleCalls()
|
||||
});
|
||||
});
|
||||
|
||||
qPromiseAll(QVector<QPromise<int>>{
|
||||
QtPromise::all(QVector<QPromise<int>>{
|
||||
p.then(nullptr, [&](int r) { values << r + 1; return r + 1; }),
|
||||
p.then(nullptr, [&](int r) { values << r + 2; return r + 2; }),
|
||||
p.then(nullptr, [&](int r) { values << r + 3; return r + 3; })
|
||||
|
Loading…
Reference in New Issue
Block a user