Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion include/rfl/DefaultVal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ struct DefaultVal {
/// @param _value The value to convert and store (will be moved)
template <class U>
requires(std::is_convertible_v<U, Type>)
DefaultVal(U&& _value) noexcept : value_(std::forward<U>(_value)) {}
DefaultVal(U&& _value) noexcept(std::is_nothrow_constructible_v<Type, U&&>)
: value_(std::forward<U>(_value)) {}

/// Constructs from a DefaultVal with compatible type.
/// @tparam U Type convertible to T
Expand Down
41 changes: 41 additions & 0 deletions tests/json/test_default_val_throwing_initializer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <gtest/gtest.h>

#include <exception>
#include <rfl.hpp>
#include <rfl/json.hpp>
#include <type_traits>

namespace test_default_val_throwing_initializer {

using Positive = rfl::Validator<int, rfl::Minimum<1>>;

static_assert(!std::is_nothrow_constructible_v<rfl::DefaultVal<Positive>, int>,
"Converting to a Validator can throw.");
static_assert(std::is_nothrow_constructible_v<rfl::DefaultVal<int>, short>);

struct InvalidInitializer {
rfl::DefaultVal<Positive> retries = 0;
};

struct ValidInitializer {
rfl::DefaultVal<Positive> retries = 5;
};

TEST(json, test_default_val_throwing_initializer_throws) {
EXPECT_THROW((void)InvalidInitializer{}, std::exception);
}

TEST(json, test_default_val_throwing_initializer_is_an_error_when_reading) {
const auto res = rfl::json::read<InvalidInitializer>(R"({"retries":3})");
ASSERT_FALSE(res);
EXPECT_EQ(res.error().what(),
"Value expected to be greater than or equal to 1, but got 0.");
}

TEST(json, test_default_val_throwing_initializer_valid_default_is_used) {
const auto res = rfl::json::read<ValidInitializer>(R"({})");
ASSERT_TRUE(res) << res.error().what();
EXPECT_EQ(res->retries.value().value(), 5);
}

} // namespace test_default_val_throwing_initializer