diff --git a/dependencies/leaf/convenience/leaf_ns.h b/dependencies/leaf/convenience/leaf_ns.h index 8f5fd88..80771ca 100644 --- a/dependencies/leaf/convenience/leaf_ns.h +++ b/dependencies/leaf/convenience/leaf_ns.h @@ -10,11 +10,7 @@ Copyright: /// Use leaf:: instead of boost::leaf for convenience -namespace leaf { - - using namespace boost::leaf; - -} +namespace leaf = boost::leaf; #endif diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e82bbb5..f0b66b5 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -76,4 +76,5 @@ add_subdirectory(libdebug) add_subdirectory(rconfig) add_subdirectory(ui) add_subdirectory(test_all) +add_subdirectory(test_helpers) diff --git a/src/test_helpers/CMakeLists.txt b/src/test_helpers/CMakeLists.txt new file mode 100644 index 0000000..5ef749c --- /dev/null +++ b/src/test_helpers/CMakeLists.txt @@ -0,0 +1,21 @@ +############################################################################### +# License: BSD Zero Clause License file +# Copyright: +# (C) 2022 Alexander Shaduri +############################################################################### + +if (NOT APP_BUILD_TESTS) + set_directory_properties(PROPERTIES EXCLUDE_FROM_ALL true) +else() + set_directory_properties(PROPERTIES EXCLUDE_FROM_ALL false) +endif() + + +add_library(test_helpers INTERFACE) + +# Relative sources are allowed only since cmake 3.13. +target_sources(test_helpers INTERFACE + ${CMAKE_CURRENT_SOURCE_DIR}/test_helpers.h +) + +target_include_directories(test_helpers INTERFACE "${CMAKE_SOURCE_DIR}/src") diff --git a/src/test_helpers/test_helpers.h b/src/test_helpers/test_helpers.h new file mode 100644 index 0000000..bccd1d7 --- /dev/null +++ b/src/test_helpers/test_helpers.h @@ -0,0 +1,50 @@ +/****************************************************************************** +License: GNU General Public License v3.0 only +Copyright: + (C) 2022 Alexander Shaduri +******************************************************************************/ +/// \file +/// \author Alexander Shaduri +/// \ingroup test_helpers +/// \weakgroup test_helpers +/// @{ + +#ifndef TEST_HELPERS_H +#define TEST_HELPERS_H + +#include "leaf_ns.h" + +#include + + + +/// From https://github.com/boostorg/leaf/issues/42 +/// \tparam TryBlock A lambda +/// \tparam E... Any number of error values with _different_ types. +/// \return true if try_block fails AND produces at least one error object that +/// compares equal to the specified error value of the same type. +template +bool try_expect_errors(TryBlock&& try_block, E&& ... expected) +{ + return leaf::try_handle_all( + [&]() -> leaf::result { + auto r = std::forward(try_block)(); + if (r) { + return false; + } + return r.error(); + }, + [&](E& e) { + return e == std::forward(expected); + }..., + [] { + return false; + }); +} + + + + +#endif + +/// @}