mirror of
https://github.com/ToruNiina/toml11.git
synced 2025-09-17 00:38:08 +08:00
chore: Merge 'fix-make-error-info'
This commit is contained in:
@@ -41,6 +41,10 @@ struct error_info
|
|||||||
std::string suffix_; // hint or something like that
|
std::string suffix_; // hint or something like that
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// forward decl
|
||||||
|
template<typename TypeConfig>
|
||||||
|
class basic_value;
|
||||||
|
|
||||||
namespace detail
|
namespace detail
|
||||||
{
|
{
|
||||||
inline error_info make_error_info_rec(error_info e)
|
inline error_info make_error_info_rec(error_info e)
|
||||||
@@ -53,6 +57,10 @@ inline error_info make_error_info_rec(error_info e, std::string s)
|
|||||||
return e;
|
return e;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename TC, typename ... Ts>
|
||||||
|
error_info make_error_info_rec(error_info e,
|
||||||
|
const basic_value<TC>& v, std::string msg, Ts&& ... tail);
|
||||||
|
|
||||||
template<typename ... Ts>
|
template<typename ... Ts>
|
||||||
error_info make_error_info_rec(error_info e,
|
error_info make_error_info_rec(error_info e,
|
||||||
source_location loc, std::string msg, Ts&& ... tail)
|
source_location loc, std::string msg, Ts&& ... tail)
|
||||||
|
@@ -2026,12 +2026,16 @@ operator>=(const basic_value<TC>& lhs, const basic_value<TC>& rhs)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// error_info helper
|
// error_info helper
|
||||||
|
namespace detail
|
||||||
|
{
|
||||||
template<typename TC, typename ... Ts>
|
template<typename TC, typename ... Ts>
|
||||||
error_info make_error_info_rec(error_info e,
|
error_info make_error_info_rec(error_info e,
|
||||||
const basic_value<TC>& v, std::string msg, Ts&& ... tail)
|
const basic_value<TC>& v, std::string msg, Ts&& ... tail)
|
||||||
{
|
{
|
||||||
return make_error_info_rec(std::move(e), v.location(), std::move(msg), std::forward<Ts>(tail)...);
|
return make_error_info_rec(std::move(e), v.location(), std::move(msg), std::forward<Ts>(tail)...);
|
||||||
}
|
}
|
||||||
|
} // detail
|
||||||
|
|
||||||
template<typename TC, typename ... Ts>
|
template<typename TC, typename ... Ts>
|
||||||
error_info make_error_info(
|
error_info make_error_info(
|
||||||
std::string title, const basic_value<TC>& v, std::string msg, Ts&& ... tail)
|
std::string title, const basic_value<TC>& v, std::string msg, Ts&& ... tail)
|
||||||
|
@@ -1,6 +1,7 @@
|
|||||||
set(TOML11_TEST_NAMES
|
set(TOML11_TEST_NAMES
|
||||||
test_comments
|
test_comments
|
||||||
test_datetime
|
test_datetime
|
||||||
|
test_error_message
|
||||||
test_find
|
test_find
|
||||||
test_find_or
|
test_find_or
|
||||||
test_format_integer
|
test_format_integer
|
||||||
|
79
tests/test_error_message.cpp
Normal file
79
tests/test_error_message.cpp
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
|
||||||
|
#include "doctest.h"
|
||||||
|
|
||||||
|
#include <toml.hpp>
|
||||||
|
|
||||||
|
TEST_CASE("testing custom error message using source_location")
|
||||||
|
{
|
||||||
|
const toml::value root = toml::parse_str(R"(
|
||||||
|
range = [0, 42]
|
||||||
|
val = 54
|
||||||
|
)");
|
||||||
|
|
||||||
|
const auto& lower = root.at("range").at(0);
|
||||||
|
const auto& upper = root.at("range").at(1);
|
||||||
|
const auto& val = root.at("val");
|
||||||
|
|
||||||
|
const auto err = toml::make_error_info("val not in range",
|
||||||
|
lower.location(), "lower limit is defined here",
|
||||||
|
upper.location(), "upper limit is defined here",
|
||||||
|
val.location(), "this is not in the range",
|
||||||
|
"Hint: upper limit is inclusive"
|
||||||
|
);
|
||||||
|
|
||||||
|
CHECK_EQ(err.title(), "val not in range");
|
||||||
|
CHECK_EQ(err.locations().size(), 3);
|
||||||
|
CHECK_EQ(err.locations().at(0).second, "lower limit is defined here");
|
||||||
|
CHECK_EQ(err.locations().at(1).second, "upper limit is defined here");
|
||||||
|
CHECK_EQ(err.locations().at(2).second, "this is not in the range" );
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("testing custom error message using value")
|
||||||
|
{
|
||||||
|
const toml::value root = toml::parse_str(R"(
|
||||||
|
range = [0, 42]
|
||||||
|
val = 54
|
||||||
|
)");
|
||||||
|
|
||||||
|
const auto& lower = root.at("range").at(0);
|
||||||
|
const auto& upper = root.at("range").at(1);
|
||||||
|
const auto& val = root.at("val");
|
||||||
|
|
||||||
|
const auto err = toml::make_error_info("val not in range",
|
||||||
|
lower, "lower limit is defined here",
|
||||||
|
upper, "upper limit is defined here",
|
||||||
|
val, "this is not in the range",
|
||||||
|
"Hint: upper limit is inclusive"
|
||||||
|
);
|
||||||
|
|
||||||
|
CHECK_EQ(err.title(), "val not in range");
|
||||||
|
CHECK_EQ(err.locations().size(), 3);
|
||||||
|
CHECK_EQ(err.locations().at(0).second, "lower limit is defined here");
|
||||||
|
CHECK_EQ(err.locations().at(1).second, "upper limit is defined here");
|
||||||
|
CHECK_EQ(err.locations().at(2).second, "this is not in the range" );
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("testing custom error message using source_location and value")
|
||||||
|
{
|
||||||
|
const toml::value root = toml::parse_str(R"(
|
||||||
|
range = [0, 42]
|
||||||
|
val = 54
|
||||||
|
)");
|
||||||
|
|
||||||
|
const auto& lower = root.at("range").at(0);
|
||||||
|
const auto& upper = root.at("range").at(1);
|
||||||
|
const auto& val = root.at("val");
|
||||||
|
|
||||||
|
const auto err = toml::make_error_info("val not in range",
|
||||||
|
lower, "lower limit is defined here",
|
||||||
|
upper, "upper limit is defined here",
|
||||||
|
val.location(), "this is not in the range",
|
||||||
|
"Hint: upper limit is inclusive"
|
||||||
|
);
|
||||||
|
|
||||||
|
CHECK_EQ(err.title(), "val not in range");
|
||||||
|
CHECK_EQ(err.locations().size(), 3);
|
||||||
|
CHECK_EQ(err.locations().at(0).second, "lower limit is defined here");
|
||||||
|
CHECK_EQ(err.locations().at(1).second, "upper limit is defined here");
|
||||||
|
CHECK_EQ(err.locations().at(2).second, "this is not in the range" );
|
||||||
|
}
|
Reference in New Issue
Block a user