mirror of
https://github.com/ToruNiina/toml11.git
synced 2025-09-18 10:28:09 +08:00
Merge branch 'main' into v4_1_0
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
set(TOML11_TEST_NAMES
|
||||
test_comments
|
||||
test_datetime
|
||||
test_error_message
|
||||
test_find
|
||||
test_find_or
|
||||
test_format_integer
|
||||
@@ -22,6 +23,7 @@ set(TOML11_TEST_NAMES
|
||||
test_parse_table
|
||||
test_result
|
||||
test_scanner
|
||||
test_serialize
|
||||
test_syntax_boolean
|
||||
test_syntax_integer
|
||||
test_syntax_floating
|
||||
|
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" );
|
||||
}
|
150
tests/test_serialize.cpp
Normal file
150
tests/test_serialize.cpp
Normal file
@@ -0,0 +1,150 @@
|
||||
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
|
||||
#include "doctest.h"
|
||||
|
||||
#include <toml.hpp>
|
||||
|
||||
#include <clocale>
|
||||
|
||||
TEST_CASE("testing serialization")
|
||||
{
|
||||
using namespace toml::literals::toml_literals;
|
||||
|
||||
const auto spec_example = R"(# This is a TOML document
|
||||
|
||||
title = "TOML Example"
|
||||
|
||||
[owner]
|
||||
name = "Tom Preston-Werner"
|
||||
dob = 1979-05-27T07:32:00-08:00
|
||||
|
||||
[database]
|
||||
enabled = true
|
||||
ports = [ 8000, 8001, 8002 ]
|
||||
data = [ ["delta", "phi"], [3.14] ]
|
||||
temp_targets = { cpu = 79.5, case = 72.0 }
|
||||
|
||||
[servers]
|
||||
|
||||
[servers.alpha]
|
||||
ip = "10.0.0.1"
|
||||
role = "frontend"
|
||||
|
||||
[servers.beta]
|
||||
ip = "10.0.0.2"
|
||||
role = "backend")"_toml;
|
||||
|
||||
// format and parse
|
||||
const auto v = toml::parse_str(toml::format(spec_example));
|
||||
|
||||
CHECK_EQ(v, spec_example);
|
||||
}
|
||||
|
||||
TEST_CASE("testing serialization with complicated keys")
|
||||
{
|
||||
using namespace toml::literals::toml_literals;
|
||||
|
||||
const auto spec_example = R"(
|
||||
[keys]
|
||||
|
||||
key = "value"
|
||||
bare_key = "value"
|
||||
bare-key = "value"
|
||||
1234 = "value"
|
||||
|
||||
"127.0.0.1" = "value"
|
||||
"character encoding" = "value"
|
||||
"ʎǝʞ" = "value"
|
||||
'key2' = "value"
|
||||
'quoted "value"' = "value"
|
||||
|
||||
"" = "blank"
|
||||
|
||||
fruits.apple.skin = "thin"
|
||||
fruits.apple.color = "red"
|
||||
|
||||
fruits.orange.skin = "thick"
|
||||
fruits.orange.color = "orange"
|
||||
|
||||
site."google.com" = true
|
||||
3.14159 = "pi"
|
||||
)"_toml;
|
||||
|
||||
// format and parse
|
||||
const auto v = toml::parse_str(toml::format(spec_example));
|
||||
|
||||
CHECK_EQ(v, spec_example);
|
||||
}
|
||||
|
||||
TEST_CASE("testing serialization with array of tables")
|
||||
{
|
||||
using namespace toml::literals::toml_literals;
|
||||
|
||||
const auto spec_example = R"(
|
||||
points = [ { x = 1, y = 2, z = 3 },
|
||||
{ x = 7, y = 8, z = 9 },
|
||||
{ x = 2, y = 4, z = 8 } ]
|
||||
|
||||
[[products]]
|
||||
name = "Hammer"
|
||||
sku = 738594937
|
||||
|
||||
[[products]] # empty table within the array
|
||||
|
||||
[[products]]
|
||||
name = "Nail"
|
||||
sku = 284758393
|
||||
|
||||
color = "gray"
|
||||
|
||||
[[fruits]]
|
||||
name = "apple"
|
||||
|
||||
[fruits.physical] # subtable
|
||||
color = "red"
|
||||
shape = "round"
|
||||
|
||||
[[fruits.varieties]] # nested array of tables
|
||||
name = "red delicious"
|
||||
|
||||
[[fruits.varieties]]
|
||||
name = "granny smith"
|
||||
|
||||
[[fruits]]
|
||||
name = "banana"
|
||||
|
||||
[[fruits.varieties]]
|
||||
name = "plantain"
|
||||
)"_toml;
|
||||
|
||||
// format and parse
|
||||
const auto v = toml::parse_str(toml::format(spec_example));
|
||||
|
||||
CHECK_EQ(v, spec_example);
|
||||
}
|
||||
|
||||
TEST_CASE("testing serialization with locale")
|
||||
{
|
||||
std::string current_locale = std::setlocale(LC_ALL, nullptr);
|
||||
|
||||
// fr_FR is a one of locales that uses `,` as a decimal separator.
|
||||
if(const char* try_hyphen = std::setlocale(LC_ALL, "fr_FR.UTF-8"))
|
||||
{
|
||||
current_locale = std::string(try_hyphen);
|
||||
}
|
||||
else if(const char* try_nohyphen = std::setlocale(LC_ALL, "fr_FR.utf8"))
|
||||
{
|
||||
current_locale = std::string(try_nohyphen);
|
||||
}
|
||||
|
||||
MESSAGE("current_locale = ", current_locale);
|
||||
|
||||
const auto v1 = toml::parse_str(R"(
|
||||
pi = 3.1415_9265
|
||||
large_int = 123_456_789
|
||||
)");
|
||||
const auto v2 = toml::parse_str(toml::format(v1));
|
||||
|
||||
// actually, it checkl if v1 is serialized correctly under FR locale
|
||||
// where 3.1415 -> 3,1415
|
||||
CHECK_EQ(v1, v2);
|
||||
}
|
Reference in New Issue
Block a user