// QtPromise #include // Qt #include using namespace QtPromise; class tst_helpers : public QObject { Q_OBJECT private Q_SLOTS: void resolve(); void resolve_void(); void resolve_promise(); void resolve_promise_void(); void allFulfilled(); void allFulfilled_void(); void allRejected(); void allRejected_void(); void allEmpty(); void allEmpty_void(); }; // class tst_helpers QTEST_MAIN(tst_helpers) #include "tst_helpers.moc" void tst_helpers::resolve() { int value = -1; auto p = QtPromise::qPromise(42); Q_STATIC_ASSERT((std::is_same >::value)); QCOMPARE(p.isFulfilled(), true); p.then([&](int res) { value = res; }).wait(); QCOMPARE(value, 42); } void tst_helpers::resolve_void() { int value = -1; auto p = QtPromise::qPromise(); Q_STATIC_ASSERT((std::is_same >::value)); QCOMPARE(p.isFulfilled(), true); p.then([&]() { value = 42; }).wait(); QCOMPARE(value, 42); } void tst_helpers::resolve_promise() { QString value; auto p = QtPromise::qPromise( QPromise([](const QPromiseResolve& resolve) { QtPromisePrivate::qtpromise_defer([=](){ resolve("foo"); }); })); Q_STATIC_ASSERT((std::is_same >::value)); QCOMPARE(p.isPending(), true); p.then([&](const QString& res) { value = res; }).wait(); QCOMPARE(p.isFulfilled(), true); QCOMPARE(value, QString("foo")); } void tst_helpers::resolve_promise_void() { QList values; auto p = QtPromise::qPromise( QPromise([&](const QPromiseResolve& resolve) { QtPromisePrivate::qtpromise_defer([=, &values](){ values << 42; resolve(); }); })); Q_STATIC_ASSERT((std::is_same >::value)); QCOMPARE(p.isPending(), true); p.then([&]() { values << 43; }).wait(); QCOMPARE(p.isFulfilled(), true); QCOMPARE(values, QList({42, 43})); } void tst_helpers::allFulfilled() { auto p0 = QtPromise::qPromise(42); auto p1 = QtPromise::qPromise(44); auto p2 = QPromise([](const QPromiseResolve& resolve) { QtPromisePrivate::qtpromise_defer([=](){ resolve(43); }); }); auto p = qPromiseAll(QVector >{p0, p2, p1}); Q_STATIC_ASSERT((std::is_same > >::value)); QCOMPARE(p.isPending(), true); QCOMPARE(p0.isFulfilled(), true); QCOMPARE(p1.isFulfilled(), true); QCOMPARE(p2.isPending(), true); QVector values; p.then([&](const QVector& res) { values = res; }).wait(); QCOMPARE(p.isFulfilled(), true); QCOMPARE(p2.isFulfilled(), true); QCOMPARE(values, QVector({42, 43, 44})); } void tst_helpers::allFulfilled_void() { auto p0 = QtPromise::qPromise(); auto p1 = QtPromise::qPromise(); auto p2 = QPromise([](const QPromiseResolve& resolve) { QtPromisePrivate::qtpromise_defer([=](){ resolve(); }); }); auto p = qPromiseAll(QVector >{p0, p2, p1}); Q_STATIC_ASSERT((std::is_same >::value)); QCOMPARE(p.isPending(), true); QCOMPARE(p0.isFulfilled(), true); QCOMPARE(p1.isFulfilled(), true); QCOMPARE(p2.isPending(), true); p.wait(); QCOMPARE(p.isFulfilled(), true); QCOMPARE(p2.isFulfilled(), true); } void tst_helpers::allRejected() { auto p0 = QtPromise::qPromise(42); auto p1 = QtPromise::qPromise(44); auto p2 = QPromise([](const QPromiseResolve&, const QPromiseReject& reject) { QtPromisePrivate::qtpromise_defer([=](){ reject(QString("foo")); }); }); auto p = qPromiseAll(QVector >{p0, p2, p1}); Q_STATIC_ASSERT((std::is_same > >::value)); QCOMPARE(p.isPending(), true); QCOMPARE(p0.isFulfilled(), true); QCOMPARE(p1.isFulfilled(), true); QCOMPARE(p2.isPending(), true); QString error; p.fail([&](const QString& err) { error = err; return QVector(); }).wait(); QCOMPARE(p.isRejected(), true); QCOMPARE(p2.isRejected(), true); QCOMPARE(error, QString("foo")); } void tst_helpers::allRejected_void() { auto p0 = QtPromise::qPromise(); auto p1 = QtPromise::qPromise(); auto p2 = QPromise([](const QPromiseResolve&, const QPromiseReject& reject) { QtPromisePrivate::qtpromise_defer([=](){ reject(QString("foo")); }); }); auto p = qPromiseAll(QVector >{p0, p2, p1}); Q_STATIC_ASSERT((std::is_same >::value)); QCOMPARE(p.isPending(), true); QCOMPARE(p0.isFulfilled(), true); QCOMPARE(p1.isFulfilled(), true); QCOMPARE(p2.isPending(), true); QString error; p.fail([&](const QString& err) { error = err; }).wait(); QCOMPARE(p.isRejected(), true); QCOMPARE(p2.isRejected(), true); QCOMPARE(error, QString("foo")); } void tst_helpers::allEmpty() { auto p = qPromiseAll(QVector >()); Q_STATIC_ASSERT((std::is_same > >::value)); QCOMPARE(p.isFulfilled(), true); QVector values; p.then([&](const QVector& res) { values = res; }).wait(); QCOMPARE(values, QVector()); } void tst_helpers::allEmpty_void() { auto p = qPromiseAll(QVector >()); Q_STATIC_ASSERT((std::is_same >::value)); QCOMPARE(p.isFulfilled(), true); }