Skip to content

Commit

Permalink
Class method call for all task added
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanPinezhaninov committed Dec 25, 2023
1 parent e9196f2 commit a528fca
Show file tree
Hide file tree
Showing 20 changed files with 911 additions and 282 deletions.
543 changes: 426 additions & 117 deletions include/async_promise.hpp

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ include(Catch)

set(HEADERS
src/common.h
src/constants.h
src/test_funcs.h
src/test_struct.h
)
Expand Down
173 changes: 164 additions & 9 deletions tests/src/all.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,162 @@
#include "common.h"


TEST_CASE("All void void", "[all]")
TEST_CASE("All with class method void void", "[all]")
{
test_struct test;

std::vector<void(test_struct::*)() const> methods
{
&test_struct::void_void,
&test_struct::void_void,
};

auto future = async::make_resolved_promise().all(methods, &test).run();

REQUIRE_NOTHROW(future.get());
}


TEST_CASE("All with class method error void void", "[all]")
{
test_struct test;

std::vector<void(test_struct::*)() const> methods
{
&test_struct::void_void,
&test_struct::error_void_void,
};

auto future = async::make_resolved_promise().all(methods, &test).run();

REQUIRE_THROWS_MATCHES(future.get(), std::runtime_error, Catch::Matchers::Message(str2));
}


TEST_CASE("All with class method void string", "[all]")
{
test_struct test;

std::vector<void(test_struct::*)(std::string) const> methods
{
&test_struct::void_string,
&test_struct::void_string,
};

auto future = async::make_resolved_promise(str1).all(methods, &test).run();

REQUIRE_NOTHROW(future.get());
}


TEST_CASE("All with class method error void string", "[all]")
{
test_struct test;

std::vector<void(test_struct::*)(std::string) const> methods
{
&test_struct::void_string,
&test_struct::error_void_string,
};

auto future = async::make_resolved_promise(str1).all(methods, &test).run();

REQUIRE_THROWS_MATCHES(future.get(), std::runtime_error, Catch::Matchers::Message(str2));
}


TEST_CASE("All with class method string void", "[all]")
{
test_struct test;

std::vector<std::string(test_struct::*)() const> methods
{
&test_struct::string_void1,
&test_struct::string_void2,
};

auto future = async::make_resolved_promise().all(methods, &test).run();

std::vector<std::string> res;
REQUIRE_NOTHROW(res = future.get());
REQUIRE_THAT(res, Catch::Matchers::RangeEquals(std::vector<std::string>{str1, str2}));
}


TEST_CASE("All with class method string void ignore arg", "[all]")
{
test_struct test;

std::vector<std::string(test_struct::*)() const> methods
{
&test_struct::string_void1,
&test_struct::string_void2,
};

auto future = async::make_resolved_promise(str1).all(methods, &test).run();

std::vector<std::string> res;
REQUIRE_NOTHROW(res = future.get());
REQUIRE_THAT(res, Catch::Matchers::RangeEquals(std::vector<std::string>{str1, str2}));
}


TEST_CASE("All with class method error string void", "[all]")
{
test_struct test;

std::vector<std::string(test_struct::*)() const> methods
{
&test_struct::string_void1,
&test_struct::error_string_void,
};

auto future = async::make_resolved_promise().all(methods, &test).run();

std::vector<std::string> res;
REQUIRE_THROWS_MATCHES(res = future.get(), std::runtime_error, Catch::Matchers::Message(str2));
REQUIRE(res.empty());
}


TEST_CASE("All with class method string string", "[all]")
{
test_struct test;

std::vector<std::string(test_struct::*)(std::string) const> methods
{
&test_struct::string_string,
&test_struct::string_string,
};

auto future = async::make_resolved_promise(str1).all(methods, &test).run();

std::vector<std::string> res;
REQUIRE_NOTHROW(res = future.get());
REQUIRE_THAT(res, Catch::Matchers::RangeEquals(std::vector<std::string>{str1, str1}));
}


TEST_CASE("All with class method error string string", "[all]")
{
test_struct test;

std::vector<std::string(test_struct::*)(std::string) const> methods
{
&test_struct::string_string,
&test_struct::error_string_string
};

auto future = async::make_resolved_promise(str1).all(methods, &test).run();

std::vector<std::string> res;
REQUIRE_THROWS_MATCHES(res = future.get(), std::runtime_error, Catch::Matchers::Message(str2));
REQUIRE(res.empty());
}



TEST_CASE("All with function void void", "[all]")
{
std::vector<void(*)()> funcs
{
Expand All @@ -41,7 +196,7 @@ TEST_CASE("All void void", "[all]")
}


TEST_CASE("All error void void", "[all]")
TEST_CASE("All with function error void void", "[all]")
{
std::vector<void(*)()> funcs
{
Expand All @@ -55,7 +210,7 @@ TEST_CASE("All error void void", "[all]")
}


TEST_CASE("All void string", "[all]")
TEST_CASE("All with function void string", "[all]")
{
std::vector<void(*)(std::string)> funcs
{
Expand All @@ -69,7 +224,7 @@ TEST_CASE("All void string", "[all]")
}


TEST_CASE("All error void string", "[all]")
TEST_CASE("All with function error void string", "[all]")
{
std::vector<void(*)(std::string)> funcs
{
Expand All @@ -83,7 +238,7 @@ TEST_CASE("All error void string", "[all]")
}


TEST_CASE("All string void", "[all]")
TEST_CASE("All with function string void", "[all]")
{
std::vector<std::string(*)()> funcs
{
Expand All @@ -99,7 +254,7 @@ TEST_CASE("All string void", "[all]")
}


TEST_CASE("All string void ignore arg", "[all]")
TEST_CASE("All with function string void ignore arg", "[all]")
{
std::vector<std::string(*)()> funcs
{
Expand All @@ -115,7 +270,7 @@ TEST_CASE("All string void ignore arg", "[all]")
}


TEST_CASE("All error string void", "[all]")
TEST_CASE("All with function error string void", "[all]")
{
std::vector<std::string(*)()> funcs
{
Expand All @@ -131,7 +286,7 @@ TEST_CASE("All error string void", "[all]")
}


TEST_CASE("All string string", "[all]")
TEST_CASE("All with function string string", "[all]")
{
std::vector<std::string(*)(std::string)> funcs
{
Expand All @@ -147,7 +302,7 @@ TEST_CASE("All string string", "[all]")
}


TEST_CASE("All error string string", "[all]")
TEST_CASE("All with function error string string", "[all]")
{
std::vector<std::string(*)(std::string)> funcs
{
Expand Down
16 changes: 8 additions & 8 deletions tests/src/any.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ TEST_CASE("Any error void void", "[any]")
{
std::vector<void(*)()> funcs
{
[] () { throw std::runtime_error{str1}; },
[] () { throw std::runtime_error{str2}; },
[] () {},
};

Expand Down Expand Up @@ -87,7 +87,7 @@ TEST_CASE("Any error void string", "[any]")
{
std::vector<void(*)(std::string)> funcs
{
[] (std::string) { throw std::runtime_error{str1}; },
[] (std::string) { throw std::runtime_error{str2}; },
[] (std::string) {},
};

Expand Down Expand Up @@ -147,15 +147,15 @@ TEST_CASE("Any error string void", "[any]")
{
std::vector<std::string(*)()> funcs
{
[] () -> std::string { throw std::runtime_error{str1}; },
[] () { return std::string{str2}; },
[] () -> std::string { throw std::runtime_error{str2}; },
[] () { return std::string{str1}; },
};

auto future = async::make_resolved_promise().any(funcs).run();

std::string res;
REQUIRE_NOTHROW(res = future.get());
REQUIRE(res == str2);
REQUIRE(res == str1);
}


Expand Down Expand Up @@ -192,15 +192,15 @@ TEST_CASE("Any error string string", "[any]")
{
std::vector<std::string(*)(std::string)> funcs
{
[] (std::string str) -> std::string { throw std::runtime_error{str1}; },
[] (std::string str) { return std::string{str2}; },
[] (std::string str) -> std::string { throw std::runtime_error{str2}; },
[] (std::string str) { return std::string{str1}; },
};

auto future = async::make_resolved_promise(str1).any(funcs).run();

std::string res;
REQUIRE_NOTHROW(res = future.get());
REQUIRE(res == str2);
REQUIRE(res == str1);
}


Expand Down
5 changes: 1 addition & 4 deletions tests/src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,8 @@
#ifndef COMMON_H
#define COMMON_H

#include "constants.h"
#include "test_funcs.h"
#include "test_struct.h"

static constexpr auto aggregate_error_message = "All functions rejected";
static constexpr auto str1 = "Hello World!";
static constexpr auto str2 = "Hello Universe!";

#endif // COMMON_H
25 changes: 25 additions & 0 deletions tests/src/constants.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/******************************************************************************
**
** Copyright (C) 2023 Ivan Pinezhaninov <ivan.pinezhaninov@gmail.com>
**
** This file is part of the async_promise - which can be found at
** https://github.com/IvanPinezhaninov/async_promise/.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
** DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
** OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
** THE USE OR OTHER DEALINGS IN THE SOFTWARE.
**
******************************************************************************/

#ifndef CONSTANTS_H
#define CONSTANTS_H

static constexpr auto aggregate_error_message = "All functions rejected";
static constexpr auto str1 = "Hello World!";
static constexpr auto str2 = "Hello Universe!";

#endif // CONSTANTS_H
Loading

0 comments on commit a528fca

Please sign in to comment.