/* /~` _ _ _|_. _ _ |_ | _ \_,(_)| | | || ||_|(_||_)|(/_ https://github.com/Naios/continuable v2.0.0 Copyright(c) 2015 - 2018 Denis Blank Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and / or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions : The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 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 CONTINUABLE_DETAIL_TESTING_HPP_INCLUDED #define CONTINUABLE_DETAIL_TESTING_HPP_INCLUDED #include #include #include #include #include #include #include namespace cti { namespace detail { namespace testing { template void assert_async_completion(C&& continuable) { auto called = std::make_shared(false); std::forward(continuable) .then([called](auto&&... args) { ASSERT_FALSE(*called); *called = true; // Workaround for our known GCC bug. util::unused(std::forward(args)...); }) .fail([](cti::error_type /*error*/) { // ... FAIL(); }); ASSERT_TRUE(*called); } template void assert_async_exception_completion(C&& continuable) { auto called = std::make_shared(false); std::forward(continuable) .then([](auto&&... args) { // Workaround for our known GCC bug. util::unused(std::forward(args)...); // ... FAIL(); }) .fail([called](cti::error_type /*error*/) { ASSERT_FALSE(*called); *called = true; }); ASSERT_TRUE(*called); } template void assert_async_never_completed(C&& continuable) { std::forward(continuable) .then([](auto&&... args) { // Workaround for our known GCC bug. util::unused(std::forward(args)...); FAIL(); }) .fail([](cti::error_type /*error*/) { // ... FAIL(); }); } template void assert_async_validation(C&& continuable, V&& validator) { assert_async_completion( std::forward(continuable) .then([validator = std::forward(validator)](auto&&... args) mutable { validator(std::forward(args)...); })); } /// Expects that the continuable is finished with the given arguments template void assert_async_binary_validation(V&& validator, C&& continuable, Args&&... expected) { using size = std::integral_constant; assert_async_validation(std::forward(continuable), [ expected_pack = std::make_tuple(std::forward(expected)...), validator = std::forward(validator) ](auto&&... args) mutable { static_assert(size::value == sizeof...(args), "Async completion handler called with a different count " "of arguments!"); validator(std::make_tuple(std::forward(args)...), expected_pack); }); } /// Expects that the continuable is finished with the given arguments template void assert_async_binary_exception_validation(V&& validator, C&& continuable, Args&& expected) { auto called = std::make_shared(false); std::forward(continuable) .then([](auto&&... args) { // Workaround for our known GCC bug. util::unused(std::forward(args)...); // ... FAIL(); }) .fail([ called, validator = std::forward(validator), expected = std::forward(expected) ](types::error_type error) { ASSERT_FALSE(*called); *called = true; #if defined(CONTINUABLE_WITH_EXCEPTIONS) try { std::rethrow_exception(error); } catch (std::decay_t& exception) { validator(exception, expected); } catch (...) { FAIL(); } #else validator(error, expected); #endif }); ASSERT_TRUE(*called); } inline auto expecting_eq_check() { return [](auto&& expected, auto&& actual) { EXPECT_EQ(std::forward(expected), std::forward(actual)); }; } inline auto asserting_eq_check() { return [](auto&& expected, auto&& actual) { ASSERT_EQ(std::forward(expected), std::forward(actual)); }; } template void assert_async_types(C&& continuable, traits::identity expected) { assert_async_validation( std::forward(continuable), [&](auto... actualPack) { auto actual = traits::identity{}; util::unused(expected, actual, std::forward(actualPack)...); static_assert( std::is_same, std::decay_t>::value, "The called arguments don't match with the expected ones!"); }); } } // namespace testing } // namespace detail } // namespace cti #endif // CONTINUABLE_DETAIL_TESTING_HPP_INCLUDED