mirror of
https://github.com/ashaduri/gsmartcontrol.git
synced 2026-09-25 13:25:37 +00:00
Added helper for testing leaf-created errors.
This commit is contained in:
+1
-5
@@ -10,11 +10,7 @@ Copyright:
|
||||
|
||||
|
||||
/// Use leaf:: instead of boost::leaf for convenience
|
||||
namespace leaf {
|
||||
|
||||
using namespace boost::leaf;
|
||||
|
||||
}
|
||||
namespace leaf = boost::leaf;
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
@@ -76,4 +76,5 @@ add_subdirectory(libdebug)
|
||||
add_subdirectory(rconfig)
|
||||
add_subdirectory(ui)
|
||||
add_subdirectory(test_all)
|
||||
add_subdirectory(test_helpers)
|
||||
|
||||
|
||||
@@ -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")
|
||||
@@ -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
|
||||
|
||||
/// @}
|
||||
Reference in New Issue
Block a user