Add support for Qt 6

This commit is contained in:
Simon Brunel 2022-06-26 18:10:20 +02:00
parent 461f09bef8
commit 9f01c130cd
42 changed files with 887 additions and 892 deletions

View File

@ -8,7 +8,8 @@ project(qtpromise VERSION 0.6.0 LANGUAGES CXX)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake") list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake")
find_package(Qt5 5.6.0 REQUIRED COMPONENTS Core) find_package(QT 5.6.0 NAMES Qt6 Qt5 REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core)
set(CMAKE_AUTOMOC ON) set(CMAKE_AUTOMOC ON)
set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD 11)
@ -17,7 +18,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_library(qtpromise INTERFACE) add_library(qtpromise INTERFACE)
add_library(qtpromise::qtpromise ALIAS qtpromise) add_library(qtpromise::qtpromise ALIAS qtpromise)
target_link_libraries(qtpromise INTERFACE Qt5::Core) target_link_libraries(qtpromise INTERFACE Qt${QT_VERSION_MAJOR}::Core)
target_include_directories(qtpromise INTERFACE "${CMAKE_CURRENT_LIST_DIR}/include") target_include_directories(qtpromise INTERFACE "${CMAKE_CURRENT_LIST_DIR}/include")
add_definitions( add_definitions(
@ -33,7 +34,6 @@ if(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
-Wpedantic -Wpedantic
-Wall -Wall
-Wextra -Wextra
-Wconversion -Wconversion
-Wdouble-promotion -Wdouble-promotion
-Wformat=2 -Wformat=2
@ -44,8 +44,6 @@ if(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
-Wsign-conversion -Wsign-conversion
-Wswitch-default -Wswitch-default
-Wunused-local-typedefs -Wunused-local-typedefs
# -Wuseless-cast # disabled due to Qt's moc warnings
-pedantic-errors -pedantic-errors
) )
@ -68,7 +66,6 @@ elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
-Wall -Wall
-Wextra -Wextra
-Wpedantic -Wpedantic
-Wsuggest-destructor-override -Wsuggest-destructor-override
-Wsuggest-override -Wsuggest-override
) )

View File

@ -13,7 +13,7 @@
[Promises/A+](https://promisesaplus.com/) implementation for [Qt/C++](https://www.qt.io/). [Promises/A+](https://promisesaplus.com/) implementation for [Qt/C++](https://www.qt.io/).
Requires [Qt 5.6](https://www.qt.io/download/) (or later) with [C++11 support enabled](https://wiki.qt.io/How_to_use_C++11_in_your_Qt_Projects). Requires [Qt 5.6](https://doc.qt.io/qt-5/) (or later) with [C++11 support enabled](https://wiki.qt.io/How_to_use_C++11_in_your_Qt_Projects) or [Qt 6](https://doc.qt.io/qt-6/).
## Documentation ## Documentation

View File

@ -17,8 +17,8 @@ function(qtpromise_add_test NAME)
endif() endif()
target_link_libraries(${_TARGET} target_link_libraries(${_TARGET}
Qt5::Concurrent Qt${QT_VERSION_MAJOR}::Concurrent
Qt5::Test Qt${QT_VERSION_MAJOR}::Test
qtpromise qtpromise
qtpromise.tests.utils qtpromise.tests.utils
${_ARG_LIBRARIES} ${_ARG_LIBRARIES}

View File

@ -15,7 +15,7 @@ footer: MIT Licensed | Copyright © Simon Brunel, https://github.com/simonbrunel
[Promises/A+](https://promisesaplus.com/) implementation for [Qt/C++](https://www.qt.io/). [Promises/A+](https://promisesaplus.com/) implementation for [Qt/C++](https://www.qt.io/).
Requires [Qt 5.6](https://www.qt.io/download/) (or later) with [C++11 support enabled](https://wiki.qt.io/How_to_use_C++11_in_your_Qt_Projects). Requires [Qt 5.6](https://doc.qt.io/qt-5/) (or later) with [C++11 support enabled](https://wiki.qt.io/How_to_use_C++11_in_your_Qt_Projects) or [Qt 6](https://doc.qt.io/qt-6/).
## Documentation ## Documentation

View File

@ -578,11 +578,18 @@ struct PromiseConverterBase<T, U, false>
return [](const T& value) { return [](const T& value) {
auto tmp = QVariant::fromValue(value); auto tmp = QVariant::fromValue(value);
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
// https://doc.qt.io/qt-6/qvariant.html#using-canconvert-and-convert-consecutively
if (tmp.canConvert(QMetaType{qMetaTypeId<U>()})
&& tmp.convert(QMetaType{qMetaTypeId<U>()})) {
return qvariant_cast<U>(tmp);
}
#else
// https://doc.qt.io/qt-5/qvariant.html#using-canconvert-and-convert-consecutively // https://doc.qt.io/qt-5/qvariant.html#using-canconvert-and-convert-consecutively
if (tmp.canConvert(qMetaTypeId<U>()) && tmp.convert(qMetaTypeId<U>())) { if (tmp.canConvert(qMetaTypeId<U>()) && tmp.convert(qMetaTypeId<U>())) {
return qvariant_cast<U>(tmp); return qvariant_cast<U>(tmp);
} }
#endif
throw QtPromise::QPromiseConversionException{}; throw QtPromise::QPromiseConversionException{};
}; };
} }

View File

@ -9,6 +9,7 @@
#define QTPROMISE_QPROMISECONNECTIONS_H #define QTPROMISE_QPROMISECONNECTIONS_H
#include <QtCore/QObject> #include <QtCore/QObject>
#include <memory> #include <memory>
namespace QtPromise { namespace QtPromise {
@ -18,7 +19,7 @@ class QPromiseConnections
public: public:
QPromiseConnections() : m_d(std::make_shared<Data>()) { } QPromiseConnections() : m_d(std::make_shared<Data>()) { }
int count() const { return m_d->connections.count(); } int count() const { return static_cast<int>(m_d->connections.count()); }
void disconnect() const { m_d->disconnect(); } void disconnect() const { m_d->disconnect(); }

View File

@ -1,4 +1,4 @@
find_package(Qt5 5.6.0 REQUIRED find_package(Qt${QT_VERSION_MAJOR} REQUIRED
COMPONENTS COMPONENTS
Concurrent Concurrent
Test Test

View File

@ -18,8 +18,6 @@
# define EXCEPT_CALL_COPY_CTOR 0 # define EXCEPT_CALL_COPY_CTOR 0
#endif #endif
using namespace QtPromise;
class tst_benchmark : public QObject class tst_benchmark : public QObject
{ {
Q_OBJECT Q_OBJECT
@ -43,7 +41,7 @@ void tst_benchmark::valueResolve()
{ {
{ // should move the value when resolved by rvalue { // should move the value when resolved by rvalue
Data::logs().reset(); Data::logs().reset();
QPromise<Data>{[&](const QPromiseResolve<Data>& resolve) { QtPromise::QPromise<Data>{[&](const QtPromise::QPromiseResolve<Data>& resolve) {
resolve(Data{42}); resolve(Data{42});
}}.wait(); }}.wait();
@ -54,7 +52,7 @@ void tst_benchmark::valueResolve()
} }
{ // should create one copy of the value when resolved by lvalue { // should create one copy of the value when resolved by lvalue
Data::logs().reset(); Data::logs().reset();
QPromise<Data>{[&](const QPromiseResolve<Data>& resolve) { QtPromise::QPromise<Data>{[&](const QtPromise::QPromiseResolve<Data>& resolve) {
Data value{42}; Data value{42};
resolve(value); resolve(value);
}}.wait(); }}.wait();
@ -70,7 +68,8 @@ void tst_benchmark::valueReject()
{ {
{ // should not create any data if rejected { // should not create any data if rejected
Data::logs().reset(); Data::logs().reset();
QPromise<Data>{[&](const QPromiseResolve<Data>&, const QPromiseReject<Data>& reject) { QtPromise::QPromise<Data>{[&](const QtPromise::QPromiseResolve<Data>&,
const QtPromise::QPromiseReject<Data>& reject) {
reject(QString{"foo"}); reject(QString{"foo"});
}}.wait(); }}.wait();
@ -83,10 +82,10 @@ void tst_benchmark::valueReject()
void tst_benchmark::valueThen() void tst_benchmark::valueThen()
{ {
{ // should not copy value on continutation if fulfilled { // should not copy value on continuation if fulfilled
int value = -1; int value = -1;
Data::logs().reset(); Data::logs().reset();
QPromise<Data>::resolve(Data{42}) QtPromise::QPromise<Data>::resolve(Data{42})
.then([&](const Data& res) { .then([&](const Data& res) {
value = res.value(); value = res.value();
}) })
@ -98,11 +97,11 @@ void tst_benchmark::valueThen()
QCOMPARE(Data::logs().refs, 0); QCOMPARE(Data::logs().refs, 0);
QCOMPARE(value, 42); QCOMPARE(value, 42);
} }
{ // should not create value on continutation if rejected { // should not create value on continuation if rejected
int value = -1; int value = -1;
QString error; QString error;
Data::logs().reset(); Data::logs().reset();
QPromise<Data>::reject(QString{"foo"}) QtPromise::QPromise<Data>::reject(QString{"foo"})
.then( .then(
[&](const Data& res) { [&](const Data& res) {
value = res.value(); value = res.value();
@ -122,7 +121,7 @@ void tst_benchmark::valueThen()
{ // should move the returned value when fulfilled { // should move the returned value when fulfilled
int value = -1; int value = -1;
Data::logs().reset(); Data::logs().reset();
QPromise<int>::resolve(42) QtPromise::QPromise<int>::resolve(42)
.then([&](int res) { .then([&](int res) {
return Data{res + 2}; return Data{res + 2};
}) })
@ -139,7 +138,7 @@ void tst_benchmark::valueThen()
} }
{ // should not create any data if handler throws { // should not create any data if handler throws
Data::logs().reset(); Data::logs().reset();
QPromise<int>::resolve(42) QtPromise::QPromise<int>::resolve(42)
.then([&](int res) { .then([&](int res) {
throw QString{"foo"}; throw QString{"foo"};
return Data{res + 2}; return Data{res + 2};
@ -155,12 +154,12 @@ void tst_benchmark::valueThen()
void tst_benchmark::valueDelayed() void tst_benchmark::valueDelayed()
{ {
{ // should not copy the value on continutation if fulfilled { // should not copy the value on continuation if fulfilled
int value = -1; int value = -1;
Data::logs().reset(); Data::logs().reset();
QPromise<int>::resolve(42) QtPromise::QPromise<int>::resolve(42)
.then([&](int res) { .then([&](int res) {
return QPromise<Data>::resolve(Data{res + 1}); return QtPromise::QPromise<Data>::resolve(Data{res + 1});
}) })
.then([&](const Data& res) { .then([&](const Data& res) {
value = res.value(); value = res.value();
@ -173,11 +172,11 @@ void tst_benchmark::valueDelayed()
QCOMPARE(Data::logs().refs, 0); QCOMPARE(Data::logs().refs, 0);
QCOMPARE(value, 43); QCOMPARE(value, 43);
} }
{ // should not create value on continutation if rejected { // should not create value on continuation if rejected
Data::logs().reset(); Data::logs().reset();
QPromise<int>::resolve(42) QtPromise::QPromise<int>::resolve(42)
.then([&]() { .then([&]() {
return QPromise<Data>::reject(QString{"foo"}); return QtPromise::QPromise<Data>::reject(QString{"foo"});
}) })
.wait(); .wait();
@ -190,10 +189,10 @@ void tst_benchmark::valueDelayed()
void tst_benchmark::valueFinally() void tst_benchmark::valueFinally()
{ {
{ // should not copy the value on continutation if fulfilled { // should not copy the value on continuation if fulfilled
int value = -1; int value = -1;
Data::logs().reset(); Data::logs().reset();
QPromise<Data>::resolve(Data{42}) QtPromise::QPromise<Data>::resolve(Data{42})
.finally([&]() { .finally([&]() {
value = 42; value = 42;
}) })
@ -205,10 +204,10 @@ void tst_benchmark::valueFinally()
QCOMPARE(Data::logs().refs, 0); QCOMPARE(Data::logs().refs, 0);
QCOMPARE(value, 42); QCOMPARE(value, 42);
} }
{ // should not create value on continutation if rejected { // should not create value on continuation if rejected
int value = -1; int value = -1;
Data::logs().reset(); Data::logs().reset();
QPromise<Data>::reject(QString{"foo"}) QtPromise::QPromise<Data>::reject(QString{"foo"})
.finally([&]() { .finally([&]() {
value = 42; value = 42;
}) })
@ -224,10 +223,10 @@ void tst_benchmark::valueFinally()
void tst_benchmark::valueTap() void tst_benchmark::valueTap()
{ {
{ // should not copy the value on continutation if fulfilled { // should not copy the value on continuation if fulfilled
int value = -1; int value = -1;
Data::logs().reset(); Data::logs().reset();
QPromise<Data>::resolve(Data{42}) QtPromise::QPromise<Data>::resolve(Data{42})
.tap([&](const Data& res) { .tap([&](const Data& res) {
value = res.value(); value = res.value();
}) })
@ -239,10 +238,10 @@ void tst_benchmark::valueTap()
QCOMPARE(Data::logs().refs, 0); QCOMPARE(Data::logs().refs, 0);
QCOMPARE(value, 42); QCOMPARE(value, 42);
} }
{ // should not create value on continutation if rejected { // should not create value on continuation if rejected
int value = -1; int value = -1;
Data::logs().reset(); Data::logs().reset();
QPromise<Data>::reject(QString{"foo"}) QtPromise::QPromise<Data>::reject(QString{"foo"})
.tap([&](const Data& res) { .tap([&](const Data& res) {
value = res.value(); value = res.value();
}) })
@ -260,7 +259,8 @@ void tst_benchmark::errorReject()
{ {
{ // should create one copy of the error when rejected by rvalue { // should create one copy of the error when rejected by rvalue
Data::logs().reset(); Data::logs().reset();
QPromise<int>{[&](const QPromiseResolve<int>&, const QPromiseReject<int>& reject) { QtPromise::QPromise<int>{[&](const QtPromise::QPromiseResolve<int>&,
const QtPromise::QPromiseReject<int>& reject) {
reject(Data{42}); reject(Data{42});
}}.wait(); }}.wait();
@ -271,7 +271,8 @@ void tst_benchmark::errorReject()
} }
{ // should create one copy of the error when rejected by lvalue (no extra copy) { // should create one copy of the error when rejected by lvalue (no extra copy)
Data::logs().reset(); Data::logs().reset();
QPromise<int>{[&](const QPromiseResolve<int>&, const QPromiseReject<int>& reject) { QtPromise::QPromise<int>{[&](const QtPromise::QPromiseResolve<int>&,
const QtPromise::QPromiseReject<int>& reject) {
Data error{42}; Data error{42};
reject(error); reject(error);
}}.wait(); }}.wait();
@ -285,10 +286,10 @@ void tst_benchmark::errorReject()
void tst_benchmark::errorThen() void tst_benchmark::errorThen()
{ {
{ // should not copy error on continutation if rejected { // should not copy error on continuation if rejected
int value = -1; int value = -1;
Data::logs().reset(); Data::logs().reset();
QPromise<void>::reject(Data{42}) QtPromise::QPromise<void>::reject(Data{42})
.fail([&](const Data& res) { .fail([&](const Data& res) {
value = res.value(); value = res.value();
}) })
@ -301,10 +302,10 @@ void tst_benchmark::errorThen()
QCOMPARE(Data::logs().refs, 0); QCOMPARE(Data::logs().refs, 0);
QCOMPARE(value, 42); QCOMPARE(value, 42);
} }
{ // should not copy error on continutation if rethrown { // should not copy error on continuation if rethrown
int value = -1; int value = -1;
Data::logs().reset(); Data::logs().reset();
QPromise<void>::reject(Data{42}) QtPromise::QPromise<void>::reject(Data{42})
.fail([](const Data&) { .fail([](const Data&) {
throw; throw;
}) })

View File

@ -12,8 +12,6 @@
#include <memory> #include <memory>
using namespace QtPromise;
class tst_cpp14_resolver_lambda_auto : public QObject class tst_cpp14_resolver_lambda_auto : public QObject
{ {
Q_OBJECT Q_OBJECT
@ -28,19 +26,19 @@ QTEST_MAIN(tst_cpp14_resolver_lambda_auto)
void tst_cpp14_resolver_lambda_auto::resolverTwoAutoArgs() void tst_cpp14_resolver_lambda_auto::resolverTwoAutoArgs()
{ {
QPromise<int> p0{[](auto resolve, auto reject) { QtPromise::QPromise<int> p0{[](auto resolve, auto reject) {
Q_UNUSED(reject) Q_UNUSED(reject)
resolve(42); resolve(42);
}}; }};
QPromise<int> p1{[](auto resolve, const auto& reject) { QtPromise::QPromise<int> p1{[](auto resolve, const auto& reject) {
Q_UNUSED(reject) Q_UNUSED(reject)
resolve(42); resolve(42);
}}; }};
QPromise<int> p2{[](const auto& resolve, auto reject) { QtPromise::QPromise<int> p2{[](const auto& resolve, auto reject) {
Q_UNUSED(reject) Q_UNUSED(reject)
resolve(42); resolve(42);
}}; }};
QPromise<int> p3{[](const auto& resolve, const auto& reject) { QtPromise::QPromise<int> p3{[](const auto& resolve, const auto& reject) {
Q_UNUSED(reject) Q_UNUSED(reject)
resolve(42); resolve(42);
}}; }};
@ -54,19 +52,19 @@ void tst_cpp14_resolver_lambda_auto::resolverTwoAutoArgs()
void tst_cpp14_resolver_lambda_auto::resolverTwoAutoArgs_void() void tst_cpp14_resolver_lambda_auto::resolverTwoAutoArgs_void()
{ {
QPromise<void> p0{[](auto resolve, auto reject) { QtPromise::QPromise<void> p0{[](auto resolve, auto reject) {
Q_UNUSED(reject) Q_UNUSED(reject)
resolve(); resolve();
}}; }};
QPromise<void> p1{[](auto resolve, const auto& reject) { QtPromise::QPromise<void> p1{[](auto resolve, const auto& reject) {
Q_UNUSED(reject) Q_UNUSED(reject)
resolve(); resolve();
}}; }};
QPromise<void> p2{[](const auto& resolve, auto reject) { QtPromise::QPromise<void> p2{[](const auto& resolve, auto reject) {
Q_UNUSED(reject) Q_UNUSED(reject)
resolve(); resolve();
}}; }};
QPromise<void> p3{[](const auto& resolve, const auto& reject) { QtPromise::QPromise<void> p3{[](const auto& resolve, const auto& reject) {
Q_UNUSED(reject) Q_UNUSED(reject)
resolve(); resolve();
}}; }};

View File

@ -14,8 +14,6 @@
#include <memory> #include <memory>
using namespace QtPromise;
class tst_deprecations_helpers_qpromise : public QObject class tst_deprecations_helpers_qpromise : public QObject
{ {
Q_OBJECT Q_OBJECT
@ -41,13 +39,13 @@ void tst_deprecations_helpers_qpromise::value()
int v0 = 42; int v0 = 42;
const int v1 = 42; const int v1 = 42;
auto p0 = qPromise(42); auto p0 = QtPromise::qPromise(42);
auto p1 = qPromise(v0); auto p1 = QtPromise::qPromise(v0);
auto p2 = qPromise(v1); auto p2 = QtPromise::qPromise(v1);
Q_STATIC_ASSERT((std::is_same<decltype(p0), QPromise<int>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p0), QtPromise::QPromise<int>>::value));
Q_STATIC_ASSERT((std::is_same<decltype(p1), QPromise<int>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p1), QtPromise::QPromise<int>>::value));
Q_STATIC_ASSERT((std::is_same<decltype(p2), QPromise<int>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p2), QtPromise::QPromise<int>>::value));
for (const auto& p : {p0, p1, p2}) { for (const auto& p : {p0, p1, p2}) {
QCOMPARE(p.isFulfilled(), true); QCOMPARE(p.isFulfilled(), true);
@ -59,9 +57,9 @@ void tst_deprecations_helpers_qpromise::value()
void tst_deprecations_helpers_qpromise::noValue() void tst_deprecations_helpers_qpromise::noValue()
{ {
auto p = qPromise(); auto p = QtPromise::qPromise();
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<void>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<void>>::value));
QCOMPARE(p.isFulfilled(), true); QCOMPARE(p.isFulfilled(), true);
QCOMPARE(waitForValue(p, -1, 42), 42); QCOMPARE(waitForValue(p, -1, 42), 42);
@ -72,9 +70,9 @@ void tst_deprecations_helpers_qpromise::moveRValue()
Data::logs().reset(); Data::logs().reset();
{ {
auto p = qPromise(Data{42}).wait(); auto p = QtPromise::qPromise(Data{42}).wait();
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<Data>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<Data>>::value));
} }
QCOMPARE(Data::logs().ctor, 1); QCOMPARE(Data::logs().ctor, 1);
@ -89,9 +87,9 @@ void tst_deprecations_helpers_qpromise::copyLValue()
{ {
Data value{42}; Data value{42};
auto p = qPromise(value).wait(); auto p = QtPromise::qPromise(value).wait();
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<Data>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<Data>>::value));
} }
QCOMPARE(Data::logs().ctor, 1); QCOMPARE(Data::logs().ctor, 1);
@ -103,24 +101,26 @@ void tst_deprecations_helpers_qpromise::copyLValue()
// https://github.com/simonbrunel/qtpromise/issues/6 // https://github.com/simonbrunel/qtpromise/issues/6
void tst_deprecations_helpers_qpromise::qtSharedPtr() void tst_deprecations_helpers_qpromise::qtSharedPtr()
{ {
using DataSPtr = QSharedPointer<Data>;
Data::logs().reset(); Data::logs().reset();
QWeakPointer<Data> wptr; QWeakPointer<Data> wptr;
{ {
auto sptr0 = QSharedPointer<Data>::create(42); auto sptr0 = DataSPtr::create(42);
const QSharedPointer<Data> sptr1 = sptr0; const DataSPtr sptr1 = sptr0;
auto p0 = qPromise(QSharedPointer<Data>::create(42)); auto p0 = QtPromise::qPromise(DataSPtr::create(42));
auto p1 = qPromise(sptr0); auto p1 = QtPromise::qPromise(sptr0);
auto p2 = qPromise(sptr1); auto p2 = QtPromise::qPromise(sptr1);
Q_STATIC_ASSERT((std::is_same<decltype(p0), QPromise<QSharedPointer<Data>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p0), QtPromise::QPromise<DataSPtr>>::value));
Q_STATIC_ASSERT((std::is_same<decltype(p1), QPromise<QSharedPointer<Data>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p1), QtPromise::QPromise<DataSPtr>>::value));
Q_STATIC_ASSERT((std::is_same<decltype(p2), QPromise<QSharedPointer<Data>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p2), QtPromise::QPromise<DataSPtr>>::value));
QCOMPARE(waitForValue(p1, QSharedPointer<Data>{}), sptr0); QCOMPARE(waitForValue(p1, DataSPtr{}), sptr0);
QCOMPARE(waitForValue(p2, QSharedPointer<Data>{}), sptr1); QCOMPARE(waitForValue(p2, DataSPtr{}), sptr1);
wptr = sptr0; wptr = sptr0;
@ -139,24 +139,26 @@ void tst_deprecations_helpers_qpromise::qtSharedPtr()
// https://github.com/simonbrunel/qtpromise/issues/6 // https://github.com/simonbrunel/qtpromise/issues/6
void tst_deprecations_helpers_qpromise::stdSharedPtr() void tst_deprecations_helpers_qpromise::stdSharedPtr()
{ {
using DataSPtr = std::shared_ptr<Data>;
Data::logs().reset(); Data::logs().reset();
std::weak_ptr<Data> wptr; std::weak_ptr<Data> wptr;
{ {
auto sptr0 = std::make_shared<Data>(42); auto sptr0 = std::make_shared<Data>(42);
const std::shared_ptr<Data> sptr1 = sptr0; const DataSPtr sptr1 = sptr0;
auto p0 = qPromise(std::make_shared<Data>(42)); auto p0 = QtPromise::qPromise(std::make_shared<Data>(42));
auto p1 = qPromise(sptr0); auto p1 = QtPromise::qPromise(sptr0);
auto p2 = qPromise(sptr1); auto p2 = QtPromise::qPromise(sptr1);
Q_STATIC_ASSERT((std::is_same<decltype(p0), QPromise<std::shared_ptr<Data>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p0), QtPromise::QPromise<DataSPtr>>::value));
Q_STATIC_ASSERT((std::is_same<decltype(p1), QPromise<std::shared_ptr<Data>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p1), QtPromise::QPromise<DataSPtr>>::value));
Q_STATIC_ASSERT((std::is_same<decltype(p2), QPromise<std::shared_ptr<Data>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p2), QtPromise::QPromise<DataSPtr>>::value));
QCOMPARE(waitForValue(p1, std::shared_ptr<Data>{}), sptr0); QCOMPARE(waitForValue(p1, DataSPtr{}), sptr0);
QCOMPARE(waitForValue(p2, std::shared_ptr<Data>{}), sptr1); QCOMPARE(waitForValue(p2, DataSPtr{}), sptr1);
wptr = sptr0; wptr = sptr0;
@ -174,22 +176,22 @@ void tst_deprecations_helpers_qpromise::stdSharedPtr()
void tst_deprecations_helpers_qpromise::typedPromise() void tst_deprecations_helpers_qpromise::typedPromise()
{ {
auto resolver = [](const QPromiseResolve<int>& resolve) { auto resolver = [](const QtPromise::QPromiseResolve<int>& resolve) {
QtPromisePrivate::qtpromise_defer([=]() { QtPromisePrivate::qtpromise_defer([=]() {
resolve(42); resolve(42);
}); });
}; };
QPromise<int> v0{resolver}; QtPromise::QPromise<int> v0{resolver};
const QPromise<int> v1 = v0; const QtPromise::QPromise<int> v1 = v0;
auto p0 = qPromise(QPromise<int>{resolver}); auto p0 = QtPromise::qPromise(QtPromise::QPromise<int>{resolver});
auto p1 = qPromise(v0); auto p1 = QtPromise::qPromise(v0);
auto p2 = qPromise(v1); auto p2 = QtPromise::qPromise(v1);
Q_STATIC_ASSERT((std::is_same<decltype(p0), QPromise<int>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p0), QtPromise::QPromise<int>>::value));
Q_STATIC_ASSERT((std::is_same<decltype(p1), QPromise<int>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p1), QtPromise::QPromise<int>>::value));
Q_STATIC_ASSERT((std::is_same<decltype(p2), QPromise<int>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p2), QtPromise::QPromise<int>>::value));
for (const auto& promise : {p0, p1, p2}) { for (const auto& promise : {p0, p1, p2}) {
QCOMPARE(promise.isPending(), true); QCOMPARE(promise.isPending(), true);
@ -201,22 +203,22 @@ void tst_deprecations_helpers_qpromise::typedPromise()
void tst_deprecations_helpers_qpromise::voidPromise() void tst_deprecations_helpers_qpromise::voidPromise()
{ {
auto resolver = [](const QPromiseResolve<void>& resolve) { auto resolver = [](const QtPromise::QPromiseResolve<void>& resolve) {
QtPromisePrivate::qtpromise_defer([=]() { QtPromisePrivate::qtpromise_defer([=]() {
resolve(); resolve();
}); });
}; };
QPromise<void> v0{resolver}; QtPromise::QPromise<void> v0{resolver};
const QPromise<void> v1 = v0; const QtPromise::QPromise<void> v1 = v0;
auto p0 = qPromise(QPromise<void>{resolver}); auto p0 = QtPromise::qPromise(QtPromise::QPromise<void>{resolver});
auto p1 = qPromise(v0); auto p1 = QtPromise::qPromise(v0);
auto p2 = qPromise(v1); auto p2 = QtPromise::qPromise(v1);
Q_STATIC_ASSERT((std::is_same<decltype(p0), QPromise<void>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p0), QtPromise::QPromise<void>>::value));
Q_STATIC_ASSERT((std::is_same<decltype(p1), QPromise<void>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p1), QtPromise::QPromise<void>>::value));
Q_STATIC_ASSERT((std::is_same<decltype(p2), QPromise<void>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p2), QtPromise::QPromise<void>>::value));
for (const auto& promise : {p0, p1, p2}) { for (const auto& promise : {p0, p1, p2}) {
QCOMPARE(promise.isPending(), true); QCOMPARE(promise.isPending(), true);
@ -234,13 +236,13 @@ void tst_deprecations_helpers_qpromise::typedFuture()
QFuture<int> v0 = QtConcurrent::run(fn); QFuture<int> v0 = QtConcurrent::run(fn);
const QFuture<int> v1 = v0; const QFuture<int> v1 = v0;
auto p0 = qPromise(QtConcurrent::run(fn)); auto p0 = QtPromise::qPromise(QtConcurrent::run(fn));
auto p1 = qPromise(v0); auto p1 = QtPromise::qPromise(v0);
auto p2 = qPromise(v1); auto p2 = QtPromise::qPromise(v1);
Q_STATIC_ASSERT((std::is_same<decltype(p0), QPromise<int>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p0), QtPromise::QPromise<int>>::value));
Q_STATIC_ASSERT((std::is_same<decltype(p1), QPromise<int>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p1), QtPromise::QPromise<int>>::value));
Q_STATIC_ASSERT((std::is_same<decltype(p2), QPromise<int>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p2), QtPromise::QPromise<int>>::value));
for (const auto& promise : {p0, p1, p2}) { for (const auto& promise : {p0, p1, p2}) {
QCOMPARE(promise.isPending(), true); QCOMPARE(promise.isPending(), true);
@ -256,13 +258,13 @@ void tst_deprecations_helpers_qpromise::voidFuture()
QFuture<void> v0 = QtConcurrent::run(fn); QFuture<void> v0 = QtConcurrent::run(fn);
const QFuture<void> v1 = v0; const QFuture<void> v1 = v0;
auto p0 = qPromise(QtConcurrent::run(fn)); auto p0 = QtPromise::qPromise(QtConcurrent::run(fn));
auto p1 = qPromise(v0); auto p1 = QtPromise::qPromise(v0);
auto p2 = qPromise(v1); auto p2 = QtPromise::qPromise(v1);
Q_STATIC_ASSERT((std::is_same<decltype(p0), QPromise<void>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p0), QtPromise::QPromise<void>>::value));
Q_STATIC_ASSERT((std::is_same<decltype(p1), QPromise<void>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p1), QtPromise::QPromise<void>>::value));
Q_STATIC_ASSERT((std::is_same<decltype(p2), QPromise<void>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p2), QtPromise::QPromise<void>>::value));
for (const auto& promise : {p0, p1, p2}) { for (const auto& promise : {p0, p1, p2}) {
QCOMPARE(promise.isPending(), true); QCOMPARE(promise.isPending(), true);

View File

@ -10,8 +10,6 @@
#include <QtPromise> #include <QtPromise>
#include <QtTest> #include <QtTest>
using namespace QtPromise;
class tst_deprecations_helpers_qpromiseall : public QObject class tst_deprecations_helpers_qpromiseall : public QObject
{ {
Q_OBJECT Q_OBJECT
@ -36,7 +34,7 @@ namespace {
template<class Sequence> template<class Sequence>
struct SequenceTester struct SequenceTester
{ {
Q_STATIC_ASSERT((std::is_same<typename Sequence::value_type, QPromise<int>>::value)); Q_STATIC_ASSERT((std::is_same<typename Sequence::value_type, QtPromise::QPromise<int>>::value));
static void exec() static void exec()
{ {
@ -48,18 +46,18 @@ struct SequenceTester
auto p = qPromiseAll(promises); auto p = qPromiseAll(promises);
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<QVector<int>>>::value));
QCOMPARE(p.isPending(), true); QCOMPARE(p.isPending(), true);
QCOMPARE(waitForValue(p, QVector<int>{}), (QVector<int>{42, 46, 43, 44})); QCOMPARE(waitForValue(p, QVector<int>{}), (QVector<int>{42, 46, 43, 44}));
} }
}; };
template<template<typename, typename...> class Sequence, typename... Args> template<template<typename, typename...> class Sequence, typename... Args>
struct SequenceTester<Sequence<QPromise<void>, Args...>> struct SequenceTester<Sequence<QtPromise::QPromise<void>, Args...>>
{ {
static void exec() static void exec()
{ {
Sequence<QPromise<void>, Args...> promises{QtPromise::resolve(), Sequence<QtPromise::QPromise<void>, Args...> promises{QtPromise::resolve(),
QtPromise::resolve(), QtPromise::resolve(),
QtPromise::resolve()}; QtPromise::resolve()};
@ -69,7 +67,7 @@ struct SequenceTester<Sequence<QPromise<void>, Args...>>
auto p = qPromiseAll(promises); auto p = qPromiseAll(promises);
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<void>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<void>>::value));
QCOMPARE(p.isPending(), true); QCOMPARE(p.isPending(), true);
QCOMPARE(waitForValue(p, -1, 42), 42); QCOMPARE(waitForValue(p, -1, 42), 42);
} }
@ -79,18 +77,18 @@ struct SequenceTester<Sequence<QPromise<void>, Args...>>
void tst_deprecations_helpers_qpromiseall::emptySequence() void tst_deprecations_helpers_qpromiseall::emptySequence()
{ {
auto p = qPromiseAll(QVector<QPromise<int>>()); auto p = qPromiseAll(QVector<QtPromise::QPromise<int>>());
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<QVector<int>>>::value));
QCOMPARE(p.isFulfilled(), true); QCOMPARE(p.isFulfilled(), true);
QCOMPARE(waitForValue(p, QVector<int>{}), QVector<int>{}); QCOMPARE(waitForValue(p, QVector<int>{}), QVector<int>{});
} }
void tst_deprecations_helpers_qpromiseall::emptySequence_void() void tst_deprecations_helpers_qpromiseall::emptySequence_void()
{ {
auto p = qPromiseAll(QVector<QPromise<void>>()); auto p = qPromiseAll(QVector<QtPromise::QPromise<void>>());
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<void>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<void>>::value));
QCOMPARE(p.isFulfilled(), true); QCOMPARE(p.isFulfilled(), true);
QCOMPARE(waitForValue(p, -1, 42), 42); QCOMPARE(waitForValue(p, -1, 42), 42);
} }
@ -99,15 +97,15 @@ void tst_deprecations_helpers_qpromiseall::allPromisesSucceed()
{ {
auto p0 = QtPromise::resolve(42); auto p0 = QtPromise::resolve(42);
auto p1 = QtPromise::resolve(44); auto p1 = QtPromise::resolve(44);
auto p2 = QPromise<int>{[](const QPromiseResolve<int>& resolve) { auto p2 = QtPromise::QPromise<int>{[](const QtPromise::QPromiseResolve<int>& resolve) {
QtPromisePrivate::qtpromise_defer([=]() { QtPromisePrivate::qtpromise_defer([=]() {
resolve(43); resolve(43);
}); });
}}; }};
auto p = qPromiseAll(QVector<QPromise<int>>{p0, p2, p1}); auto p = QtPromise::qPromiseAll(QVector<QtPromise::QPromise<int>>{p0, p2, p1});
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<QVector<int>>>::value));
QCOMPARE(p0.isFulfilled(), true); QCOMPARE(p0.isFulfilled(), true);
QCOMPARE(p1.isFulfilled(), true); QCOMPARE(p1.isFulfilled(), true);
QCOMPARE(p2.isPending(), true); QCOMPARE(p2.isPending(), true);
@ -120,15 +118,15 @@ void tst_deprecations_helpers_qpromiseall::allPromisesSucceed_void()
{ {
auto p0 = QtPromise::resolve(); auto p0 = QtPromise::resolve();
auto p1 = QtPromise::resolve(); auto p1 = QtPromise::resolve();
auto p2 = QPromise<void>{[](const QPromiseResolve<void>& resolve) { auto p2 = QtPromise::QPromise<void>{[](const QtPromise::QPromiseResolve<void>& resolve) {
QtPromisePrivate::qtpromise_defer([=]() { QtPromisePrivate::qtpromise_defer([=]() {
resolve(); resolve();
}); });
}}; }};
auto p = qPromiseAll(QVector<QPromise<void>>{p0, p2, p1}); auto p = QtPromise::qPromiseAll(QVector<QtPromise::QPromise<void>>{p0, p2, p1});
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<void>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<void>>::value));
QCOMPARE(p0.isFulfilled(), true); QCOMPARE(p0.isFulfilled(), true);
QCOMPARE(p1.isFulfilled(), true); QCOMPARE(p1.isFulfilled(), true);
QCOMPARE(p2.isPending(), true); QCOMPARE(p2.isPending(), true);
@ -141,15 +139,16 @@ void tst_deprecations_helpers_qpromiseall::atLeastOnePromiseReject()
{ {
auto p0 = QtPromise::resolve(42); auto p0 = QtPromise::resolve(42);
auto p1 = QtPromise::resolve(44); auto p1 = QtPromise::resolve(44);
auto p2 = QPromise<int>{[](const QPromiseResolve<int>&, const QPromiseReject<int>& reject) { auto p2 = QtPromise::QPromise<int>{
[](const QtPromise::QPromiseResolve<int>&, const QtPromise::QPromiseReject<int>& reject) {
QtPromisePrivate::qtpromise_defer([=]() { QtPromisePrivate::qtpromise_defer([=]() {
reject(QString{"foo"}); reject(QString{"foo"});
}); });
}}; }};
auto p = qPromiseAll(QVector<QPromise<int>>{p0, p2, p1}); auto p = QtPromise::qPromiseAll(QVector<QtPromise::QPromise<int>>{p0, p2, p1});
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<QVector<int>>>::value));
QCOMPARE(p0.isFulfilled(), true); QCOMPARE(p0.isFulfilled(), true);
QCOMPARE(p1.isFulfilled(), true); QCOMPARE(p1.isFulfilled(), true);
QCOMPARE(p2.isPending(), true); QCOMPARE(p2.isPending(), true);
@ -162,15 +161,16 @@ void tst_deprecations_helpers_qpromiseall::atLeastOnePromiseReject_void()
{ {
auto p0 = QtPromise::resolve(); auto p0 = QtPromise::resolve();
auto p1 = QtPromise::resolve(); auto p1 = QtPromise::resolve();
auto p2 = QPromise<void>{[](const QPromiseResolve<void>&, const QPromiseReject<void>& reject) { auto p2 = QtPromise::QPromise<void>{
[](const QtPromise::QPromiseResolve<void>&, const QtPromise::QPromiseReject<void>& reject) {
QtPromisePrivate::qtpromise_defer([=]() { QtPromisePrivate::qtpromise_defer([=]() {
reject(QString{"foo"}); reject(QString{"foo"});
}); });
}}; }};
auto p = qPromiseAll(QVector<QPromise<void>>{p0, p2, p1}); auto p = QtPromise::qPromiseAll(QVector<QtPromise::QPromise<void>>{p0, p2, p1});
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<void>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<void>>::value));
QCOMPARE(p0.isFulfilled(), true); QCOMPARE(p0.isFulfilled(), true);
QCOMPARE(p1.isFulfilled(), true); QCOMPARE(p1.isFulfilled(), true);
QCOMPARE(p2.isPending(), true); QCOMPARE(p2.isPending(), true);
@ -185,9 +185,9 @@ void tst_deprecations_helpers_qpromiseall::preserveOrder()
auto p1 = QtPromise::resolve(43).delay(100); auto p1 = QtPromise::resolve(43).delay(100);
auto p2 = QtPromise::resolve(44).delay(250); auto p2 = QtPromise::resolve(44).delay(250);
auto p = qPromiseAll(QVector<QPromise<int>>{p0, p1, p2}); auto p = QtPromise::qPromiseAll(QVector<QtPromise::QPromise<int>>{p0, p1, p2});
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<QVector<int>>>::value));
QCOMPARE(p0.isPending(), true); QCOMPARE(p0.isPending(), true);
QCOMPARE(p1.isPending(), true); QCOMPARE(p1.isPending(), true);
QCOMPARE(p2.isPending(), true); QCOMPARE(p2.isPending(), true);
@ -203,16 +203,16 @@ void tst_deprecations_helpers_qpromiseall::preserveOrder()
void tst_deprecations_helpers_qpromiseall::sequenceTypes() void tst_deprecations_helpers_qpromiseall::sequenceTypes()
{ {
SequenceTester<QList<QPromise<int>>>::exec(); SequenceTester<QList<QtPromise::QPromise<int>>>::exec();
// SequenceTester<QVector<QPromise<int>>>::exec(); // SequenceTester<QVector<QtPromise::QPromise<int>>>::exec();
SequenceTester<std::list<QPromise<int>>>::exec(); SequenceTester<std::list<QtPromise::QPromise<int>>>::exec();
SequenceTester<std::vector<QPromise<int>>>::exec(); SequenceTester<std::vector<QtPromise::QPromise<int>>>::exec();
} }
void tst_deprecations_helpers_qpromiseall::sequenceTypes_void() void tst_deprecations_helpers_qpromiseall::sequenceTypes_void()
{ {
SequenceTester<QList<QPromise<void>>>::exec(); SequenceTester<QList<QtPromise::QPromise<void>>>::exec();
// SequenceTester<QVector<QPromise<void>>>::exec(); // SequenceTester<QVector<QtPromise::QPromise<void>>>::exec();
SequenceTester<std::list<QPromise<void>>>::exec(); SequenceTester<std::list<QtPromise::QPromise<void>>>::exec();
SequenceTester<std::vector<QPromise<void>>>::exec(); SequenceTester<std::vector<QtPromise::QPromise<void>>>::exec();
} }

View File

@ -10,8 +10,6 @@
#include <QtPromise> #include <QtPromise>
#include <QtTest> #include <QtTest>
using namespace QtPromise;
class tst_deprecations_qpromise_all : public QObject class tst_deprecations_qpromise_all : public QObject
{ {
Q_OBJECT Q_OBJECT
@ -36,7 +34,7 @@ namespace {
template<class Sequence> template<class Sequence>
struct SequenceTester struct SequenceTester
{ {
Q_STATIC_ASSERT((std::is_same<typename Sequence::value_type, QPromise<int>>::value)); Q_STATIC_ASSERT((std::is_same<typename Sequence::value_type, QtPromise::QPromise<int>>::value));
static void exec() static void exec()
{ {
@ -46,9 +44,9 @@ struct SequenceTester
promises.insert(++promises.begin(), QtPromise::resolve(46)); promises.insert(++promises.begin(), QtPromise::resolve(46));
promises.pop_back(); promises.pop_back();
auto p = QPromise<int>::all(promises); auto p = QtPromise::QPromise<int>::all(promises);
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<QVector<int>>>::value));
QCOMPARE(p.isPending(), true); QCOMPARE(p.isPending(), true);
QCOMPARE(waitForValue(p, QVector<int>{}), (QVector<int>{42, 46, 43, 44})); QCOMPARE(waitForValue(p, QVector<int>{}), (QVector<int>{42, 46, 43, 44}));
@ -56,11 +54,11 @@ struct SequenceTester
}; };
template<template<typename, typename...> class Sequence, typename... Args> template<template<typename, typename...> class Sequence, typename... Args>
struct SequenceTester<Sequence<QPromise<void>, Args...>> struct SequenceTester<Sequence<QtPromise::QPromise<void>, Args...>>
{ {
static void exec() static void exec()
{ {
Sequence<QPromise<void>, Args...> promises{QtPromise::resolve(), Sequence<QtPromise::QPromise<void>, Args...> promises{QtPromise::resolve(),
QtPromise::resolve(), QtPromise::resolve(),
QtPromise::resolve()}; QtPromise::resolve()};
@ -68,9 +66,9 @@ struct SequenceTester<Sequence<QPromise<void>, Args...>>
promises.insert(++promises.begin(), QtPromise::resolve()); promises.insert(++promises.begin(), QtPromise::resolve());
promises.pop_back(); promises.pop_back();
auto p = QPromise<void>::all(promises); auto p = QtPromise::QPromise<void>::all(promises);
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<void>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<void>>::value));
QCOMPARE(p.isPending(), true); QCOMPARE(p.isPending(), true);
QCOMPARE(waitForValue(p, -1, 42), 42); QCOMPARE(waitForValue(p, -1, 42), 42);
@ -81,9 +79,9 @@ struct SequenceTester<Sequence<QPromise<void>, Args...>>
void tst_deprecations_qpromise_all::emptySequence() void tst_deprecations_qpromise_all::emptySequence()
{ {
auto p = QPromise<int>::all(QVector<QPromise<int>>{}); auto p = QtPromise::QPromise<int>::all(QVector<QtPromise::QPromise<int>>{});
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<QVector<int>>>::value));
QCOMPARE(p.isFulfilled(), true); QCOMPARE(p.isFulfilled(), true);
QCOMPARE(waitForValue(p, QVector<int>{}), QVector<int>{}); QCOMPARE(waitForValue(p, QVector<int>{}), QVector<int>{});
@ -91,9 +89,9 @@ void tst_deprecations_qpromise_all::emptySequence()
void tst_deprecations_qpromise_all::emptySequence_void() void tst_deprecations_qpromise_all::emptySequence_void()
{ {
auto p = QPromise<void>::all(QVector<QPromise<void>>{}); auto p = QtPromise::QPromise<void>::all(QVector<QtPromise::QPromise<void>>{});
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<void>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<void>>::value));
QCOMPARE(p.isFulfilled(), true); QCOMPARE(p.isFulfilled(), true);
QCOMPARE(waitForValue(p, -1, 42), 42); QCOMPARE(waitForValue(p, -1, 42), 42);
@ -103,15 +101,15 @@ void tst_deprecations_qpromise_all::allPromisesSucceed()
{ {
auto p0 = QtPromise::resolve(42); auto p0 = QtPromise::resolve(42);
auto p1 = QtPromise::resolve(44); auto p1 = QtPromise::resolve(44);
auto p2 = QPromise<int>{[](const QPromiseResolve<int>& resolve) { auto p2 = QtPromise::QPromise<int>{[](const QtPromise::QPromiseResolve<int>& resolve) {
QtPromisePrivate::qtpromise_defer([=]() { QtPromisePrivate::qtpromise_defer([=]() {
resolve(43); resolve(43);
}); });
}}; }};
auto p = QPromise<int>::all(QVector<QPromise<int>>{p0, p2, p1}); auto p = QtPromise::QPromise<int>::all(QVector<QtPromise::QPromise<int>>{p0, p2, p1});
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<QVector<int>>>::value));
QCOMPARE(p0.isFulfilled(), true); QCOMPARE(p0.isFulfilled(), true);
QCOMPARE(p1.isFulfilled(), true); QCOMPARE(p1.isFulfilled(), true);
@ -125,15 +123,15 @@ void tst_deprecations_qpromise_all::allPromisesSucceed_void()
{ {
auto p0 = QtPromise::resolve(); auto p0 = QtPromise::resolve();
auto p1 = QtPromise::resolve(); auto p1 = QtPromise::resolve();
auto p2 = QPromise<void>{[](const QPromiseResolve<void>& resolve) { auto p2 = QtPromise::QPromise<void>{[](const QtPromise::QPromiseResolve<void>& resolve) {
QtPromisePrivate::qtpromise_defer([=]() { QtPromisePrivate::qtpromise_defer([=]() {
resolve(); resolve();
}); });
}}; }};
auto p = QPromise<void>::all(QVector<QPromise<void>>{p0, p2, p1}); auto p = QtPromise::QPromise<void>::all(QVector<QtPromise::QPromise<void>>{p0, p2, p1});
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<void>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<void>>::value));
QCOMPARE(p0.isFulfilled(), true); QCOMPARE(p0.isFulfilled(), true);
QCOMPARE(p1.isFulfilled(), true); QCOMPARE(p1.isFulfilled(), true);
@ -147,15 +145,16 @@ void tst_deprecations_qpromise_all::atLeastOnePromiseReject()
{ {
auto p0 = QtPromise::resolve(42); auto p0 = QtPromise::resolve(42);
auto p1 = QtPromise::resolve(44); auto p1 = QtPromise::resolve(44);
auto p2 = QPromise<int>{[](const QPromiseResolve<int>&, const QPromiseReject<int>& reject) { auto p2 = QtPromise::QPromise<int>{
[](const QtPromise::QPromiseResolve<int>&, const QtPromise::QPromiseReject<int>& reject) {
QtPromisePrivate::qtpromise_defer([=]() { QtPromisePrivate::qtpromise_defer([=]() {
reject(QString{"foo"}); reject(QString{"foo"});
}); });
}}; }};
auto p = QPromise<int>::all(QVector<QPromise<int>>{p0, p2, p1}); auto p = QtPromise::QPromise<int>::all(QVector<QtPromise::QPromise<int>>{p0, p2, p1});
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<QVector<int>>>::value));
QCOMPARE(p0.isFulfilled(), true); QCOMPARE(p0.isFulfilled(), true);
QCOMPARE(p1.isFulfilled(), true); QCOMPARE(p1.isFulfilled(), true);
@ -169,15 +168,16 @@ void tst_deprecations_qpromise_all::atLeastOnePromiseReject_void()
{ {
auto p0 = QtPromise::resolve(); auto p0 = QtPromise::resolve();
auto p1 = QtPromise::resolve(); auto p1 = QtPromise::resolve();
auto p2 = QPromise<void>{[](const QPromiseResolve<void>&, const QPromiseReject<void>& reject) { auto p2 = QtPromise::QPromise<void>{
[](const QtPromise::QPromiseResolve<void>&, const QtPromise::QPromiseReject<void>& reject) {
QtPromisePrivate::qtpromise_defer([=]() { QtPromisePrivate::qtpromise_defer([=]() {
reject(QString{"foo"}); reject(QString{"foo"});
}); });
}}; }};
auto p = QPromise<void>::all(QVector<QPromise<void>>{p0, p2, p1}); auto p = QtPromise::QPromise<void>::all(QVector<QtPromise::QPromise<void>>{p0, p2, p1});
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<void>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<void>>::value));
QCOMPARE(p0.isFulfilled(), true); QCOMPARE(p0.isFulfilled(), true);
QCOMPARE(p1.isFulfilled(), true); QCOMPARE(p1.isFulfilled(), true);
@ -193,9 +193,9 @@ void tst_deprecations_qpromise_all::preserveOrder()
auto p1 = QtPromise::resolve(43).delay(100); auto p1 = QtPromise::resolve(43).delay(100);
auto p2 = QtPromise::resolve(44).delay(250); auto p2 = QtPromise::resolve(44).delay(250);
auto p = QPromise<int>::all(QVector<QPromise<int>>{p0, p1, p2}); auto p = QtPromise::QPromise<int>::all(QVector<QtPromise::QPromise<int>>{p0, p1, p2});
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<QVector<int>>>::value));
QCOMPARE(p0.isPending(), true); QCOMPARE(p0.isPending(), true);
QCOMPARE(p1.isPending(), true); QCOMPARE(p1.isPending(), true);
@ -212,16 +212,16 @@ void tst_deprecations_qpromise_all::preserveOrder()
void tst_deprecations_qpromise_all::sequenceTypes() void tst_deprecations_qpromise_all::sequenceTypes()
{ {
SequenceTester<QList<QPromise<int>>>::exec(); SequenceTester<QList<QtPromise::QPromise<int>>>::exec();
// SequenceTester<QVector<QPromise<int>>>::exec(); // SequenceTester<QVector<QtPromise::QPromise<int>>>::exec();
SequenceTester<std::list<QPromise<int>>>::exec(); SequenceTester<std::list<QtPromise::QPromise<int>>>::exec();
SequenceTester<std::vector<QPromise<int>>>::exec(); SequenceTester<std::vector<QtPromise::QPromise<int>>>::exec();
} }
void tst_deprecations_qpromise_all::sequenceTypes_void() void tst_deprecations_qpromise_all::sequenceTypes_void()
{ {
SequenceTester<QList<QPromise<void>>>::exec(); SequenceTester<QList<QtPromise::QPromise<void>>>::exec();
// SequenceTester<QVector<QPromise<void>>>::exec(); // SequenceTester<QVector<QtPromise::QPromise<void>>>::exec();
SequenceTester<std::list<QPromise<void>>>::exec(); SequenceTester<std::list<QtPromise::QPromise<void>>>::exec();
SequenceTester<std::vector<QPromise<void>>>::exec(); SequenceTester<std::vector<QtPromise::QPromise<void>>>::exec();
} }

View File

@ -11,8 +11,6 @@
#include <QtPromise> #include <QtPromise>
#include <QtTest> #include <QtTest>
using namespace QtPromise;
class tst_exceptions : public QObject class tst_exceptions : public QObject
{ {
Q_OBJECT Q_OBJECT
@ -46,25 +44,25 @@ void verify()
void tst_exceptions::canceled() void tst_exceptions::canceled()
{ {
verify<QPromiseCanceledException>(); verify<QtPromise::QPromiseCanceledException>();
} }
void tst_exceptions::context() void tst_exceptions::context()
{ {
verify<QPromiseContextException>(); verify<QtPromise::QPromiseContextException>();
} }
void tst_exceptions::conversion() void tst_exceptions::conversion()
{ {
verify<QPromiseConversionException>(); verify<QtPromise::QPromiseConversionException>();
} }
void tst_exceptions::timeout() void tst_exceptions::timeout()
{ {
verify<QPromiseTimeoutException>(); verify<QtPromise::QPromiseTimeoutException>();
} }
void tst_exceptions::undefined() void tst_exceptions::undefined()
{ {
verify<QPromiseUndefinedException>(); verify<QtPromise::QPromiseUndefinedException>();
} }

View File

@ -9,8 +9,6 @@
#include <QtPromise> #include <QtPromise>
#include <QtTest> #include <QtTest>
using namespace QtPromise;
class tst_future : public QObject class tst_future : public QObject
{ {
Q_OBJECT Q_OBJECT
@ -58,7 +56,7 @@ void tst_future::fulfilled()
return 42; return 42;
})); }));
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<int>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<int>>::value));
QCOMPARE(p.isPending(), true); QCOMPARE(p.isPending(), true);
p.then([&](int res) { p.then([&](int res) {
@ -74,7 +72,7 @@ void tst_future::fulfilled_void()
int result = -1; int result = -1;
auto p = QtPromise::resolve(QtConcurrent::run([]() {})); auto p = QtPromise::resolve(QtConcurrent::run([]() {}));
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<void>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<void>>::value));
QCOMPARE(p.isPending(), true); QCOMPARE(p.isPending(), true);
p.then([&]() { p.then([&]() {
@ -93,7 +91,7 @@ void tst_future::rejected()
return 42; return 42;
})); }));
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<int>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<int>>::value));
QCOMPARE(p.isPending(), true); QCOMPARE(p.isPending(), true);
p.fail([&](const MyException& e) { p.fail([&](const MyException& e) {
@ -112,7 +110,7 @@ void tst_future::rejected_void()
throw MyException{"foo"}; throw MyException{"foo"};
})); }));
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<void>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<void>>::value));
QCOMPARE(p.isPending(), true); QCOMPARE(p.isPending(), true);
@ -132,7 +130,7 @@ void tst_future::unhandled()
return 42; return 42;
})); }));
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<int>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<int>>::value));
QCOMPARE(p.isPending(), true); QCOMPARE(p.isPending(), true);
@ -157,7 +155,7 @@ void tst_future::unhandled_void()
throw QString{"foo"}; throw QString{"foo"};
})); }));
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<void>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<void>>::value));
QCOMPARE(p.isPending(), true); QCOMPARE(p.isPending(), true);
p.fail([&](const QString& err) { p.fail([&](const QString& err) {
@ -179,7 +177,7 @@ void tst_future::canceled()
QCOMPARE(p.isPending(), true); QCOMPARE(p.isPending(), true);
p.fail([&](const QPromiseCanceledException&) { p.fail([&](const QtPromise::QPromiseCanceledException&) {
error = "canceled"; error = "canceled";
return -1; return -1;
}).wait(); }).wait();
@ -195,7 +193,7 @@ void tst_future::canceled_void()
QCOMPARE(p.isPending(), true); QCOMPARE(p.isPending(), true);
p.fail([&](const QPromiseCanceledException&) { p.fail([&](const QtPromise::QPromiseCanceledException&) {
error = "canceled"; error = "canceled";
}).wait(); }).wait();
@ -207,12 +205,12 @@ void tst_future::canceledFromThread()
{ {
QString error; QString error;
auto p = QtPromise::resolve(QtConcurrent::run([]() { auto p = QtPromise::resolve(QtConcurrent::run([]() {
throw QPromiseCanceledException{}; throw QtPromise::QPromiseCanceledException{};
})); }));
QCOMPARE(p.isPending(), true); QCOMPARE(p.isPending(), true);
p.fail([&](const QPromiseCanceledException&) { p.fail([&](const QtPromise::QPromiseCanceledException&) {
error = "bar"; error = "bar";
}).wait(); }).wait();
@ -269,7 +267,7 @@ void tst_future::then_void()
void tst_future::fail() void tst_future::fail()
{ {
QString result; QString result;
auto input = QPromise<QString>::reject(MyException{"bar"}); auto input = QtPromise::QPromise<QString>::reject(MyException{"bar"});
auto output = input.fail([](const MyException& e) { auto output = input.fail([](const MyException& e) {
return QtConcurrent::run( return QtConcurrent::run(
[](const QString& error) { [](const QString& error) {
@ -294,7 +292,7 @@ void tst_future::fail()
void tst_future::fail_void() void tst_future::fail_void()
{ {
QString result; QString result;
auto input = QPromise<void>::reject(MyException{"bar"}); auto input = QtPromise::QPromise<void>::reject(MyException{"bar"});
auto output = input.fail([&](const MyException& e) { auto output = input.fail([&](const MyException& e) {
return QtConcurrent::run( return QtConcurrent::run(
[&](const QString& error) { [&](const QString& error) {
@ -325,7 +323,7 @@ void tst_future::finally()
}); });
}); });
Q_STATIC_ASSERT((std::is_same<decltype(output), QPromise<int>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(output), QtPromise::QPromise<int>>::value));
QCOMPARE(input.isFulfilled(), true); QCOMPARE(input.isFulfilled(), true);
QCOMPARE(output.isPending(), true); QCOMPARE(output.isPending(), true);
@ -350,7 +348,7 @@ void tst_future::finallyRejected()
}); });
}); });
Q_STATIC_ASSERT((std::is_same<decltype(output), QPromise<int>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(output), QtPromise::QPromise<int>>::value));
QCOMPARE(input.isFulfilled(), true); QCOMPARE(input.isFulfilled(), true);
QCOMPARE(output.isPending(), true); QCOMPARE(output.isPending(), true);

View File

@ -10,8 +10,6 @@
#include <QtPromise> #include <QtPromise>
#include <QtTest> #include <QtTest>
using namespace QtPromise;
class tst_helpers_all : public QObject class tst_helpers_all : public QObject
{ {
Q_OBJECT Q_OBJECT
@ -36,7 +34,7 @@ namespace {
template<class Sequence> template<class Sequence>
struct SequenceTester struct SequenceTester
{ {
Q_STATIC_ASSERT((std::is_same<typename Sequence::value_type, QPromise<int>>::value)); Q_STATIC_ASSERT((std::is_same<typename Sequence::value_type, QtPromise::QPromise<int>>::value));
static void exec() static void exec()
{ {
@ -48,18 +46,18 @@ struct SequenceTester
auto p = QtPromise::all(promises); auto p = QtPromise::all(promises);
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<QVector<int>>>::value));
QCOMPARE(p.isPending(), true); QCOMPARE(p.isPending(), true);
QCOMPARE(waitForValue(p, QVector<int>{}), (QVector<int>{42, 46, 43, 44})); QCOMPARE(waitForValue(p, QVector<int>{}), (QVector<int>{42, 46, 43, 44}));
} }
}; };
template<template<typename, typename...> class Sequence, typename... Args> template<template<typename, typename...> class Sequence, typename... Args>
struct SequenceTester<Sequence<QPromise<void>, Args...>> struct SequenceTester<Sequence<QtPromise::QPromise<void>, Args...>>
{ {
static void exec() static void exec()
{ {
Sequence<QPromise<void>, Args...> promises{QtPromise::resolve(), Sequence<QtPromise::QPromise<void>, Args...> promises{QtPromise::resolve(),
QtPromise::resolve(), QtPromise::resolve(),
QtPromise::resolve()}; QtPromise::resolve()};
@ -69,7 +67,7 @@ struct SequenceTester<Sequence<QPromise<void>, Args...>>
auto p = QtPromise::all(promises); auto p = QtPromise::all(promises);
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<void>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<void>>::value));
QCOMPARE(p.isPending(), true); QCOMPARE(p.isPending(), true);
QCOMPARE(waitForValue(p, -1, 42), 42); QCOMPARE(waitForValue(p, -1, 42), 42);
} }
@ -79,18 +77,18 @@ struct SequenceTester<Sequence<QPromise<void>, Args...>>
void tst_helpers_all::emptySequence() void tst_helpers_all::emptySequence()
{ {
auto p = QtPromise::all(QVector<QPromise<int>>()); auto p = QtPromise::all(QVector<QtPromise::QPromise<int>>());
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<QVector<int>>>::value));
QCOMPARE(p.isFulfilled(), true); QCOMPARE(p.isFulfilled(), true);
QCOMPARE(waitForValue(p, QVector<int>{}), QVector<int>{}); QCOMPARE(waitForValue(p, QVector<int>{}), QVector<int>{});
} }
void tst_helpers_all::emptySequence_void() void tst_helpers_all::emptySequence_void()
{ {
auto p = QtPromise::all(QVector<QPromise<void>>()); auto p = QtPromise::all(QVector<QtPromise::QPromise<void>>());
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<void>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<void>>::value));
QCOMPARE(p.isFulfilled(), true); QCOMPARE(p.isFulfilled(), true);
QCOMPARE(waitForValue(p, -1, 42), 42); QCOMPARE(waitForValue(p, -1, 42), 42);
} }
@ -99,15 +97,15 @@ void tst_helpers_all::allPromisesSucceed()
{ {
auto p0 = QtPromise::resolve(42); auto p0 = QtPromise::resolve(42);
auto p1 = QtPromise::resolve(44); auto p1 = QtPromise::resolve(44);
auto p2 = QPromise<int>{[](const QPromiseResolve<int>& resolve) { auto p2 = QtPromise::QPromise<int>{[](const QtPromise::QPromiseResolve<int>& resolve) {
QtPromisePrivate::qtpromise_defer([=]() { QtPromisePrivate::qtpromise_defer([=]() {
resolve(43); resolve(43);
}); });
}}; }};
auto p = QtPromise::all(QVector<QPromise<int>>{p0, p2, p1}); auto p = QtPromise::all(QVector<QtPromise::QPromise<int>>{p0, p2, p1});
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<QVector<int>>>::value));
QCOMPARE(p0.isFulfilled(), true); QCOMPARE(p0.isFulfilled(), true);
QCOMPARE(p1.isFulfilled(), true); QCOMPARE(p1.isFulfilled(), true);
QCOMPARE(p2.isPending(), true); QCOMPARE(p2.isPending(), true);
@ -120,15 +118,15 @@ void tst_helpers_all::allPromisesSucceed_void()
{ {
auto p0 = QtPromise::resolve(); auto p0 = QtPromise::resolve();
auto p1 = QtPromise::resolve(); auto p1 = QtPromise::resolve();
auto p2 = QPromise<void>{[](const QPromiseResolve<void>& resolve) { auto p2 = QtPromise::QPromise<void>{[](const QtPromise::QPromiseResolve<void>& resolve) {
QtPromisePrivate::qtpromise_defer([=]() { QtPromisePrivate::qtpromise_defer([=]() {
resolve(); resolve();
}); });
}}; }};
auto p = QtPromise::all(QVector<QPromise<void>>{p0, p2, p1}); auto p = QtPromise::all(QVector<QtPromise::QPromise<void>>{p0, p2, p1});
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<void>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<void>>::value));
QCOMPARE(p0.isFulfilled(), true); QCOMPARE(p0.isFulfilled(), true);
QCOMPARE(p1.isFulfilled(), true); QCOMPARE(p1.isFulfilled(), true);
QCOMPARE(p2.isPending(), true); QCOMPARE(p2.isPending(), true);
@ -141,15 +139,16 @@ void tst_helpers_all::atLeastOnePromiseReject()
{ {
auto p0 = QtPromise::resolve(42); auto p0 = QtPromise::resolve(42);
auto p1 = QtPromise::resolve(44); auto p1 = QtPromise::resolve(44);
auto p2 = QPromise<int>{[](const QPromiseResolve<int>&, const QPromiseReject<int>& reject) { auto p2 = QtPromise::QPromise<int>{
[](const QtPromise::QPromiseResolve<int>&, const QtPromise::QPromiseReject<int>& reject) {
QtPromisePrivate::qtpromise_defer([=]() { QtPromisePrivate::qtpromise_defer([=]() {
reject(QString{"foo"}); reject(QString{"foo"});
}); });
}}; }};
auto p = QtPromise::all(QVector<QPromise<int>>{p0, p2, p1}); auto p = QtPromise::all(QVector<QtPromise::QPromise<int>>{p0, p2, p1});
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<QVector<int>>>::value));
QCOMPARE(p0.isFulfilled(), true); QCOMPARE(p0.isFulfilled(), true);
QCOMPARE(p1.isFulfilled(), true); QCOMPARE(p1.isFulfilled(), true);
QCOMPARE(p2.isPending(), true); QCOMPARE(p2.isPending(), true);
@ -162,15 +161,16 @@ void tst_helpers_all::atLeastOnePromiseReject_void()
{ {
auto p0 = QtPromise::resolve(); auto p0 = QtPromise::resolve();
auto p1 = QtPromise::resolve(); auto p1 = QtPromise::resolve();
auto p2 = QPromise<void>{[](const QPromiseResolve<void>&, const QPromiseReject<void>& reject) { auto p2 = QtPromise::QPromise<void>{
[](const QtPromise::QPromiseResolve<void>&, const QtPromise::QPromiseReject<void>& reject) {
QtPromisePrivate::qtpromise_defer([=]() { QtPromisePrivate::qtpromise_defer([=]() {
reject(QString{"foo"}); reject(QString{"foo"});
}); });
}}; }};
auto p = QtPromise::all(QVector<QPromise<void>>{p0, p2, p1}); auto p = QtPromise::all(QVector<QtPromise::QPromise<void>>{p0, p2, p1});
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<void>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<void>>::value));
QCOMPARE(p0.isFulfilled(), true); QCOMPARE(p0.isFulfilled(), true);
QCOMPARE(p1.isFulfilled(), true); QCOMPARE(p1.isFulfilled(), true);
QCOMPARE(p2.isPending(), true); QCOMPARE(p2.isPending(), true);
@ -185,9 +185,9 @@ void tst_helpers_all::preserveOrder()
auto p1 = QtPromise::resolve(43).delay(100); auto p1 = QtPromise::resolve(43).delay(100);
auto p2 = QtPromise::resolve(44).delay(250); auto p2 = QtPromise::resolve(44).delay(250);
auto p = QtPromise::all(QVector<QPromise<int>>{p0, p1, p2}); auto p = QtPromise::all(QVector<QtPromise::QPromise<int>>{p0, p1, p2});
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<QVector<int>>>::value));
QCOMPARE(p0.isPending(), true); QCOMPARE(p0.isPending(), true);
QCOMPARE(p1.isPending(), true); QCOMPARE(p1.isPending(), true);
QCOMPARE(p2.isPending(), true); QCOMPARE(p2.isPending(), true);
@ -203,16 +203,16 @@ void tst_helpers_all::preserveOrder()
void tst_helpers_all::sequenceTypes() void tst_helpers_all::sequenceTypes()
{ {
SequenceTester<QList<QPromise<int>>>::exec(); SequenceTester<QList<QtPromise::QPromise<int>>>::exec();
// SequenceTester<QVector<QPromise<int>>>::exec(); // SequenceTester<QVector<QPromise<int>>>::exec();
SequenceTester<std::list<QPromise<int>>>::exec(); SequenceTester<std::list<QtPromise::QPromise<int>>>::exec();
SequenceTester<std::vector<QPromise<int>>>::exec(); SequenceTester<std::vector<QtPromise::QPromise<int>>>::exec();
} }
void tst_helpers_all::sequenceTypes_void() void tst_helpers_all::sequenceTypes_void()
{ {
SequenceTester<QList<QPromise<void>>>::exec(); SequenceTester<QList<QtPromise::QPromise<void>>>::exec();
// SequenceTester<QVector<QPromise<void>>>::exec(); // SequenceTester<QVector<QPromise<void>>>::exec();
SequenceTester<std::list<QPromise<void>>>::exec(); SequenceTester<std::list<QtPromise::QPromise<void>>>::exec();
SequenceTester<std::vector<QPromise<void>>>::exec(); SequenceTester<std::vector<QtPromise::QPromise<void>>>::exec();
} }

View File

@ -13,8 +13,6 @@
#include <memory> #include <memory>
using namespace QtPromise;
class tst_helpers_attempt : public QObject class tst_helpers_attempt : public QObject
{ {
Q_OBJECT Q_OBJECT
@ -35,7 +33,7 @@ void tst_helpers_attempt::voidResult()
{ {
auto p = QtPromise::attempt([]() {}); auto p = QtPromise::attempt([]() {});
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<void>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<void>>::value));
QCOMPARE(p.isFulfilled(), true); QCOMPARE(p.isFulfilled(), true);
QCOMPARE(waitForValue(p, -1, 42), 42); QCOMPARE(waitForValue(p, -1, 42), 42);
} }
@ -46,7 +44,7 @@ void tst_helpers_attempt::typedResult()
return QString{"foo"}; return QString{"foo"};
}); });
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QString>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<QString>>::value));
QCOMPARE(p.isFulfilled(), true); QCOMPARE(p.isFulfilled(), true);
QCOMPARE(waitForValue(p, QString{}), QString{"foo"}); QCOMPARE(waitForValue(p, QString{}), QString{"foo"});
} }
@ -59,7 +57,7 @@ void tst_helpers_attempt::futureResult()
}); });
}); });
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QString>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<QString>>::value));
QCOMPARE(p.isPending(), true); QCOMPARE(p.isPending(), true);
QCOMPARE(waitForValue(p, QString{}), QString{"foo"}); QCOMPARE(waitForValue(p, QString{}), QString{"foo"});
} }
@ -70,7 +68,7 @@ void tst_helpers_attempt::promiseResult()
return QtPromise::resolve(42).delay(200); return QtPromise::resolve(42).delay(200);
}); });
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<int>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<int>>::value));
QCOMPARE(p.isPending(), true); QCOMPARE(p.isPending(), true);
QCOMPARE(waitForValue(p, -1), 42); QCOMPARE(waitForValue(p, -1), 42);
} }
@ -84,7 +82,7 @@ void tst_helpers_attempt::functorThrows()
return 42; return 42;
}); });
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<int>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<int>>::value));
QCOMPARE(p.isRejected(), true); QCOMPARE(p.isRejected(), true);
QCOMPARE(waitForError(p, QString{}), QString{"bar"}); QCOMPARE(waitForError(p, QString{}), QString{"bar"});
} }
@ -98,7 +96,7 @@ void tst_helpers_attempt::callWithParams()
42, 42,
"foo"); "foo");
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QString>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<QString>>::value));
QCOMPARE(p.isFulfilled(), true); QCOMPARE(p.isFulfilled(), true);
QCOMPARE(waitForValue(p, QString{}), QString{"42:foo"}); QCOMPARE(waitForValue(p, QString{}), QString{"42:foo"});
} }

View File

@ -11,8 +11,6 @@
#include <QtPromise> #include <QtPromise>
#include <QtTest> #include <QtTest>
using namespace QtPromise;
class tst_helpers_connect : public QObject class tst_helpers_connect : public QObject
{ {
Q_OBJECT Q_OBJECT
@ -47,7 +45,7 @@ void tst_helpers_connect::resolveOneSenderNoArg()
}); });
auto p = QtPromise::connect(&sender, &Object::noArgSignal); auto p = QtPromise::connect(&sender, &Object::noArgSignal);
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<void>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<void>>::value));
QCOMPARE(sender.hasConnections(), true); QCOMPARE(sender.hasConnections(), true);
QCOMPARE(p.isPending(), true); QCOMPARE(p.isPending(), true);
QCOMPARE(waitForValue(p, -1, 42), 42); QCOMPARE(waitForValue(p, -1, 42), 42);
@ -62,7 +60,7 @@ void tst_helpers_connect::resolveOneSenderOneArg()
}); });
auto p = QtPromise::connect(&sender, &Object::oneArgSignal); auto p = QtPromise::connect(&sender, &Object::oneArgSignal);
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QString>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<QString>>::value));
QCOMPARE(sender.hasConnections(), true); QCOMPARE(sender.hasConnections(), true);
QCOMPARE(p.isPending(), true); QCOMPARE(p.isPending(), true);
QCOMPARE(waitForValue(p, QString{}), QString{"foo"}); QCOMPARE(waitForValue(p, QString{}), QString{"foo"});
@ -77,7 +75,7 @@ void tst_helpers_connect::resolveOneSenderManyArgs()
}); });
auto p = QtPromise::connect(&sender, &Object::twoArgsSignal); auto p = QtPromise::connect(&sender, &Object::twoArgsSignal);
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<int>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<int>>::value));
QCOMPARE(sender.hasConnections(), true); QCOMPARE(sender.hasConnections(), true);
QCOMPARE(p.isPending(), true); QCOMPARE(p.isPending(), true);
QCOMPARE(waitForValue(p, -1), 42); QCOMPARE(waitForValue(p, -1), 42);
@ -92,10 +90,10 @@ void tst_helpers_connect::rejectOneSenderNoArg()
}); });
auto p = QtPromise::connect(&sender, &Object::oneArgSignal, &Object::noArgSignal); auto p = QtPromise::connect(&sender, &Object::oneArgSignal, &Object::noArgSignal);
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QString>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<QString>>::value));
QCOMPARE(sender.hasConnections(), true); QCOMPARE(sender.hasConnections(), true);
QCOMPARE(p.isPending(), true); QCOMPARE(p.isPending(), true);
QCOMPARE(waitForRejected<QPromiseUndefinedException>(p), true); QCOMPARE(waitForRejected<QtPromise::QPromiseUndefinedException>(p), true);
QCOMPARE(sender.hasConnections(), false); QCOMPARE(sender.hasConnections(), false);
} }
@ -107,7 +105,7 @@ void tst_helpers_connect::rejectOneSenderOneArg()
}); });
auto p = QtPromise::connect(&sender, &Object::noArgSignal, &Object::oneArgSignal); auto p = QtPromise::connect(&sender, &Object::noArgSignal, &Object::oneArgSignal);
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<void>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<void>>::value));
QCOMPARE(sender.hasConnections(), true); QCOMPARE(sender.hasConnections(), true);
QCOMPARE(p.isPending(), true); QCOMPARE(p.isPending(), true);
QCOMPARE(waitForError(p, QString{}), QString{"bar"}); QCOMPARE(waitForError(p, QString{}), QString{"bar"});
@ -122,7 +120,7 @@ void tst_helpers_connect::rejectOneSenderManyArgs()
}); });
auto p = QtPromise::connect(&sender, &Object::noArgSignal, &Object::twoArgsSignal); auto p = QtPromise::connect(&sender, &Object::noArgSignal, &Object::twoArgsSignal);
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<void>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<void>>::value));
QCOMPARE(sender.hasConnections(), true); QCOMPARE(sender.hasConnections(), true);
QCOMPARE(p.isPending(), true); QCOMPARE(p.isPending(), true);
QCOMPARE(waitForError(p, -1), 42); QCOMPARE(waitForError(p, -1), 42);
@ -137,9 +135,9 @@ void tst_helpers_connect::rejectOneSenderDestroyed()
}); });
auto p = QtPromise::connect(sender, &Object::twoArgsSignal); auto p = QtPromise::connect(sender, &Object::twoArgsSignal);
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<int>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<int>>::value));
QCOMPARE(p.isPending(), true); QCOMPARE(p.isPending(), true);
QCOMPARE(waitForRejected<QPromiseContextException>(p), true); QCOMPARE(waitForRejected<QtPromise::QPromiseContextException>(p), true);
} }
void tst_helpers_connect::rejectTwoSendersNoArg() void tst_helpers_connect::rejectTwoSendersNoArg()
@ -150,11 +148,11 @@ void tst_helpers_connect::rejectTwoSendersNoArg()
}); });
auto p = QtPromise::connect(&s0, &Object::noArgSignal, &s1, &Object::noArgSignal); auto p = QtPromise::connect(&s0, &Object::noArgSignal, &s1, &Object::noArgSignal);
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<void>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<void>>::value));
QCOMPARE(s0.hasConnections(), true); QCOMPARE(s0.hasConnections(), true);
QCOMPARE(s1.hasConnections(), true); QCOMPARE(s1.hasConnections(), true);
QCOMPARE(p.isPending(), true); QCOMPARE(p.isPending(), true);
QCOMPARE(waitForRejected<QPromiseUndefinedException>(p), true); QCOMPARE(waitForRejected<QtPromise::QPromiseUndefinedException>(p), true);
QCOMPARE(s0.hasConnections(), false); QCOMPARE(s0.hasConnections(), false);
QCOMPARE(s1.hasConnections(), false); QCOMPARE(s1.hasConnections(), false);
} }
@ -167,7 +165,7 @@ void tst_helpers_connect::rejectTwoSendersOneArg()
}); });
auto p = QtPromise::connect(&s0, &Object::noArgSignal, &s1, &Object::oneArgSignal); auto p = QtPromise::connect(&s0, &Object::noArgSignal, &s1, &Object::oneArgSignal);
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<void>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<void>>::value));
QCOMPARE(s0.hasConnections(), true); QCOMPARE(s0.hasConnections(), true);
QCOMPARE(s1.hasConnections(), true); QCOMPARE(s1.hasConnections(), true);
QCOMPARE(p.isPending(), true); QCOMPARE(p.isPending(), true);
@ -184,7 +182,7 @@ void tst_helpers_connect::rejectTwoSendersManyArgs()
}); });
auto p = QtPromise::connect(&s0, &Object::noArgSignal, &s1, &Object::twoArgsSignal); auto p = QtPromise::connect(&s0, &Object::noArgSignal, &s1, &Object::twoArgsSignal);
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<void>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<void>>::value));
QCOMPARE(s0.hasConnections(), true); QCOMPARE(s0.hasConnections(), true);
QCOMPARE(s1.hasConnections(), true); QCOMPARE(s1.hasConnections(), true);
QCOMPARE(p.isPending(), true); QCOMPARE(p.isPending(), true);
@ -209,7 +207,7 @@ void tst_helpers_connect::rejectTwoSendersDestroyed()
}); });
auto p = QtPromise::connect(s0, &Object::noArgSignal, s1, &Object::twoArgsSignal); auto p = QtPromise::connect(s0, &Object::noArgSignal, s1, &Object::twoArgsSignal);
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<void>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<void>>::value));
QCOMPARE(p.isPending(), true); QCOMPARE(p.isPending(), true);
QCOMPARE(waitForValue(p, -1, 42), 42); QCOMPARE(waitForValue(p, -1, 42), 42);
} }

View File

@ -10,8 +10,6 @@
#include <QtPromise> #include <QtPromise>
#include <QtTest> #include <QtTest>
using namespace QtPromise;
class tst_helpers_each : public QObject class tst_helpers_each : public QObject
{ {
Q_OBJECT Q_OBJECT
@ -42,7 +40,7 @@ struct SequenceTester
values << i << v; values << i << v;
}); });
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<Sequence>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<Sequence>>::value));
QCOMPARE(waitForValue(p, Sequence{}), (Sequence{42, 43, 44})); QCOMPARE(waitForValue(p, Sequence{}), (Sequence{42, 43, 44}));
QCOMPARE(values, (QVector<int>{0, 42, 1, 43, 2, 44})); QCOMPARE(values, (QVector<int>{0, 42, 1, 43, 2, 44}));
} }
@ -57,7 +55,7 @@ void tst_helpers_each::emptySequence()
values << v; values << v;
}); });
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<QVector<int>>>::value));
QCOMPARE(waitForValue(p, QVector<int>{}), QVector<int>{}); QCOMPARE(waitForValue(p, QVector<int>{}), QVector<int>{});
QCOMPARE(values, QVector<int>{}); QCOMPARE(values, QVector<int>{});
} }
@ -69,7 +67,7 @@ void tst_helpers_each::preserveValues()
values << v + 1; values << v + 1;
}); });
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<QVector<int>>>::value));
QCOMPARE(waitForValue(p, QVector<int>{}), (QVector<int>{42, 43, 44})); QCOMPARE(waitForValue(p, QVector<int>{}), (QVector<int>{42, 43, 44}));
QCOMPARE(values, (QVector<int>{43, 44, 45})); QCOMPARE(values, (QVector<int>{43, 44, 45}));
} }
@ -82,7 +80,7 @@ void tst_helpers_each::ignoreResult()
return "Foo"; return "Foo";
}); });
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<QVector<int>>>::value));
QCOMPARE(waitForValue(p, QVector<int>{}), (QVector<int>{42, 43, 44})); QCOMPARE(waitForValue(p, QVector<int>{}), (QVector<int>{42, 43, 44}));
QCOMPARE(values, (QVector<int>{43, 44, 45})); QCOMPARE(values, (QVector<int>{43, 44, 45}));
} }
@ -91,7 +89,7 @@ void tst_helpers_each::delayedFulfilled()
{ {
QMap<int, int> values; QMap<int, int> values;
auto p = QtPromise::each(QVector<int>{42, 43, 44}, [&](int v, int index) { auto p = QtPromise::each(QVector<int>{42, 43, 44}, [&](int v, int index) {
return QPromise<int>{[&](const QPromiseResolve<int>& resolve) { return QtPromise::QPromise<int>{[&](const QtPromise::QPromiseResolve<int>& resolve) {
QtPromisePrivate::qtpromise_defer([=, &values]() { QtPromisePrivate::qtpromise_defer([=, &values]() {
values[v] = index; values[v] = index;
resolve(42); resolve(42);
@ -99,7 +97,7 @@ void tst_helpers_each::delayedFulfilled()
}}; }};
}); });
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<QVector<int>>>::value));
QCOMPARE(waitForValue(p, QVector<int>{}), (QVector<int>{42, 43, 44})); QCOMPARE(waitForValue(p, QVector<int>{}), (QVector<int>{42, 43, 44}));
QMap<int, int> expected{{42, 0}, {43, 1}, {44, 2}}; QMap<int, int> expected{{42, 0}, {43, 1}, {44, 2}};
QCOMPARE(values, expected); QCOMPARE(values, expected);
@ -108,8 +106,8 @@ void tst_helpers_each::delayedFulfilled()
void tst_helpers_each::delayedRejected() void tst_helpers_each::delayedRejected()
{ {
auto p = QtPromise::each(QVector<int>{42, 43, 44}, [](int v, ...) { auto p = QtPromise::each(QVector<int>{42, 43, 44}, [](int v, ...) {
return QPromise<int>{ return QtPromise::QPromise<int>{[&](const QtPromise::QPromiseResolve<int>& resolve,
[&](const QPromiseResolve<int>& resolve, const QPromiseReject<int>& reject) { const QtPromise::QPromiseReject<int>& reject) {
QtPromisePrivate::qtpromise_defer([=]() { QtPromisePrivate::qtpromise_defer([=]() {
if (v == 43) { if (v == 43) {
reject(QString{"foo"}); reject(QString{"foo"});
@ -119,7 +117,7 @@ void tst_helpers_each::delayedRejected()
}}; }};
}); });
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<QVector<int>>>::value));
QCOMPARE(waitForError(p, QString{}), QString{"foo"}); QCOMPARE(waitForError(p, QString{}), QString{"foo"});
} }
@ -131,7 +129,7 @@ void tst_helpers_each::functorThrows()
} }
}); });
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<QVector<int>>>::value));
QCOMPARE(waitForError(p, QString{}), QString{"foo"}); QCOMPARE(waitForError(p, QString{}), QString{"foo"});
} }
@ -142,7 +140,7 @@ void tst_helpers_each::functorArguments()
values << i << v; values << i << v;
}); });
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<QVector<int>>>::value));
QCOMPARE(waitForValue(p, QVector<int>{}), (QVector<int>{42, 43, 44})); QCOMPARE(waitForValue(p, QVector<int>{}), (QVector<int>{42, 43, 44}));
QCOMPARE(values, (QVector<int>{0, 42, 1, 43, 2, 44})); QCOMPARE(values, (QVector<int>{0, 42, 1, 43, 2, 44}));
} }

View File

@ -11,8 +11,6 @@
#include <QtPromise> #include <QtPromise>
#include <QtTest> #include <QtTest>
using namespace QtPromise;
class tst_helpers_filter : public QObject class tst_helpers_filter : public QObject
{ {
Q_OBJECT Q_OBJECT
@ -43,7 +41,7 @@ struct SequenceTester
return v % 3 == 0; return v % 3 == 0;
}); });
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<Sequence>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<Sequence>>::value));
QCOMPARE(waitForValue(p, Sequence{}), (Sequence{42, 45, 48, 51})); QCOMPARE(waitForValue(p, Sequence{}), (Sequence{42, 45, 48, 51}));
} }
}; };
@ -56,7 +54,7 @@ void tst_helpers_filter::emptySequence()
return v % 2 == 0; return v % 2 == 0;
}); });
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<QVector<int>>>::value));
QCOMPARE(waitForValue(p, QVector<int>{}), QVector<int>{}); QCOMPARE(waitForValue(p, QVector<int>{}), QVector<int>{});
} }
@ -66,29 +64,29 @@ void tst_helpers_filter::filterValues()
return v % 2 == 0; return v % 2 == 0;
}); });
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<QVector<int>>>::value));
QCOMPARE(waitForValue(p, QVector<int>{}), (QVector<int>{42, 44})); QCOMPARE(waitForValue(p, QVector<int>{}), (QVector<int>{42, 44}));
} }
void tst_helpers_filter::delayedFulfilled() void tst_helpers_filter::delayedFulfilled()
{ {
auto p = QtPromise::filter(QVector<int>{42, 43, 44}, [](int v, ...) { auto p = QtPromise::filter(QVector<int>{42, 43, 44}, [](int v, ...) {
return QPromise<bool>{[&](const QPromiseResolve<bool>& resolve) { return QtPromise::QPromise<bool>{[&](const QtPromise::QPromiseResolve<bool>& resolve) {
QtPromisePrivate::qtpromise_defer([=]() { QtPromisePrivate::qtpromise_defer([=]() {
resolve(v % 2 == 0); resolve(v % 2 == 0);
}); });
}}; }};
}); });
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<QVector<int>>>::value));
QCOMPARE(waitForValue(p, QVector<int>{}), (QVector<int>{42, 44})); QCOMPARE(waitForValue(p, QVector<int>{}), (QVector<int>{42, 44}));
} }
void tst_helpers_filter::delayedRejected() void tst_helpers_filter::delayedRejected()
{ {
auto p = QtPromise::filter(QVector<int>{42, 43, 44}, [](int v, ...) { auto p = QtPromise::filter(QVector<int>{42, 43, 44}, [](int v, ...) {
return QPromise<bool>{ return QtPromise::QPromise<bool>{[&](const QtPromise::QPromiseResolve<bool>& resolve,
[&](const QPromiseResolve<bool>& resolve, const QPromiseReject<bool>& reject) { const QtPromise::QPromiseReject<bool>& reject) {
QtPromisePrivate::qtpromise_defer([=]() { QtPromisePrivate::qtpromise_defer([=]() {
if (v == 44) { if (v == 44) {
reject(QString{"foo"}); reject(QString{"foo"});
@ -98,7 +96,7 @@ void tst_helpers_filter::delayedRejected()
}}; }};
}); });
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<QVector<int>>>::value));
QCOMPARE(waitForError(p, QString{}), QString{"foo"}); QCOMPARE(waitForError(p, QString{}), QString{"foo"});
} }
@ -111,7 +109,7 @@ void tst_helpers_filter::functorThrows()
return true; return true;
}); });
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<QVector<int>>>::value));
QCOMPARE(waitForError(p, QString{}), QString{"foo"}); QCOMPARE(waitForError(p, QString{}), QString{"foo"});
} }
@ -123,7 +121,7 @@ void tst_helpers_filter::functorArguments()
return i % 2 == 0; return i % 2 == 0;
}); });
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<QVector<int>>>::value));
QCOMPARE(waitForValue(p, QVector<int>{}), (QVector<int>{42, 44})); QCOMPARE(waitForValue(p, QVector<int>{}), (QVector<int>{42, 44}));
QMap<int, int> expected{{42, 0}, {43, 1}, {44, 2}}; QMap<int, int> expected{{42, 0}, {43, 1}, {44, 2}};
QCOMPARE(args, expected); QCOMPARE(args, expected);
@ -132,10 +130,10 @@ void tst_helpers_filter::functorArguments()
void tst_helpers_filter::preserveOrder() void tst_helpers_filter::preserveOrder()
{ {
auto p = QtPromise::filter(QVector<int>{500, 100, 300, 250, 400}, [](int v, ...) { auto p = QtPromise::filter(QVector<int>{500, 100, 300, 250, 400}, [](int v, ...) {
return QPromise<bool>::resolve(v > 200).delay(v); return QtPromise::QPromise<bool>::resolve(v > 200).delay(v);
}); });
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<QVector<int>>>::value));
QCOMPARE(waitForValue(p, QVector<int>{}), (QVector<int>{500, 300, 250, 400})); QCOMPARE(waitForValue(p, QVector<int>{}), (QVector<int>{500, 300, 250, 400}));
} }

View File

@ -10,8 +10,6 @@
#include <QtPromise> #include <QtPromise>
#include <QtTest> #include <QtTest>
using namespace QtPromise;
class tst_helpers_map : public QObject class tst_helpers_map : public QObject
{ {
Q_OBJECT Q_OBJECT
@ -42,7 +40,7 @@ struct SequenceTester
return QString::number(v + 1); return QString::number(v + 1);
}); });
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<QString>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<QVector<QString>>>::value));
QCOMPARE(waitForValue(p, QVector<QString>{}), (QVector<QString>{"43", "44", "45"})); QCOMPARE(waitForValue(p, QVector<QString>{}), (QVector<QString>{"43", "44", "45"}));
} }
}; };
@ -55,7 +53,7 @@ void tst_helpers_map::emptySequence()
return v + 1; return v + 1;
}); });
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<QVector<int>>>::value));
QCOMPARE(waitForValue(p, QVector<int>{}), (QVector<int>{})); QCOMPARE(waitForValue(p, QVector<int>{}), (QVector<int>{}));
} }
@ -65,7 +63,7 @@ void tst_helpers_map::modifyValues()
return v + 1; return v + 1;
}); });
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<QVector<int>>>::value));
QCOMPARE(waitForValue(p, QVector<int>{}), (QVector<int>{43, 44, 45})); QCOMPARE(waitForValue(p, QVector<int>{}), (QVector<int>{43, 44, 45}));
} }
@ -75,29 +73,29 @@ void tst_helpers_map::convertValues()
return QString::number(v + 1); return QString::number(v + 1);
}); });
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<QString>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<QVector<QString>>>::value));
QCOMPARE(waitForValue(p, QVector<QString>{}), (QVector<QString>{"43", "44", "45"})); QCOMPARE(waitForValue(p, QVector<QString>{}), (QVector<QString>{"43", "44", "45"}));
} }
void tst_helpers_map::delayedFulfilled() void tst_helpers_map::delayedFulfilled()
{ {
auto p = QtPromise::map(QVector<int>{42, 43, 44}, [](int v, ...) { auto p = QtPromise::map(QVector<int>{42, 43, 44}, [](int v, ...) {
return QPromise<int>{[&](const QPromiseResolve<int>& resolve) { return QtPromise::QPromise<int>{[&](const QtPromise::QPromiseResolve<int>& resolve) {
QtPromisePrivate::qtpromise_defer([=]() { QtPromisePrivate::qtpromise_defer([=]() {
resolve(v + 1); resolve(v + 1);
}); });
}}; }};
}); });
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<QVector<int>>>::value));
QCOMPARE(waitForValue(p, QVector<int>{}), (QVector<int>{43, 44, 45})); QCOMPARE(waitForValue(p, QVector<int>{}), (QVector<int>{43, 44, 45}));
} }
void tst_helpers_map::delayedRejected() void tst_helpers_map::delayedRejected()
{ {
auto p = QtPromise::map(QVector<int>{42, 43, 44}, [](int v, ...) { auto p = QtPromise::map(QVector<int>{42, 43, 44}, [](int v, ...) {
return QPromise<int>{ return QtPromise::QPromise<int>{[&](const QtPromise::QPromiseResolve<int>& resolve,
[&](const QPromiseResolve<int>& resolve, const QPromiseReject<int>& reject) { const QtPromise::QPromiseReject<int>& reject) {
QtPromisePrivate::qtpromise_defer([=]() { QtPromisePrivate::qtpromise_defer([=]() {
if (v == 43) { if (v == 43) {
reject(QString{"foo"}); reject(QString{"foo"});
@ -107,7 +105,7 @@ void tst_helpers_map::delayedRejected()
}}; }};
}); });
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<QVector<int>>>::value));
QCOMPARE(waitForError(p, QString{}), QString{"foo"}); QCOMPARE(waitForError(p, QString{}), QString{"foo"});
} }
@ -120,7 +118,7 @@ void tst_helpers_map::functorThrows()
return v + 1; return v + 1;
}); });
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<QVector<int>>>::value));
QCOMPARE(waitForError(p, QString{}), QString{"foo"}); QCOMPARE(waitForError(p, QString{}), QString{"foo"});
} }
@ -130,17 +128,17 @@ void tst_helpers_map::functorArguments()
return v * i; return v * i;
}); });
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<QVector<int>>>::value));
QCOMPARE(waitForValue(p, QVector<int>{}), (QVector<int>{0, 42, 84})); QCOMPARE(waitForValue(p, QVector<int>{}), (QVector<int>{0, 42, 84}));
} }
void tst_helpers_map::preserveOrder() void tst_helpers_map::preserveOrder()
{ {
auto p = QtPromise::map(QVector<int>{500, 100, 250}, [](int v, ...) { auto p = QtPromise::map(QVector<int>{500, 100, 250}, [](int v, ...) {
return QPromise<int>::resolve(v + 1).delay(v); return QtPromise::QPromise<int>::resolve(v + 1).delay(v);
}); });
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<QVector<int>>>::value));
QCOMPARE(waitForValue(p, QVector<int>{}), (QVector<int>{501, 101, 251})); QCOMPARE(waitForValue(p, QVector<int>{}), (QVector<int>{501, 101, 251}));
} }

View File

@ -11,8 +11,6 @@
#include <QtPromise> #include <QtPromise>
#include <QtTest> #include <QtTest>
using namespace QtPromise;
class tst_helpers_reduce : public QObject class tst_helpers_reduce : public QObject
{ {
Q_OBJECT Q_OBJECT
@ -57,8 +55,8 @@ struct SequenceTester
}, },
QtPromise::resolve(2).delay(100)); QtPromise::resolve(2).delay(100));
Q_STATIC_ASSERT((std::is_same<decltype(p0), QPromise<int>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p0), QtPromise::QPromise<int>>::value));
Q_STATIC_ASSERT((std::is_same<decltype(p1), QPromise<int>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p1), QtPromise::QPromise<int>>::value));
QCOMPARE(p0.isPending(), true); QCOMPARE(p0.isPending(), true);
QCOMPARE(p1.isPending(), true); QCOMPARE(p1.isPending(), true);
@ -85,7 +83,7 @@ void tst_helpers_reduce::emptySequence()
// NOTE(SB): reduce() on an empty sequence without an initial value is an error! // NOTE(SB): reduce() on an empty sequence without an initial value is an error!
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<int>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<int>>::value));
QCOMPARE(waitForValue(p, -1), 42); QCOMPARE(waitForValue(p, -1), 42);
QCOMPARE(called, false); QCOMPARE(called, false);
@ -109,8 +107,8 @@ void tst_helpers_reduce::regularValues()
}, },
2); 2);
Q_STATIC_ASSERT((std::is_same<decltype(p0), QPromise<int>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p0), QtPromise::QPromise<int>>::value));
Q_STATIC_ASSERT((std::is_same<decltype(p1), QPromise<int>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p1), QtPromise::QPromise<int>>::value));
QCOMPARE(p0.isPending(), true); QCOMPARE(p0.isPending(), true);
QCOMPARE(p1.isPending(), true); QCOMPARE(p1.isPending(), true);
@ -122,7 +120,7 @@ void tst_helpers_reduce::regularValues()
void tst_helpers_reduce::promiseValues() void tst_helpers_reduce::promiseValues()
{ {
QVector<QPromise<int>> inputs{QtPromise::resolve(4).delay(400), QVector<QtPromise::QPromise<int>> inputs{QtPromise::resolve(4).delay(400),
QtPromise::resolve(6).delay(300), QtPromise::resolve(6).delay(300),
QtPromise::resolve(8).delay(200)}; QtPromise::resolve(8).delay(200)};
QVector<int> v0; QVector<int> v0;
@ -140,8 +138,8 @@ void tst_helpers_reduce::promiseValues()
}, },
2); 2);
Q_STATIC_ASSERT((std::is_same<decltype(p0), QPromise<int>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p0), QtPromise::QPromise<int>>::value));
Q_STATIC_ASSERT((std::is_same<decltype(p1), QPromise<int>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p1), QtPromise::QPromise<int>>::value));
QCOMPARE(p0.isPending(), true); QCOMPARE(p0.isPending(), true);
QCOMPARE(p1.isPending(), true); QCOMPARE(p1.isPending(), true);
@ -164,7 +162,7 @@ void tst_helpers_reduce::convertResultType()
// NOTE(SB): when no initial value is given, the result type is the sequence type. // NOTE(SB): when no initial value is given, the result type is the sequence type.
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QString>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<QString>>::value));
QCOMPARE(p.isPending(), true); QCOMPARE(p.isPending(), true);
QCOMPARE(waitForValue(p, QString{}), QString{"foo:4:0:6:1:8:2"}); QCOMPARE(waitForValue(p, QString{}), QString{"foo:4:0:6:1:8:2"});
@ -182,7 +180,7 @@ void tst_helpers_reduce::delayedInitialValue()
}, },
QtPromise::resolve(2).delay(100)); QtPromise::resolve(2).delay(100));
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<int>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<int>>::value));
QCOMPARE(p.isPending(), true); QCOMPARE(p.isPending(), true);
QCOMPARE(waitForValue(p, -1), 23); QCOMPARE(waitForValue(p, -1), 23);
@ -207,8 +205,8 @@ void tst_helpers_reduce::delayedFulfilled()
}, },
2); 2);
Q_STATIC_ASSERT((std::is_same<decltype(p0), QPromise<int>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p0), QtPromise::QPromise<int>>::value));
Q_STATIC_ASSERT((std::is_same<decltype(p1), QPromise<int>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p1), QtPromise::QPromise<int>>::value));
QCOMPARE(p0.isPending(), true); QCOMPARE(p0.isPending(), true);
QCOMPARE(p1.isPending(), true); QCOMPARE(p1.isPending(), true);
@ -227,7 +225,7 @@ void tst_helpers_reduce::delayedRejected()
auto p0 = QtPromise::reduce(inputs, [&](int acc, int cur, int idx) { auto p0 = QtPromise::reduce(inputs, [&](int acc, int cur, int idx) {
v0 << acc << cur << idx; v0 << acc << cur << idx;
if (cur == 6) { if (cur == 6) {
return QPromise<int>::reject(QString{"foo"}); return QtPromise::QPromise<int>::reject(QString{"foo"});
} }
return QtPromise::resolve(acc + cur + idx); return QtPromise::resolve(acc + cur + idx);
}); });
@ -236,14 +234,14 @@ void tst_helpers_reduce::delayedRejected()
[&](int acc, int cur, int idx) { [&](int acc, int cur, int idx) {
v1 << acc << cur << idx; v1 << acc << cur << idx;
if (cur == 6) { if (cur == 6) {
return QPromise<int>::reject(QString{"bar"}); return QtPromise::QPromise<int>::reject(QString{"bar"});
} }
return QtPromise::resolve(acc + cur + idx); return QtPromise::resolve(acc + cur + idx);
}, },
2); 2);
Q_STATIC_ASSERT((std::is_same<decltype(p0), QPromise<int>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p0), QtPromise::QPromise<int>>::value));
Q_STATIC_ASSERT((std::is_same<decltype(p1), QPromise<int>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p1), QtPromise::QPromise<int>>::value));
QCOMPARE(p0.isPending(), true); QCOMPARE(p0.isPending(), true);
QCOMPARE(p1.isPending(), true); QCOMPARE(p1.isPending(), true);
@ -277,8 +275,8 @@ void tst_helpers_reduce::functorThrows()
}, },
2); 2);
Q_STATIC_ASSERT((std::is_same<decltype(p0), QPromise<int>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p0), QtPromise::QPromise<int>>::value));
Q_STATIC_ASSERT((std::is_same<decltype(p1), QPromise<int>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p1), QtPromise::QPromise<int>>::value));
QCOMPARE(p0.isPending(), true); QCOMPARE(p0.isPending(), true);
QCOMPARE(p1.isPending(), true); QCOMPARE(p1.isPending(), true);
@ -292,10 +290,10 @@ void tst_helpers_reduce::sequenceTypes()
{ {
#if (QT_VERSION < QT_VERSION_CHECK(5, 15, 0)) #if (QT_VERSION < QT_VERSION_CHECK(5, 15, 0))
// QLinkedList is deprecated since Qt 5.15. // QLinkedList is deprecated since Qt 5.15.
SequenceTester<QLinkedList<QPromise<int>>>::exec(); SequenceTester<QLinkedList<QtPromise::QPromise<int>>>::exec();
#endif #endif
SequenceTester<QList<QPromise<int>>>::exec(); SequenceTester<QList<QtPromise::QPromise<int>>>::exec();
SequenceTester<QVector<QPromise<int>>>::exec(); SequenceTester<QVector<QtPromise::QPromise<int>>>::exec();
SequenceTester<std::list<QPromise<int>>>::exec(); SequenceTester<std::list<QtPromise::QPromise<int>>>::exec();
SequenceTester<std::vector<QPromise<int>>>::exec(); SequenceTester<std::vector<QtPromise::QPromise<int>>>::exec();
} }

View File

@ -12,8 +12,6 @@
#include <memory> #include <memory>
using namespace QtPromise;
class tst_helpers_reject : public QObject class tst_helpers_reject : public QObject
{ {
Q_OBJECT Q_OBJECT
@ -29,7 +27,7 @@ QTEST_MAIN(tst_helpers_reject)
void tst_helpers_reject::rejectWithValue() void tst_helpers_reject::rejectWithValue()
{ {
auto p = QPromise<int>::reject(42); auto p = QtPromise::QPromise<int>::reject(42);
QCOMPARE(p.isRejected(), true); QCOMPARE(p.isRejected(), true);
QCOMPARE(waitForError(p, -1), 42); QCOMPARE(waitForError(p, -1), 42);
@ -42,7 +40,7 @@ void tst_helpers_reject::rejectWithQSharedPtr()
{ {
auto sptr = QSharedPointer<int>::create(42); auto sptr = QSharedPointer<int>::create(42);
auto p = QPromise<int>::reject(sptr); auto p = QtPromise::QPromise<int>::reject(sptr);
QCOMPARE(waitForError(p, QSharedPointer<int>{}), sptr); QCOMPARE(waitForError(p, QSharedPointer<int>{}), sptr);
@ -62,7 +60,7 @@ void tst_helpers_reject::rejectWithStdSharedPtr()
{ {
auto sptr = std::make_shared<int>(42); auto sptr = std::make_shared<int>(42);
auto p = QPromise<int>::reject(sptr); auto p = QtPromise::QPromise<int>::reject(sptr);
QCOMPARE(waitForError(p, std::shared_ptr<int>{}), sptr); QCOMPARE(waitForError(p, std::shared_ptr<int>{}), sptr);

View File

@ -14,8 +14,6 @@
#include <memory> #include <memory>
using namespace QtPromise;
class tst_helpers_resolve : public QObject class tst_helpers_resolve : public QObject
{ {
Q_OBJECT Q_OBJECT
@ -45,9 +43,9 @@ void tst_helpers_resolve::value()
auto p1 = QtPromise::resolve(v0); auto p1 = QtPromise::resolve(v0);
auto p2 = QtPromise::resolve(v1); auto p2 = QtPromise::resolve(v1);
Q_STATIC_ASSERT((std::is_same<decltype(p0), QPromise<int>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p0), QtPromise::QPromise<int>>::value));
Q_STATIC_ASSERT((std::is_same<decltype(p1), QPromise<int>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p1), QtPromise::QPromise<int>>::value));
Q_STATIC_ASSERT((std::is_same<decltype(p2), QPromise<int>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p2), QtPromise::QPromise<int>>::value));
for (const auto& p : {p0, p1, p2}) { for (const auto& p : {p0, p1, p2}) {
QCOMPARE(p.isFulfilled(), true); QCOMPARE(p.isFulfilled(), true);
@ -61,7 +59,7 @@ void tst_helpers_resolve::noValue()
{ {
auto p = QtPromise::resolve(); auto p = QtPromise::resolve();
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<void>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<void>>::value));
QCOMPARE(p.isFulfilled(), true); QCOMPARE(p.isFulfilled(), true);
QCOMPARE(waitForValue(p, -1, 42), 42); QCOMPARE(waitForValue(p, -1, 42), 42);
@ -74,7 +72,7 @@ void tst_helpers_resolve::moveRValue()
{ {
auto p = QtPromise::resolve(Data{42}).wait(); auto p = QtPromise::resolve(Data{42}).wait();
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<Data>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<Data>>::value));
} }
QCOMPARE(Data::logs().ctor, 1); QCOMPARE(Data::logs().ctor, 1);
@ -91,7 +89,7 @@ void tst_helpers_resolve::copyLValue()
Data value{42}; Data value{42};
auto p = QtPromise::resolve(value).wait(); auto p = QtPromise::resolve(value).wait();
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<Data>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<Data>>::value));
} }
QCOMPARE(Data::logs().ctor, 1); QCOMPARE(Data::logs().ctor, 1);
@ -103,24 +101,26 @@ void tst_helpers_resolve::copyLValue()
// https://github.com/simonbrunel/qtpromise/issues/6 // https://github.com/simonbrunel/qtpromise/issues/6
void tst_helpers_resolve::qtSharedPtr() void tst_helpers_resolve::qtSharedPtr()
{ {
using DataSPtr = QSharedPointer<Data>;
Data::logs().reset(); Data::logs().reset();
QWeakPointer<Data> wptr; QWeakPointer<Data> wptr;
{ {
auto sptr0 = QSharedPointer<Data>::create(42); auto sptr0 = DataSPtr::create(42);
const QSharedPointer<Data> sptr1 = sptr0; const DataSPtr sptr1 = sptr0;
auto p0 = QtPromise::resolve(QSharedPointer<Data>::create(42)); auto p0 = QtPromise::resolve(DataSPtr::create(42));
auto p1 = QtPromise::resolve(sptr0); auto p1 = QtPromise::resolve(sptr0);
auto p2 = QtPromise::resolve(sptr1); auto p2 = QtPromise::resolve(sptr1);
Q_STATIC_ASSERT((std::is_same<decltype(p0), QPromise<QSharedPointer<Data>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p0), QtPromise::QPromise<DataSPtr>>::value));
Q_STATIC_ASSERT((std::is_same<decltype(p1), QPromise<QSharedPointer<Data>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p1), QtPromise::QPromise<DataSPtr>>::value));
Q_STATIC_ASSERT((std::is_same<decltype(p2), QPromise<QSharedPointer<Data>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p2), QtPromise::QPromise<DataSPtr>>::value));
QCOMPARE(waitForValue(p1, QSharedPointer<Data>{}), sptr0); QCOMPARE(waitForValue(p1, DataSPtr{}), sptr0);
QCOMPARE(waitForValue(p2, QSharedPointer<Data>{}), sptr1); QCOMPARE(waitForValue(p2, DataSPtr{}), sptr1);
wptr = sptr0; wptr = sptr0;
@ -139,24 +139,26 @@ void tst_helpers_resolve::qtSharedPtr()
// https://github.com/simonbrunel/qtpromise/issues/6 // https://github.com/simonbrunel/qtpromise/issues/6
void tst_helpers_resolve::stdSharedPtr() void tst_helpers_resolve::stdSharedPtr()
{ {
using DataSPtr = std::shared_ptr<Data>;
Data::logs().reset(); Data::logs().reset();
std::weak_ptr<Data> wptr; std::weak_ptr<Data> wptr;
{ {
auto sptr0 = std::make_shared<Data>(42); auto sptr0 = std::make_shared<Data>(42);
const std::shared_ptr<Data> sptr1 = sptr0; const DataSPtr sptr1 = sptr0;
auto p0 = QtPromise::resolve(std::make_shared<Data>(42)); auto p0 = QtPromise::resolve(std::make_shared<Data>(42));
auto p1 = QtPromise::resolve(sptr0); auto p1 = QtPromise::resolve(sptr0);
auto p2 = QtPromise::resolve(sptr1); auto p2 = QtPromise::resolve(sptr1);
Q_STATIC_ASSERT((std::is_same<decltype(p0), QPromise<std::shared_ptr<Data>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p0), QtPromise::QPromise<DataSPtr>>::value));
Q_STATIC_ASSERT((std::is_same<decltype(p1), QPromise<std::shared_ptr<Data>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p1), QtPromise::QPromise<DataSPtr>>::value));
Q_STATIC_ASSERT((std::is_same<decltype(p2), QPromise<std::shared_ptr<Data>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p2), QtPromise::QPromise<DataSPtr>>::value));
QCOMPARE(waitForValue(p1, std::shared_ptr<Data>{}), sptr0); QCOMPARE(waitForValue(p1, DataSPtr{}), sptr0);
QCOMPARE(waitForValue(p2, std::shared_ptr<Data>{}), sptr1); QCOMPARE(waitForValue(p2, DataSPtr{}), sptr1);
wptr = sptr0; wptr = sptr0;
@ -174,22 +176,22 @@ void tst_helpers_resolve::stdSharedPtr()
void tst_helpers_resolve::typedPromise() void tst_helpers_resolve::typedPromise()
{ {
auto resolver = [](const QPromiseResolve<int>& resolve) { auto resolver = [](const QtPromise::QPromiseResolve<int>& resolve) {
QtPromisePrivate::qtpromise_defer([=]() { QtPromisePrivate::qtpromise_defer([=]() {
resolve(42); resolve(42);
}); });
}; };
QPromise<int> v0{resolver}; QtPromise::QPromise<int> v0{resolver};
const QPromise<int> v1 = v0; const QtPromise::QPromise<int> v1 = v0;
auto p0 = QtPromise::resolve(QPromise<int>{resolver}); auto p0 = QtPromise::resolve(QtPromise::QPromise<int>{resolver});
auto p1 = QtPromise::resolve(v0); auto p1 = QtPromise::resolve(v0);
auto p2 = QtPromise::resolve(v1); auto p2 = QtPromise::resolve(v1);
Q_STATIC_ASSERT((std::is_same<decltype(p0), QPromise<int>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p0), QtPromise::QPromise<int>>::value));
Q_STATIC_ASSERT((std::is_same<decltype(p1), QPromise<int>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p1), QtPromise::QPromise<int>>::value));
Q_STATIC_ASSERT((std::is_same<decltype(p2), QPromise<int>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p2), QtPromise::QPromise<int>>::value));
for (const auto& promise : {p0, p1, p2}) { for (const auto& promise : {p0, p1, p2}) {
QCOMPARE(promise.isPending(), true); QCOMPARE(promise.isPending(), true);
@ -201,22 +203,22 @@ void tst_helpers_resolve::typedPromise()
void tst_helpers_resolve::voidPromise() void tst_helpers_resolve::voidPromise()
{ {
auto resolver = [](const QPromiseResolve<void>& resolve) { auto resolver = [](const QtPromise::QPromiseResolve<void>& resolve) {
QtPromisePrivate::qtpromise_defer([=]() { QtPromisePrivate::qtpromise_defer([=]() {
resolve(); resolve();
}); });
}; };
QPromise<void> v0{resolver}; QtPromise::QPromise<void> v0{resolver};
const QPromise<void> v1 = v0; const QtPromise::QPromise<void> v1 = v0;
auto p0 = QtPromise::resolve(QPromise<void>{resolver}); auto p0 = QtPromise::resolve(QtPromise::QPromise<void>{resolver});
auto p1 = QtPromise::resolve(v0); auto p1 = QtPromise::resolve(v0);
auto p2 = QtPromise::resolve(v1); auto p2 = QtPromise::resolve(v1);
Q_STATIC_ASSERT((std::is_same<decltype(p0), QPromise<void>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p0), QtPromise::QPromise<void>>::value));
Q_STATIC_ASSERT((std::is_same<decltype(p1), QPromise<void>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p1), QtPromise::QPromise<void>>::value));
Q_STATIC_ASSERT((std::is_same<decltype(p2), QPromise<void>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p2), QtPromise::QPromise<void>>::value));
for (const auto& promise : {p0, p1, p2}) { for (const auto& promise : {p0, p1, p2}) {
QCOMPARE(promise.isPending(), true); QCOMPARE(promise.isPending(), true);
@ -238,9 +240,9 @@ void tst_helpers_resolve::typedFuture()
auto p1 = QtPromise::resolve(v0); auto p1 = QtPromise::resolve(v0);
auto p2 = QtPromise::resolve(v1); auto p2 = QtPromise::resolve(v1);
Q_STATIC_ASSERT((std::is_same<decltype(p0), QPromise<int>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p0), QtPromise::QPromise<int>>::value));
Q_STATIC_ASSERT((std::is_same<decltype(p1), QPromise<int>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p1), QtPromise::QPromise<int>>::value));
Q_STATIC_ASSERT((std::is_same<decltype(p2), QPromise<int>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p2), QtPromise::QPromise<int>>::value));
for (const auto& promise : {p0, p1, p2}) { for (const auto& promise : {p0, p1, p2}) {
QCOMPARE(promise.isPending(), true); QCOMPARE(promise.isPending(), true);
@ -260,9 +262,9 @@ void tst_helpers_resolve::voidFuture()
auto p1 = QtPromise::resolve(v0); auto p1 = QtPromise::resolve(v0);
auto p2 = QtPromise::resolve(v1); auto p2 = QtPromise::resolve(v1);
Q_STATIC_ASSERT((std::is_same<decltype(p0), QPromise<void>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p0), QtPromise::QPromise<void>>::value));
Q_STATIC_ASSERT((std::is_same<decltype(p1), QPromise<void>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p1), QtPromise::QPromise<void>>::value));
Q_STATIC_ASSERT((std::is_same<decltype(p2), QPromise<void>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p2), QtPromise::QPromise<void>>::value));
for (const auto& promise : {p0, p1, p2}) { for (const auto& promise : {p0, p1, p2}) {
QCOMPARE(promise.isPending(), true); QCOMPARE(promise.isPending(), true);

View File

@ -12,8 +12,6 @@
#include <memory> #include <memory>
using namespace QtPromise;
class tst_qpromise_construct : public QObject class tst_qpromise_construct : public QObject
{ {
Q_OBJECT Q_OBJECT
@ -46,7 +44,7 @@ QTEST_MAIN(tst_qpromise_construct)
void tst_qpromise_construct::resolveSyncOneArg() void tst_qpromise_construct::resolveSyncOneArg()
{ {
QPromise<int> p{[](const QPromiseResolve<int>& resolve) { QtPromise::QPromise<int> p{[](const QtPromise::QPromiseResolve<int>& resolve) {
resolve(42); resolve(42);
}}; }};
@ -57,7 +55,7 @@ void tst_qpromise_construct::resolveSyncOneArg()
void tst_qpromise_construct::resolveSyncOneArg_void() void tst_qpromise_construct::resolveSyncOneArg_void()
{ {
QPromise<void> p{[](const QPromiseResolve<void>& resolve) { QtPromise::QPromise<void> p{[](const QtPromise::QPromiseResolve<void>& resolve) {
resolve(); resolve();
}}; }};
@ -68,7 +66,8 @@ void tst_qpromise_construct::resolveSyncOneArg_void()
void tst_qpromise_construct::resolveSyncTwoArgs() void tst_qpromise_construct::resolveSyncTwoArgs()
{ {
QPromise<int> p{[](const QPromiseResolve<int>& resolve, const QPromiseReject<int>&) { QtPromise::QPromise<int> p{
[](const QtPromise::QPromiseResolve<int>& resolve, const QtPromise::QPromiseReject<int>&) {
resolve(42); resolve(42);
}}; }};
@ -79,7 +78,8 @@ void tst_qpromise_construct::resolveSyncTwoArgs()
void tst_qpromise_construct::resolveSyncTwoArgs_void() void tst_qpromise_construct::resolveSyncTwoArgs_void()
{ {
QPromise<void> p{[](const QPromiseResolve<void>& resolve, const QPromiseReject<void>&) { QtPromise::QPromise<void> p{[](const QtPromise::QPromiseResolve<void>& resolve,
const QtPromise::QPromiseReject<void>&) {
resolve(); resolve();
}}; }};
@ -90,7 +90,7 @@ void tst_qpromise_construct::resolveSyncTwoArgs_void()
void tst_qpromise_construct::resolveAsyncOneArg() void tst_qpromise_construct::resolveAsyncOneArg()
{ {
QPromise<int> p{[](const QPromiseResolve<int>& resolve) { QtPromise::QPromise<int> p{[](const QtPromise::QPromiseResolve<int>& resolve) {
QtPromisePrivate::qtpromise_defer([=]() { QtPromisePrivate::qtpromise_defer([=]() {
resolve(42); resolve(42);
}); });
@ -104,7 +104,7 @@ void tst_qpromise_construct::resolveAsyncOneArg()
void tst_qpromise_construct::resolveAsyncOneArg_void() void tst_qpromise_construct::resolveAsyncOneArg_void()
{ {
QPromise<void> p{[](const QPromiseResolve<void>& resolve) { QtPromise::QPromise<void> p{[](const QtPromise::QPromiseResolve<void>& resolve) {
QtPromisePrivate::qtpromise_defer([=]() { QtPromisePrivate::qtpromise_defer([=]() {
resolve(); resolve();
}); });
@ -118,7 +118,8 @@ void tst_qpromise_construct::resolveAsyncOneArg_void()
void tst_qpromise_construct::resolveAsyncTwoArgs() void tst_qpromise_construct::resolveAsyncTwoArgs()
{ {
QPromise<int> p{[](const QPromiseResolve<int>& resolve, const QPromiseReject<int>&) { QtPromise::QPromise<int> p{
[](const QtPromise::QPromiseResolve<int>& resolve, const QtPromise::QPromiseReject<int>&) {
QtPromisePrivate::qtpromise_defer([=]() { QtPromisePrivate::qtpromise_defer([=]() {
resolve(42); resolve(42);
}); });
@ -132,7 +133,8 @@ void tst_qpromise_construct::resolveAsyncTwoArgs()
void tst_qpromise_construct::resolveAsyncTwoArgs_void() void tst_qpromise_construct::resolveAsyncTwoArgs_void()
{ {
QPromise<void> p{[](const QPromiseResolve<void>& resolve, const QPromiseReject<void>&) { QtPromise::QPromise<void> p{[](const QtPromise::QPromiseResolve<void>& resolve,
const QtPromise::QPromiseReject<void>&) {
QtPromisePrivate::qtpromise_defer([=]() { QtPromisePrivate::qtpromise_defer([=]() {
resolve(); resolve();
}); });
@ -146,7 +148,8 @@ void tst_qpromise_construct::resolveAsyncTwoArgs_void()
void tst_qpromise_construct::rejectSync() void tst_qpromise_construct::rejectSync()
{ {
QPromise<int> p{[](const QPromiseResolve<int>&, const QPromiseReject<int>& reject) { QtPromise::QPromise<int> p{
[](const QtPromise::QPromiseResolve<int>&, const QtPromise::QPromiseReject<int>& reject) {
reject(QString{"foo"}); reject(QString{"foo"});
}}; }};
@ -157,7 +160,8 @@ void tst_qpromise_construct::rejectSync()
void tst_qpromise_construct::rejectSync_void() void tst_qpromise_construct::rejectSync_void()
{ {
QPromise<void> p{[](const QPromiseResolve<void>&, const QPromiseReject<void>& reject) { QtPromise::QPromise<void> p{
[](const QtPromise::QPromiseResolve<void>&, const QtPromise::QPromiseReject<void>& reject) {
reject(QString{"foo"}); reject(QString{"foo"});
}}; }};
@ -168,7 +172,8 @@ void tst_qpromise_construct::rejectSync_void()
void tst_qpromise_construct::rejectAsync() void tst_qpromise_construct::rejectAsync()
{ {
QPromise<int> p{[](const QPromiseResolve<int>&, const QPromiseReject<int>& reject) { QtPromise::QPromise<int> p{
[](const QtPromise::QPromiseResolve<int>&, const QtPromise::QPromiseReject<int>& reject) {
QtPromisePrivate::qtpromise_defer([=]() { QtPromisePrivate::qtpromise_defer([=]() {
reject(QString{"foo"}); reject(QString{"foo"});
}); });
@ -182,7 +187,8 @@ void tst_qpromise_construct::rejectAsync()
void tst_qpromise_construct::rejectAsync_void() void tst_qpromise_construct::rejectAsync_void()
{ {
QPromise<void> p{[](const QPromiseResolve<void>&, const QPromiseReject<void>& reject) { QtPromise::QPromise<void> p{
[](const QtPromise::QPromiseResolve<void>&, const QtPromise::QPromiseReject<void>& reject) {
QtPromisePrivate::qtpromise_defer([=]() { QtPromisePrivate::qtpromise_defer([=]() {
reject(QString{"foo"}); reject(QString{"foo"});
}); });
@ -196,7 +202,7 @@ void tst_qpromise_construct::rejectAsync_void()
void tst_qpromise_construct::rejectThrowOneArg() void tst_qpromise_construct::rejectThrowOneArg()
{ {
QPromise<int> p{[](const QPromiseResolve<int>&) { QtPromise::QPromise<int> p{[](const QtPromise::QPromiseResolve<int>&) {
throw QString{"foo"}; throw QString{"foo"};
}}; }};
@ -207,7 +213,7 @@ void tst_qpromise_construct::rejectThrowOneArg()
void tst_qpromise_construct::rejectThrowOneArg_void() void tst_qpromise_construct::rejectThrowOneArg_void()
{ {
QPromise<void> p{[](const QPromiseResolve<void>&) { QtPromise::QPromise<void> p{[](const QtPromise::QPromiseResolve<void>&) {
throw QString{"foo"}; throw QString{"foo"};
}}; }};
@ -218,7 +224,8 @@ void tst_qpromise_construct::rejectThrowOneArg_void()
void tst_qpromise_construct::rejectThrowTwoArgs() void tst_qpromise_construct::rejectThrowTwoArgs()
{ {
QPromise<int> p{[](const QPromiseResolve<int>&, const QPromiseReject<int>&) { QtPromise::QPromise<int> p{
[](const QtPromise::QPromiseResolve<int>&, const QtPromise::QPromiseReject<int>&) {
throw QString{"foo"}; throw QString{"foo"};
}}; }};
@ -229,7 +236,8 @@ void tst_qpromise_construct::rejectThrowTwoArgs()
void tst_qpromise_construct::rejectThrowTwoArgs_void() void tst_qpromise_construct::rejectThrowTwoArgs_void()
{ {
QPromise<void> p{[](const QPromiseResolve<void>&, const QPromiseReject<void>&) { QtPromise::QPromise<void> p{
[](const QtPromise::QPromiseResolve<void>&, const QtPromise::QPromiseReject<void>&) {
throw QString{"foo"}; throw QString{"foo"};
}}; }};
@ -240,26 +248,28 @@ void tst_qpromise_construct::rejectThrowTwoArgs_void()
void tst_qpromise_construct::rejectUndefined() void tst_qpromise_construct::rejectUndefined()
{ {
QPromise<int> p{[](const QPromiseResolve<int>&, const QPromiseReject<int>& reject) { QtPromise::QPromise<int> p{
[](const QtPromise::QPromiseResolve<int>&, const QtPromise::QPromiseReject<int>& reject) {
QtPromisePrivate::qtpromise_defer([=]() { QtPromisePrivate::qtpromise_defer([=]() {
reject(); reject();
}); });
}}; }};
QCOMPARE(p.isPending(), true); QCOMPARE(p.isPending(), true);
QCOMPARE(waitForRejected<QPromiseUndefinedException>(p), true); QCOMPARE(waitForRejected<QtPromise::QPromiseUndefinedException>(p), true);
} }
void tst_qpromise_construct::rejectUndefined_void() void tst_qpromise_construct::rejectUndefined_void()
{ {
QPromise<void> p{[](const QPromiseResolve<void>&, const QPromiseReject<void>& reject) { QtPromise::QPromise<void> p{
[](const QtPromise::QPromiseResolve<void>&, const QtPromise::QPromiseReject<void>& reject) {
QtPromisePrivate::qtpromise_defer([=]() { QtPromisePrivate::qtpromise_defer([=]() {
reject(); reject();
}); });
}}; }};
QCOMPARE(p.isPending(), true); QCOMPARE(p.isPending(), true);
QCOMPARE(waitForRejected<QPromiseUndefinedException>(p), true); QCOMPARE(waitForRejected<QtPromise::QPromiseUndefinedException>(p), true);
} }
// https://github.com/simonbrunel/qtpromise/issues/6 // https://github.com/simonbrunel/qtpromise/issues/6
@ -270,9 +280,9 @@ void tst_qpromise_construct::connectAndResolve()
std::weak_ptr<int> wptr; std::weak_ptr<int> wptr;
{ {
auto p = auto p = QtPromise::QPromise<std::shared_ptr<int>>{
QPromise<std::shared_ptr<int>>{[&](const QPromiseResolve<std::shared_ptr<int>>& resolve, [&](const QtPromise::QPromiseResolve<std::shared_ptr<int>>& resolve,
const QPromiseReject<std::shared_ptr<int>>& reject) { const QtPromise::QPromiseReject<std::shared_ptr<int>>& reject) {
connect(object.data(), connect(object.data(),
&QObject::objectNameChanged, &QObject::objectNameChanged,
[=, &wptr](const QString& name) { [=, &wptr](const QString& name) {
@ -307,8 +317,8 @@ void tst_qpromise_construct::connectAndReject()
std::weak_ptr<int> wptr; std::weak_ptr<int> wptr;
{ {
auto p = QPromise<int>{[&](const QPromiseResolve<int>& resolve, auto p = QtPromise::QPromise<int>{[&](const QtPromise::QPromiseResolve<int>& resolve,
const QPromiseReject<int>& reject) { const QtPromise::QPromiseReject<int>& reject) {
connect(object.data(), &QObject::objectNameChanged, [=, &wptr](const QString& name) { connect(object.data(), &QObject::objectNameChanged, [=, &wptr](const QString& name) {
auto sptr = std::make_shared<int>(42); auto sptr = std::make_shared<int>(42);

View File

@ -12,8 +12,6 @@
#include <chrono> #include <chrono>
using namespace QtPromise;
class tst_qpromise_convert : public QObject class tst_qpromise_convert : public QObject
{ {
Q_OBJECT Q_OBJECT
@ -76,7 +74,7 @@ void tst_qpromise_convert::fulfillTAsU()
{ {
auto p = QtPromise::resolve(42.13).convert<int>(); auto p = QtPromise::resolve(42.13).convert<int>();
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<int>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<int>>::value));
QCOMPARE(waitForValue(p, -1), 42); QCOMPARE(waitForValue(p, -1), 42);
QVERIFY(p.isFulfilled()); QVERIFY(p.isFulfilled());
@ -86,7 +84,7 @@ void tst_qpromise_convert::fulfillTAsU()
{ {
auto p = QtPromise::resolve(Enum1::Value1).convert<int>(); auto p = QtPromise::resolve(Enum1::Value1).convert<int>();
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<int>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<int>>::value));
QCOMPARE(waitForValue(p, -1), 1); QCOMPARE(waitForValue(p, -1), 1);
QVERIFY(p.isFulfilled()); QVERIFY(p.isFulfilled());
@ -96,7 +94,7 @@ void tst_qpromise_convert::fulfillTAsU()
{ {
auto p = QtPromise::resolve(1).convert<Enum1>(); auto p = QtPromise::resolve(1).convert<Enum1>();
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<Enum1>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<Enum1>>::value));
QCOMPARE(waitForValue(p, Enum1::Value0), Enum1::Value1); QCOMPARE(waitForValue(p, Enum1::Value0), Enum1::Value1);
QVERIFY(p.isFulfilled()); QVERIFY(p.isFulfilled());
@ -106,7 +104,7 @@ void tst_qpromise_convert::fulfillTAsU()
{ {
auto p = QtPromise::resolve(Enum1::Value1).convert<Enum2>(); auto p = QtPromise::resolve(Enum1::Value1).convert<Enum2>();
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<Enum2>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<Enum2>>::value));
QCOMPARE(waitForValue(p, Enum2::Value0), Enum2::Value1); QCOMPARE(waitForValue(p, Enum2::Value0), Enum2::Value1);
QVERIFY(p.isFulfilled()); QVERIFY(p.isFulfilled());
@ -117,7 +115,7 @@ void tst_qpromise_convert::fulfillTAsU()
{ {
auto p = QtPromise::resolve(QByteArray{"foo"}).convert<QString>(); auto p = QtPromise::resolve(QByteArray{"foo"}).convert<QString>();
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QString>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<QString>>::value));
QCOMPARE(waitForValue(p, QString{}), QString{"foo"}); QCOMPARE(waitForValue(p, QString{}), QString{"foo"});
QVERIFY(p.isFulfilled()); QVERIFY(p.isFulfilled());
@ -128,7 +126,7 @@ void tst_qpromise_convert::fulfillTAsU()
{ {
auto p = QtPromise::resolve(Foo{42}).convert<Bar>(); auto p = QtPromise::resolve(Foo{42}).convert<Bar>();
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<Bar>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<Bar>>::value));
QCOMPARE(waitForValue(p, Bar{}), Bar{42}); QCOMPARE(waitForValue(p, Bar{}), Bar{42});
QVERIFY(p.isFulfilled()); QVERIFY(p.isFulfilled());
@ -138,7 +136,7 @@ void tst_qpromise_convert::fulfillTAsU()
{ {
auto p = QtPromise::resolve(42).convert<QString>(); auto p = QtPromise::resolve(42).convert<QString>();
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QString>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<QString>>::value));
QCOMPARE(waitForValue(p, QString{}), QString{"42"}); QCOMPARE(waitForValue(p, QString{}), QString{"42"});
QVERIFY(p.isFulfilled()); QVERIFY(p.isFulfilled());
@ -149,7 +147,7 @@ void tst_qpromise_convert::fulfillTAsU()
{ {
auto p = QtPromise::resolve(Foo{42}).convert<QString>(); auto p = QtPromise::resolve(Foo{42}).convert<QString>();
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QString>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<QString>>::value));
QCOMPARE(waitForValue(p, QString{}), QString{"Foo{42}"}); QCOMPARE(waitForValue(p, QString{}), QString{"Foo{42}"});
QVERIFY(p.isFulfilled()); QVERIFY(p.isFulfilled());
@ -160,7 +158,7 @@ void tst_qpromise_convert::fulfillTAsVoid()
{ {
auto p = QtPromise::resolve(42).convert<void>(); auto p = QtPromise::resolve(42).convert<void>();
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<void>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<void>>::value));
QCOMPARE(waitForValue(p, -1, 42), 42); QCOMPARE(waitForValue(p, -1, 42), 42);
QVERIFY(p.isFulfilled()); QVERIFY(p.isFulfilled());
@ -172,7 +170,7 @@ void tst_qpromise_convert::fulfillTAsQVariant()
{ {
auto p = QtPromise::resolve(42).convert<QVariant>(); auto p = QtPromise::resolve(42).convert<QVariant>();
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVariant>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<QVariant>>::value));
QCOMPARE(waitForValue(p, QVariant{}), QVariant{42}); QCOMPARE(waitForValue(p, QVariant{}), QVariant{42});
QVERIFY(p.isFulfilled()); QVERIFY(p.isFulfilled());
@ -182,7 +180,7 @@ void tst_qpromise_convert::fulfillTAsQVariant()
{ {
auto p = QtPromise::resolve(Foo{42}).convert<QVariant>(); auto p = QtPromise::resolve(Foo{42}).convert<QVariant>();
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVariant>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<QVariant>>::value));
QVariant value = waitForValue(p, QVariant{}); QVariant value = waitForValue(p, QVariant{});
QCOMPARE(value, QVariant::fromValue(Foo{42})); QCOMPARE(value, QVariant::fromValue(Foo{42}));
@ -197,7 +195,7 @@ void tst_qpromise_convert::fulfillQVariantAsU()
{ {
auto p = QtPromise::resolve(QVariant{42}).convert<int>(); auto p = QtPromise::resolve(QVariant{42}).convert<int>();
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<int>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<int>>::value));
QCOMPARE(waitForValue(p, -1), 42); QCOMPARE(waitForValue(p, -1), 42);
QVERIFY(p.isFulfilled()); QVERIFY(p.isFulfilled());
@ -208,7 +206,7 @@ void tst_qpromise_convert::fulfillQVariantAsU()
{ {
auto p = QtPromise::resolve(QVariant{"42"}).convert<int>(); auto p = QtPromise::resolve(QVariant{"42"}).convert<int>();
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<int>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<int>>::value));
QCOMPARE(waitForValue(p, -1), 42); QCOMPARE(waitForValue(p, -1), 42);
QVERIFY(p.isFulfilled()); QVERIFY(p.isFulfilled());
@ -218,7 +216,7 @@ void tst_qpromise_convert::fulfillQVariantAsU()
{ {
auto p = QtPromise::resolve(QVariant::fromValue(Foo{42})).convert<Foo>(); auto p = QtPromise::resolve(QVariant::fromValue(Foo{42})).convert<Foo>();
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<Foo>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<Foo>>::value));
QCOMPARE(waitForValue(p, Foo{}), Foo{42}); QCOMPARE(waitForValue(p, Foo{}), Foo{42});
QVERIFY(p.isFulfilled()); QVERIFY(p.isFulfilled());
@ -229,7 +227,7 @@ void tst_qpromise_convert::fulfillQVariantAsVoid()
{ {
auto p = QtPromise::resolve(QVariant{42}).convert<void>(); auto p = QtPromise::resolve(QVariant{42}).convert<void>();
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<void>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<void>>::value));
QCOMPARE(waitForValue(p, -1, 42), 42); QCOMPARE(waitForValue(p, -1, 42), 42);
QVERIFY(p.isFulfilled()); QVERIFY(p.isFulfilled());
@ -241,26 +239,26 @@ void tst_qpromise_convert::rejectUnconvertibleTypes()
{ {
auto p = QtPromise::resolve(QString{"42foo"}).convert<int>(); auto p = QtPromise::resolve(QString{"42foo"}).convert<int>();
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<int>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<int>>::value));
QVERIFY(waitForRejected<QPromiseConversionException>(p)); QVERIFY(waitForRejected<QtPromise::QPromiseConversionException>(p));
} }
// A user-defined type unconvertible to string because there is no converter. // A user-defined type unconvertible to string because there is no converter.
{ {
auto p = QtPromise::resolve(QVariant::fromValue(Bar{42})).convert<QString>(); auto p = QtPromise::resolve(QVariant::fromValue(Bar{42})).convert<QString>();
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QString>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<QString>>::value));
QVERIFY(waitForRejected<QPromiseConversionException>(p)); QVERIFY(waitForRejected<QtPromise::QPromiseConversionException>(p));
} }
// A standard library type unconvertible to a primitive type because there is no converter. // A standard library type unconvertible to a primitive type because there is no converter.
{ {
auto p = QtPromise::resolve(std::vector<int>{42, -42}).convert<int>(); auto p = QtPromise::resolve(std::vector<int>{42, -42}).convert<int>();
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<int>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<int>>::value));
QVERIFY(waitForRejected<QPromiseConversionException>(p)); QVERIFY(waitForRejected<QtPromise::QPromiseConversionException>(p));
} }
} }

View File

@ -12,8 +12,6 @@
#include <chrono> #include <chrono>
using namespace QtPromise;
class tst_qpromise_delay : public QObject class tst_qpromise_delay : public QObject
{ {
Q_OBJECT Q_OBJECT
@ -36,7 +34,7 @@ void tst_qpromise_delay::fulfilled()
timer.start(); timer.start();
auto p = QPromise<int>::resolve(42).delay(1000).finally([&]() { auto p = QtPromise::QPromise<int>::resolve(42).delay(1000).finally([&]() {
elapsed = timer.elapsed(); elapsed = timer.elapsed();
}); });
@ -57,7 +55,7 @@ void tst_qpromise_delay::rejected()
timer.start(); timer.start();
auto p = QPromise<int>::reject(QString{"foo"}).delay(1000).finally([&]() { auto p = QtPromise::QPromise<int>::reject(QString{"foo"}).delay(1000).finally([&]() {
elapsed = timer.elapsed(); elapsed = timer.elapsed();
}); });
@ -73,7 +71,7 @@ void tst_qpromise_delay::fulfilledStdChrono()
timer.start(); timer.start();
auto p = QPromise<int>::resolve(42).delay(std::chrono::seconds{1}).finally([&]() { auto p = QtPromise::QPromise<int>::resolve(42).delay(std::chrono::seconds{1}).finally([&]() {
elapsed = timer.elapsed(); elapsed = timer.elapsed();
}); });
@ -94,7 +92,9 @@ void tst_qpromise_delay::rejectedStdChrono()
timer.start(); timer.start();
auto p = QPromise<int>::reject(QString{"foo"}).delay(std::chrono::seconds{1}).finally([&]() { auto p = QtPromise::QPromise<int>::reject(QString{"foo"})
.delay(std::chrono::seconds{1})
.finally([&]() {
elapsed = timer.elapsed(); elapsed = timer.elapsed();
}); });

View File

@ -10,8 +10,6 @@
#include <QtPromise> #include <QtPromise>
#include <QtTest> #include <QtTest>
using namespace QtPromise;
class tst_qpromise_each : public QObject class tst_qpromise_each : public QObject
{ {
Q_OBJECT Q_OBJECT
@ -56,7 +54,7 @@ struct SequenceTester
values << v + 2; values << v + 2;
}); });
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<Sequence>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<Sequence>>::value));
QCOMPARE(waitForValue(p, Sequence{}), (Sequence{42, 43, 44})); QCOMPARE(waitForValue(p, Sequence{}), (Sequence{42, 43, 44}));
QVector<int> expected{0, 42, 1, 43, 2, 44, 42, 43, 44, 43, 44, 45, -1, -1, -1, 44, 45, 46}; QVector<int> expected{0, 42, 1, 43, 2, 44, 42, 43, 44, 43, 44, 45, -1, -1, -1, 44, 45, 46};
@ -69,11 +67,11 @@ struct SequenceTester
void tst_qpromise_each::emptySequence() void tst_qpromise_each::emptySequence()
{ {
QVector<int> values; QVector<int> values;
auto p = QPromise<QVector<int>>::resolve({}).each([&](int v, ...) { auto p = QtPromise::QPromise<QVector<int>>::resolve({}).each([&](int v, ...) {
values << v; values << v;
}); });
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<QVector<int>>>::value));
QCOMPARE(waitForValue(p, QVector<int>{}), QVector<int>{}); QCOMPARE(waitForValue(p, QVector<int>{}), QVector<int>{});
QCOMPARE(values, (QVector<int>{})); QCOMPARE(values, (QVector<int>{}));
} }
@ -81,11 +79,11 @@ void tst_qpromise_each::emptySequence()
void tst_qpromise_each::preserveValues() void tst_qpromise_each::preserveValues()
{ {
QVector<int> values; QVector<int> values;
auto p = QPromise<QVector<int>>::resolve({42, 43, 44}).each([&](int v, ...) { auto p = QtPromise::QPromise<QVector<int>>::resolve({42, 43, 44}).each([&](int v, ...) {
values << v + 1; values << v + 1;
}); });
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<QVector<int>>>::value));
QCOMPARE(waitForValue(p, QVector<int>{}), (QVector<int>{42, 43, 44})); QCOMPARE(waitForValue(p, QVector<int>{}), (QVector<int>{42, 43, 44}));
QCOMPARE(values, (QVector<int>{43, 44, 45})); QCOMPARE(values, (QVector<int>{43, 44, 45}));
} }
@ -93,12 +91,12 @@ void tst_qpromise_each::preserveValues()
void tst_qpromise_each::ignoreResult() void tst_qpromise_each::ignoreResult()
{ {
QVector<int> values; QVector<int> values;
auto p = QPromise<QVector<int>>::resolve({42, 43, 44}).each([&](int v, ...) { auto p = QtPromise::QPromise<QVector<int>>::resolve({42, 43, 44}).each([&](int v, ...) {
values << v + 1; values << v + 1;
return "Foo"; return "Foo";
}); });
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<QVector<int>>>::value));
QCOMPARE(waitForValue(p, QVector<int>{}), (QVector<int>{42, 43, 44})); QCOMPARE(waitForValue(p, QVector<int>{}), (QVector<int>{42, 43, 44}));
QCOMPARE(values, (QVector<int>{43, 44, 45})); QCOMPARE(values, (QVector<int>{43, 44, 45}));
} }
@ -106,14 +104,14 @@ void tst_qpromise_each::ignoreResult()
void tst_qpromise_each::delayedFulfilled() void tst_qpromise_each::delayedFulfilled()
{ {
QMap<int, int> values; QMap<int, int> values;
auto p = QPromise<QVector<int>>::resolve({42, 43, 44}).each([&](int v, int index) { auto p = QtPromise::QPromise<QVector<int>>::resolve({42, 43, 44}).each([&](int v, int index) {
return QPromise<void>::resolve().delay(250).then([=, &values]() { return QtPromise::QPromise<void>::resolve().delay(250).then([=, &values]() {
values[v] = index; values[v] = index;
return 42; return 42;
}); });
}); });
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<QVector<int>>>::value));
QCOMPARE(waitForValue(p, QVector<int>{}), (QVector<int>{42, 43, 44})); QCOMPARE(waitForValue(p, QVector<int>{}), (QVector<int>{42, 43, 44}));
QMap<int, int> expected{{42, 0}, {43, 1}, {44, 2}}; QMap<int, int> expected{{42, 0}, {43, 1}, {44, 2}};
QCOMPARE(values, expected); QCOMPARE(values, expected);
@ -121,9 +119,9 @@ void tst_qpromise_each::delayedFulfilled()
void tst_qpromise_each::delayedRejected() void tst_qpromise_each::delayedRejected()
{ {
auto p = QPromise<QVector<int>>::resolve({42, 43, 44}).each([](int v, ...) { auto p = QtPromise::QPromise<QVector<int>>::resolve({42, 43, 44}).each([](int v, ...) {
return QPromise<int>{ return QtPromise::QPromise<int>{[&](const QtPromise::QPromiseResolve<int>& resolve,
[&](const QPromiseResolve<int>& resolve, const QPromiseReject<int>& reject) { const QtPromise::QPromiseReject<int>& reject) {
QtPromisePrivate::qtpromise_defer([=]() { QtPromisePrivate::qtpromise_defer([=]() {
if (v == 44) { if (v == 44) {
reject(QString{"foo"}); reject(QString{"foo"});
@ -133,30 +131,30 @@ void tst_qpromise_each::delayedRejected()
}}; }};
}); });
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<QVector<int>>>::value));
QCOMPARE(waitForError(p, QString{}), QString{"foo"}); QCOMPARE(waitForError(p, QString{}), QString{"foo"});
} }
void tst_qpromise_each::functorThrows() void tst_qpromise_each::functorThrows()
{ {
auto p = QPromise<QVector<int>>::resolve({42, 43, 44}).each([](int v, ...) { auto p = QtPromise::QPromise<QVector<int>>::resolve({42, 43, 44}).each([](int v, ...) {
if (v == 44) { if (v == 44) {
throw QString{"foo"}; throw QString{"foo"};
} }
}); });
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<QVector<int>>>::value));
QCOMPARE(waitForError(p, QString{}), QString{"foo"}); QCOMPARE(waitForError(p, QString{}), QString{"foo"});
} }
void tst_qpromise_each::functorArguments() void tst_qpromise_each::functorArguments()
{ {
QVector<int> values; QVector<int> values;
auto p = QPromise<QVector<int>>::resolve({42, 43, 44}).each([&](int v, int i) { auto p = QtPromise::QPromise<QVector<int>>::resolve({42, 43, 44}).each([&](int v, int i) {
values << i << v; values << i << v;
}); });
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<QVector<int>>>::value));
QCOMPARE(waitForValue(p, QVector<int>{}), (QVector<int>{42, 43, 44})); QCOMPARE(waitForValue(p, QVector<int>{}), (QVector<int>{42, 43, 44}));
QCOMPARE(values, (QVector<int>{0, 42, 1, 43, 2, 44})); QCOMPARE(values, (QVector<int>{0, 42, 1, 43, 2, 44}));
} }

View File

@ -10,8 +10,6 @@
#include <QtPromise> #include <QtPromise>
#include <QtTest> #include <QtTest>
using namespace QtPromise;
class tst_qpromise_fail : public QObject class tst_qpromise_fail : public QObject
{ {
Q_OBJECT Q_OBJECT
@ -72,7 +70,7 @@ private:
void tst_qpromise_fail::sameType() void tst_qpromise_fail::sameType()
{ {
// http://en.cppreference.com/w/cpp/error/exception // http://en.cppreference.com/w/cpp/error/exception
auto p = QPromise<int>::reject(std::out_of_range("foo")); auto p = QtPromise::QPromise<int>::reject(std::out_of_range("foo"));
QString error; QString error;
p.fail([&](const std::domain_error& e) { p.fail([&](const std::domain_error& e) {
@ -95,7 +93,7 @@ void tst_qpromise_fail::sameType()
void tst_qpromise_fail::baseClass() void tst_qpromise_fail::baseClass()
{ {
// http://en.cppreference.com/w/cpp/error/exception // http://en.cppreference.com/w/cpp/error/exception
auto p = QPromise<int>::reject(std::out_of_range("foo")); auto p = QtPromise::QPromise<int>::reject(std::out_of_range("foo"));
QString error; QString error;
p.fail([&](const std::runtime_error& e) { p.fail([&](const std::runtime_error& e) {
@ -117,7 +115,7 @@ void tst_qpromise_fail::baseClass()
void tst_qpromise_fail::catchAll() void tst_qpromise_fail::catchAll()
{ {
auto p = QPromise<int>::reject(std::out_of_range("foo")); auto p = QtPromise::QPromise<int>::reject(std::out_of_range("foo"));
QString error; QString error;
p.fail([&](const std::runtime_error& e) { p.fail([&](const std::runtime_error& e) {
@ -140,18 +138,18 @@ void tst_qpromise_fail::catchAll()
void tst_qpromise_fail::functionPtrHandlers() void tst_qpromise_fail::functionPtrHandlers()
{ {
{ // Global functions. { // Global functions.
auto p0 = QPromise<float>::reject(kErr).fail(&fnNoArg); auto p0 = QtPromise::QPromise<float>::reject(kErr).fail(&fnNoArg);
auto p1 = QPromise<float>::reject(kErr).fail(&fnArgByVal); auto p1 = QtPromise::QPromise<float>::reject(kErr).fail(&fnArgByVal);
auto p2 = QPromise<float>::reject(kErr).fail(&fnArgByRef); auto p2 = QtPromise::QPromise<float>::reject(kErr).fail(&fnArgByRef);
QCOMPARE(waitForValue(p0, kFail), kRes); QCOMPARE(waitForValue(p0, kFail), kRes);
QCOMPARE(waitForValue(p1, kFail), kRes); QCOMPARE(waitForValue(p1, kFail), kRes);
QCOMPARE(waitForValue(p2, kFail), kRes); QCOMPARE(waitForValue(p2, kFail), kRes);
} }
{ // Static member functions. { // Static member functions.
auto p0 = QPromise<float>::reject(kErr).fail(&Klass::kFnNoArg); auto p0 = QtPromise::QPromise<float>::reject(kErr).fail(&Klass::kFnNoArg);
auto p1 = QPromise<float>::reject(kErr).fail(&Klass::kFnArgByVal); auto p1 = QtPromise::QPromise<float>::reject(kErr).fail(&Klass::kFnArgByVal);
auto p2 = QPromise<float>::reject(kErr).fail(&Klass::kFnArgByRef); auto p2 = QtPromise::QPromise<float>::reject(kErr).fail(&Klass::kFnArgByRef);
QCOMPARE(waitForValue(p0, kFail), kRes); QCOMPARE(waitForValue(p0, kFail), kRes);
QCOMPARE(waitForValue(p1, kFail), kRes); QCOMPARE(waitForValue(p1, kFail), kRes);
@ -167,9 +165,9 @@ void tst_qpromise_fail::stdFunctionHandlers()
std::function<float(QString)> stdFnArgByVal = fnArgByVal; std::function<float(QString)> stdFnArgByVal = fnArgByVal;
std::function<float(const QString&)> stdFnArgByRef = fnArgByRef; std::function<float(const QString&)> stdFnArgByRef = fnArgByRef;
auto p0 = QPromise<float>::reject(kErr).fail(stdFnNoArg); auto p0 = QtPromise::QPromise<float>::reject(kErr).fail(stdFnNoArg);
auto p1 = QPromise<float>::reject(kErr).fail(stdFnArgByVal); auto p1 = QtPromise::QPromise<float>::reject(kErr).fail(stdFnArgByVal);
auto p2 = QPromise<float>::reject(kErr).fail(stdFnArgByRef); auto p2 = QtPromise::QPromise<float>::reject(kErr).fail(stdFnArgByRef);
QCOMPARE(waitForValue(p0, kFail), kRes); QCOMPARE(waitForValue(p0, kFail), kRes);
QCOMPARE(waitForValue(p1, kFail), kRes); QCOMPARE(waitForValue(p1, kFail), kRes);
@ -180,19 +178,20 @@ void tst_qpromise_fail::stdFunctionHandlers()
const std::function<float(QString)> stdFnArgByVal = fnArgByVal; const std::function<float(QString)> stdFnArgByVal = fnArgByVal;
const std::function<float(const QString&)> stdFnArgByRef = fnArgByRef; const std::function<float(const QString&)> stdFnArgByRef = fnArgByRef;
auto p0 = QPromise<float>::reject(kErr).fail(stdFnNoArg); auto p0 = QtPromise::QPromise<float>::reject(kErr).fail(stdFnNoArg);
auto p1 = QPromise<float>::reject(kErr).fail(stdFnArgByVal); auto p1 = QtPromise::QPromise<float>::reject(kErr).fail(stdFnArgByVal);
auto p2 = QPromise<float>::reject(kErr).fail(stdFnArgByRef); auto p2 = QtPromise::QPromise<float>::reject(kErr).fail(stdFnArgByRef);
QCOMPARE(waitForValue(p0, kFail), kRes); QCOMPARE(waitForValue(p0, kFail), kRes);
QCOMPARE(waitForValue(p1, kFail), kRes); QCOMPARE(waitForValue(p1, kFail), kRes);
QCOMPARE(waitForValue(p2, kFail), kRes); QCOMPARE(waitForValue(p2, kFail), kRes);
} }
{ // rvalue. { // rvalue.
auto p0 = QPromise<float>::reject(kErr).fail(std::function<float()>{fnNoArg}); auto p0 = QtPromise::QPromise<float>::reject(kErr).fail(std::function<float()>{fnNoArg});
auto p1 = QPromise<float>::reject(kErr).fail(std::function<float(QString)>{fnArgByVal}); auto p1 = QtPromise::QPromise<float>::reject(kErr).fail(
auto p2 = std::function<float(QString)>{fnArgByVal});
QPromise<float>::reject(kErr).fail(std::function<float(const QString&)>{fnArgByRef}); auto p2 = QtPromise::QPromise<float>::reject(kErr).fail(
std::function<float(const QString&)>{fnArgByRef});
QCOMPARE(waitForValue(p0, kFail), kRes); QCOMPARE(waitForValue(p0, kFail), kRes);
QCOMPARE(waitForValue(p1, kFail), kRes); QCOMPARE(waitForValue(p1, kFail), kRes);
@ -213,9 +212,9 @@ void tst_qpromise_fail::stdBindHandlers()
const std::function<float(const QString&)> bindArgByRef = const std::function<float(const QString&)> bindArgByRef =
std::bind(&Klass::fnArgByRef, &obj, _1); std::bind(&Klass::fnArgByRef, &obj, _1);
auto p0 = QPromise<float>::reject(kErr).fail(bindNoArg); auto p0 = QtPromise::QPromise<float>::reject(kErr).fail(bindNoArg);
auto p1 = QPromise<float>::reject(kErr).fail(bindArgByVal); auto p1 = QtPromise::QPromise<float>::reject(kErr).fail(bindArgByVal);
auto p2 = QPromise<float>::reject(kErr).fail(bindArgByRef); auto p2 = QtPromise::QPromise<float>::reject(kErr).fail(bindArgByRef);
QCOMPARE(waitForValue(p0, kFail), val); QCOMPARE(waitForValue(p0, kFail), val);
QCOMPARE(waitForValue(p1, kFail), val + kRes); QCOMPARE(waitForValue(p1, kFail), val + kRes);
@ -235,9 +234,9 @@ void tst_qpromise_fail::lambdaHandlers()
return v.toFloat(); return v.toFloat();
}; };
auto p0 = QPromise<float>::reject(kErr).fail(lambdaNoArg); auto p0 = QtPromise::QPromise<float>::reject(kErr).fail(lambdaNoArg);
auto p1 = QPromise<float>::reject(kErr).fail(lambdaArgByVal); auto p1 = QtPromise::QPromise<float>::reject(kErr).fail(lambdaArgByVal);
auto p2 = QPromise<float>::reject(kErr).fail(lambdaArgByRef); auto p2 = QtPromise::QPromise<float>::reject(kErr).fail(lambdaArgByRef);
QCOMPARE(waitForValue(p0, kFail), kRes); QCOMPARE(waitForValue(p0, kFail), kRes);
QCOMPARE(waitForValue(p1, kFail), kRes); QCOMPARE(waitForValue(p1, kFail), kRes);
@ -254,22 +253,22 @@ void tst_qpromise_fail::lambdaHandlers()
return v.toFloat(); return v.toFloat();
}; };
auto p0 = QPromise<float>::reject(kErr).fail(lambdaNoArg); auto p0 = QtPromise::QPromise<float>::reject(kErr).fail(lambdaNoArg);
auto p1 = QPromise<float>::reject(kErr).fail(lambdaArgByVal); auto p1 = QtPromise::QPromise<float>::reject(kErr).fail(lambdaArgByVal);
auto p2 = QPromise<float>::reject(kErr).fail(lambdaArgByRef); auto p2 = QtPromise::QPromise<float>::reject(kErr).fail(lambdaArgByRef);
QCOMPARE(waitForValue(p0, kFail), kRes); QCOMPARE(waitForValue(p0, kFail), kRes);
QCOMPARE(waitForValue(p1, kFail), kRes); QCOMPARE(waitForValue(p1, kFail), kRes);
QCOMPARE(waitForValue(p2, kFail), kRes); QCOMPARE(waitForValue(p2, kFail), kRes);
} }
{ // rvalue. { // rvalue.
auto p0 = QPromise<float>::reject(kErr).fail([]() { auto p0 = QtPromise::QPromise<float>::reject(kErr).fail([]() {
return kRes; return kRes;
}); });
auto p1 = QPromise<float>::reject(kErr).fail([](QString v) { auto p1 = QtPromise::QPromise<float>::reject(kErr).fail([](QString v) {
return v.toFloat(); return v.toFloat();
}); });
auto p2 = QPromise<float>::reject(kErr).fail([](const QString& v) { auto p2 = QtPromise::QPromise<float>::reject(kErr).fail([](const QString& v) {
return v.toFloat(); return v.toFloat();
}); });

View File

@ -11,8 +11,6 @@
#include <QtPromise> #include <QtPromise>
#include <QtTest> #include <QtTest>
using namespace QtPromise;
class tst_qpromise_filter : public QObject class tst_qpromise_filter : public QObject
{ {
Q_OBJECT Q_OBJECT
@ -43,13 +41,13 @@ struct SequenceTester
return v > 42 && v < 51; return v > 42 && v < 51;
}) })
.filter([](int, int i) { .filter([](int, int i) {
return QPromise<bool>::resolve(i % 2 == 0); return QtPromise::QPromise<bool>::resolve(i % 2 == 0);
}) })
.filter([](int v, ...) { .filter([](int v, ...) {
return v != 45; return v != 45;
}); });
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<Sequence>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<Sequence>>::value));
QCOMPARE(waitForValue(p, Sequence{}), (Sequence{43, 47, 49})); QCOMPARE(waitForValue(p, Sequence{}), (Sequence{43, 47, 49}));
} }
}; };
@ -58,43 +56,43 @@ struct SequenceTester
void tst_qpromise_filter::emptySequence() void tst_qpromise_filter::emptySequence()
{ {
auto p = QPromise<QVector<int>>::resolve({}).filter([](int v, ...) { auto p = QtPromise::QPromise<QVector<int>>::resolve({}).filter([](int v, ...) {
return v % 2 == 0; return v % 2 == 0;
}); });
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<QVector<int>>>::value));
QCOMPARE(waitForValue(p, QVector<int>{}), QVector<int>{}); QCOMPARE(waitForValue(p, QVector<int>{}), QVector<int>{});
} }
void tst_qpromise_filter::filterValues() void tst_qpromise_filter::filterValues()
{ {
auto p = QPromise<QVector<int>>::resolve({42, 43, 44}).filter([](int v, ...) { auto p = QtPromise::QPromise<QVector<int>>::resolve({42, 43, 44}).filter([](int v, ...) {
return v % 2 == 0; return v % 2 == 0;
}); });
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<QVector<int>>>::value));
QCOMPARE(waitForValue(p, QVector<int>{}), (QVector<int>{42, 44})); QCOMPARE(waitForValue(p, QVector<int>{}), (QVector<int>{42, 44}));
} }
void tst_qpromise_filter::delayedFulfilled() void tst_qpromise_filter::delayedFulfilled()
{ {
auto p = QPromise<QVector<int>>::resolve({42, 43, 44}).filter([](int v, ...) { auto p = QtPromise::QPromise<QVector<int>>::resolve({42, 43, 44}).filter([](int v, ...) {
return QPromise<bool>{[&](const QPromiseResolve<bool>& resolve) { return QtPromise::QPromise<bool>{[&](const QtPromise::QPromiseResolve<bool>& resolve) {
QtPromisePrivate::qtpromise_defer([=]() { QtPromisePrivate::qtpromise_defer([=]() {
resolve(v % 2 == 0); resolve(v % 2 == 0);
}); });
}}; }};
}); });
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<QVector<int>>>::value));
QCOMPARE(waitForValue(p, QVector<int>{}), (QVector<int>{42, 44})); QCOMPARE(waitForValue(p, QVector<int>{}), (QVector<int>{42, 44}));
} }
void tst_qpromise_filter::delayedRejected() void tst_qpromise_filter::delayedRejected()
{ {
auto p = QPromise<QVector<int>>::resolve({42, 43, 44}).filter([](int v, ...) { auto p = QtPromise::QPromise<QVector<int>>::resolve({42, 43, 44}).filter([](int v, ...) {
return QPromise<bool>{ return QtPromise::QPromise<bool>{[&](const QtPromise::QPromiseResolve<bool>& resolve,
[&](const QPromiseResolve<bool>& resolve, const QPromiseReject<bool>& reject) { const QtPromise::QPromiseReject<bool>& reject) {
QtPromisePrivate::qtpromise_defer([=]() { QtPromisePrivate::qtpromise_defer([=]() {
if (v == 43) { if (v == 43) {
reject(QString{"foo"}); reject(QString{"foo"});
@ -104,32 +102,32 @@ void tst_qpromise_filter::delayedRejected()
}}; }};
}); });
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<QVector<int>>>::value));
QCOMPARE(waitForError(p, QString{}), QString{"foo"}); QCOMPARE(waitForError(p, QString{}), QString{"foo"});
} }
void tst_qpromise_filter::functorThrows() void tst_qpromise_filter::functorThrows()
{ {
auto p = QPromise<QVector<int>>::resolve({42, 43, 44}).filter([](int v, ...) { auto p = QtPromise::QPromise<QVector<int>>::resolve({42, 43, 44}).filter([](int v, ...) {
if (v == 43) { if (v == 43) {
throw QString{"foo"}; throw QString{"foo"};
} }
return true; return true;
}); });
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<QVector<int>>>::value));
QCOMPARE(waitForError(p, QString{}), QString{"foo"}); QCOMPARE(waitForError(p, QString{}), QString{"foo"});
} }
void tst_qpromise_filter::functorArguments() void tst_qpromise_filter::functorArguments()
{ {
QMap<int, int> args; QMap<int, int> args;
auto p = QPromise<QVector<int>>::resolve({42, 43, 44}).filter([&](int v, int i) { auto p = QtPromise::QPromise<QVector<int>>::resolve({42, 43, 44}).filter([&](int v, int i) {
args[v] = i; args[v] = i;
return i % 2 == 0; return i % 2 == 0;
}); });
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<QVector<int>>>::value));
QCOMPARE(waitForValue(p, QVector<int>{}), (QVector<int>{42, 44})); QCOMPARE(waitForValue(p, QVector<int>{}), (QVector<int>{42, 44}));
QMap<int, int> expected{{42, 0}, {43, 1}, {44, 2}}; QMap<int, int> expected{{42, 0}, {43, 1}, {44, 2}};
QCOMPARE(args, expected); QCOMPARE(args, expected);
@ -137,11 +135,12 @@ void tst_qpromise_filter::functorArguments()
void tst_qpromise_filter::preserveOrder() void tst_qpromise_filter::preserveOrder()
{ {
auto p = QPromise<QVector<int>>::resolve({250, 50, 100, 400, 300}).filter([](int v, ...) { auto p =
return QPromise<bool>::resolve(v > 200).delay(v); QtPromise::QPromise<QVector<int>>::resolve({250, 50, 100, 400, 300}).filter([](int v, ...) {
return QtPromise::QPromise<bool>::resolve(v > 200).delay(v);
}); });
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<QVector<int>>>::value));
QCOMPARE(waitForValue(p, QVector<int>{}), (QVector<int>{250, 400, 300})); QCOMPARE(waitForValue(p, QVector<int>{}), (QVector<int>{250, 400, 300}));
} }

View File

@ -10,8 +10,6 @@
#include <QtPromise> #include <QtPromise>
#include <QtTest> #include <QtTest>
using namespace QtPromise;
class tst_qpromise_finally : public QObject class tst_qpromise_finally : public QObject
{ {
Q_OBJECT Q_OBJECT
@ -37,12 +35,12 @@ QTEST_MAIN(tst_qpromise_finally)
void tst_qpromise_finally::fulfilledSync() void tst_qpromise_finally::fulfilledSync()
{ {
int value = -1; int value = -1;
auto p = QPromise<int>::resolve(42).finally([&]() { auto p = QtPromise::QPromise<int>::resolve(42).finally([&]() {
value = 8; value = 8;
return 16; // ignored! return 16; // ignored!
}); });
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<int>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<int>>::value));
QCOMPARE(waitForValue(p, -1), 42); QCOMPARE(waitForValue(p, -1), 42);
QCOMPARE(p.isFulfilled(), true); QCOMPARE(p.isFulfilled(), true);
QCOMPARE(value, 8); QCOMPARE(value, 8);
@ -51,12 +49,12 @@ void tst_qpromise_finally::fulfilledSync()
void tst_qpromise_finally::fulfilledSync_void() void tst_qpromise_finally::fulfilledSync_void()
{ {
int value = -1; int value = -1;
auto p = QPromise<void>::resolve().finally([&]() { auto p = QtPromise::QPromise<void>::resolve().finally([&]() {
value = 8; value = 8;
return 16; // ignored! return 16; // ignored!
}); });
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<void>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<void>>::value));
QCOMPARE(waitForValue(p, -1, 42), 42); QCOMPARE(waitForValue(p, -1, 42), 42);
QCOMPARE(p.isFulfilled(), true); QCOMPARE(p.isFulfilled(), true);
QCOMPARE(value, 8); QCOMPARE(value, 8);
@ -64,22 +62,22 @@ void tst_qpromise_finally::fulfilledSync_void()
void tst_qpromise_finally::fulfilledThrows() void tst_qpromise_finally::fulfilledThrows()
{ {
auto p = QPromise<int>::resolve(42).finally([&]() { auto p = QtPromise::QPromise<int>::resolve(42).finally([&]() {
throw QString{"bar"}; throw QString{"bar"};
}); });
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<int>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<int>>::value));
QCOMPARE(waitForError(p, QString{}), QString{"bar"}); QCOMPARE(waitForError(p, QString{}), QString{"bar"});
QCOMPARE(p.isRejected(), true); QCOMPARE(p.isRejected(), true);
} }
void tst_qpromise_finally::fulfilledThrows_void() void tst_qpromise_finally::fulfilledThrows_void()
{ {
auto p = QPromise<void>::resolve().finally([&]() { auto p = QtPromise::QPromise<void>::resolve().finally([&]() {
throw QString{"bar"}; throw QString{"bar"};
}); });
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<void>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<void>>::value));
QCOMPARE(waitForError(p, QString{}), QString{"bar"}); QCOMPARE(waitForError(p, QString{}), QString{"bar"});
QCOMPARE(p.isRejected(), true); QCOMPARE(p.isRejected(), true);
} }
@ -87,8 +85,8 @@ void tst_qpromise_finally::fulfilledThrows_void()
void tst_qpromise_finally::fulfilledAsyncResolve() void tst_qpromise_finally::fulfilledAsyncResolve()
{ {
QVector<int> values; QVector<int> values;
auto p = QPromise<int>::resolve(42).finally([&]() { auto p = QtPromise::QPromise<int>::resolve(42).finally([&]() {
QPromise<int> p{[&](const QPromiseResolve<int>& resolve) { QtPromise::QPromise<int> p{[&](const QtPromise::QPromiseResolve<int>& resolve) {
QtPromisePrivate::qtpromise_defer([=, &values]() { QtPromisePrivate::qtpromise_defer([=, &values]() {
values << 64; values << 64;
resolve(16); // ignored! resolve(16); // ignored!
@ -106,8 +104,9 @@ void tst_qpromise_finally::fulfilledAsyncResolve()
void tst_qpromise_finally::fulfilledAsyncReject() void tst_qpromise_finally::fulfilledAsyncReject()
{ {
auto p = QPromise<int>::resolve(42).finally([]() { auto p = QtPromise::QPromise<int>::resolve(42).finally([]() {
return QPromise<int>{[](const QPromiseResolve<int>&, const QPromiseReject<int>& reject) { return QtPromise::QPromise<int>{[](const QtPromise::QPromiseResolve<int>&,
const QtPromise::QPromiseReject<int>& reject) {
QtPromisePrivate::qtpromise_defer([=]() { QtPromisePrivate::qtpromise_defer([=]() {
reject(QString{"bar"}); reject(QString{"bar"});
}); });
@ -121,12 +120,12 @@ void tst_qpromise_finally::fulfilledAsyncReject()
void tst_qpromise_finally::rejectedSync() void tst_qpromise_finally::rejectedSync()
{ {
int value = -1; int value = -1;
auto p = QPromise<int>::reject(QString{"foo"}).finally([&]() { auto p = QtPromise::QPromise<int>::reject(QString{"foo"}).finally([&]() {
value = 8; value = 8;
return 16; // ignored! return 16; // ignored!
}); });
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<int>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<int>>::value));
QCOMPARE(waitForError(p, QString{}), QString{"foo"}); QCOMPARE(waitForError(p, QString{}), QString{"foo"});
QCOMPARE(p.isRejected(), true); QCOMPARE(p.isRejected(), true);
QCOMPARE(value, 8); QCOMPARE(value, 8);
@ -135,12 +134,12 @@ void tst_qpromise_finally::rejectedSync()
void tst_qpromise_finally::rejectedSync_void() void tst_qpromise_finally::rejectedSync_void()
{ {
int value = -1; int value = -1;
auto p = QPromise<void>::reject(QString{"foo"}).finally([&]() { auto p = QtPromise::QPromise<void>::reject(QString{"foo"}).finally([&]() {
value = 8; value = 8;
return 16; // ignored! return 16; // ignored!
}); });
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<void>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<void>>::value));
QCOMPARE(waitForError(p, QString{}), QString{"foo"}); QCOMPARE(waitForError(p, QString{}), QString{"foo"});
QCOMPARE(p.isRejected(), true); QCOMPARE(p.isRejected(), true);
QCOMPARE(value, 8); QCOMPARE(value, 8);
@ -148,22 +147,22 @@ void tst_qpromise_finally::rejectedSync_void()
void tst_qpromise_finally::rejectedThrows() void tst_qpromise_finally::rejectedThrows()
{ {
auto p = QPromise<int>::reject(QString{"foo"}).finally([&]() { auto p = QtPromise::QPromise<int>::reject(QString{"foo"}).finally([&]() {
throw QString{"bar"}; throw QString{"bar"};
}); });
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<int>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<int>>::value));
QCOMPARE(waitForError(p, QString{}), QString{"bar"}); QCOMPARE(waitForError(p, QString{}), QString{"bar"});
QCOMPARE(p.isRejected(), true); QCOMPARE(p.isRejected(), true);
} }
void tst_qpromise_finally::rejectedThrows_void() void tst_qpromise_finally::rejectedThrows_void()
{ {
auto p = QPromise<void>::reject(QString{"foo"}).finally([&]() { auto p = QtPromise::QPromise<void>::reject(QString{"foo"}).finally([&]() {
throw QString{"bar"}; throw QString{"bar"};
}); });
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<void>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<void>>::value));
QCOMPARE(waitForError(p, QString{}), QString{"bar"}); QCOMPARE(waitForError(p, QString{}), QString{"bar"});
QCOMPARE(p.isRejected(), true); QCOMPARE(p.isRejected(), true);
} }
@ -171,8 +170,8 @@ void tst_qpromise_finally::rejectedThrows_void()
void tst_qpromise_finally::rejectedAsyncResolve() void tst_qpromise_finally::rejectedAsyncResolve()
{ {
QVector<int> values; QVector<int> values;
auto p = QPromise<int>::reject(QString{"foo"}).finally([&]() { auto p = QtPromise::QPromise<int>::reject(QString{"foo"}).finally([&]() {
QPromise<int> p{[&](const QPromiseResolve<int>& resolve) { QtPromise::QPromise<int> p{[&](const QtPromise::QPromiseResolve<int>& resolve) {
QtPromisePrivate::qtpromise_defer([=, &values]() { QtPromisePrivate::qtpromise_defer([=, &values]() {
values << 64; values << 64;
resolve(16); // ignored! resolve(16); // ignored!
@ -194,8 +193,9 @@ void tst_qpromise_finally::rejectedAsyncResolve()
void tst_qpromise_finally::rejectedAsyncReject() void tst_qpromise_finally::rejectedAsyncReject()
{ {
auto p = QPromise<int>::reject(QString{"foo"}).finally([]() { auto p = QtPromise::QPromise<int>::reject(QString{"foo"}).finally([]() {
return QPromise<int>{[](const QPromiseResolve<int>&, const QPromiseReject<int>& reject) { return QtPromise::QPromise<int>{[](const QtPromise::QPromiseResolve<int>&,
const QtPromise::QPromiseReject<int>& reject) {
QtPromisePrivate::qtpromise_defer([=]() { QtPromisePrivate::qtpromise_defer([=]() {
reject(QString{"bar"}); reject(QString{"bar"});
}); });

View File

@ -10,8 +10,6 @@
#include <QtPromise> #include <QtPromise>
#include <QtTest> #include <QtTest>
using namespace QtPromise;
class tst_qpromise_map : public QObject class tst_qpromise_map : public QObject
{ {
Q_OBJECT Q_OBJECT
@ -52,7 +50,7 @@ struct SequenceTester
return QString::fromUtf8(v); return QString::fromUtf8(v);
}); });
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<QString>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<QVector<QString>>>::value));
QCOMPARE(waitForValue(p, QVector<QString>{}), QCOMPARE(waitForValue(p, QVector<QString>{}),
(QVector<QString>{"0:43!", "1:44!", "2:45!"})); (QVector<QString>{"0:43!", "1:44!", "2:45!"}));
} }
@ -66,7 +64,7 @@ void tst_qpromise_map::emptySequence()
return v + 1; return v + 1;
}); });
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<QVector<int>>>::value));
QCOMPARE(waitForValue(p, QVector<int>{}), QVector<int>{}); QCOMPARE(waitForValue(p, QVector<int>{}), QVector<int>{});
} }
@ -76,7 +74,7 @@ void tst_qpromise_map::modifyValues()
return v + 1; return v + 1;
}); });
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<QVector<int>>>::value));
QCOMPARE(waitForValue(p, QVector<int>{}), (QVector<int>{43, 44, 45})); QCOMPARE(waitForValue(p, QVector<int>{}), (QVector<int>{43, 44, 45}));
} }
@ -86,29 +84,29 @@ void tst_qpromise_map::convertValues()
return QString::number(v + 1); return QString::number(v + 1);
}); });
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<QString>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<QVector<QString>>>::value));
QCOMPARE(waitForValue(p, QVector<QString>{}), (QVector<QString>{"43", "44", "45"})); QCOMPARE(waitForValue(p, QVector<QString>{}), (QVector<QString>{"43", "44", "45"}));
} }
void tst_qpromise_map::delayedFulfilled() void tst_qpromise_map::delayedFulfilled()
{ {
auto p = QtPromise::resolve(QVector<int>{42, 43, 44}).map([](int v, ...) { auto p = QtPromise::resolve(QVector<int>{42, 43, 44}).map([](int v, ...) {
return QPromise<int>{[&](const QPromiseResolve<int>& resolve) { return QtPromise::QPromise<int>{[&](const QtPromise::QPromiseResolve<int>& resolve) {
QtPromisePrivate::qtpromise_defer([=]() { QtPromisePrivate::qtpromise_defer([=]() {
resolve(v + 1); resolve(v + 1);
}); });
}}; }};
}); });
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<QVector<int>>>::value));
QCOMPARE(waitForValue(p, QVector<int>{}), (QVector<int>{43, 44, 45})); QCOMPARE(waitForValue(p, QVector<int>{}), (QVector<int>{43, 44, 45}));
} }
void tst_qpromise_map::delayedRejected() void tst_qpromise_map::delayedRejected()
{ {
auto p = QtPromise::resolve(QVector<int>{42, 43, 44}).map([](int v, ...) { auto p = QtPromise::resolve(QVector<int>{42, 43, 44}).map([](int v, ...) {
return QPromise<int>{ return QtPromise::QPromise<int>{[&](const QtPromise::QPromiseResolve<int>& resolve,
[&](const QPromiseResolve<int>& resolve, const QPromiseReject<int>& reject) { const QtPromise::QPromiseReject<int>& reject) {
QtPromisePrivate::qtpromise_defer([=]() { QtPromisePrivate::qtpromise_defer([=]() {
if (v == 43) { if (v == 43) {
reject(QString{"foo"}); reject(QString{"foo"});
@ -118,7 +116,7 @@ void tst_qpromise_map::delayedRejected()
}}; }};
}); });
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<QVector<int>>>::value));
QCOMPARE(waitForError(p, QString{}), QString{"foo"}); QCOMPARE(waitForError(p, QString{}), QString{"foo"});
} }
@ -131,7 +129,7 @@ void tst_qpromise_map::functorThrows()
return v + 1; return v + 1;
}); });
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<QVector<int>>>::value));
QCOMPARE(waitForError(p, QString{}), QString{"foo"}); QCOMPARE(waitForError(p, QString{}), QString{"foo"});
} }
@ -141,7 +139,7 @@ void tst_qpromise_map::functorArguments()
return v * i; return v * i;
}); });
Q_STATIC_ASSERT((std::is_same<decltype(p1), QPromise<QVector<int>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p1), QtPromise::QPromise<QVector<int>>>::value));
QCOMPARE(waitForValue(p1, QVector<int>{}), (QVector<int>{0, 42, 84})); QCOMPARE(waitForValue(p1, QVector<int>{}), (QVector<int>{0, 42, 84}));
} }
@ -151,7 +149,7 @@ void tst_qpromise_map::preserveOrder()
return QtPromise::resolve(v + 1).delay(v); return QtPromise::resolve(v + 1).delay(v);
}); });
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QVector<int>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<QVector<int>>>::value));
QCOMPARE(waitForValue(p, QVector<int>{}), (QVector<int>{251, 501, 101})); QCOMPARE(waitForValue(p, QVector<int>{}), (QVector<int>{251, 501, 101}));
} }

View File

@ -10,8 +10,6 @@
#include <QtPromise> #include <QtPromise>
#include <QtTest> #include <QtTest>
using namespace QtPromise;
class tst_qpromise_operators : public QObject class tst_qpromise_operators : public QObject
{ {
Q_OBJECT Q_OBJECT
@ -34,12 +32,13 @@ QTEST_MAIN(tst_qpromise_operators)
void tst_qpromise_operators::move() void tst_qpromise_operators::move()
{ {
auto p0 = QPromise<int>::resolve(42); auto p0 = QtPromise::QPromise<int>::resolve(42);
QCOMPARE(p0.isFulfilled(), true); QCOMPARE(p0.isFulfilled(), true);
QCOMPARE(waitForValue(p0, -1), 42); QCOMPARE(waitForValue(p0, -1), 42);
p0 = QPromise<int>{[](const QPromiseResolve<int>&, const QPromiseReject<int>& reject) { p0 = QtPromise::QPromise<int>{
[](const QtPromise::QPromiseResolve<int>&, const QtPromise::QPromiseReject<int>& reject) {
QtPromisePrivate::qtpromise_defer([=]() { QtPromisePrivate::qtpromise_defer([=]() {
reject(QString{"foo"}); reject(QString{"foo"});
}); });
@ -48,7 +47,7 @@ void tst_qpromise_operators::move()
QCOMPARE(p0.isPending(), true); QCOMPARE(p0.isPending(), true);
QCOMPARE(waitForError(p0, QString{}), QString{"foo"}); QCOMPARE(waitForError(p0, QString{}), QString{"foo"});
p0 = QPromise<int>{[](const QPromiseResolve<int>& resolve) { p0 = QtPromise::QPromise<int>{[](const QtPromise::QPromiseResolve<int>& resolve) {
QtPromisePrivate::qtpromise_defer([=]() { QtPromisePrivate::qtpromise_defer([=]() {
resolve(43); resolve(43);
}); });
@ -60,12 +59,13 @@ void tst_qpromise_operators::move()
void tst_qpromise_operators::move_void() void tst_qpromise_operators::move_void()
{ {
auto p0 = QPromise<void>::resolve(); auto p0 = QtPromise::QPromise<void>::resolve();
QCOMPARE(p0.isFulfilled(), true); QCOMPARE(p0.isFulfilled(), true);
QCOMPARE(waitForValue(p0, -1, 42), 42); QCOMPARE(waitForValue(p0, -1, 42), 42);
p0 = QPromise<void>{[](const QPromiseResolve<void>&, const QPromiseReject<void>& reject) { p0 = QtPromise::QPromise<void>{
[](const QtPromise::QPromiseResolve<void>&, const QtPromise::QPromiseReject<void>& reject) {
QtPromisePrivate::qtpromise_defer([=]() { QtPromisePrivate::qtpromise_defer([=]() {
reject(QString{"foo"}); reject(QString{"foo"});
}); });
@ -74,7 +74,7 @@ void tst_qpromise_operators::move_void()
QCOMPARE(p0.isPending(), true); QCOMPARE(p0.isPending(), true);
QCOMPARE(waitForError(p0, QString{}), QString{"foo"}); QCOMPARE(waitForError(p0, QString{}), QString{"foo"});
p0 = QPromise<void>{[](const QPromiseResolve<void>& resolve) { p0 = QtPromise::QPromise<void>{[](const QtPromise::QPromiseResolve<void>& resolve) {
QtPromisePrivate::qtpromise_defer([=]() { QtPromisePrivate::qtpromise_defer([=]() {
resolve(); resolve();
}); });
@ -86,13 +86,14 @@ void tst_qpromise_operators::move_void()
void tst_qpromise_operators::copy() void tst_qpromise_operators::copy()
{ {
auto p0 = QPromise<int>{[](const QPromiseResolve<int>&, const QPromiseReject<int>& reject) { auto p0 = QtPromise::QPromise<int>{
[](const QtPromise::QPromiseResolve<int>&, const QtPromise::QPromiseReject<int>& reject) {
QtPromisePrivate::qtpromise_defer([=]() { QtPromisePrivate::qtpromise_defer([=]() {
reject(QString{"foo"}); reject(QString{"foo"});
}); });
}}; }};
auto p1 = QPromise<int>{[](const QPromiseResolve<int>& resolve) { auto p1 = QtPromise::QPromise<int>{[](const QtPromise::QPromiseResolve<int>& resolve) {
QtPromisePrivate::qtpromise_defer([=]() { QtPromisePrivate::qtpromise_defer([=]() {
resolve(42); resolve(42);
}); });
@ -113,13 +114,14 @@ void tst_qpromise_operators::copy()
void tst_qpromise_operators::copy_void() void tst_qpromise_operators::copy_void()
{ {
auto p0 = QPromise<void>{[](const QPromiseResolve<void>&, const QPromiseReject<void>& reject) { auto p0 = QtPromise::QPromise<void>{
[](const QtPromise::QPromiseResolve<void>&, const QtPromise::QPromiseReject<void>& reject) {
QtPromisePrivate::qtpromise_defer([=]() { QtPromisePrivate::qtpromise_defer([=]() {
reject(QString{"foo"}); reject(QString{"foo"});
}); });
}}; }};
auto p1 = QPromise<void>{[](const QPromiseResolve<void>& resolve) { auto p1 = QtPromise::QPromise<void>{[](const QtPromise::QPromiseResolve<void>& resolve) {
QtPromisePrivate::qtpromise_defer([=]() { QtPromisePrivate::qtpromise_defer([=]() {
resolve(); resolve();
}); });
@ -143,8 +145,8 @@ void tst_qpromise_operators::copy_void()
void tst_qpromise_operators::equalTo() void tst_qpromise_operators::equalTo()
{ {
auto p0 = QPromise<int>::resolve(42); auto p0 = QtPromise::QPromise<int>::resolve(42);
auto p1 = QPromise<int>::resolve(42); auto p1 = QtPromise::QPromise<int>::resolve(42);
auto p2 = p1; auto p2 = p1;
auto p3(p2); auto p3(p2);
@ -158,8 +160,8 @@ void tst_qpromise_operators::equalTo()
void tst_qpromise_operators::equalTo_void() void tst_qpromise_operators::equalTo_void()
{ {
auto p0 = QPromise<void>::resolve(); auto p0 = QtPromise::QPromise<void>::resolve();
auto p1 = QPromise<void>::resolve(); auto p1 = QtPromise::QPromise<void>::resolve();
auto p2 = p1; auto p2 = p1;
auto p3(p2); auto p3(p2);
@ -173,8 +175,8 @@ void tst_qpromise_operators::equalTo_void()
void tst_qpromise_operators::notEqualTo() void tst_qpromise_operators::notEqualTo()
{ {
auto p0 = QPromise<int>::resolve(42); auto p0 = QtPromise::QPromise<int>::resolve(42);
auto p1 = QPromise<int>::resolve(42); auto p1 = QtPromise::QPromise<int>::resolve(42);
auto p2 = p1; auto p2 = p1;
auto p3(p2); auto p3(p2);
@ -188,8 +190,8 @@ void tst_qpromise_operators::notEqualTo()
void tst_qpromise_operators::notEqualTo_void() void tst_qpromise_operators::notEqualTo_void()
{ {
auto p0 = QPromise<void>::resolve(); auto p0 = QtPromise::QPromise<void>::resolve();
auto p1 = QPromise<void>::resolve(); auto p1 = QtPromise::QPromise<void>::resolve();
auto p2 = p1; auto p2 = p1;
auto p3(p2); auto p3(p2);
@ -203,10 +205,10 @@ void tst_qpromise_operators::notEqualTo_void()
void tst_qpromise_operators::chaining() void tst_qpromise_operators::chaining()
{ {
auto p = QPromise<int>::resolve(1); auto p = QtPromise::QPromise<int>::resolve(1);
for (int i = 0; i < 4; ++i) { for (int i = 0; i < 4; ++i) {
p = p.then([](int res) { p = p.then([](int res) {
return QPromise<int>::resolve(res * 2); return QtPromise::QPromise<int>::resolve(res * 2);
}); });
} }
@ -218,12 +220,12 @@ void tst_qpromise_operators::chaining_void()
{ {
QVector<int> values; QVector<int> values;
auto p = QPromise<void>::resolve(); auto p = QtPromise::QPromise<void>::resolve();
for (int i = 0; i < 4; ++i) { for (int i = 0; i < 4; ++i) {
p = p.then([i, &values]() { p = p.then([i, &values]() {
values.append(i * 2); values.append(i * 2);
return QPromise<void>::resolve(); return QtPromise::QPromise<void>::resolve();
}); });
} }

View File

@ -11,8 +11,6 @@
#include <QtPromise> #include <QtPromise>
#include <QtTest> #include <QtTest>
using namespace QtPromise;
class tst_qpromise_reduce : public QObject class tst_qpromise_reduce : public QObject
{ {
Q_OBJECT Q_OBJECT
@ -56,8 +54,8 @@ struct SequenceTester
}, },
QtPromise::resolve(2).delay(100)); QtPromise::resolve(2).delay(100));
Q_STATIC_ASSERT((std::is_same<decltype(p0), QPromise<int>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p0), QtPromise::QPromise<int>>::value));
Q_STATIC_ASSERT((std::is_same<decltype(p1), QPromise<int>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p1), QtPromise::QPromise<int>>::value));
QCOMPARE(p0.isPending(), true); QCOMPARE(p0.isPending(), true);
QCOMPARE(p1.isPending(), true); QCOMPARE(p1.isPending(), true);
@ -84,7 +82,7 @@ void tst_qpromise_reduce::emptySequence()
// NOTE(SB): reduce() on an empty sequence without an initial value is an error! // NOTE(SB): reduce() on an empty sequence without an initial value is an error!
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<int>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<int>>::value));
QCOMPARE(waitForValue(p, -1), 42); QCOMPARE(waitForValue(p, -1), 42);
QCOMPARE(called, false); QCOMPARE(called, false);
@ -107,8 +105,8 @@ void tst_qpromise_reduce::regularValues()
}, },
2); 2);
Q_STATIC_ASSERT((std::is_same<decltype(p0), QPromise<int>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p0), QtPromise::QPromise<int>>::value));
Q_STATIC_ASSERT((std::is_same<decltype(p1), QPromise<int>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p1), QtPromise::QPromise<int>>::value));
QCOMPARE(p0.isPending(), true); QCOMPARE(p0.isPending(), true);
QCOMPARE(p1.isPending(), true); QCOMPARE(p1.isPending(), true);
@ -120,7 +118,7 @@ void tst_qpromise_reduce::regularValues()
void tst_qpromise_reduce::promiseValues() void tst_qpromise_reduce::promiseValues()
{ {
QVector<QPromise<int>> inputs{QtPromise::resolve(4).delay(400), QVector<QtPromise::QPromise<int>> inputs{QtPromise::resolve(4).delay(400),
QtPromise::resolve(6).delay(300), QtPromise::resolve(6).delay(300),
QtPromise::resolve(8).delay(200)}; QtPromise::resolve(8).delay(200)};
QVector<int> v0; QVector<int> v0;
@ -137,8 +135,8 @@ void tst_qpromise_reduce::promiseValues()
}, },
2); 2);
Q_STATIC_ASSERT((std::is_same<decltype(p0), QPromise<int>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p0), QtPromise::QPromise<int>>::value));
Q_STATIC_ASSERT((std::is_same<decltype(p1), QPromise<int>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p1), QtPromise::QPromise<int>>::value));
QCOMPARE(p0.isPending(), true); QCOMPARE(p0.isPending(), true);
QCOMPARE(p1.isPending(), true); QCOMPARE(p1.isPending(), true);
@ -160,7 +158,7 @@ void tst_qpromise_reduce::convertResultType()
// NOTE(SB): when no initial value is given, the result type is the sequence type. // NOTE(SB): when no initial value is given, the result type is the sequence type.
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QString>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<QString>>::value));
QCOMPARE(p.isPending(), true); QCOMPARE(p.isPending(), true);
QCOMPARE(waitForValue(p, QString{}), QString{"foo:4:0:6:1:8:2"}); QCOMPARE(waitForValue(p, QString{}), QString{"foo:4:0:6:1:8:2"});
@ -178,7 +176,7 @@ void tst_qpromise_reduce::delayedInitialValue()
}, },
QtPromise::resolve(2).delay(100)); QtPromise::resolve(2).delay(100));
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<int>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<int>>::value));
QCOMPARE(p.isPending(), true); QCOMPARE(p.isPending(), true);
QCOMPARE(waitForValue(p, -1), 23); QCOMPARE(waitForValue(p, -1), 23);
@ -202,8 +200,8 @@ void tst_qpromise_reduce::delayedFulfilled()
}, },
2); 2);
Q_STATIC_ASSERT((std::is_same<decltype(p0), QPromise<int>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p0), QtPromise::QPromise<int>>::value));
Q_STATIC_ASSERT((std::is_same<decltype(p1), QPromise<int>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p1), QtPromise::QPromise<int>>::value));
QCOMPARE(p0.isPending(), true); QCOMPARE(p0.isPending(), true);
QCOMPARE(p1.isPending(), true); QCOMPARE(p1.isPending(), true);
@ -222,7 +220,7 @@ void tst_qpromise_reduce::delayedRejected()
auto p0 = QtPromise::resolve(inputs).reduce([&](int acc, int cur, int idx) { auto p0 = QtPromise::resolve(inputs).reduce([&](int acc, int cur, int idx) {
v0 << acc << cur << idx; v0 << acc << cur << idx;
if (cur == 6) { if (cur == 6) {
return QPromise<int>::reject(QString{"foo"}); return QtPromise::QPromise<int>::reject(QString{"foo"});
} }
return QtPromise::resolve(acc + cur + idx); return QtPromise::resolve(acc + cur + idx);
}); });
@ -230,14 +228,14 @@ void tst_qpromise_reduce::delayedRejected()
[&](int acc, int cur, int idx) { [&](int acc, int cur, int idx) {
v1 << acc << cur << idx; v1 << acc << cur << idx;
if (cur == 6) { if (cur == 6) {
return QPromise<int>::reject(QString{"bar"}); return QtPromise::QPromise<int>::reject(QString{"bar"});
} }
return QtPromise::resolve(acc + cur + idx); return QtPromise::resolve(acc + cur + idx);
}, },
2); 2);
Q_STATIC_ASSERT((std::is_same<decltype(p0), QPromise<int>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p0), QtPromise::QPromise<int>>::value));
Q_STATIC_ASSERT((std::is_same<decltype(p1), QPromise<int>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p1), QtPromise::QPromise<int>>::value));
QCOMPARE(p0.isPending(), true); QCOMPARE(p0.isPending(), true);
QCOMPARE(p1.isPending(), true); QCOMPARE(p1.isPending(), true);
@ -270,8 +268,8 @@ void tst_qpromise_reduce::functorThrows()
}, },
2); 2);
Q_STATIC_ASSERT((std::is_same<decltype(p0), QPromise<int>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p0), QtPromise::QPromise<int>>::value));
Q_STATIC_ASSERT((std::is_same<decltype(p1), QPromise<int>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p1), QtPromise::QPromise<int>>::value));
QCOMPARE(p0.isPending(), true); QCOMPARE(p0.isPending(), true);
QCOMPARE(p1.isPending(), true); QCOMPARE(p1.isPending(), true);
@ -285,10 +283,10 @@ void tst_qpromise_reduce::sequenceTypes()
{ {
#if (QT_VERSION < QT_VERSION_CHECK(5, 15, 0)) #if (QT_VERSION < QT_VERSION_CHECK(5, 15, 0))
// QLinkedList is deprecated since Qt 5.15. // QLinkedList is deprecated since Qt 5.15.
SequenceTester<QLinkedList<QPromise<int>>>::exec(); SequenceTester<QLinkedList<QtPromise::QPromise<int>>>::exec();
#endif #endif
SequenceTester<QList<QPromise<int>>>::exec(); SequenceTester<QList<QtPromise::QPromise<int>>>::exec();
SequenceTester<QVector<QPromise<int>>>::exec(); SequenceTester<QVector<QtPromise::QPromise<int>>>::exec();
SequenceTester<std::list<QPromise<int>>>::exec(); SequenceTester<std::list<QtPromise::QPromise<int>>>::exec();
SequenceTester<std::vector<QPromise<int>>>::exec(); SequenceTester<std::vector<QtPromise::QPromise<int>>>::exec();
} }

View File

@ -13,8 +13,6 @@
#include <memory> #include <memory>
using namespace QtPromise;
class tst_qpromise_resolve : public QObject class tst_qpromise_resolve : public QObject
{ {
Q_OBJECT Q_OBJECT
@ -36,13 +34,13 @@ void tst_qpromise_resolve::value()
int v0 = 42; int v0 = 42;
const int v1 = 42; const int v1 = 42;
auto p0 = QPromise<int>::resolve(42); auto p0 = QtPromise::QPromise<int>::resolve(42);
auto p1 = QPromise<int>::resolve(v0); auto p1 = QtPromise::QPromise<int>::resolve(v0);
auto p2 = QPromise<int>::resolve(v1); auto p2 = QtPromise::QPromise<int>::resolve(v1);
Q_STATIC_ASSERT((std::is_same<decltype(p0), QPromise<int>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p0), QtPromise::QPromise<int>>::value));
Q_STATIC_ASSERT((std::is_same<decltype(p1), QPromise<int>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p1), QtPromise::QPromise<int>>::value));
Q_STATIC_ASSERT((std::is_same<decltype(p2), QPromise<int>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p2), QtPromise::QPromise<int>>::value));
for (const auto& p : {p0, p1, p2}) { for (const auto& p : {p0, p1, p2}) {
QCOMPARE(p.isFulfilled(), true); QCOMPARE(p.isFulfilled(), true);
@ -54,9 +52,9 @@ void tst_qpromise_resolve::value()
void tst_qpromise_resolve::noValue() void tst_qpromise_resolve::noValue()
{ {
auto p = QPromise<void>::resolve(); auto p = QtPromise::QPromise<void>::resolve();
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<void>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<void>>::value));
QCOMPARE(p.isFulfilled(), true); QCOMPARE(p.isFulfilled(), true);
QCOMPARE(waitForValue(p, -1, 42), 42); QCOMPARE(waitForValue(p, -1, 42), 42);
@ -69,7 +67,7 @@ void tst_qpromise_resolve::moveRValue()
{ {
auto p = QtPromise::resolve(Data{42}).wait(); auto p = QtPromise::resolve(Data{42}).wait();
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<Data>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<Data>>::value));
} }
QCOMPARE(Data::logs().ctor, 1); QCOMPARE(Data::logs().ctor, 1);
@ -86,7 +84,7 @@ void tst_qpromise_resolve::copyLValue()
Data value{42}; Data value{42};
auto p = QtPromise::resolve(value).wait(); auto p = QtPromise::resolve(value).wait();
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<Data>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<Data>>::value));
} }
QCOMPARE(Data::logs().ctor, 1); QCOMPARE(Data::logs().ctor, 1);
@ -98,24 +96,26 @@ void tst_qpromise_resolve::copyLValue()
// https://github.com/simonbrunel/qtpromise/issues/6 // https://github.com/simonbrunel/qtpromise/issues/6
void tst_qpromise_resolve::qtSharedPtr() void tst_qpromise_resolve::qtSharedPtr()
{ {
using DataSPtr = QSharedPointer<Data>;
Data::logs().reset(); Data::logs().reset();
QWeakPointer<Data> wptr; QWeakPointer<Data> wptr;
{ {
auto sptr0 = QSharedPointer<Data>::create(42); auto sptr0 = DataSPtr::create(42);
const QSharedPointer<Data> sptr1 = sptr0; const DataSPtr sptr1 = sptr0;
auto p0 = QPromise<QSharedPointer<Data>>::resolve(QSharedPointer<Data>::create(42)); auto p0 = QtPromise::QPromise<DataSPtr>::resolve(DataSPtr::create(42));
auto p1 = QPromise<QSharedPointer<Data>>::resolve(sptr0); auto p1 = QtPromise::QPromise<DataSPtr>::resolve(sptr0);
auto p2 = QPromise<QSharedPointer<Data>>::resolve(sptr1); auto p2 = QtPromise::QPromise<DataSPtr>::resolve(sptr1);
Q_STATIC_ASSERT((std::is_same<decltype(p0), QPromise<QSharedPointer<Data>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p0), QtPromise::QPromise<DataSPtr>>::value));
Q_STATIC_ASSERT((std::is_same<decltype(p1), QPromise<QSharedPointer<Data>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p1), QtPromise::QPromise<DataSPtr>>::value));
Q_STATIC_ASSERT((std::is_same<decltype(p2), QPromise<QSharedPointer<Data>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p2), QtPromise::QPromise<DataSPtr>>::value));
QCOMPARE(waitForValue(p1, QSharedPointer<Data>{}), sptr0); QCOMPARE(waitForValue(p1, DataSPtr{}), sptr0);
QCOMPARE(waitForValue(p2, QSharedPointer<Data>{}), sptr1); QCOMPARE(waitForValue(p2, DataSPtr{}), sptr1);
wptr = sptr0; wptr = sptr0;
@ -134,24 +134,26 @@ void tst_qpromise_resolve::qtSharedPtr()
// https://github.com/simonbrunel/qtpromise/issues/6 // https://github.com/simonbrunel/qtpromise/issues/6
void tst_qpromise_resolve::stdSharedPtr() void tst_qpromise_resolve::stdSharedPtr()
{ {
using DataSPtr = std::shared_ptr<Data>;
Data::logs().reset(); Data::logs().reset();
std::weak_ptr<Data> wptr; std::weak_ptr<Data> wptr;
{ {
auto sptr0 = std::make_shared<Data>(42); auto sptr0 = std::make_shared<Data>(42);
const std::shared_ptr<Data> sptr1 = sptr0; const DataSPtr sptr1 = sptr0;
auto p0 = QPromise<std::shared_ptr<Data>>::resolve(std::make_shared<Data>(42)); auto p0 = QtPromise::QPromise<DataSPtr>::resolve(std::make_shared<Data>(42));
auto p1 = QPromise<std::shared_ptr<Data>>::resolve(sptr0); auto p1 = QtPromise::QPromise<DataSPtr>::resolve(sptr0);
auto p2 = QPromise<std::shared_ptr<Data>>::resolve(sptr1); auto p2 = QtPromise::QPromise<DataSPtr>::resolve(sptr1);
Q_STATIC_ASSERT((std::is_same<decltype(p0), QPromise<std::shared_ptr<Data>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p0), QtPromise::QPromise<DataSPtr>>::value));
Q_STATIC_ASSERT((std::is_same<decltype(p1), QPromise<std::shared_ptr<Data>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p1), QtPromise::QPromise<DataSPtr>>::value));
Q_STATIC_ASSERT((std::is_same<decltype(p2), QPromise<std::shared_ptr<Data>>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p2), QtPromise::QPromise<DataSPtr>>::value));
QCOMPARE(waitForValue(p1, std::shared_ptr<Data>{}), sptr0); QCOMPARE(waitForValue(p1, DataSPtr{}), sptr0);
QCOMPARE(waitForValue(p2, std::shared_ptr<Data>{}), sptr1); QCOMPARE(waitForValue(p2, DataSPtr{}), sptr1);
wptr = sptr0; wptr = sptr0;

View File

@ -10,8 +10,6 @@
#include <QtPromise> #include <QtPromise>
#include <QtTest> #include <QtTest>
using namespace QtPromise;
class tst_qpromise_tap : public QObject class tst_qpromise_tap : public QObject
{ {
Q_OBJECT Q_OBJECT
@ -33,7 +31,7 @@ QTEST_MAIN(tst_qpromise_tap)
void tst_qpromise_tap::fulfilledSync() void tst_qpromise_tap::fulfilledSync()
{ {
int value = -1; int value = -1;
auto p = QPromise<int>::resolve(42).tap([&](int res) { auto p = QtPromise::QPromise<int>::resolve(42).tap([&](int res) {
value = res + 1; value = res + 1;
return 8; return 8;
}); });
@ -46,7 +44,7 @@ void tst_qpromise_tap::fulfilledSync()
void tst_qpromise_tap::fulfilledSync_void() void tst_qpromise_tap::fulfilledSync_void()
{ {
int value = -1; int value = -1;
auto p = QPromise<void>::resolve().tap([&]() { auto p = QtPromise::QPromise<void>::resolve().tap([&]() {
value = 43; value = 43;
return 8; return 8;
}); });
@ -58,7 +56,7 @@ void tst_qpromise_tap::fulfilledSync_void()
void tst_qpromise_tap::fulfilledThrows() void tst_qpromise_tap::fulfilledThrows()
{ {
auto p = QPromise<int>::resolve(42).tap([&](int) { auto p = QtPromise::QPromise<int>::resolve(42).tap([&](int) {
throw QString{"foo"}; throw QString{"foo"};
}); });
@ -68,7 +66,7 @@ void tst_qpromise_tap::fulfilledThrows()
void tst_qpromise_tap::fulfilledThrows_void() void tst_qpromise_tap::fulfilledThrows_void()
{ {
auto p = QPromise<void>::resolve().tap([&]() { auto p = QtPromise::QPromise<void>::resolve().tap([&]() {
throw QString{"foo"}; throw QString{"foo"};
}); });
@ -79,8 +77,8 @@ void tst_qpromise_tap::fulfilledThrows_void()
void tst_qpromise_tap::fulfilledAsyncResolve() void tst_qpromise_tap::fulfilledAsyncResolve()
{ {
QVector<int> values; QVector<int> values;
auto p = QPromise<int>::resolve(1).tap([&](int) { auto p = QtPromise::QPromise<int>::resolve(1).tap([&](int) {
QPromise<int> p{[&](const QPromiseResolve<int>& resolve) { QtPromise::QPromise<int> p{[&](const QtPromise::QPromiseResolve<int>& resolve) {
QtPromisePrivate::qtpromise_defer([=, &values]() { QtPromisePrivate::qtpromise_defer([=, &values]() {
values << 3; values << 3;
resolve(4); // ignored! resolve(4); // ignored!
@ -102,8 +100,9 @@ void tst_qpromise_tap::fulfilledAsyncResolve()
void tst_qpromise_tap::fulfilledAsyncReject() void tst_qpromise_tap::fulfilledAsyncReject()
{ {
QVector<int> values; QVector<int> values;
auto p = QPromise<int>::resolve(1).tap([&](int) { auto p = QtPromise::QPromise<int>::resolve(1).tap([&](int) {
QPromise<int> p{[&](const QPromiseResolve<int>&, const QPromiseReject<int>& reject) { QtPromise::QPromise<int> p{[&](const QtPromise::QPromiseResolve<int>&,
const QtPromise::QPromiseReject<int>& reject) {
QtPromisePrivate::qtpromise_defer([=, &values]() { QtPromisePrivate::qtpromise_defer([=, &values]() {
values << 3; values << 3;
reject(QString{"foo"}); reject(QString{"foo"});
@ -126,7 +125,7 @@ void tst_qpromise_tap::fulfilledAsyncReject()
void tst_qpromise_tap::rejectedSync() void tst_qpromise_tap::rejectedSync()
{ {
int value = -1; int value = -1;
auto p = QPromise<int>::reject(QString{"foo"}).tap([&](int res) { auto p = QtPromise::QPromise<int>::reject(QString{"foo"}).tap([&](int res) {
value = res + 1; value = res + 1;
}); });
@ -138,7 +137,7 @@ void tst_qpromise_tap::rejectedSync()
void tst_qpromise_tap::rejectedSync_void() void tst_qpromise_tap::rejectedSync_void()
{ {
int value = -1; int value = -1;
auto p = QPromise<void>::reject(QString{"foo"}).tap([&]() { auto p = QtPromise::QPromise<void>::reject(QString{"foo"}).tap([&]() {
value = 43; value = 43;
}); });

View File

@ -10,8 +10,6 @@
#include <QtPromise> #include <QtPromise>
#include <QtTest> #include <QtTest>
using namespace QtPromise;
class tst_qpromise_tapfail : public QObject class tst_qpromise_tapfail : public QObject
{ {
Q_OBJECT Q_OBJECT
@ -33,7 +31,7 @@ QTEST_MAIN(tst_qpromise_tapfail)
void tst_qpromise_tapfail::fulfilled() void tst_qpromise_tapfail::fulfilled()
{ {
int value = -1; int value = -1;
auto p = QPromise<int>::resolve(42).tapFail([&]() { auto p = QtPromise::QPromise<int>::resolve(42).tapFail([&]() {
value = 43; value = 43;
}); });
@ -45,7 +43,7 @@ void tst_qpromise_tapfail::fulfilled()
void tst_qpromise_tapfail::fulfilled_void() void tst_qpromise_tapfail::fulfilled_void()
{ {
int value = -1; int value = -1;
auto p = QPromise<void>::resolve().tapFail([&]() { auto p = QtPromise::QPromise<void>::resolve().tapFail([&]() {
value = 43; value = 43;
}); });
@ -58,7 +56,7 @@ void tst_qpromise_tapfail::rejected()
{ {
QStringList errors; QStringList errors;
auto p0 = QPromise<int>::reject(QString{"foo"}).tapFail([&](const QString& err) { auto p0 = QtPromise::QPromise<int>::reject(QString{"foo"}).tapFail([&](const QString& err) {
errors << "1:" + err; errors << "1:" + err;
}); });
@ -78,7 +76,7 @@ void tst_qpromise_tapfail::rejected_void()
{ {
QStringList errors; QStringList errors;
auto p0 = QPromise<void>::reject(QString{"foo"}).tapFail([&](const QString& err) { auto p0 = QtPromise::QPromise<void>::reject(QString{"foo"}).tapFail([&](const QString& err) {
errors << "1:" + err; errors << "1:" + err;
}); });
@ -95,7 +93,7 @@ void tst_qpromise_tapfail::rejected_void()
void tst_qpromise_tapfail::throws() void tst_qpromise_tapfail::throws()
{ {
auto p = QPromise<int>::reject(QString{"foo"}).tapFail([&]() { auto p = QtPromise::QPromise<int>::reject(QString{"foo"}).tapFail([&]() {
throw QString{"bar"}; throw QString{"bar"};
}); });
@ -105,7 +103,7 @@ void tst_qpromise_tapfail::throws()
void tst_qpromise_tapfail::throws_void() void tst_qpromise_tapfail::throws_void()
{ {
auto p = QPromise<void>::reject(QString{"foo"}).tapFail([&]() { auto p = QtPromise::QPromise<void>::reject(QString{"foo"}).tapFail([&]() {
throw QString{"bar"}; throw QString{"bar"};
}); });
@ -116,8 +114,8 @@ void tst_qpromise_tapfail::throws_void()
void tst_qpromise_tapfail::delayedResolved() void tst_qpromise_tapfail::delayedResolved()
{ {
QVector<int> values; QVector<int> values;
auto p = QPromise<int>::reject(QString{"foo"}).tapFail([&]() { auto p = QtPromise::QPromise<int>::reject(QString{"foo"}).tapFail([&]() {
QPromise<void> p{[&](const QPromiseResolve<void>& resolve) { QtPromise::QPromise<void> p{[&](const QtPromise::QPromiseResolve<void>& resolve) {
QtPromisePrivate::qtpromise_defer([=, &values]() { QtPromisePrivate::qtpromise_defer([=, &values]() {
values << 3; values << 3;
resolve(); // ignored! resolve(); // ignored!
@ -135,8 +133,9 @@ void tst_qpromise_tapfail::delayedResolved()
void tst_qpromise_tapfail::delayedRejected() void tst_qpromise_tapfail::delayedRejected()
{ {
QVector<int> values; QVector<int> values;
auto p = QPromise<int>::reject(QString{"foo"}).tapFail([&]() { auto p = QtPromise::QPromise<int>::reject(QString{"foo"}).tapFail([&]() {
QPromise<void> p{[&](const QPromiseResolve<void>&, const QPromiseReject<void>& reject) { QtPromise::QPromise<void> p{[&](const QtPromise::QPromiseResolve<void>&,
const QtPromise::QPromiseReject<void>& reject) {
QtPromisePrivate::qtpromise_defer([=, &values]() { QtPromisePrivate::qtpromise_defer([=, &values]() {
values << 3; values << 3;
reject(QString{"bar"}); reject(QString{"bar"});

View File

@ -12,8 +12,6 @@
#include <functional> #include <functional>
using namespace QtPromise;
class tst_qpromise_then : public QObject class tst_qpromise_then : public QObject
{ {
Q_OBJECT Q_OBJECT
@ -76,7 +74,7 @@ void tst_qpromise_then::resolveSync()
{ {
QVariantList values; QVariantList values;
auto input = QPromise<int>::resolve(42); auto input = QtPromise::QPromise<int>::resolve(42);
auto output = input.then([&](int res) { auto output = input.then([&](int res) {
values << res; values << res;
return QString::number(res + 1); return QString::number(res + 1);
@ -98,22 +96,23 @@ void tst_qpromise_then::resolveSync()
void tst_qpromise_then::resolveAsync() void tst_qpromise_then::resolveAsync()
{ {
auto p = QPromise<int>::resolve(42).then([](int res) { auto p = QtPromise::QPromise<int>::resolve(42).then([](int res) {
return QPromise<QString>{[=](const QPromiseResolve<QString>& resolve) { return QtPromise::QPromise<QString>{
[=](const QtPromise::QPromiseResolve<QString>& resolve) {
QtPromisePrivate::qtpromise_defer([=]() { QtPromisePrivate::qtpromise_defer([=]() {
resolve(QString{"foo%1"}.arg(res)); resolve(QString{"foo%1"}.arg(res));
}); });
}}; }};
}); });
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<QString>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<QString>>::value));
QCOMPARE(waitForValue(p, QString{}), QString{"foo42"}); QCOMPARE(waitForValue(p, QString{}), QString{"foo42"});
QCOMPARE(p.isFulfilled(), true); QCOMPARE(p.isFulfilled(), true);
} }
void tst_qpromise_then::rejectSync() void tst_qpromise_then::rejectSync()
{ {
auto input = QPromise<int>::resolve(42); auto input = QtPromise::QPromise<int>::resolve(42);
auto output = input.then([](int res) { auto output = input.then([](int res) {
throw QString{"foo%1"}.arg(res); throw QString{"foo%1"}.arg(res);
return 42; return 42;
@ -136,43 +135,43 @@ void tst_qpromise_then::rejectSync()
void tst_qpromise_then::rejectAsync() void tst_qpromise_then::rejectAsync()
{ {
auto p = QPromise<int>::resolve(42).then([](int res) { auto p = QtPromise::QPromise<int>::resolve(42).then([](int res) {
return QPromise<void>{ return QtPromise::QPromise<void>{[=](const QtPromise::QPromiseResolve<void>&,
[=](const QPromiseResolve<void>&, const QPromiseReject<void>& reject) { const QtPromise::QPromiseReject<void>& reject) {
QtPromisePrivate::qtpromise_defer([=]() { QtPromisePrivate::qtpromise_defer([=]() {
reject(QString{"foo%1"}.arg(res)); reject(QString{"foo%1"}.arg(res));
}); });
}}; }};
}); });
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<void>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<void>>::value));
QCOMPARE(waitForError(p, QString{}), QString{"foo42"}); QCOMPARE(waitForError(p, QString{}), QString{"foo42"});
QCOMPARE(p.isRejected(), true); QCOMPARE(p.isRejected(), true);
} }
void tst_qpromise_then::skipResult() void tst_qpromise_then::skipResult()
{ {
auto p = QPromise<int>::resolve(42); auto p = QtPromise::QPromise<int>::resolve(42);
int value = -1; int value = -1;
p.then([&]() { p.then([&]() {
value = 43; value = 43;
}).wait(); }).wait();
Q_STATIC_ASSERT((std::is_same<decltype(p), QPromise<int>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p), QtPromise::QPromise<int>>::value));
QCOMPARE(value, 43); QCOMPARE(value, 43);
} }
void tst_qpromise_then::nullHandler() void tst_qpromise_then::nullHandler()
{ {
{ // resolved { // resolved
auto p = QPromise<int>::resolve(42).then(nullptr); auto p = QtPromise::QPromise<int>::resolve(42).then(nullptr);
QCOMPARE(waitForValue(p, -1), 42); QCOMPARE(waitForValue(p, -1), 42);
QCOMPARE(p.isFulfilled(), true); QCOMPARE(p.isFulfilled(), true);
} }
{ // rejected { // rejected
auto p = QPromise<int>::reject(QString{"foo"}).then(nullptr); auto p = QtPromise::QPromise<int>::reject(QString{"foo"}).then(nullptr);
QCOMPARE(waitForError(p, QString{}), QString{"foo"}); QCOMPARE(waitForError(p, QString{}), QString{"foo"});
QCOMPARE(p.isRejected(), true); QCOMPARE(p.isRejected(), true);

View File

@ -12,8 +12,6 @@
#include <chrono> #include <chrono>
using namespace QtPromise;
class tst_qpromise_timeout : public QObject class tst_qpromise_timeout : public QObject
{ {
Q_OBJECT Q_OBJECT
@ -38,7 +36,7 @@ void tst_qpromise_timeout::fulfilled()
timer.start(); timer.start();
auto p = QPromise<int>{[](const QPromiseResolve<int>& resolve) { auto p = QtPromise::QPromise<int>{[](const QtPromise::QPromiseResolve<int>& resolve) {
QTimer::singleShot(1000, [=]() { QTimer::singleShot(1000, [=]() {
resolve(42); resolve(42);
}); });
@ -59,7 +57,8 @@ void tst_qpromise_timeout::rejected()
timer.start(); timer.start();
auto p = QPromise<int>{[](const QPromiseResolve<int>&, const QPromiseReject<int>& reject) { auto p = QtPromise::QPromise<int>{[](const QtPromise::QPromiseResolve<int>&,
const QtPromise::QPromiseReject<int>& reject) {
QTimer::singleShot(1000, [=]() { QTimer::singleShot(1000, [=]() {
reject(QString{"foo"}); reject(QString{"foo"});
}); });
@ -81,7 +80,7 @@ void tst_qpromise_timeout::timeout()
timer.start(); timer.start();
auto p = QPromise<int>{[](const QPromiseResolve<int>& resolve) { auto p = QtPromise::QPromise<int>{[](const QtPromise::QPromiseResolve<int>& resolve) {
QTimer::singleShot(4000, [=]() { QTimer::singleShot(4000, [=]() {
resolve(42); resolve(42);
}); });
@ -90,7 +89,7 @@ void tst_qpromise_timeout::timeout()
elapsed = timer.elapsed(); elapsed = timer.elapsed();
}); });
p.fail([&](const QPromiseTimeoutException&) { p.fail([&](const QtPromise::QPromiseTimeoutException&) {
failed = true; failed = true;
return -1; return -1;
}).wait(); }).wait();
@ -113,7 +112,7 @@ void tst_qpromise_timeout::fulfilledStdChrono()
timer.start(); timer.start();
auto p = QPromise<int>{[](const QPromiseResolve<int>& resolve) { auto p = QtPromise::QPromise<int>{[](const QtPromise::QPromiseResolve<int>& resolve) {
QTimer::singleShot(1000, [=]() { QTimer::singleShot(1000, [=]() {
resolve(42); resolve(42);
}); });
@ -134,7 +133,8 @@ void tst_qpromise_timeout::rejectedStdChrono()
timer.start(); timer.start();
auto p = QPromise<int>{[](const QPromiseResolve<int>&, const QPromiseReject<int>& reject) { auto p = QtPromise::QPromise<int>{[](const QtPromise::QPromiseResolve<int>&,
const QtPromise::QPromiseReject<int>& reject) {
QTimer::singleShot(1000, [=]() { QTimer::singleShot(1000, [=]() {
reject(QString{"foo"}); reject(QString{"foo"});
}); });
@ -156,7 +156,7 @@ void tst_qpromise_timeout::timeoutStdChrono()
timer.start(); timer.start();
auto p = QPromise<int>{[](const QPromiseResolve<int>& resolve) { auto p = QtPromise::QPromise<int>{[](const QtPromise::QPromiseResolve<int>& resolve) {
QTimer::singleShot(4000, [=]() { QTimer::singleShot(4000, [=]() {
resolve(42); resolve(42);
}); });
@ -165,7 +165,7 @@ void tst_qpromise_timeout::timeoutStdChrono()
elapsed = timer.elapsed(); elapsed = timer.elapsed();
}); });
p.fail([&](const QPromiseTimeoutException&) { p.fail([&](const QtPromise::QPromiseTimeoutException&) {
failed = true; failed = true;
return -1; return -1;
}).wait(); }).wait();

View File

@ -11,8 +11,6 @@
#include <QtPromise> #include <QtPromise>
#include <QtTest> #include <QtTest>
using namespace QtPromise;
class tst_qpromiseconnections : public QObject class tst_qpromiseconnections : public QObject
{ {
Q_OBJECT Q_OBJECT
@ -31,7 +29,7 @@ void tst_qpromiseconnections::connections()
{ {
Object sender; Object sender;
QPromiseConnections connections; QtPromise::QPromiseConnections connections;
QCOMPARE(sender.hasConnections(), false); QCOMPARE(sender.hasConnections(), false);
QCOMPARE(connections.count(), 0); QCOMPARE(connections.count(), 0);
@ -53,7 +51,7 @@ void tst_qpromiseconnections::destruction()
Object sender; Object sender;
{ {
QPromiseConnections connections; QtPromise::QPromiseConnections connections;
QCOMPARE(sender.hasConnections(), false); QCOMPARE(sender.hasConnections(), false);
QCOMPARE(connections.count(), 0); QCOMPARE(connections.count(), 0);
@ -67,7 +65,7 @@ void tst_qpromiseconnections::destruction()
void tst_qpromiseconnections::senderDestroyed() void tst_qpromiseconnections::senderDestroyed()
{ {
QPromiseConnections connections; QtPromise::QPromiseConnections connections;
QCOMPARE(connections.count(), 0); QCOMPARE(connections.count(), 0);
{ {

View File

@ -8,8 +8,6 @@
#include <QtPromise> #include <QtPromise>
#include <QtTest> #include <QtTest>
using namespace QtPromise;
// https://promisesaplus.com/#requirements // https://promisesaplus.com/#requirements
class tst_requirements : public QObject class tst_requirements : public QObject
{ {
@ -43,7 +41,7 @@ void tst_requirements::statePending()
// 2.1.1. When pending, a promise: // 2.1.1. When pending, a promise:
// 2.1.1.1. may transition to either the fulfilled state // 2.1.1.1. may transition to either the fulfilled state
{ {
QPromise<int> p{[&](const QPromiseResolve<int>& resolve) { QtPromise::QPromise<int> p{[&](const QtPromise::QPromiseResolve<int>& resolve) {
QtPromisePrivate::qtpromise_defer([=]() { QtPromisePrivate::qtpromise_defer([=]() {
resolve(42); resolve(42);
}); });
@ -62,7 +60,8 @@ void tst_requirements::statePending()
// 2.1.1.1. ... or the rejected state // 2.1.1.1. ... or the rejected state
{ {
QPromise<int> p{[&](const QPromiseResolve<int>&, const QPromiseReject<int>& reject) { QtPromise::QPromise<int> p{[&](const QtPromise::QPromiseResolve<int>&,
const QtPromise::QPromiseReject<int>& reject) {
QtPromisePrivate::qtpromise_defer([=]() { QtPromisePrivate::qtpromise_defer([=]() {
reject(QString{"foo"}); reject(QString{"foo"});
}); });
@ -86,7 +85,8 @@ void tst_requirements::stateFulfilled()
int value = -1; int value = -1;
// 2.1.2. When fulfilled, a promise: // 2.1.2. When fulfilled, a promise:
QPromise<int> p{[](const QPromiseResolve<int>& resolve, const QPromiseReject<int>& reject) { QtPromise::QPromise<int> p{[](const QtPromise::QPromiseResolve<int>& resolve,
const QtPromise::QPromiseReject<int>& reject) {
QtPromisePrivate::qtpromise_defer([=]() { QtPromisePrivate::qtpromise_defer([=]() {
// 2.1.2.2. must have a value, which must not change. // 2.1.2.2. must have a value, which must not change.
resolve(42); resolve(42);
@ -120,7 +120,8 @@ void tst_requirements::stateRejected()
int value = -1; int value = -1;
// 2.1.3 When rejected, a promise: // 2.1.3 When rejected, a promise:
QPromise<int> p{[](const QPromiseResolve<int>& resolve, const QPromiseReject<int>& reject) { QtPromise::QPromise<int> p{[](const QtPromise::QPromiseResolve<int>& resolve,
const QtPromise::QPromiseReject<int>& reject) {
QtPromisePrivate::qtpromise_defer([=]() { QtPromisePrivate::qtpromise_defer([=]() {
// 2.1.3.2. must have a reason, which must not change. // 2.1.3.2. must have a reason, which must not change.
reject(QString{"foo"}); reject(QString{"foo"});
@ -154,7 +155,7 @@ void tst_requirements::thenArguments()
{ {
QString error; QString error;
int value = -1; int value = -1;
QPromise<int>::resolve(42) QtPromise::QPromise<int>::resolve(42)
.then( .then(
[&](int res) { [&](int res) {
value = res; value = res;
@ -170,7 +171,7 @@ void tst_requirements::thenArguments()
{ {
QString error; QString error;
int value = -1; int value = -1;
QPromise<int>::reject(QString{"foo"}) QtPromise::QPromise<int>::reject(QString{"foo"})
.then( .then(
[&](int res) { [&](int res) {
value = res; value = res;
@ -187,7 +188,7 @@ void tst_requirements::thenArguments()
// 2.2.1. onFulfilled is an optional arguments: // 2.2.1. onFulfilled is an optional arguments:
{ {
QString error; QString error;
QPromise<int>::reject(QString{"foo"}) QtPromise::QPromise<int>::reject(QString{"foo"})
.then(nullptr, .then(nullptr,
[&](const QString& err) { [&](const QString& err) {
error = err; error = err;
@ -201,7 +202,7 @@ void tst_requirements::thenArguments()
// 2.2.1. onRejected is an optional arguments: // 2.2.1. onRejected is an optional arguments:
{ {
int value = -1; int value = -1;
QPromise<int>::resolve(42) QtPromise::QPromise<int>::resolve(42)
.then([&value](int res) { .then([&value](int res) {
value = res; value = res;
}) })
@ -219,7 +220,7 @@ void tst_requirements::thenOnFulfilled()
{ {
// 2.2.2. If onFulfilled is a function: // 2.2.2. If onFulfilled is a function:
QVector<int> values; QVector<int> values;
QPromise<int> p0{[](const QPromiseResolve<int>& resolve) { QtPromise::QPromise<int> p0{[](const QtPromise::QPromiseResolve<int>& resolve) {
QtPromisePrivate::qtpromise_defer([=]() { QtPromisePrivate::qtpromise_defer([=]() {
// 2.2.2.3. it must not be called more than once // 2.2.2.3. it must not be called more than once
resolve(42); resolve(42);
@ -249,7 +250,8 @@ void tst_requirements::thenOnRejected()
{ {
// 2.2.3. If onRejected is a function: // 2.2.3. If onRejected is a function:
QStringList errors; QStringList errors;
QPromise<void> p0{[](const QPromiseResolve<void>&, const QPromiseReject<void>& reject) { QtPromise::QPromise<void> p0{
[](const QtPromise::QPromiseResolve<void>&, const QtPromise::QPromiseReject<void>& reject) {
QtPromisePrivate::qtpromise_defer([=]() { QtPromisePrivate::qtpromise_defer([=]() {
// 2.2.3.3. it must not be called more than once. // 2.2.3.3. it must not be called more than once.
reject(QString{"foo"}); reject(QString{"foo"});
@ -282,7 +284,7 @@ void tst_requirements::thenAsynchronous()
// loop turn in which then is called, and with a fresh stack). // loop turn in which then is called, and with a fresh stack).
int value = -1; int value = -1;
auto p0 = QPromise<int>::resolve(42); auto p0 = QtPromise::QPromise<int>::resolve(42);
QVERIFY(p0.isFulfilled()); QVERIFY(p0.isFulfilled());
auto p1 = p0.then([&](int res) { auto p1 = p0.then([&](int res) {
@ -304,13 +306,13 @@ void tst_requirements::thenMultipleCalls()
// must execute in the order of their originating calls to then: // must execute in the order of their originating calls to then:
{ {
QVector<int> values; QVector<int> values;
QPromise<int> p{[](const QPromiseResolve<int>& resolve) { QtPromise::QPromise<int> p{[](const QtPromise::QPromiseResolve<int>& resolve) {
QtPromisePrivate::qtpromise_defer([=]() { QtPromisePrivate::qtpromise_defer([=]() {
resolve(42); resolve(42);
}); });
}}; }};
QtPromise::all(QVector<QPromise<void>>{p.then([&](int r) { QtPromise::all(QVector<QtPromise::QPromise<void>>{p.then([&](int r) {
values << r + 1; values << r + 1;
}), }),
p.then([&](int r) { p.then([&](int r) {
@ -328,13 +330,14 @@ void tst_requirements::thenMultipleCalls()
// must execute in the order of their originating calls to then: // must execute in the order of their originating calls to then:
{ {
QVector<int> values; QVector<int> values;
QPromise<int> p{[](const QPromiseResolve<int>&, const QPromiseReject<int>& reject) { QtPromise::QPromise<int> p{[](const QtPromise::QPromiseResolve<int>&,
const QtPromise::QPromiseReject<int>& reject) {
QtPromisePrivate::qtpromise_defer([=]() { QtPromisePrivate::qtpromise_defer([=]() {
reject(8); reject(8);
}); });
}}; }};
QtPromise::all(QVector<QPromise<int>>{p.then(nullptr, QtPromise::all(QVector<QtPromise::QPromise<int>>{p.then(nullptr,
[&](int r) { [&](int r) {
values << r + 1; values << r + 1;
return r + 1; return r + 1;
@ -362,10 +365,13 @@ void tst_requirements::thenHandlers()
auto handler = []() { auto handler = []() {
return 42; return 42;
}; };
auto p1 = QPromise<int>::resolve(42); auto p1 = QtPromise::QPromise<int>::resolve(42);
Q_STATIC_ASSERT((std::is_same<decltype(p1.then(handler, nullptr)), QPromise<int>>::value)); Q_STATIC_ASSERT(
Q_STATIC_ASSERT((std::is_same<decltype(p1.then(nullptr, handler)), QPromise<int>>::value)); (std::is_same<decltype(p1.then(handler, nullptr)), QtPromise::QPromise<int>>::value));
Q_STATIC_ASSERT((std::is_same<decltype(p1.then(handler, handler)), QPromise<int>>::value)); Q_STATIC_ASSERT(
(std::is_same<decltype(p1.then(nullptr, handler)), QtPromise::QPromise<int>>::value));
Q_STATIC_ASSERT(
(std::is_same<decltype(p1.then(handler, handler)), QtPromise::QPromise<int>>::value));
} }
// 2.2.7.1. If either onFulfilled or onRejected returns a value x, run the // 2.2.7.1. If either onFulfilled or onRejected returns a value x, run the
@ -375,7 +381,7 @@ void tst_requirements::thenHandlers()
// p2 must be rejected with e as the reason. // p2 must be rejected with e as the reason.
{ {
QString reason; QString reason;
auto p1 = QPromise<int>::resolve(42); auto p1 = QtPromise::QPromise<int>::resolve(42);
auto p2 = p1.then([]() { auto p2 = p1.then([]() {
throw QString{"foo"}; throw QString{"foo"};
}); });
@ -389,7 +395,7 @@ void tst_requirements::thenHandlers()
} }
{ {
QString reason; QString reason;
auto p1 = QPromise<int>::reject(QString{"foo"}); auto p1 = QtPromise::QPromise<int>::reject(QString{"foo"});
auto p2 = p1.then(nullptr, []() { auto p2 = p1.then(nullptr, []() {
throw QString{"bar"}; throw QString{"bar"};
return 42; return 42;
@ -408,11 +414,11 @@ void tst_requirements::thenHandlers()
// p2 must be fulfilled with the same value as promise1 // p2 must be fulfilled with the same value as promise1
{ {
QString value; QString value;
auto p1 = QPromise<QString>::resolve("42"); auto p1 = QtPromise::QPromise<QString>::resolve("42");
auto p2 = p1.then(nullptr, []() { auto p2 = p1.then(nullptr, []() {
return QString{}; return QString{};
}); });
Q_STATIC_ASSERT((std::is_same<decltype(p2), QPromise<QString>>::value)); Q_STATIC_ASSERT((std::is_same<decltype(p2), QtPromise::QPromise<QString>>::value));
p2.then([&](const QString& e) { p2.then([&](const QString& e) {
value = e; value = e;
}).wait(); }).wait();

View File

@ -7,6 +7,6 @@ add_library(${PROJECT_NAME} STATIC
) )
target_link_libraries(${PROJECT_NAME} PUBLIC target_link_libraries(${PROJECT_NAME} PUBLIC
Qt5::Core Qt${QT_VERSION_MAJOR}::Core
qtpromise qtpromise
) )

View File

@ -9,8 +9,6 @@
#include <QtPromise> #include <QtPromise>
#include <QtTest> #include <QtTest>
using namespace QtPromise;
class tst_thread : public QObject class tst_thread : public QObject
{ {
Q_OBJECT Q_OBJECT
@ -35,8 +33,8 @@ void tst_thread::resolve()
QThread* target = nullptr; QThread* target = nullptr;
QThread* source = nullptr; QThread* source = nullptr;
QPromise<int>{[&](const QPromiseResolve<int>& resolve) { QtPromise::QPromise<int>{[&](const QtPromise::QPromiseResolve<int>& resolve) {
QtConcurrent::run([=, &source]() { std::ignore = QtConcurrent::run([=, &source]() {
source = QThread::currentThread(); source = QThread::currentThread();
resolve(42); resolve(42);
}); });
@ -59,8 +57,8 @@ void tst_thread::resolve_void()
QThread* target = nullptr; QThread* target = nullptr;
QThread* source = nullptr; QThread* source = nullptr;
QPromise<void>{[&](const QPromiseResolve<void>& resolve) { QtPromise::QPromise<void>{[&](const QtPromise::QPromiseResolve<void>& resolve) {
QtConcurrent::run([=, &source]() { std::ignore = QtConcurrent::run([=, &source]() {
source = QThread::currentThread(); source = QThread::currentThread();
resolve(); resolve();
}); });
@ -83,8 +81,9 @@ void tst_thread::reject()
QThread* target = nullptr; QThread* target = nullptr;
QThread* source = nullptr; QThread* source = nullptr;
QPromise<int>{[&](const QPromiseResolve<int>&, const QPromiseReject<int>& reject) { QtPromise::QPromise<int>{
QtConcurrent::run([=, &source]() { [&](const QtPromise::QPromiseResolve<int>&, const QtPromise::QPromiseReject<int>& reject) {
std::ignore = QtConcurrent::run([=, &source]() {
source = QThread::currentThread(); source = QThread::currentThread();
reject(QString{"foo"}); reject(QString{"foo"});
}); });
@ -105,7 +104,7 @@ void tst_thread::reject()
void tst_thread::then() void tst_thread::then()
{ {
QThread* source = nullptr; QThread* source = nullptr;
QPromise<int> p{[&](const QPromiseResolve<int>& resolve) { QtPromise::QPromise<int> p{[&](const QtPromise::QPromiseResolve<int>& resolve) {
source = QThread::currentThread(); source = QThread::currentThread();
resolve(42); resolve(42);
}}; }};
@ -113,7 +112,7 @@ void tst_thread::then()
int value = -1; int value = -1;
QThread* target = nullptr; QThread* target = nullptr;
QtPromise::resolve(QtConcurrent::run( QtPromise::resolve(QtConcurrent::run(
[&](const QPromise<int>& p) { [&](const QtPromise::QPromise<int>& p) {
p.then([&](int res) { p.then([&](int res) {
target = QThread::currentThread(); target = QThread::currentThread();
value = res; value = res;
@ -131,7 +130,7 @@ void tst_thread::then()
void tst_thread::then_void() void tst_thread::then_void()
{ {
QThread* source = nullptr; QThread* source = nullptr;
QPromise<void> p{[&](const QPromiseResolve<void>& resolve) { QtPromise::QPromise<void> p{[&](const QtPromise::QPromiseResolve<void>& resolve) {
source = QThread::currentThread(); source = QThread::currentThread();
resolve(); resolve();
}}; }};
@ -139,7 +138,7 @@ void tst_thread::then_void()
int value = -1; int value = -1;
QThread* target = nullptr; QThread* target = nullptr;
QtPromise::resolve(QtConcurrent::run( QtPromise::resolve(QtConcurrent::run(
[&](const QPromise<void>& p) { [&](const QtPromise::QPromise<void>& p) {
p.then([&]() { p.then([&]() {
target = QThread::currentThread(); target = QThread::currentThread();
value = 43; value = 43;
@ -157,7 +156,8 @@ void tst_thread::then_void()
void tst_thread::fail() void tst_thread::fail()
{ {
QThread* source = nullptr; QThread* source = nullptr;
QPromise<int> p{[&](const QPromiseResolve<int>&, const QPromiseReject<int>& reject) { QtPromise::QPromise<int> p{
[&](const QtPromise::QPromiseResolve<int>&, const QtPromise::QPromiseReject<int>& reject) {
source = QThread::currentThread(); source = QThread::currentThread();
reject(QString{"foo"}); reject(QString{"foo"});
}}; }};
@ -165,7 +165,7 @@ void tst_thread::fail()
QString error; QString error;
QThread* target = nullptr; QThread* target = nullptr;
QtPromise::resolve(QtConcurrent::run( QtPromise::resolve(QtConcurrent::run(
[&](const QPromise<int>& p) { [&](const QtPromise::QPromise<int>& p) {
p.fail([&](const QString& err) { p.fail([&](const QString& err) {
target = QThread::currentThread(); target = QThread::currentThread();
error = err; error = err;
@ -184,7 +184,7 @@ void tst_thread::fail()
void tst_thread::finally() void tst_thread::finally()
{ {
QThread* source = nullptr; QThread* source = nullptr;
QPromise<int> p{[&](const QPromiseResolve<int>& resolve) { QtPromise::QPromise<int> p{[&](const QtPromise::QPromiseResolve<int>& resolve) {
source = QThread::currentThread(); source = QThread::currentThread();
resolve(42); resolve(42);
}}; }};
@ -192,7 +192,7 @@ void tst_thread::finally()
int value = -1; int value = -1;
QThread* target = nullptr; QThread* target = nullptr;
QtPromise::resolve(QtConcurrent::run( QtPromise::resolve(QtConcurrent::run(
[&](const QPromise<int>& p) { [&](const QtPromise::QPromise<int>& p) {
p.finally([&]() { p.finally([&]() {
target = QThread::currentThread(); target = QThread::currentThread();
value = 43; value = 43;