/* Copyright(c) 2015 - 2019 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 TEST_CONTINUABLE_HPP__ #define TEST_CONTINUABLE_HPP__ #include #include #include #include #include #include using cti::detail::identity; using cti::detail::util::unused; inline auto to_hint(identity<> /*hint*/) { return identity{}; } template auto to_hint(identity hint) { return hint; } template auto supplier_of(Args&&... args) { return [values = std::make_tuple(std::forward(args)...)]( auto&& promise) mutable { EXPECT_TRUE(promise); cti::detail::traits::unpack( [&](auto&&... passed) { promise.set_value(std::forward(passed)...); }, std::move(values)); }; } template auto exception_supplier_of(Arg&& arg) { return [arg = std::forward(arg)](auto&& promise) mutable { promise.set_exception(std::move(arg)); }; } template class continuation_provider : public ::testing::Test, public Provider { public: template auto invoke(T&& type) { return this->make(identity<>{}, identity{}, [type = std::forward(type)](auto&& promise) mutable { EXPECT_TRUE(promise); promise.set_value(); }); } template auto supply(Args&&... args) { #ifdef UNIT_TEST_READY_CONTINUABLES return cti::make_ready_continuable(std::forward(args)...); #else identity...> arg_types; auto hint_types = to_hint(arg_types); return this->make(arg_types, hint_types, supplier_of(std::forward(args)...)); #endif // UNIT_TEST_READY_CONTINUABLES } template > auto supply_exception(Arg&& arg, Hint hint = {}) { return this->make(hint, to_hint(hint), exception_supplier_of(std::forward(arg))); } }; inline auto empty_caller() { return [](auto&& promise) { promise.set_value(); }; } inline auto empty_continuable() { return cti::make_continuable(empty_caller()); } struct provide_copyable { template auto make(identity, identity, T&& callback) { return cti::make_continuable(std::forward(callback)); } }; struct provide_unique { template auto make(identity, identity, T&& callback) { return cti::make_continuable([ callback = std::forward(callback), guard = std::make_unique(0) ](auto&&... args) mutable { (void)(*guard); return std::move(callback)(std::forward(args)...); }); } }; template