mirror of
https://github.com/nlohmann/json.git
synced 2024-08-23 13:32:28 +02:00
303 lines
8.4 KiB
C++
303 lines
8.4 KiB
C++
#define CATCH_CONFIG_MAIN
|
|
#include "catch.hpp"
|
|
|
|
#include "json.hpp"
|
|
using nlohmann::json;
|
|
|
|
#include <array>
|
|
#include <deque>
|
|
#include <forward_list>
|
|
#include <list>
|
|
#include <map>
|
|
#include <set>
|
|
#include <string>
|
|
#include <unordered_map>
|
|
#include <unordered_set>
|
|
#include <vector>
|
|
|
|
TEST_CASE("Constructors")
|
|
{
|
|
SECTION("create an empty value with a given type")
|
|
{
|
|
SECTION("null")
|
|
{
|
|
auto t = json::value_t::null;
|
|
json j(t);
|
|
CHECK(j.type() == t);
|
|
}
|
|
|
|
SECTION("object")
|
|
{
|
|
auto t = json::value_t::object;
|
|
json j(t);
|
|
CHECK(j.type() == t);
|
|
}
|
|
|
|
SECTION("array")
|
|
{
|
|
auto t = json::value_t::array;
|
|
json j(t);
|
|
CHECK(j.type() == t);
|
|
}
|
|
|
|
SECTION("boolean")
|
|
{
|
|
auto t = json::value_t::boolean;
|
|
json j(t);
|
|
CHECK(j.type() == t);
|
|
}
|
|
|
|
SECTION("string")
|
|
{
|
|
auto t = json::value_t::string;
|
|
json j(t);
|
|
CHECK(j.type() == t);
|
|
}
|
|
|
|
SECTION("number_integer")
|
|
{
|
|
auto t = json::value_t::number_integer;
|
|
json j(t);
|
|
CHECK(j.type() == t);
|
|
}
|
|
|
|
SECTION("number_float")
|
|
{
|
|
auto t = json::value_t::number_float;
|
|
json j(t);
|
|
CHECK(j.type() == t);
|
|
}
|
|
}
|
|
|
|
SECTION("create a null object (implicitly)")
|
|
{
|
|
SECTION("no parameter")
|
|
{
|
|
json j{};
|
|
CHECK(j.type() == json::value_t::null);
|
|
}
|
|
}
|
|
|
|
SECTION("create a null object (explicitly)")
|
|
{
|
|
SECTION("parameter")
|
|
{
|
|
json j(nullptr);
|
|
CHECK(j.type() == json::value_t::null);
|
|
}
|
|
}
|
|
|
|
SECTION("create an object (explicit)")
|
|
{
|
|
SECTION("empty object")
|
|
{
|
|
json::object_t o;
|
|
json j(o);
|
|
CHECK(j.type() == json::value_t::object);
|
|
}
|
|
|
|
SECTION("filled object")
|
|
{
|
|
json::object_t o {{"a", json(1)}, {"b", json(2.2)}, {"c", json(false)}, {"d", json("string")}, {"e", json()}};
|
|
json j(o);
|
|
CHECK(j.type() == json::value_t::object);
|
|
}
|
|
}
|
|
|
|
SECTION("create an object (implicit)")
|
|
{
|
|
// reference object
|
|
json::object_t o_reference {{"a", json(1)}, {"b", json(2.2)}, {"c", json(false)}, {"d", json("string")}, {"e", json()}};
|
|
json j_reference(o_reference);
|
|
|
|
SECTION("std::map<std::string, json>")
|
|
{
|
|
std::map<std::string, json> o {{"a", json(1)}, {"b", json(2.2)}, {"c", json(false)}, {"d", json("string")}, {"e", json()}};
|
|
json j(o);
|
|
CHECK(j.type() == json::value_t::object);
|
|
CHECK(j == j_reference);
|
|
}
|
|
|
|
SECTION("std::map<const char*, json>")
|
|
{
|
|
std::map<const char*, json> o {{"a", json(1)}, {"b", json(2.2)}, {"c", json(false)}, {"d", json("string")}, {"e", json()}};
|
|
json j(o);
|
|
CHECK(j.type() == json::value_t::object);
|
|
CHECK(j == j_reference);
|
|
}
|
|
|
|
SECTION("std::multimap<std::string, json>")
|
|
{
|
|
std::multimap<std::string, json> o {{"a", json(1)}, {"b", json(2.2)}, {"c", json(false)}, {"d", json("string")}, {"e", json()}};
|
|
json j(o);
|
|
CHECK(j.type() == json::value_t::object);
|
|
CHECK(j == j_reference);
|
|
}
|
|
|
|
SECTION("std::unordered_map<std::string, json>")
|
|
{
|
|
std::unordered_map<std::string, json> o {{"a", json(1)}, {"b", json(2.2)}, {"c", json(false)}, {"d", json("string")}, {"e", json()}};
|
|
json j(o);
|
|
CHECK(j.type() == json::value_t::object);
|
|
CHECK(j == j_reference);
|
|
}
|
|
|
|
SECTION("std::unordered_multimap<std::string, json>")
|
|
{
|
|
std::unordered_multimap<std::string, json> o {{"a", json(1)}, {"b", json(2.2)}, {"c", json(false)}, {"d", json("string")}, {"e", json()}};
|
|
json j(o);
|
|
CHECK(j.type() == json::value_t::object);
|
|
CHECK(j == j_reference);
|
|
}
|
|
|
|
SECTION("associative container literal")
|
|
{
|
|
json j({{"a", json(1)}, {"b", json(2.2)}, {"c", json(false)}, {"d", json("string")}, {"e", json()}});
|
|
CHECK(j.type() == json::value_t::object);
|
|
CHECK(j == j_reference);
|
|
}
|
|
}
|
|
|
|
SECTION("create an array (explicit)")
|
|
{
|
|
SECTION("empty array")
|
|
{
|
|
json::array_t a;
|
|
json j(a);
|
|
CHECK(j.type() == json::value_t::array);
|
|
}
|
|
|
|
SECTION("filled array")
|
|
{
|
|
json::array_t a {json(1), json(2.2), json(false), json("string"), json()};
|
|
json j(a);
|
|
CHECK(j.type() == json::value_t::array);
|
|
}
|
|
}
|
|
|
|
SECTION("create an array (implicit)")
|
|
{
|
|
// reference array
|
|
json::array_t a_reference {json(1), json(2.2), json(false), json("string"), json()};
|
|
json j_reference(a_reference);
|
|
|
|
SECTION("std::list<json>")
|
|
{
|
|
std::list<json> a {json(1), json(2.2), json(false), json("string"), json()};
|
|
json j(a);
|
|
CHECK(j.type() == json::value_t::array);
|
|
CHECK(j == j_reference);
|
|
}
|
|
|
|
SECTION("std::forward_list<json>")
|
|
{
|
|
std::forward_list<json> a {json(1), json(2.2), json(false), json("string"), json()};
|
|
json j(a);
|
|
CHECK(j.type() == json::value_t::array);
|
|
CHECK(j == j_reference);
|
|
}
|
|
|
|
SECTION("std::array<json>")
|
|
{
|
|
std::array<json, 5> a {{json(1), json(2.2), json(false), json("string"), json()}};
|
|
json j(a);
|
|
CHECK(j.type() == json::value_t::array);
|
|
CHECK(j == j_reference);
|
|
}
|
|
|
|
SECTION("std::vector<json>")
|
|
{
|
|
std::vector<json> a {json(1), json(2.2), json(false), json("string"), json()};
|
|
json j(a);
|
|
CHECK(j.type() == json::value_t::array);
|
|
CHECK(j == j_reference);
|
|
}
|
|
|
|
SECTION("std::deque<json>")
|
|
{
|
|
std::deque<json> a {json(1), json(2.2), json(false), json("string"), json()};
|
|
json j(a);
|
|
CHECK(j.type() == json::value_t::array);
|
|
CHECK(j == j_reference);
|
|
}
|
|
|
|
SECTION("std::set<json>")
|
|
{
|
|
std::set<json> a {json(1), json(2.2), json(false), json("string"), json()};
|
|
json j(a);
|
|
CHECK(j.type() == json::value_t::array);
|
|
// we cannot really check for equality here
|
|
}
|
|
|
|
SECTION("std::unordered_set<json>")
|
|
{
|
|
std::unordered_set<json> a {json(1), json(2.2), json(false), json("string"), json()};
|
|
json j(a);
|
|
CHECK(j.type() == json::value_t::array);
|
|
// we cannot really check for equality here
|
|
}
|
|
|
|
SECTION("sequence container literal")
|
|
{
|
|
json j({json(1), json(2.2), json(false), json("string"), json()});
|
|
CHECK(j.type() == json::value_t::array);
|
|
CHECK(j == j_reference);
|
|
}
|
|
}
|
|
|
|
SECTION("create a string (explicit)")
|
|
{
|
|
SECTION("empty string")
|
|
{
|
|
json::string_t s;
|
|
json j(s);
|
|
CHECK(j.type() == json::value_t::string);
|
|
}
|
|
|
|
SECTION("filled string")
|
|
{
|
|
json::string_t s {"Hello world"};
|
|
json j(s);
|
|
CHECK(j.type() == json::value_t::string);
|
|
}
|
|
}
|
|
|
|
SECTION("create an string (implicit)")
|
|
{
|
|
// reference string
|
|
json::string_t s_reference {"Hello world"};
|
|
json j_reference(s_reference);
|
|
|
|
SECTION("std::string")
|
|
{
|
|
std::string s {"Hello world"};
|
|
json j(s);
|
|
CHECK(j.type() == json::value_t::string);
|
|
CHECK(j == j_reference);
|
|
}
|
|
|
|
SECTION("char[]")
|
|
{
|
|
char s[] {"Hello world"};
|
|
json j(s);
|
|
CHECK(j.type() == json::value_t::string);
|
|
CHECK(j == j_reference);
|
|
}
|
|
|
|
SECTION("const char*")
|
|
{
|
|
const char* s {"Hello world"};
|
|
json j(s);
|
|
CHECK(j.type() == json::value_t::string);
|
|
CHECK(j == j_reference);
|
|
}
|
|
|
|
SECTION("string literal")
|
|
{
|
|
json j("Hello world");
|
|
CHECK(j.type() == json::value_t::string);
|
|
CHECK(j == j_reference);
|
|
}
|
|
}
|
|
}
|