Commit d39ab557 authored by octal's avatar octal

More work on Promises

parent 955ae9ac
This diff is collapsed.
...@@ -51,6 +51,32 @@ TEST(async_test, basic_test) { ...@@ -51,6 +51,32 @@ TEST(async_test, basic_test) {
} }
TEST(async_test, void_promise) {
Async::Promise<void> p1(
[](Async::Resolver& resolve, Async::Rejection& reject) {
resolve();
});
ASSERT_TRUE(p1.isFulfilled());
bool thenCalled { false };
p1.then([&]() {
thenCalled = true;
}, Async::NoExcept);
ASSERT_TRUE(thenCalled);
Async::Promise<int> p2(
[](Async::Resolver& resolve, Async::Rejection& reject) {
ASSERT_THROW(resolve(), Async::Error);
});
Async::Promise<void> p3(
[](Async::Resolver& resolve, Async::Rejection& reject) {
ASSERT_THROW(resolve(10), Async::Error);
});
}
TEST(async_test, chain_test) { TEST(async_test, chain_test) {
Async::Promise<int> p1( Async::Promise<int> p1(
[](Async::Resolver& resolve, Async::Rejection& reject) { [](Async::Resolver& resolve, Async::Rejection& reject) {
...@@ -72,5 +98,56 @@ TEST(async_test, chain_test) { ...@@ -72,5 +98,56 @@ TEST(async_test, chain_test) {
.then([](double result) { std::cout << "Result = " << result << std::endl; }, .then([](double result) { std::cout << "Result = " << result << std::endl; },
Async::IgnoreException); Async::IgnoreException);
enum class Test { Foo, Bar };
Async::Promise<Test> p3(
[](Async::Resolver& resolve, Async::Rejection& reject) {
resolve(Test::Foo);
});
p3
.then([](Test result) {
return Async::Promise<std::string>(
[=](Async::Resolver& resolve, Async::Rejection&) {
switch (result) {
case Test::Foo:
resolve(std::string("Foo"));
break;
case Test::Bar:
resolve(std::string("Bar"));
}
}); }, Async::NoExcept)
.then([](std::string str) {
ASSERT_EQ(str, "Foo");
}, Async::NoExcept);
Async::Promise<Test> p4(
[](Async::Resolver& resolve, Async::Rejection& reject) {
resolve(Test::Bar);
});
p4
.then(
[](Test result) {
return Async::Promise<std::string>(
[=](Async::Resolver& resolve, Async::Rejection& reject) {
switch (result) {
case Test::Foo:
resolve(std::string("Foo"));
break;
case Test::Bar:
reject(std::runtime_error("Invalid"));
}
});
},
Async::NoExcept)
.then(
[](std::string str) {
ASSERT_TRUE(false);
},
[](std::exception_ptr exc) {
ASSERT_THROW(std::rethrow_exception(exc), std::runtime_error);
});
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment