mirror of
https://github.com/nlohmann/json.git
synced 2026-06-10 14:49:44 +00:00
Add C++17 copies of the test binaries (#3101)
* ⚗️ add C++17 copies of the test binaries * ⚗️ use proper header for filesystem * 🚨 fix warnings * ⚗️ do not use too old compilers with C++17 * ✅ add test * 🔨 add more constraints #3097 * ⚗️ use fix from https://github.com/nlohmann/json/pull/3101#issuecomment-998788786 * ⚗️ use fix from https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90050 * 👷 use published CI image
This commit is contained in:
@@ -41,11 +41,11 @@ TEST_CASE("byte_container_with_subtype")
|
||||
nlohmann::byte_container_with_subtype<std::vector<std::uint8_t>> container;
|
||||
|
||||
CHECK(!container.has_subtype());
|
||||
CHECK(container.subtype() == subtype_type(-1));
|
||||
CHECK(container.subtype() == static_cast<subtype_type>(-1));
|
||||
|
||||
container.clear_subtype();
|
||||
CHECK(!container.has_subtype());
|
||||
CHECK(container.subtype() == subtype_type(-1));
|
||||
CHECK(container.subtype() == static_cast<subtype_type>(-1));
|
||||
|
||||
container.set_subtype(42);
|
||||
CHECK(container.has_subtype());
|
||||
@@ -60,7 +60,7 @@ TEST_CASE("byte_container_with_subtype")
|
||||
|
||||
container.clear_subtype();
|
||||
CHECK(!container.has_subtype());
|
||||
CHECK(container.subtype() == subtype_type(-1));
|
||||
CHECK(container.subtype() == static_cast<subtype_type>(-1));
|
||||
}
|
||||
|
||||
SECTION("comparisons")
|
||||
|
||||
@@ -610,7 +610,7 @@ TEST_CASE("CBOR")
|
||||
|
||||
SECTION("-32768..-129 (int 16)")
|
||||
{
|
||||
for (int16_t i = -32768; i <= int16_t(-129); ++i)
|
||||
for (int16_t i = -32768; i <= static_cast<std::int16_t>(-129); ++i)
|
||||
{
|
||||
CAPTURE(i)
|
||||
|
||||
|
||||
@@ -93,7 +93,7 @@ class SaxEventLogger
|
||||
|
||||
bool start_object(std::size_t elements)
|
||||
{
|
||||
if (elements == std::size_t(-1))
|
||||
if (elements == static_cast<std::size_t>(-1))
|
||||
{
|
||||
events.emplace_back("start_object()");
|
||||
}
|
||||
@@ -118,7 +118,7 @@ class SaxEventLogger
|
||||
|
||||
bool start_array(std::size_t elements)
|
||||
{
|
||||
if (elements == std::size_t(-1))
|
||||
if (elements == static_cast<std::size_t>(-1))
|
||||
{
|
||||
events.emplace_back("start_array()");
|
||||
}
|
||||
|
||||
@@ -93,7 +93,7 @@ struct SaxEventLogger : public nlohmann::json_sax<json>
|
||||
|
||||
bool start_object(std::size_t elements) override
|
||||
{
|
||||
if (elements == std::size_t(-1))
|
||||
if (elements == static_cast<std::size_t>(-1))
|
||||
{
|
||||
events.emplace_back("start_object()");
|
||||
}
|
||||
@@ -118,7 +118,7 @@ struct SaxEventLogger : public nlohmann::json_sax<json>
|
||||
|
||||
bool start_array(std::size_t elements) override
|
||||
{
|
||||
if (elements == std::size_t(-1))
|
||||
if (elements == static_cast<std::size_t>(-1))
|
||||
{
|
||||
events.emplace_back("start_array()");
|
||||
}
|
||||
@@ -148,7 +148,7 @@ struct SaxEventLoggerExitAfterStartObject : public SaxEventLogger
|
||||
{
|
||||
bool start_object(std::size_t elements) override
|
||||
{
|
||||
if (elements == std::size_t(-1))
|
||||
if (elements == static_cast<std::size_t>(-1))
|
||||
{
|
||||
events.emplace_back("start_object()");
|
||||
}
|
||||
@@ -173,7 +173,7 @@ struct SaxEventLoggerExitAfterStartArray : public SaxEventLogger
|
||||
{
|
||||
bool start_array(std::size_t elements) override
|
||||
{
|
||||
if (elements == std::size_t(-1))
|
||||
if (elements == static_cast<std::size_t>(-1))
|
||||
{
|
||||
events.emplace_back("start_array()");
|
||||
}
|
||||
|
||||
@@ -56,7 +56,7 @@ TEST_CASE("hash<nlohmann::json>")
|
||||
|
||||
// number
|
||||
hashes.insert(std::hash<json> {}(json(0)));
|
||||
hashes.insert(std::hash<json> {}(json(unsigned(0))));
|
||||
hashes.insert(std::hash<json> {}(json(static_cast<unsigned>(0))));
|
||||
|
||||
hashes.insert(std::hash<json> {}(json(-1)));
|
||||
hashes.insert(std::hash<json> {}(json(0.0)));
|
||||
@@ -105,7 +105,7 @@ TEST_CASE("hash<nlohmann::ordered_json>")
|
||||
|
||||
// number
|
||||
hashes.insert(std::hash<ordered_json> {}(ordered_json(0)));
|
||||
hashes.insert(std::hash<ordered_json> {}(ordered_json(unsigned(0))));
|
||||
hashes.insert(std::hash<ordered_json> {}(ordered_json(static_cast<unsigned>(0))));
|
||||
|
||||
hashes.insert(std::hash<ordered_json> {}(ordered_json(-1)));
|
||||
hashes.insert(std::hash<ordered_json> {}(ordered_json(0.0)));
|
||||
|
||||
@@ -446,7 +446,7 @@ TEST_CASE("MessagePack")
|
||||
|
||||
SECTION("-32768..-129 (int 16)")
|
||||
{
|
||||
for (int16_t i = -32768; i <= int16_t(-129); ++i)
|
||||
for (int16_t i = -32768; i <= static_cast<std::int16_t>(-129); ++i)
|
||||
{
|
||||
CAPTURE(i)
|
||||
|
||||
|
||||
@@ -170,7 +170,7 @@ TEST_CASE("regression tests 1")
|
||||
json::number_float_t f1{j1};
|
||||
CHECK(std::isnan(f1));
|
||||
|
||||
json j2 = json::number_float_t(NAN);
|
||||
json j2 = static_cast<json::number_float_t>(NAN);
|
||||
CHECK(j2.is_number_float());
|
||||
json::number_float_t f2{j2};
|
||||
CHECK(std::isnan(f2));
|
||||
@@ -183,7 +183,7 @@ TEST_CASE("regression tests 1")
|
||||
json::number_float_t f1{j1};
|
||||
CHECK(!std::isfinite(f1));
|
||||
|
||||
json j2 = json::number_float_t(INFINITY);
|
||||
json j2 = static_cast<json::number_float_t>(INFINITY);
|
||||
CHECK(j2.is_number_float());
|
||||
json::number_float_t f2{j2};
|
||||
CHECK(!std::isfinite(f2));
|
||||
@@ -205,7 +205,7 @@ TEST_CASE("regression tests 1")
|
||||
// check if the actual value was stored
|
||||
CHECK(j2 == 102);
|
||||
|
||||
static_assert(std::is_same<decltype(anon_enum_value), decltype(u)>::value, "");
|
||||
static_assert(std::is_same<decltype(anon_enum_value), decltype(u)>::value, "types must be the same");
|
||||
|
||||
j.push_back(json::object(
|
||||
{
|
||||
@@ -567,17 +567,17 @@ TEST_CASE("regression tests 1")
|
||||
|
||||
SECTION("issue #378 - locale-independent num-to-str")
|
||||
{
|
||||
setlocale(LC_NUMERIC, "de_DE.UTF-8");
|
||||
static_cast<void>(setlocale(LC_NUMERIC, "de_DE.UTF-8"));
|
||||
|
||||
// verify that dumped correctly with '.' and no grouping
|
||||
const json j1 = 12345.67;
|
||||
CHECK(json(12345.67).dump() == "12345.67");
|
||||
setlocale(LC_NUMERIC, "C");
|
||||
static_cast<void>(setlocale(LC_NUMERIC, "C"));
|
||||
}
|
||||
|
||||
SECTION("issue #379 - locale-independent str-to-num")
|
||||
{
|
||||
setlocale(LC_NUMERIC, "de_DE.UTF-8");
|
||||
static_cast<void>(setlocale(LC_NUMERIC, "de_DE.UTF-8"));
|
||||
|
||||
// verify that parsed correctly despite using strtod internally
|
||||
CHECK(json::parse("3.14").get<double>() == 3.14);
|
||||
@@ -910,7 +910,7 @@ TEST_CASE("regression tests 1")
|
||||
++i;
|
||||
}
|
||||
|
||||
std::remove("test.json");
|
||||
static_cast<void>(std::remove("test.json"));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -47,10 +47,82 @@ using ordered_json = nlohmann::ordered_json;
|
||||
#endif
|
||||
|
||||
#ifdef JSON_HAS_CPP_17
|
||||
#include <filesystem>
|
||||
#include <variant>
|
||||
|
||||
#if !defined(JSON_HAS_FILESYSTEM) && !defined(JSON_HAS_EXPERIMENTAL_FILESYSTEM)
|
||||
#if defined(__cpp_lib_filesystem)
|
||||
#define JSON_HAS_FILESYSTEM 1
|
||||
#elif defined(__cpp_lib_experimental_filesystem)
|
||||
#define JSON_HAS_EXPERIMENTAL_FILESYSTEM 1
|
||||
#elif !defined(__has_include)
|
||||
#define JSON_HAS_EXPERIMENTAL_FILESYSTEM 1
|
||||
#elif __has_include(<filesystem>)
|
||||
#define JSON_HAS_FILESYSTEM 1
|
||||
#elif __has_include(<experimental/filesystem>)
|
||||
#define JSON_HAS_EXPERIMENTAL_FILESYSTEM 1
|
||||
#endif
|
||||
|
||||
// std::filesystem does not work on MinGW GCC 8: https://sourceforge.net/p/mingw-w64/bugs/737/
|
||||
#if defined(__MINGW32__) && defined(__GNUC__) && __GNUC__ == 8
|
||||
#undef JSON_HAS_FILESYSTEM
|
||||
#undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
|
||||
#endif
|
||||
|
||||
// no filesystem support before GCC 8: https://en.cppreference.com/w/cpp/compiler_support
|
||||
#if defined(__GNUC__) && __GNUC__ < 8 && !defined(__clang__)
|
||||
#undef JSON_HAS_FILESYSTEM
|
||||
#undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
|
||||
#endif
|
||||
|
||||
// no filesystem support before Clang 7: https://en.cppreference.com/w/cpp/compiler_support
|
||||
#if defined(__clang_major__) && __clang_major__ < 7
|
||||
#undef JSON_HAS_FILESYSTEM
|
||||
#undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
|
||||
#endif
|
||||
|
||||
// no filesystem support before MSVC 19.14: https://en.cppreference.com/w/cpp/compiler_support
|
||||
#if defined(_MSC_VER) && _MSC_VER < 1940
|
||||
#undef JSON_HAS_FILESYSTEM
|
||||
#undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
|
||||
#endif
|
||||
|
||||
// no filesystem support before iOS 13
|
||||
#if defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && __IPHONE_OS_VERSION_MIN_REQUIRED < 130000
|
||||
#undef JSON_HAS_FILESYSTEM
|
||||
#undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
|
||||
#endif
|
||||
|
||||
// no filesystem support before macOS Catalina
|
||||
#if defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED < 101500
|
||||
#undef JSON_HAS_FILESYSTEM
|
||||
#undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifndef JSON_HAS_EXPERIMENTAL_FILESYSTEM
|
||||
#define JSON_HAS_EXPERIMENTAL_FILESYSTEM 0
|
||||
#endif
|
||||
|
||||
#ifndef JSON_HAS_FILESYSTEM
|
||||
#define JSON_HAS_FILESYSTEM 0
|
||||
#endif
|
||||
|
||||
#if JSON_HAS_EXPERIMENTAL_FILESYSTEM
|
||||
#include <experimental/filesystem>
|
||||
namespace nlohmann::detail
|
||||
{
|
||||
namespace std_fs = std::experimental::filesystem;
|
||||
} // namespace nlohmann::detail
|
||||
#elif JSON_HAS_FILESYSTEM
|
||||
#include <filesystem>
|
||||
namespace nlohmann::detail
|
||||
{
|
||||
namespace std_fs = std::filesystem;
|
||||
} // namespace nlohmann::detail
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef JSON_HAS_CPP_20
|
||||
#include <span>
|
||||
#endif
|
||||
@@ -351,9 +423,7 @@ TEST_CASE("regression tests 2")
|
||||
#ifdef JSON_HAS_CPP_17
|
||||
SECTION("issue #1292 - Serializing std::variant causes stack overflow")
|
||||
{
|
||||
static_assert(
|
||||
!std::is_constructible<json, std::variant<int, float>>::value,
|
||||
"");
|
||||
static_assert(!std::is_constructible<json, std::variant<int, float>>::value, "unexpected value");
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -492,15 +562,15 @@ TEST_CASE("regression tests 2")
|
||||
|
||||
SECTION("issue #1805 - A pair<T1, T2> is json constructible only if T1 and T2 are json constructible")
|
||||
{
|
||||
static_assert(!std::is_constructible<json, std::pair<std::string, NotSerializableData>>::value, "");
|
||||
static_assert(!std::is_constructible<json, std::pair<NotSerializableData, std::string>>::value, "");
|
||||
static_assert(std::is_constructible<json, std::pair<int, std::string>>::value, "");
|
||||
static_assert(!std::is_constructible<json, std::pair<std::string, NotSerializableData>>::value, "unexpected result");
|
||||
static_assert(!std::is_constructible<json, std::pair<NotSerializableData, std::string>>::value, "unexpected result");
|
||||
static_assert(std::is_constructible<json, std::pair<int, std::string>>::value, "unexpected result");
|
||||
}
|
||||
SECTION("issue #1825 - A tuple<Args..> is json constructible only if all T in Args are json constructible")
|
||||
{
|
||||
static_assert(!std::is_constructible<json, std::tuple<std::string, NotSerializableData>>::value, "");
|
||||
static_assert(!std::is_constructible<json, std::tuple<NotSerializableData, std::string>>::value, "");
|
||||
static_assert(std::is_constructible<json, std::tuple<int, std::string>>::value, "");
|
||||
static_assert(!std::is_constructible<json, std::tuple<std::string, NotSerializableData>>::value, "unexpected result");
|
||||
static_assert(!std::is_constructible<json, std::tuple<NotSerializableData, std::string>>::value, "unexpected result");
|
||||
static_assert(std::is_constructible<json, std::tuple<int, std::string>>::value, "unexpected result");
|
||||
}
|
||||
|
||||
SECTION("issue #1983 - JSON patch diff for op=add formation is not as per standard (RFC 6902)")
|
||||
@@ -697,7 +767,7 @@ TEST_CASE("regression tests 2")
|
||||
|
||||
SECTION("issue #2825 - Properly constrain the basic_json conversion operator")
|
||||
{
|
||||
static_assert(std::is_copy_assignable<nlohmann::ordered_json>::value, "");
|
||||
static_assert(std::is_copy_assignable<nlohmann::ordered_json>::value, "ordered_json must be copy assignable");
|
||||
}
|
||||
|
||||
SECTION("issue #2958 - Inserting in unordered json using a pointer retains the leading slash")
|
||||
@@ -728,14 +798,16 @@ TEST_CASE("regression tests 2")
|
||||
CHECK(j == k);
|
||||
}
|
||||
|
||||
#ifdef JSON_HAS_CPP_17
|
||||
#if JSON_HAS_FILESYSTEM || JSON_HAS_EXPERIMENTAL_FILESYSTEM
|
||||
SECTION("issue #3070 - Version 3.10.3 breaks backward-compatibility with 3.10.2 ")
|
||||
{
|
||||
std::filesystem::path text_path("/tmp/text.txt");
|
||||
nlohmann::detail::std_fs::path text_path("/tmp/text.txt");
|
||||
json j(text_path);
|
||||
|
||||
const auto j_path = j.get<std::filesystem::path>();
|
||||
const auto j_path = j.get<nlohmann::detail::std_fs::path>();
|
||||
CHECK(j_path == text_path);
|
||||
|
||||
CHECK_THROWS_WITH_AS(nlohmann::detail::std_fs::path(json(1)), "[json.exception.type_error.302] type must be string, but is number", json::type_error);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
Reference in New Issue
Block a user