Merge pull request #1 from BlackMATov/dev

Dev
This commit is contained in:
BlackMat MATov
2018-12-09 22:46:24 +07:00
committed by GitHub
2 changed files with 660 additions and 153 deletions

View File

@@ -25,31 +25,92 @@ namespace promise_hpp
class promise; class promise;
// //
// make_promise // is_promise
// //
template < typename R, typename F > namespace impl
promise<R> make_promise(F&& f) { {
promise<R> result; template < typename T >
struct is_promise_impl
: std::false_type {};
auto resolver = std::bind([](promise<R>& p, auto&& v){ template < typename R >
p.resolve(std::forward<decltype(v)>(v)); struct is_promise_impl<promise<R>>
}, result, std::placeholders::_1); : std::true_type {};
}
auto rejector = std::bind([](promise<R>& p, auto&& e){ template < typename T >
p.reject(std::forward<decltype(e)>(e)); struct is_promise
}, result, std::placeholders::_1); : impl::is_promise_impl<std::remove_cv_t<T>> {};
try { //
invoke_hpp::invoke( // is_promise_r
std::forward<F>(f), //
std::move(resolver),
std::move(rejector));
} catch (...) {
result.reject(std::current_exception());
}
return result; namespace impl
{
template < typename R, typename T >
struct is_promise_r_impl
: std::false_type {};
template < typename R, typename PR >
struct is_promise_r_impl<R, promise<PR>>
: std::is_convertible<PR, R> {};
}
template < typename R, typename T >
struct is_promise_r
: impl::is_promise_r_impl<R, std::remove_cv_t<T>> {};
//
// detail
//
namespace detail
{
class noncopyable {
public:
noncopyable(const noncopyable&) = delete;
noncopyable& operator=(const noncopyable&) = delete;
protected:
noncopyable() = default;
~noncopyable() = default;
};
template < typename T >
class storage final : private noncopyable {
public:
storage() = default;
~storage() noexcept(std::is_nothrow_destructible<T>::value) {
if ( initialized_ ) {
ptr_()->~T();
}
}
template < typename U >
void set(U&& value) noexcept(std::is_nothrow_constructible<T,U&&>::value) {
assert(!initialized_);
::new(ptr_()) T(std::forward<U>(value));
initialized_ = true;
}
const T& value() const noexcept {
assert(initialized_);
return *ptr_();
}
private:
T* ptr_() noexcept {
return reinterpret_cast<T*>(&data_);
}
const T* ptr_() const noexcept {
return reinterpret_cast<const T*>(&data_);
}
private:
std::aligned_storage_t<sizeof(T), alignof(T)> data_;
bool initialized_ = false;
};
} }
// //
@@ -59,6 +120,8 @@ namespace promise_hpp
template < typename T > template < typename T >
class promise final { class promise final {
public: public:
using value_type = T;
enum class status : std::uint8_t { enum class status : std::uint8_t {
pending, pending,
resolved, resolved,
@@ -70,7 +133,64 @@ namespace promise_hpp
template < typename ResolveF template < typename ResolveF
, typename ResolveFR = invoke_hpp::invoke_result_t<ResolveF,T> > , typename ResolveFR = invoke_hpp::invoke_result_t<ResolveF,T> >
promise<ResolveFR> then(ResolveF&& on_resolve) { std::enable_if_t<
is_promise<ResolveFR>::value && !std::is_void<typename ResolveFR::value_type>::value,
promise<typename ResolveFR::value_type>>
then(ResolveF&& on_resolve) {
promise<typename ResolveFR::value_type> next;
then([
n = next,
f = std::forward<ResolveF>(on_resolve)
](const T& v) mutable {
auto np = invoke_hpp::invoke(
std::forward<decltype(f)>(f),
v);
np.then([n = n](const typename ResolveFR::value_type& nv) mutable {
n.resolve(nv);
}).fail([n = n](std::exception_ptr e) mutable {
n.reject(e);
});
}).fail([n = next](std::exception_ptr e) mutable {
n.reject(e);
});
return next;
}
template < typename ResolveF
, typename ResolveFR = invoke_hpp::invoke_result_t<ResolveF,T> >
std::enable_if_t<
is_promise<ResolveFR>::value && std::is_void<typename ResolveFR::value_type>::value,
promise<typename ResolveFR::value_type>>
then(ResolveF&& on_resolve) {
promise<typename ResolveFR::value_type> next;
then([
n = next,
f = std::forward<ResolveF>(on_resolve)
](const T& v) mutable {
auto np = invoke_hpp::invoke(
std::forward<decltype(f)>(f),
v);
np.then([n = n]() mutable {
n.resolve();
}).fail([n = n](std::exception_ptr e) mutable {
n.reject(e);
});
}).fail([n = next](std::exception_ptr e) mutable {
n.reject(e);
});
return next;
}
template < typename ResolveF
, typename ResolveFR = invoke_hpp::invoke_result_t<ResolveF,T> >
std::enable_if_t<
!is_promise<ResolveFR>::value,
promise<ResolveFR>>
then(ResolveF&& on_resolve) {
promise<ResolveFR> next; promise<ResolveFR> next;
state_->attach( state_->attach(
next, next,
@@ -79,18 +199,6 @@ namespace promise_hpp
return next; return next;
} }
template < typename ResolveF
, typename RejectF
, typename ResolveFR = invoke_hpp::invoke_result_t<ResolveF,T> >
promise<ResolveFR> then(ResolveF&& on_resolve, RejectF&& on_reject) {
promise<ResolveFR> next;
state_->attach(
next,
std::forward<ResolveF>(on_resolve),
std::forward<RejectF>(on_reject));
return next;
}
template < typename RejectF > template < typename RejectF >
promise<T> fail(RejectF&& on_reject) { promise<T> fail(RejectF&& on_reject) {
promise<T> next; promise<T> next;
@@ -121,50 +229,10 @@ namespace promise_hpp
class state; class state;
std::shared_ptr<state> state_; std::shared_ptr<state> state_;
private: private:
class storage final { class state final : private detail::noncopyable {
public:
storage() = default;
storage(const storage&) = delete;
storage& operator=(const storage&) = delete;
~storage() noexcept(std::is_nothrow_destructible<T>::value) {
if ( initialized_ ) {
ptr_()->~T();
}
}
template < typename U >
void set(U&& value) {
assert(!initialized_);
::new(ptr_()) T(std::forward<U>(value));
initialized_ = true;
}
const T& value() const noexcept {
assert(initialized_);
return *ptr_();
}
private:
T* ptr_() noexcept {
return reinterpret_cast<T*>(&data_);
}
const T* ptr_() const noexcept {
return reinterpret_cast<const T*>(&data_);
}
private:
std::aligned_storage_t<sizeof(T), alignof(T)> data_;
bool initialized_ = false;
};
class state final {
public: public:
state() = default; state() = default;
state(const state&) = delete;
state& operator=(const state&) = delete;
template < typename U > template < typename U >
void resolve(U&& value) { void resolve(U&& value) {
std::lock_guard<std::mutex> guard(mutex_); std::lock_guard<std::mutex> guard(mutex_);
@@ -188,30 +256,45 @@ namespace promise_hpp
template < typename U, typename ResolveF, typename RejectF > template < typename U, typename ResolveF, typename RejectF >
std::enable_if_t<!std::is_void<U>::value, void> std::enable_if_t<!std::is_void<U>::value, void>
attach(promise<U>& other, ResolveF&& resolve, RejectF&& reject) { attach(promise<U>& next, ResolveF&& resolve, RejectF&& reject) {
auto resolve_h = std::bind([](promise<U>& p, const ResolveF& f, const T& v){ auto resolve_h = [
n = next,
f = std::forward<ResolveF>(resolve)
](const T& v) mutable {
try { try {
p.resolve(invoke_hpp::invoke(f, v)); auto r = invoke_hpp::invoke(
std::forward<decltype(f)>(f),
v);
n.resolve(std::move(r));
} catch (...) { } catch (...) {
p.reject(std::current_exception()); n.reject(std::current_exception());
} }
}, other, std::forward<ResolveF>(resolve), std::placeholders::_1); };
auto reject_h = std::bind([](promise<U>& p, const RejectF& f, std::exception_ptr e){ auto reject_h = [
n = next,
f = std::forward<RejectF>(reject)
](std::exception_ptr e) mutable {
try { try {
invoke_hpp::invoke(f, e); invoke_hpp::invoke(
p.reject(e); std::forward<decltype(f)>(f),
e);
n.reject(e);
} catch (...) { } catch (...) {
p.reject(std::current_exception()); n.reject(std::current_exception());
} }
}, other, std::forward<RejectF>(reject), std::placeholders::_1); };
std::lock_guard<std::mutex> guard(mutex_); std::lock_guard<std::mutex> guard(mutex_);
if ( status_ == status::resolved ) { if ( status_ == status::resolved ) {
resolve_h(storage_.value()); invoke_hpp::invoke(
std::move(resolve_h),
storage_.value());
} else if ( status_ == status::rejected ) { } else if ( status_ == status::rejected ) {
reject_h(exception_); invoke_hpp::invoke(
std::move(reject_h),
exception_);
} else { } else {
handlers_.emplace_back( handlers_.emplace_back(
std::move(resolve_h), std::move(resolve_h),
@@ -221,31 +304,45 @@ namespace promise_hpp
template < typename U, typename ResolveF, typename RejectF > template < typename U, typename ResolveF, typename RejectF >
std::enable_if_t<std::is_void<U>::value, void> std::enable_if_t<std::is_void<U>::value, void>
attach(promise<U>& other, ResolveF&& resolve, RejectF&& reject) { attach(promise<U>& next, ResolveF&& resolve, RejectF&& reject) {
auto resolve_h = std::bind([](promise<U>& p, const ResolveF& f, const T& v){ auto resolve_h = [
n = next,
f = std::forward<ResolveF>(resolve)
](const T& v) mutable {
try { try {
invoke_hpp::invoke(f, v); invoke_hpp::invoke(
p.resolve(); std::forward<decltype(f)>(f),
v);
n.resolve();
} catch (...) { } catch (...) {
p.reject(std::current_exception()); n.reject(std::current_exception());
} }
}, other, std::forward<ResolveF>(resolve), std::placeholders::_1); };
auto reject_h = std::bind([](promise<U>& p, const RejectF& f, std::exception_ptr e){ auto reject_h = [
n = next,
f = std::forward<RejectF>(reject)
](std::exception_ptr e) mutable {
try { try {
invoke_hpp::invoke(f, e); invoke_hpp::invoke(
p.reject(e); std::forward<decltype(f)>(f),
e);
n.reject(e);
} catch (...) { } catch (...) {
p.reject(std::current_exception()); n.reject(std::current_exception());
} }
}, other, std::forward<RejectF>(reject), std::placeholders::_1); };
std::lock_guard<std::mutex> guard(mutex_); std::lock_guard<std::mutex> guard(mutex_);
if ( status_ == status::resolved ) { if ( status_ == status::resolved ) {
resolve_h(storage_.value()); invoke_hpp::invoke(
std::move(resolve_h),
storage_.value());
} else if ( status_ == status::rejected ) { } else if ( status_ == status::rejected ) {
reject_h(exception_); invoke_hpp::invoke(
std::move(reject_h),
exception_);
} else { } else {
handlers_.emplace_back( handlers_.emplace_back(
std::move(resolve_h), std::move(resolve_h),
@@ -268,7 +365,7 @@ namespace promise_hpp
handlers_.clear(); handlers_.clear();
} }
private: private:
storage storage_; detail::storage<T> storage_;
status status_ = status::pending; status status_ = status::pending;
std::exception_ptr exception_ = nullptr; std::exception_ptr exception_ = nullptr;
@@ -298,6 +395,8 @@ namespace promise_hpp
template <> template <>
class promise<void> final { class promise<void> final {
public: public:
using value_type = void;
enum class status : std::uint8_t { enum class status : std::uint8_t {
pending, pending,
resolved, resolved,
@@ -309,7 +408,62 @@ namespace promise_hpp
template < typename ResolveF template < typename ResolveF
, typename ResolveFR = invoke_hpp::invoke_result_t<ResolveF> > , typename ResolveFR = invoke_hpp::invoke_result_t<ResolveF> >
promise<ResolveFR> then(ResolveF&& on_resolve) { std::enable_if_t<
is_promise<ResolveFR>::value && !std::is_void<typename ResolveFR::value_type>::value,
promise<typename ResolveFR::value_type>>
then(ResolveF&& on_resolve) {
promise<typename ResolveFR::value_type> next;
then([
n = next,
f = std::forward<ResolveF>(on_resolve)
]() mutable {
auto np = invoke_hpp::invoke(
std::forward<decltype(f)>(f));
np.then([n = n](const typename ResolveFR::value_type& nv) mutable {
n.resolve(nv);
}).fail([n = n](std::exception_ptr e) mutable {
n.reject(e);
});
}).fail([n = next](std::exception_ptr e) mutable {
n.reject(e);
});
return next;
}
template < typename ResolveF
, typename ResolveFR = invoke_hpp::invoke_result_t<ResolveF> >
std::enable_if_t<
is_promise<ResolveFR>::value && std::is_void<typename ResolveFR::value_type>::value,
promise<typename ResolveFR::value_type>>
then(ResolveF&& on_resolve) {
promise<typename ResolveFR::value_type> next;
then([
n = next,
f = std::forward<ResolveF>(on_resolve)
]() mutable {
auto np = invoke_hpp::invoke(
std::forward<decltype(f)>(f));
np.then([n = n]() mutable {
n.resolve();
}).fail([n = n](std::exception_ptr e) mutable {
n.reject(e);
});
}).fail([n = next](std::exception_ptr e) mutable {
n.reject(e);
});
return next;
}
template < typename ResolveF
, typename ResolveFR = invoke_hpp::invoke_result_t<ResolveF> >
std::enable_if_t<
!is_promise<ResolveFR>::value,
promise<ResolveFR>>
then(ResolveF&& on_resolve) {
promise<ResolveFR> next; promise<ResolveFR> next;
state_->attach( state_->attach(
next, next,
@@ -318,18 +472,6 @@ namespace promise_hpp
return next; return next;
} }
template < typename ResolveF
, typename RejectF
, typename ResolveFR = invoke_hpp::invoke_result_t<ResolveF> >
promise<ResolveFR> then(ResolveF&& on_resolve, RejectF&& on_reject) {
promise<ResolveFR> next;
state_->attach(
next,
std::forward<ResolveF>(on_resolve),
std::forward<RejectF>(on_reject));
return next;
}
template < typename RejectF > template < typename RejectF >
promise<void> fail(RejectF&& on_reject) { promise<void> fail(RejectF&& on_reject) {
promise<void> next; promise<void> next;
@@ -359,13 +501,10 @@ namespace promise_hpp
class state; class state;
std::shared_ptr<state> state_; std::shared_ptr<state> state_;
private: private:
class state final { class state final : private detail::noncopyable {
public: public:
state() = default; state() = default;
state(const state&) = delete;
state& operator=(const state&) = delete;
void resolve() { void resolve() {
std::lock_guard<std::mutex> guard(mutex_); std::lock_guard<std::mutex> guard(mutex_);
if ( status_ != status::pending ) { if ( status_ != status::pending ) {
@@ -387,30 +526,43 @@ namespace promise_hpp
template < typename U, typename ResolveF, typename RejectF > template < typename U, typename ResolveF, typename RejectF >
std::enable_if_t<!std::is_void<U>::value, void> std::enable_if_t<!std::is_void<U>::value, void>
attach(promise<U>& other, ResolveF&& resolve, RejectF&& reject) { attach(promise<U>& next, ResolveF&& resolve, RejectF&& reject) {
auto resolve_h = std::bind([](promise<U>& p, const ResolveF& f){ auto resolve_h = [
n = next,
f = std::forward<ResolveF>(resolve)
]() mutable {
try { try {
p.resolve(invoke_hpp::invoke(f)); auto r = invoke_hpp::invoke(
std::forward<decltype(f)>(f));
n.resolve(std::move(r));
} catch (...) { } catch (...) {
p.reject(std::current_exception()); n.reject(std::current_exception());
} }
}, other, std::forward<ResolveF>(resolve)); };
auto reject_h = std::bind([](promise<U>& p, const RejectF& f, std::exception_ptr e){ auto reject_h = [
n = next,
f = std::forward<RejectF>(reject)
](std::exception_ptr e) mutable {
try { try {
invoke_hpp::invoke(f, e); invoke_hpp::invoke(
p.reject(e); std::forward<decltype(f)>(f),
e);
n.reject(e);
} catch (...) { } catch (...) {
p.reject(std::current_exception()); n.reject(std::current_exception());
} }
}, other, std::forward<RejectF>(reject), std::placeholders::_1); };
std::lock_guard<std::mutex> guard(mutex_); std::lock_guard<std::mutex> guard(mutex_);
if ( status_ == status::resolved ) { if ( status_ == status::resolved ) {
resolve_h(); invoke_hpp::invoke(
std::move(resolve_h));
} else if ( status_ == status::rejected ) { } else if ( status_ == status::rejected ) {
reject_h(exception_); invoke_hpp::invoke(
std::move(reject_h),
exception_);
} else { } else {
handlers_.emplace_back( handlers_.emplace_back(
std::move(resolve_h), std::move(resolve_h),
@@ -420,31 +572,43 @@ namespace promise_hpp
template < typename U, typename ResolveF, typename RejectF > template < typename U, typename ResolveF, typename RejectF >
std::enable_if_t<std::is_void<U>::value, void> std::enable_if_t<std::is_void<U>::value, void>
attach(promise<U>& other, ResolveF&& resolve, RejectF&& reject) { attach(promise<U>& next, ResolveF&& resolve, RejectF&& reject) {
auto resolve_h = std::bind([](promise<U>& p, const ResolveF& f){ auto resolve_h = [
n = next,
f = std::forward<ResolveF>(resolve)
]() mutable {
try { try {
invoke_hpp::invoke(f); invoke_hpp::invoke(
p.resolve(); std::forward<decltype(f)>(f));
n.resolve();
} catch (...) { } catch (...) {
p.reject(std::current_exception()); n.reject(std::current_exception());
} }
}, other, std::forward<ResolveF>(resolve)); };
auto reject_h = std::bind([](promise<U>& p, const RejectF& f, std::exception_ptr e){ auto reject_h = [
n = next,
f = std::forward<RejectF>(reject)
](std::exception_ptr e) mutable {
try { try {
invoke_hpp::invoke(f, e); invoke_hpp::invoke(
p.reject(e); std::forward<decltype(f)>(f),
e);
n.reject(e);
} catch (...) { } catch (...) {
p.reject(std::current_exception()); n.reject(std::current_exception());
} }
}, other, std::forward<RejectF>(reject), std::placeholders::_1); };
std::lock_guard<std::mutex> guard(mutex_); std::lock_guard<std::mutex> guard(mutex_);
if ( status_ == status::resolved ) { if ( status_ == status::resolved ) {
resolve_h(); invoke_hpp::invoke(
std::move(resolve_h));
} else if ( status_ == status::rejected ) { } else if ( status_ == status::rejected ) {
reject_h(exception_); invoke_hpp::invoke(
std::move(reject_h),
exception_);
} else { } else {
handlers_.emplace_back( handlers_.emplace_back(
std::move(resolve_h), std::move(resolve_h),
@@ -487,4 +651,63 @@ namespace promise_hpp
std::vector<handler> handlers_; std::vector<handler> handlers_;
}; };
}; };
//
// make_promise
//
template < typename R, typename F >
promise<R> make_promise(F&& f) {
promise<R> result;
auto resolver = [
p = result
](auto&& v) mutable {
p.resolve(std::forward<decltype(v)>(v));
};
auto rejector = [
p = result
](auto&& e) mutable {
p.reject(std::forward<decltype(e)>(e));
};
try {
invoke_hpp::invoke(
std::forward<F>(f),
std::move(resolver),
std::move(rejector));
} catch (...) {
result.reject(std::current_exception());
}
return result;
}
inline promise<void> make_resolved_promise() {
promise<void> result;
result.resolve();
return result;
}
template < typename R >
promise<std::decay_t<R>> make_resolved_promise(R&& v) {
promise<std::decay_t<R>> result;
result.resolve(std::forward<R>(v));
return result;
}
template < typename E >
promise<void> make_rejected_promise(E&& e) {
promise<void> result;
result.reject(std::forward<E>(e));
return result;
}
template < typename R, typename E >
promise<R> make_rejected_promise(E&& e) {
promise<R> result;
result.reject(std::forward<E>(e));
return result;
}
} }

308
tests.cpp
View File

@@ -6,6 +6,9 @@ namespace pr = promise_hpp;
namespace namespace
{ {
struct obj_t {
};
bool check_hello_fail_exception(std::exception_ptr e) { bool check_hello_fail_exception(std::exception_ptr e) {
try { try {
std::rethrow_exception(e); std::rethrow_exception(e);
@@ -17,6 +20,79 @@ namespace
} }
} }
TEST_CASE("is_promise") {
SECTION("positive") {
static_assert(
pr::is_promise<pr::promise<void>>::value,
"unit test fail");
static_assert(
pr::is_promise<const pr::promise<void>>::value,
"unit test fail");
static_assert(
pr::is_promise<const volatile pr::promise<void>>::value,
"unit test fail");
static_assert(
pr::is_promise<pr::promise<int>>::value,
"unit test fail");
static_assert(
pr::is_promise<const pr::promise<int>>::value,
"unit test fail");
static_assert(
pr::is_promise<const volatile pr::promise<int>>::value,
"unit test fail");
}
SECTION("negative") {
static_assert(
!pr::is_promise<pr::promise<void>&>::value,
"unit test fail");
static_assert(
!pr::is_promise<const pr::promise<void>*>::value,
"unit test fail");
static_assert(
!pr::is_promise<const volatile pr::promise<int>&>::value,
"unit test fail");
static_assert(
!pr::is_promise<int>::value,
"unit test fail");
static_assert(
!pr::is_promise<void>::value,
"unit test fail");
static_assert(
!pr::is_promise<const volatile int>::value,
"unit test fail");
}
}
TEST_CASE("is_promise_r") {
SECTION("positive") {
static_assert(
pr::is_promise_r<void, pr::promise<void>>::value,
"unit test fail");
static_assert(
pr::is_promise_r<int, const pr::promise<int>>::value,
"unit test fail");
static_assert(
pr::is_promise_r<long, const pr::promise<int>>::value,
"unit test fail");
}
SECTION("negative") {
static_assert(
!pr::is_promise_r<void, pr::promise<int>>::value,
"unit test fail");
static_assert(
!pr::is_promise_r<void, const pr::promise<int>>::value,
"unit test fail");
static_assert(
!pr::is_promise_r<int, pr::promise<obj_t>>::value,
"unit test fail");
static_assert(
!pr::is_promise_r<long, int>::value,
"unit test fail");
}
}
TEST_CASE("promise") { TEST_CASE("promise") {
SECTION("resolved") { SECTION("resolved") {
{ {
@@ -38,16 +114,6 @@ TEST_CASE("promise") {
}); });
REQUIRE(check_42_int == 42); REQUIRE(check_42_int == 42);
} }
{
int check_42_int = 0;
pr::promise<int>()
.resolve(42)
.then([&check_42_int](int value){
check_42_int = value;
}, [](std::exception_ptr){
});
REQUIRE(check_42_int == 42);
}
{ {
int check_84_int = 0; int check_84_int = 0;
bool check_void_call = false; bool check_void_call = false;
@@ -91,7 +157,7 @@ TEST_CASE("promise") {
pr::promise<int>() pr::promise<int>()
.reject(ee) .reject(ee)
.then([](int){ .then([](int){
}).fail([&call_fail_with_logic_error](const std::exception_ptr& e){ }).fail([&call_fail_with_logic_error](std::exception_ptr e){
call_fail_with_logic_error = check_hello_fail_exception(e); call_fail_with_logic_error = check_hello_fail_exception(e);
}); });
REQUIRE(call_fail_with_logic_error); REQUIRE(call_fail_with_logic_error);
@@ -102,7 +168,7 @@ TEST_CASE("promise") {
pr::promise<int>() pr::promise<int>()
.reject(std::make_exception_ptr(ee)) .reject(std::make_exception_ptr(ee))
.then([](int){ .then([](int){
}).fail([&call_fail_with_logic_error](const std::exception_ptr& e){ }).fail([&call_fail_with_logic_error](std::exception_ptr e){
call_fail_with_logic_error = check_hello_fail_exception(e); call_fail_with_logic_error = check_hello_fail_exception(e);
}); });
REQUIRE(call_fail_with_logic_error); REQUIRE(call_fail_with_logic_error);
@@ -191,6 +257,42 @@ TEST_CASE("promise") {
REQUIRE(call_fail_with_logic_error); REQUIRE(call_fail_with_logic_error);
} }
} }
SECTION("make_resolved_promise") {
{
bool call_check = false;
pr::make_resolved_promise()
.then([&call_check]{
call_check = true;
});
REQUIRE(call_check);
}
{
int check_42_int = 0;
pr::make_resolved_promise(42)
.then([&check_42_int](int value){
check_42_int = value;
});
REQUIRE(check_42_int == 42);
}
}
SECTION("make_rejected_promise") {
{
bool call_fail_with_logic_error = false;
pr::make_rejected_promise<int>(std::logic_error("hello fail"))
.fail([&call_fail_with_logic_error](std::exception_ptr e){
call_fail_with_logic_error = check_hello_fail_exception(e);
});
REQUIRE(call_fail_with_logic_error);
}
{
bool call_fail_with_logic_error = 0;
pr::make_rejected_promise(std::logic_error("hello fail"))
.fail([&call_fail_with_logic_error](std::exception_ptr e){
call_fail_with_logic_error = check_hello_fail_exception(e);
});
REQUIRE(call_fail_with_logic_error);
}
}
SECTION("exceptions") { SECTION("exceptions") {
{ {
bool not_call_then_on_reject = true; bool not_call_then_on_reject = true;
@@ -270,4 +372,186 @@ TEST_CASE("promise") {
REQUIRE(pb_value == 21); REQUIRE(pb_value == 21);
} }
} }
SECTION("chaining") {
{
int check_84_int = 0;
auto p1 = pr::make_resolved_promise(42);
auto p2 = pr::make_resolved_promise(84);
p1.then([&p2](int v){
return p2;
}).then([&check_84_int](int v2){
check_84_int = v2;
});
REQUIRE(check_84_int == 84);
}
{
int check_84_int = 0;
auto p1 = pr::make_resolved_promise();
auto p2 = pr::make_resolved_promise(84);
p1.then([&p2](){
return p2;
}).then([&check_84_int](int v){
check_84_int = v;
});
REQUIRE(check_84_int == 84);
}
{
int check_84_int = 0;
auto p1 = pr::make_resolved_promise(42);
auto p2 = pr::make_resolved_promise();
p1.then([&p2](int v){
return p2;
}).then([&check_84_int](){
check_84_int = 84;
});
REQUIRE(check_84_int == 84);
}
{
int check_84_int = 0;
auto p1 = pr::make_resolved_promise();
auto p2 = pr::make_resolved_promise();
p1.then([&p2](){
return p2;
}).then([&check_84_int](){
check_84_int = 84;
});
REQUIRE(check_84_int == 84);
}
}
SECTION("typed_chaining_fails") {
{
bool call_fail_with_logic_error = false;
auto p1 = pr::make_resolved_promise(42);
auto p2 = pr::make_resolved_promise(84);
p1.then([&p2](int v){
(void)v;
throw std::logic_error("hello fail");
return p2;
}).then([&call_fail_with_logic_error](int v2){
(void)v2;
}).fail([&call_fail_with_logic_error](std::exception_ptr e){
call_fail_with_logic_error = check_hello_fail_exception(e);
});
REQUIRE(call_fail_with_logic_error);
}
{
bool call_fail_with_logic_error = false;
auto p1 = pr::make_resolved_promise(42);
auto p2 = pr::make_resolved_promise(84);
p1.then([&p2](int v){
(void)v;
return p2;
}).then([&call_fail_with_logic_error](int v2){
(void)v2;
throw std::logic_error("hello fail");
}).fail([&call_fail_with_logic_error](std::exception_ptr e){
call_fail_with_logic_error = check_hello_fail_exception(e);
});
REQUIRE(call_fail_with_logic_error);
}
{
bool call_fail_with_logic_error = false;
auto p1 = pr::make_rejected_promise<int>(std::logic_error("hello fail"));
auto p2 = pr::make_resolved_promise(84);
p1.then([&p2](int v){
(void)v;
return p2;
}).then([&call_fail_with_logic_error](int v2){
(void)v2;
}).fail([&call_fail_with_logic_error](std::exception_ptr e){
call_fail_with_logic_error = check_hello_fail_exception(e);
});
REQUIRE(call_fail_with_logic_error);
}
{
bool call_fail_with_logic_error = false;
auto p1 = pr::make_resolved_promise(42);
auto p2 = pr::make_rejected_promise<int>(std::logic_error("hello fail"));
p1.then([&p2](int v){
(void)v;
return p2;
}).then([&call_fail_with_logic_error](int v2){
(void)v2;
}).fail([&call_fail_with_logic_error](std::exception_ptr e){
call_fail_with_logic_error = check_hello_fail_exception(e);
});
REQUIRE(call_fail_with_logic_error);
}
}
SECTION("void_chaining_fails") {
{
bool call_fail_with_logic_error = false;
auto p1 = pr::make_resolved_promise();
auto p2 = pr::make_resolved_promise();
p1.then([&p2](){
throw std::logic_error("hello fail");
return p2;
}).then([&call_fail_with_logic_error](){
}).fail([&call_fail_with_logic_error](std::exception_ptr e){
call_fail_with_logic_error = check_hello_fail_exception(e);
});
REQUIRE(call_fail_with_logic_error);
}
{
bool call_fail_with_logic_error = false;
auto p1 = pr::make_resolved_promise();
auto p2 = pr::make_resolved_promise();
p1.then([&p2](){
return p2;
}).then([&call_fail_with_logic_error](){
throw std::logic_error("hello fail");
}).fail([&call_fail_with_logic_error](std::exception_ptr e){
call_fail_with_logic_error = check_hello_fail_exception(e);
});
REQUIRE(call_fail_with_logic_error);
}
{
bool call_fail_with_logic_error = false;
auto p1 = pr::make_rejected_promise<void>(std::logic_error("hello fail"));
auto p2 = pr::make_resolved_promise();
p1.then([&p2](){
return p2;
}).then([&call_fail_with_logic_error](){
}).fail([&call_fail_with_logic_error](std::exception_ptr e){
call_fail_with_logic_error = check_hello_fail_exception(e);
});
REQUIRE(call_fail_with_logic_error);
}
{
bool call_fail_with_logic_error = false;
auto p1 = pr::make_resolved_promise();
auto p2 = pr::make_rejected_promise<void>(std::logic_error("hello fail"));
p1.then([&p2](){
return p2;
}).then([&call_fail_with_logic_error](){
}).fail([&call_fail_with_logic_error](std::exception_ptr e){
call_fail_with_logic_error = check_hello_fail_exception(e);
});
REQUIRE(call_fail_with_logic_error);
}
}
} }