Reorganize helpers unit tests

This commit is contained in:
Simon Brunel 2018-05-22 09:52:05 +02:00
parent bdf3619469
commit 54d88f16a3
10 changed files with 279 additions and 368 deletions

View File

@ -1,4 +1,4 @@
TARGET = tst_qpromise_all TARGET = tst_helpers_all
SOURCES += $$PWD/tst_all.cpp SOURCES += $$PWD/tst_all.cpp
include(../../qtpromise.pri) include(../../qtpromise.pri)

View File

@ -0,0 +1,221 @@
// Tests
#include "../../shared/utils.h"
// QtPromise
#include <QtPromise>
// Qt
#include <QtTest>
using namespace QtPromise;
class tst_helpers_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_helpers_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::qPromise(42),
QtPromise::qPromise(43),
QtPromise::qPromise(44)
};
promises.push_back(QtPromise::qPromise(45));
promises.insert(++promises.begin(), QtPromise::qPromise(46));
promises.pop_back();
auto p = QtPromise::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::qPromise(),
QtPromise::qPromise(),
QtPromise::qPromise()
};
promises.push_back(QtPromise::qPromise());
promises.insert(++promises.begin(), QtPromise::qPromise());
promises.pop_back();
auto p = QtPromise::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_helpers_all::emptySequence()
{
auto p = QtPromise::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_helpers_all::emptySequence_void()
{
auto p = QtPromise::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_helpers_all::allPromisesSucceed()
{
auto p0 = QtPromise::qPromise(42);
auto p1 = QtPromise::qPromise(44);
auto p2 = QPromise<int>([](const QPromiseResolve<int>& resolve) {
QtPromisePrivate::qtpromise_defer([=](){
resolve(43);
});
});
auto p = QtPromise::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_helpers_all::allPromisesSucceed_void()
{
auto p0 = QtPromise::qPromise();
auto p1 = QtPromise::qPromise();
auto p2 = QPromise<void>([](const QPromiseResolve<void>& resolve) {
QtPromisePrivate::qtpromise_defer([=](){
resolve();
});
});
auto p = QtPromise::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_helpers_all::atLeastOnePromiseReject()
{
auto p0 = QtPromise::qPromise(42);
auto p1 = QtPromise::qPromise(44);
auto p2 = QPromise<int>([](const QPromiseResolve<int>&, const QPromiseReject<int>& reject) {
QtPromisePrivate::qtpromise_defer([=](){
reject(QString("foo"));
});
});
auto p = QtPromise::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_helpers_all::atLeastOnePromiseReject_void()
{
auto p0 = QtPromise::qPromise();
auto p1 = QtPromise::qPromise();
auto p2 = QPromise<void>([](const QPromiseResolve<void>&, const QPromiseReject<void>& reject) {
QtPromisePrivate::qtpromise_defer([=](){
reject(QString("foo"));
});
});
auto p = QtPromise::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_helpers_all::preserveOrder()
{
auto p0 = QtPromise::qPromise(42).delay(500);
auto p1 = QtPromise::qPromise(43).delay(100);
auto p2 = QtPromise::qPromise(44).delay(250);
auto p = QtPromise::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_helpers_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_helpers_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();
}

View File

@ -1,4 +1,5 @@
TARGET = tst_helpers TEMPLATE = subdirs
SOURCES += $$PWD/tst_helpers.cpp SUBDIRS += \
all \
include(../qtpromise.pri) reject \
resolve

View File

@ -1,4 +1,4 @@
TARGET = tst_qpromise_reject TARGET = tst_helpers_reject
SOURCES += $$PWD/tst_reject.cpp SOURCES += $$PWD/tst_reject.cpp
include(../../qtpromise.pri) include(../../qtpromise.pri)

View File

@ -12,7 +12,7 @@
using namespace QtPromise; using namespace QtPromise;
class tst_qpromise_reject : public QObject class tst_helpers_reject : public QObject
{ {
Q_OBJECT Q_OBJECT
@ -22,10 +22,10 @@ private Q_SLOTS:
void rejectWithStdSharedPtr(); void rejectWithStdSharedPtr();
}; };
QTEST_MAIN(tst_qpromise_reject) QTEST_MAIN(tst_helpers_reject)
#include "tst_reject.moc" #include "tst_reject.moc"
void tst_qpromise_reject::rejectWithValue() void tst_helpers_reject::rejectWithValue()
{ {
auto p = QPromise<int>::reject(42); auto p = QPromise<int>::reject(42);
@ -34,7 +34,7 @@ void tst_qpromise_reject::rejectWithValue()
} }
// https://github.com/simonbrunel/qtpromise/issues/6 // https://github.com/simonbrunel/qtpromise/issues/6
void tst_qpromise_reject::rejectWithQSharedPtr() void tst_helpers_reject::rejectWithQSharedPtr()
{ {
QWeakPointer<int> wptr; QWeakPointer<int> wptr;
@ -54,7 +54,7 @@ void tst_qpromise_reject::rejectWithQSharedPtr()
} }
// https://github.com/simonbrunel/qtpromise/issues/6 // https://github.com/simonbrunel/qtpromise/issues/6
void tst_qpromise_reject::rejectWithStdSharedPtr() void tst_helpers_reject::rejectWithStdSharedPtr()
{ {
std::weak_ptr<int> wptr; std::weak_ptr<int> wptr;

View File

@ -1,4 +1,4 @@
TARGET = tst_qpromise_resolve TARGET = tst_helpers_resolve
SOURCES += $$PWD/tst_resolve.cpp SOURCES += $$PWD/tst_resolve.cpp
include(../../qtpromise.pri) include(../../qtpromise.pri)

View File

@ -12,41 +12,80 @@
using namespace QtPromise; using namespace QtPromise;
class tst_qpromise_resolve : public QObject class tst_helpers_resolve : public QObject
{ {
Q_OBJECT Q_OBJECT
private Q_SLOTS: private Q_SLOTS:
void resolveWithValue(); void resolveWithValue();
void resolveWithNoValue(); void resolveWithNoValue();
void resolveWithTypedPromise();
void resolveWithVoidPromise();
void resolveWithQSharedPtr(); void resolveWithQSharedPtr();
void resolveWithStdSharedPtr(); void resolveWithStdSharedPtr();
}; };
QTEST_MAIN(tst_qpromise_resolve) QTEST_MAIN(tst_helpers_resolve)
#include "tst_resolve.moc" #include "tst_resolve.moc"
void tst_qpromise_resolve::resolveWithValue() void tst_helpers_resolve::resolveWithValue()
{ {
const int value = 42; const int value = 42;
auto p0 = QPromise<int>::resolve(value); auto p0 = QPromise<int>::resolve(value);
auto p1 = QPromise<int>::resolve(43); auto p1 = QPromise<int>::resolve(43);
Q_STATIC_ASSERT((std::is_same<decltype(p0), QPromise<int>>::value));
Q_STATIC_ASSERT((std::is_same<decltype(p1), QPromise<int>>::value));
QCOMPARE(p0.isFulfilled(), true); QCOMPARE(p0.isFulfilled(), true);
QCOMPARE(p1.isFulfilled(), true); QCOMPARE(p1.isFulfilled(), true);
QCOMPARE(waitForValue(p0, -1), 42); QCOMPARE(waitForValue(p0, -1), 42);
QCOMPARE(waitForValue(p1, -1), 43); QCOMPARE(waitForValue(p1, -1), 43);
} }
void tst_qpromise_resolve::resolveWithNoValue() void tst_helpers_resolve::resolveWithNoValue()
{ {
auto p = QPromise<void>::resolve(); auto p = QPromise<void>::resolve();
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<void>>::value));
QCOMPARE(p.isFulfilled(), true); QCOMPARE(p.isFulfilled(), true);
QCOMPARE(waitForValue(p, -1, 42), 42);
}
void tst_helpers_resolve::resolveWithTypedPromise()
{
auto p = QtPromise::qPromise(
QPromise<QString>([](const QPromiseResolve<QString>& resolve) {
QtPromisePrivate::qtpromise_defer([=](){
resolve("foo");
});
}));
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QString>>::value));
QCOMPARE(p.isPending(), true);
QCOMPARE(waitForValue(p, QString()), QString("foo"));
}
void tst_helpers_resolve::resolveWithVoidPromise()
{
int check;
auto p = QtPromise::qPromise(
QPromise<void>([&](const QPromiseResolve<void>& resolve) {
QtPromisePrivate::qtpromise_defer([=, &check](){
check = 8;
resolve();
});
}));
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<void>>::value));
QCOMPARE(p.isPending(), true);
QCOMPARE(waitForValue(p, -1, 42), 42);
QCOMPARE(check, 8);
} }
// https://github.com/simonbrunel/qtpromise/issues/6 // https://github.com/simonbrunel/qtpromise/issues/6
void tst_qpromise_resolve::resolveWithQSharedPtr() void tst_helpers_resolve::resolveWithQSharedPtr()
{ {
QWeakPointer<int> wptr; QWeakPointer<int> wptr;
@ -66,7 +105,7 @@ void tst_qpromise_resolve::resolveWithQSharedPtr()
} }
// https://github.com/simonbrunel/qtpromise/issues/6 // https://github.com/simonbrunel/qtpromise/issues/6
void tst_qpromise_resolve::resolveWithStdSharedPtr() void tst_helpers_resolve::resolveWithStdSharedPtr()
{ {
std::weak_ptr<int> wptr; std::weak_ptr<int> wptr;

View File

@ -1,244 +0,0 @@
// QtPromise
#include <QtPromise>
// Qt
#include <QtTest>
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<decltype(p), QPromise<int>>::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<decltype(p), QPromise<void>>::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<QString>([](const QPromiseResolve<QString>& resolve) {
QtPromisePrivate::qtpromise_defer([=](){
resolve("foo");
});
}));
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QString>>::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<int> values;
auto p = QtPromise::qPromise(
QPromise<void>([&](const QPromiseResolve<void>& resolve) {
QtPromisePrivate::qtpromise_defer([=, &values](){
values << 42;
resolve();
});
}));
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<void>>::value));
QCOMPARE(p.isPending(), true);
p.then([&]() {
values << 43;
}).wait();
QCOMPARE(p.isFulfilled(), true);
QCOMPARE(values, QList<int>({42, 43}));
}
void tst_helpers::allFulfilled()
{
auto p0 = QtPromise::qPromise(42);
auto p1 = QtPromise::qPromise(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(p.isPending(), true);
QCOMPARE(p0.isFulfilled(), true);
QCOMPARE(p1.isFulfilled(), true);
QCOMPARE(p2.isPending(), true);
QVector<int> values;
p.then([&](const QVector<int>& res) {
values = res;
}).wait();
QCOMPARE(p.isFulfilled(), true);
QCOMPARE(p2.isFulfilled(), true);
QCOMPARE(values, QVector<int>({42, 43, 44}));
}
void tst_helpers::allFulfilled_void()
{
auto p0 = QtPromise::qPromise();
auto p1 = QtPromise::qPromise();
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(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<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(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<int>();
}).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<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(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<QPromise<int>>());
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value));
QCOMPARE(p.isFulfilled(), true);
QVector<int> values;
p.then([&](const QVector<int>& res) {
values = res;
}).wait();
QCOMPARE(values, QVector<int>());
}
void tst_helpers::allEmpty_void()
{
auto p = qPromiseAll(QVector<QPromise<void>>());
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<void>>::value));
QCOMPARE(p.isFulfilled(), true);
}

View File

@ -1,103 +0,0 @@
// Tests
#include "../../shared/utils.h"
// QtPromise
#include <QtPromise>
// Qt
#include <QtTest>
using namespace QtPromise;
class tst_qpromise_all : public QObject
{
Q_OBJECT
private Q_SLOTS:
void qList();
//void qVector();
void stdList();
void stdVector();
};
QTEST_MAIN(tst_qpromise_all)
#include "tst_all.moc"
namespace {
template <class Sequence>
struct SequenceTester
{
};
template <template <typename, typename...> class Sequence, typename ...Args>
struct SequenceTester<Sequence<QPromise<int>, Args...>>
{
static void exec()
{
Sequence<QPromise<int>, Args...> promises{
QPromise<int>::resolve(42),
QPromise<int>::resolve(43),
QPromise<int>::resolve(44)
};
promises.push_back(QPromise<int>::resolve(45));
promises.insert(++promises.begin(), QPromise<int>::resolve(46));
promises.pop_back();
auto p = QPromise<int>::all(promises);
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value));
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{
QPromise<void>::resolve(),
QPromise<void>::resolve(),
QPromise<void>::resolve()
};
promises.push_back(QPromise<void>::resolve());
promises.insert(++promises.begin(), QPromise<void>::resolve());
promises.pop_back();
auto p = QPromise<void>::all(promises);
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<void>>::value));
QCOMPARE(waitForValue(p, -1, 42), 42);
}
};
} // anonymous namespace
void tst_qpromise_all::qList()
{
SequenceTester<QList<QPromise<int>>>::exec();
SequenceTester<QList<QPromise<void>>>::exec();
}
// QVector::push_back/append isn't supported since it requires a default
// constructor (see https://github.com/simonbrunel/qtpromise/issues/3)
//void tst_qpromise_all::qVector()
//{
// SequenceTester<QVector<QPromise<int>>>::exec();
// SequenceTester<QVector<QPromise<void>>>::exec();
//}
void tst_qpromise_all::stdList()
{
SequenceTester<std::list<QPromise<int>>>::exec();
SequenceTester<std::list<QPromise<void>>>::exec();
}
void tst_qpromise_all::stdVector()
{
SequenceTester<std::vector<QPromise<int>>>::exec();
SequenceTester<std::vector<QPromise<void>>>::exec();
}

View File

@ -1,13 +1,10 @@
TEMPLATE = subdirs TEMPLATE = subdirs
SUBDIRS += \ SUBDIRS += \
all \
construct \ construct \
delay \ delay \
fail \ fail \
finally \ finally \
operators \ operators \
reject \
resolve \
tap \ tap \
tapfail \ tapfail \
then \ then \