179 lines
6.0 KiB
C++
179 lines
6.0 KiB
C++
#include <Void/Functional/Adapters.hpp>
|
|
#include <Void/Functional/Error.hpp>
|
|
#include <Void/Functional/Result.hpp>
|
|
#include <Void/Testing/Assert.hpp>
|
|
#include <Void/Testing/Test.hpp>
|
|
|
|
#include <cerrno>
|
|
#include <cstddef>
|
|
#include <exception>
|
|
#include <expected>
|
|
#include <memory>
|
|
#include <new>
|
|
#include <optional>
|
|
#include <stdexcept>
|
|
#include <string>
|
|
#include <type_traits>
|
|
|
|
Void::Testing::Test compact_status_is_noexcept_and_empty_message()
|
|
{
|
|
constexpr Void::Result::Status st{
|
|
Void::Result::ErrorKind::overloaded,
|
|
Void::Result::ErrorCode{Void::Result::kAsyncErrorDomain, 42}};
|
|
static_assert(std::is_nothrow_constructible_v<Void::Result::Error,
|
|
Void::Result::Status>);
|
|
static_assert(std::is_nothrow_move_constructible_v<Void::Result::Error>);
|
|
static_assert(noexcept(Void::Result::fail(st)));
|
|
|
|
Void::Result::Error err{st};
|
|
auto r = Void::Result::fail(st);
|
|
|
|
Void::Testing::expect(err.kind() == Void::Result::ErrorKind::overloaded);
|
|
Void::Testing::expect(err.code() == st.code);
|
|
Void::Testing::expect(err.message().empty());
|
|
Void::Testing::expect(err.cause() == nullptr);
|
|
Void::Testing::expect(!err.truncated());
|
|
Void::Testing::expect(r.error().kind() ==
|
|
Void::Result::ErrorKind::overloaded);
|
|
}
|
|
|
|
Void::Testing::Test context_chain_caps_at_16_and_preserves_root()
|
|
{
|
|
Void::Result::Error err{
|
|
Void::Result::ErrorKind::not_found, "root missing file",
|
|
Void::Result::ErrorCode{Void::Result::kSystemErrorDomain, ENOENT},
|
|
std::error_code{ENOENT, std::generic_category()}};
|
|
|
|
for (int i = 1; i <= 19; ++i) {
|
|
err = std::move(err).with_context("layer_" + std::to_string(i));
|
|
}
|
|
|
|
Void::Testing::expect_eq(err.depth(),
|
|
Void::Result::kDefaultMaxErrorFrames);
|
|
Void::Testing::expect(err.truncated());
|
|
Void::Testing::expect(err.kind() == Void::Result::ErrorKind::not_found);
|
|
Void::Testing::expect_eq(err.code().domain,
|
|
Void::Result::kSystemErrorDomain);
|
|
Void::Testing::expect_eq(err.code().value, ENOENT);
|
|
Void::Testing::expect_eq(err.system_code().value(), ENOENT);
|
|
Void::Testing::expect_eq(err.message(), std::string{"layer_19"});
|
|
|
|
const Void::Result::Error* tail = &err;
|
|
std::size_t count = 1;
|
|
while (tail->cause() != nullptr) {
|
|
tail = tail->cause().get();
|
|
++count;
|
|
}
|
|
Void::Testing::expect_eq(count, Void::Result::kDefaultMaxErrorFrames);
|
|
Void::Testing::expect_eq(tail->message(), std::string{"root missing file"});
|
|
|
|
const std::string rendered = err.format();
|
|
Void::Testing::expect(rendered.find("root missing file") !=
|
|
std::string::npos);
|
|
Void::Testing::expect(rendered.find("[truncated:") != std::string::npos);
|
|
}
|
|
|
|
Void::Testing::Test message_and_format_bounds_enforced()
|
|
{
|
|
std::string huge(3000, 'x');
|
|
Void::Result::Error single{Void::Result::ErrorKind::invalid_argument,
|
|
std::move(huge)};
|
|
Void::Testing::expect(single.truncated());
|
|
Void::Testing::expect(single.message().size() <=
|
|
Void::Result::kDefaultMaxErrorMessageBytes);
|
|
Void::Testing::expect(single.message().ends_with("...[truncated]"));
|
|
|
|
// Build a 16-frame chain of 1 KiB messages to check total formatted cap
|
|
// (~16+ KiB > kDefaultMaxFormattedErrorBytes).
|
|
std::string chunk(1000, 'y');
|
|
Void::Result::Error chain{Void::Result::ErrorKind::corrupt, chunk};
|
|
for (int i = 0; i < 16; ++i) {
|
|
chain = std::move(chain).with_context(chunk);
|
|
}
|
|
const std::string formatted = chain.format();
|
|
Void::Testing::expect(formatted.size() <=
|
|
Void::Result::kDefaultMaxFormattedErrorBytes);
|
|
}
|
|
|
|
Void::Testing::Test adapt_handles_move_only_void_and_skips_mapper_on_ok()
|
|
{
|
|
bool mapper_called = false;
|
|
auto ok_ptr = Void::Result::adapt(
|
|
std::expected<std::unique_ptr<int>, int>{std::make_unique<int>(99)},
|
|
[&](int) {
|
|
mapper_called = true;
|
|
return Void::Result::Error{"bad"};
|
|
});
|
|
Void::Testing::expect(!mapper_called);
|
|
Void::Testing::expect(ok_ptr.has_value() && **ok_ptr == 99);
|
|
|
|
auto err_void = Void::Result::adapt(
|
|
std::expected<void, int>{std::unexpected{7}}, [&](int v) {
|
|
mapper_called = true;
|
|
return Void::Result::Error{
|
|
Void::Result::ErrorKind::invalid_argument,
|
|
"code " + std::to_string(v)};
|
|
});
|
|
Void::Testing::expect(mapper_called);
|
|
Void::Testing::expect(!err_void.has_value());
|
|
Void::Testing::expect(err_void.error().kind() ==
|
|
Void::Result::ErrorKind::invalid_argument);
|
|
}
|
|
|
|
Void::Testing::Test try_invoke_maps_known_and_rethrows_unknown_and_bad_alloc()
|
|
{
|
|
auto map_known = [](std::exception_ptr ep)
|
|
-> std::optional<Void::Result::Error> {
|
|
try {
|
|
std::rethrow_exception(ep);
|
|
} catch (const std::invalid_argument& ex) {
|
|
return Void::Result::Error{
|
|
Void::Result::ErrorKind::invalid_argument, ex.what()};
|
|
} catch (...) {
|
|
return std::nullopt;
|
|
}
|
|
};
|
|
|
|
auto mapped = Void::Result::try_invoke(
|
|
[]() -> int { throw std::invalid_argument{"bad arg"}; }, map_known);
|
|
Void::Testing::expect(!mapped.has_value());
|
|
Void::Testing::expect(mapped.error().kind() ==
|
|
Void::Result::ErrorKind::invalid_argument);
|
|
|
|
bool rethrew_unknown = false;
|
|
try {
|
|
(void)Void::Result::try_invoke(
|
|
[]() -> void { throw std::runtime_error{"unhandled"}; }, map_known);
|
|
} catch (const std::runtime_error&) {
|
|
rethrew_unknown = true;
|
|
}
|
|
Void::Testing::expect(rethrew_unknown);
|
|
|
|
bool rethrew_bad_alloc = false;
|
|
auto catch_all_mapper = [](std::exception_ptr)
|
|
-> std::optional<Void::Result::Error> {
|
|
return Void::Result::Error{"should not catch bad_alloc"};
|
|
};
|
|
try {
|
|
(void)Void::Result::try_invoke(
|
|
[]() -> int { throw std::bad_alloc{}; }, catch_all_mapper);
|
|
} catch (const std::bad_alloc&) {
|
|
rethrew_bad_alloc = true;
|
|
}
|
|
Void::Testing::expect(rethrew_bad_alloc);
|
|
}
|
|
|
|
Void::Testing::Test from_errno_maps_categories()
|
|
{
|
|
const auto nf = Void::Result::from_errno(ENOENT);
|
|
Void::Testing::expect(nf.kind() == Void::Result::ErrorKind::not_found);
|
|
Void::Testing::expect_eq(nf.system_code().value(), ENOENT);
|
|
|
|
const auto to = Void::Result::from_errno(ETIMEDOUT, "socket wait");
|
|
Void::Testing::expect(to.kind() == Void::Result::ErrorKind::timeout);
|
|
Void::Testing::expect_eq(to.message(), std::string{"socket wait"});
|
|
|
|
const auto ex = Void::Result::from_errno(ENOSPC);
|
|
Void::Testing::expect(ex.kind() == Void::Result::ErrorKind::exhausted);
|
|
}
|