Added helper for testing leaf-created errors.

This commit is contained in:
Alexander Shaduri
2022-02-16 17:32:20 +04:00
parent 2d7a3a4dd9
commit 5b7c2f6dbb
4 changed files with 73 additions and 5 deletions
+1 -5
View File
@@ -10,11 +10,7 @@ Copyright:
/// Use leaf:: instead of boost::leaf for convenience
namespace leaf {
using namespace boost::leaf;
}
namespace leaf = boost::leaf;
#endif
+1
View File
@@ -76,4 +76,5 @@ add_subdirectory(libdebug)
add_subdirectory(rconfig)
add_subdirectory(ui)
add_subdirectory(test_all)
add_subdirectory(test_helpers)
+21
View File
@@ -0,0 +1,21 @@
###############################################################################
# License: BSD Zero Clause License file
# Copyright:
# (C) 2022 Alexander Shaduri <ashaduri@gmail.com>
###############################################################################
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")
+50
View File
@@ -0,0 +1,50 @@
/******************************************************************************
License: GNU General Public License v3.0 only
Copyright:
(C) 2022 Alexander Shaduri <ashaduri@gmail.com>
******************************************************************************/
/// \file
/// \author Alexander Shaduri
/// \ingroup test_helpers
/// \weakgroup test_helpers
/// @{
#ifndef TEST_HELPERS_H
#define TEST_HELPERS_H
#include "leaf_ns.h"
#include <utility>
/// 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<class... E, class TryBlock>
bool try_expect_errors(TryBlock&& try_block, E&& ... expected)
{
return leaf::try_handle_all(
[&]() -> leaf::result<bool> {
auto r = std::forward<TryBlock>(try_block)();
if (r) {
return false;
}
return r.error();
},
[&](E& e) {
return e == std::forward<E>(expected);
}...,
[] {
return false;
});
}
#endif
/// @}