Unverified Commit 39546432 authored by Shariar Azad Riday's avatar Shariar Azad Riday Committed by GitHub

Add requirement_failed exception

parent 16b40d40
...@@ -30,6 +30,7 @@ caf_add_component( ...@@ -30,6 +30,7 @@ caf_add_component(
caf/test/nesting_error.cpp caf/test/nesting_error.cpp
caf/test/registry.cpp caf/test/registry.cpp
caf/test/reporter.cpp caf/test/reporter.cpp
caf/test/requirement_error.cpp
caf/test/runnable.cpp caf/test/runnable.cpp
caf/test/runner.cpp caf/test/runner.cpp
caf/test/scenario.cpp caf/test/scenario.cpp
......
// This file is part of CAF, the C++ Actor Framework. See the file LICENSE in
// the main distribution directory for license terms and copyright or visit
// https://github.com/actor-framework/actor-framework/blob/master/LICENSE.
#include "caf/test/requirement_error.hpp"
#include "caf/detail/format.hpp"
namespace caf::test {
std::string requirement_error::message() const {
return detail::format("requirement failed at {}:{}", loc_.file_name(),
loc_.line());
}
[[noreturn]] void
requirement_error::raise_impl(const detail::source_location& loc) {
#ifdef CAF_ENABLE_EXCEPTIONS
throw requirement_error{loc};
#else
auto msg = requirement_error{loc}.message();
fprintf(stderr, "[FATAL] critical error: %s\n", msg.c_str());
abort();
#endif
}
} // namespace caf::test
// This file is part of CAF, the C++ Actor Framework. See the file LICENSE in
// the main distribution directory for license terms and copyright or visit
// https://github.com/actor-framework/actor-framework/blob/master/LICENSE.
#pragma once
#include "caf/detail/source_location.hpp"
#include "caf/detail/test_export.hpp"
#include <string>
namespace caf::test {
/// Thrown when a requirement check fails. When `CAF_ENABLE_EXCEPTIONS` is off,
/// the `raise` functions terminate the program instead.
class CAF_TEST_EXPORT requirement_error {
public:
constexpr requirement_error(const requirement_error&) noexcept = default;
constexpr requirement_error& operator=(const requirement_error&) noexcept
= default;
/// Returns a human-readable error message.
std::string message() const;
/// Returns the source location of the error.
constexpr const detail::source_location& location() const noexcept {
return loc_;
}
/// Throws a `requirement_error` to indicate that requirement check failed.
[[noreturn]] static void raise(const detail::source_location& loc
= detail::source_location::current()) {
raise_impl(loc);
}
private:
constexpr explicit requirement_error(
const detail::source_location& loc) noexcept
: loc_(loc) {
// nop
}
[[noreturn]] static void raise_impl(const detail::source_location& loc);
detail::source_location loc_;
};
} // namespace caf::test
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include "caf/test/block_type.hpp" #include "caf/test/block_type.hpp"
#include "caf/test/fwd.hpp" #include "caf/test/fwd.hpp"
#include "caf/test/reporter.hpp" #include "caf/test/reporter.hpp"
#include "caf/test/requirement_error.hpp"
#include "caf/config.hpp" #include "caf/config.hpp"
#include "caf/deep_to_string.hpp" #include "caf/deep_to_string.hpp"
...@@ -53,7 +54,7 @@ public: ...@@ -53,7 +54,7 @@ public:
} else { } else {
reporter::instance().fail(fwl.value, fwl.location); reporter::instance().fail(fwl.value, fwl.location);
} }
CAF_RAISE_ERROR(std::logic_error, "requirement failed: abort test"); requirement_error::raise(fwl.location);
} }
/// Generates a message with the INFO severity level. /// Generates a message with the INFO severity level.
......
...@@ -170,6 +170,8 @@ int runner::run(int argc, char** argv) { ...@@ -170,6 +170,8 @@ int runner::run(int argc, char** argv) {
} catch (const nesting_error& ex) { } catch (const nesting_error& ex) {
default_reporter->unhandled_exception(ex.message(), ex.location()); default_reporter->unhandled_exception(ex.message(), ex.location());
default_reporter->end_test(); default_reporter->end_test();
} catch (const requirement_error& ex) {
default_reporter->end_test();
} catch (const std::exception& ex) { } catch (const std::exception& ex) {
default_reporter->unhandled_exception(ex.what()); default_reporter->unhandled_exception(ex.what());
default_reporter->end_test(); default_reporter->end_test();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment