1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include <doctest/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);
}
|