json/test/unit.cpp

144 lines
3.9 KiB
C++
Raw Normal View History

2015-01-31 22:13:11 +03:00
#define CATCH_CONFIG_MAIN
#include "catch.hpp"
#include "json.hpp"
using nlohmann::json;
2015-02-08 15:25:09 +03:00
#include <unordered_map>
2015-01-31 22:13:11 +03:00
2015-02-08 15:25:09 +03:00
TEST_CASE("Constructors")
2015-01-31 22:13:11 +03:00
{
2015-02-08 15:25:09 +03:00
SECTION("create an empty value with a given type")
2015-01-31 22:13:11 +03:00
{
2015-02-08 15:25:09 +03:00
SECTION("null")
2015-01-31 22:13:11 +03:00
{
2015-02-08 15:25:09 +03:00
auto t = json::value_t::null;
json j(t);
CHECK(j.type() == t);
2015-01-31 22:13:11 +03:00
}
2015-02-08 15:25:09 +03:00
SECTION("object")
2015-01-31 22:13:11 +03:00
{
2015-02-08 15:25:09 +03:00
auto t = json::value_t::object;
json j(t);
CHECK(j.type() == t);
2015-01-31 22:13:11 +03:00
}
2015-02-08 15:25:09 +03:00
SECTION("array")
2015-01-31 22:13:11 +03:00
{
2015-02-08 15:25:09 +03:00
auto t = json::value_t::array;
json j(t);
CHECK(j.type() == t);
2015-01-31 22:13:11 +03:00
}
2015-02-08 15:25:09 +03:00
SECTION("boolean")
2015-01-31 22:13:11 +03:00
{
2015-02-08 15:25:09 +03:00
auto t = json::value_t::boolean;
json j(t);
CHECK(j.type() == t);
2015-01-31 22:13:11 +03:00
}
2015-02-08 15:25:09 +03:00
SECTION("number_integer")
2015-01-31 22:13:11 +03:00
{
2015-02-08 15:25:09 +03:00
auto t = json::value_t::number_integer;
json j(t);
CHECK(j.type() == t);
2015-01-31 22:13:11 +03:00
}
2015-02-08 15:25:09 +03:00
SECTION("number_float")
2015-01-31 22:13:11 +03:00
{
2015-02-08 15:25:09 +03:00
auto t = json::value_t::number_float;
json j(t);
CHECK(j.type() == t);
2015-01-31 22:13:11 +03:00
}
}
2015-02-08 15:25:09 +03:00
SECTION("create a null object (implicitly)")
2015-01-31 22:13:11 +03:00
{
2015-02-08 15:25:09 +03:00
SECTION("no parameter")
2015-01-31 22:13:11 +03:00
{
2015-02-08 15:25:09 +03:00
json j;
CHECK(j.type() == json::value_t::null);
2015-01-31 22:13:11 +03:00
}
}
2015-02-08 15:25:09 +03:00
SECTION("create a null object (explicitly)")
2015-01-31 22:13:11 +03:00
{
2015-02-08 15:25:09 +03:00
SECTION("parameter")
2015-01-31 22:13:11 +03:00
{
2015-02-08 15:25:09 +03:00
json j(nullptr);
CHECK(j.type() == json::value_t::null);
2015-01-31 22:13:11 +03:00
}
2015-02-08 15:25:09 +03:00
SECTION("assignment")
2015-01-31 22:13:11 +03:00
{
2015-02-08 15:25:09 +03:00
json j = nullptr;
CHECK(j.type() == json::value_t::null);
2015-01-31 22:13:11 +03:00
}
}
2015-02-08 15:25:09 +03:00
SECTION("create an object (explicit)")
2015-01-31 22:13:11 +03:00
{
2015-02-08 15:25:09 +03:00
SECTION("empty object")
2015-01-31 22:13:11 +03:00
{
2015-02-08 15:25:09 +03:00
json::object_t o;
json j(o);
CHECK(j.type() == json::value_t::object);
2015-01-31 22:13:11 +03:00
}
2015-02-08 15:25:09 +03:00
SECTION("filled object")
2015-01-31 22:13:11 +03:00
{
2015-02-08 15:25:09 +03:00
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);
2015-01-31 22:13:11 +03:00
}
}
2015-02-08 15:25:09 +03:00
SECTION("create an object (implicit)")
2015-01-31 22:13:11 +03:00
{
2015-02-08 15:25:09 +03:00
// 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>")
2015-01-31 22:13:11 +03:00
{
2015-02-08 15:25:09 +03:00
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);
2015-01-31 22:13:11 +03:00
}
2015-02-08 15:25:09 +03:00
SECTION("std::map<const char*, json>")
2015-01-31 22:13:11 +03:00
{
2015-02-08 15:25:09 +03:00
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);
2015-01-31 22:13:11 +03:00
}
2015-02-08 15:25:09 +03:00
SECTION("std::multimap<std::string, json>")
2015-01-31 22:13:11 +03:00
{
2015-02-08 15:25:09 +03:00
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);
2015-01-31 22:13:11 +03:00
}
2015-02-08 15:25:09 +03:00
SECTION("std::unordered_map<std::string, json>")
2015-01-31 22:13:11 +03:00
{
2015-02-08 15:25:09 +03:00
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);
2015-01-31 22:13:11 +03:00
}
2015-02-08 15:25:09 +03:00
SECTION("std::unordered_multimap<std::string, json>")
2015-01-31 22:13:11 +03:00
{
2015-02-08 15:25:09 +03:00
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);
2015-01-31 22:13:11 +03:00
}
}
}