Commit 5ec43360 authored by Niels's avatar Niels

more test cases

parent 385865c4
......@@ -350,7 +350,7 @@ class basic_json
// if object is wanted but impossible, throw an exception
if (manual_type == value_t::object and not is_object)
{
throw std::logic_error("cannot create JSON object");
throw std::logic_error("cannot create JSON object from initializer list");
}
}
......@@ -378,17 +378,9 @@ class basic_json
}
inline static basic_json object(list_init_t l = list_init_t())
{
// if more than one element is in the initializer list, wrap it
if (l.size() > 1)
{
return basic_json({l}, false, value_t::object);
}
else
{
return basic_json(l, false, value_t::object);
}
}
///////////////////////////////////////
// other constructors and destructor //
......
......@@ -3,6 +3,8 @@
#include "json.hpp"
#include <unordered_map>
#include <list>
#include <sstream>
using nlohmann::json;
......@@ -62,6 +64,49 @@ TEST_CASE()
json array_not_object = { json::array({"currency", "USD"}), json::array({"value", 42.99}) };
std::cerr << "array_not_object: " << array_not_object << std::endl;
}
{
CHECK_THROWS_AS(json::object({1, 2, 3}), std::logic_error);
}
{
CHECK(json::object({{"foo", 1}, {"bar", 2}, {"baz", 3}}).size() == 3);
CHECK(json::object({{"foo", 1}}).size() == 1);
CHECK(json::object().size() == 0);
}
{
json j = json::object({{"foo", 1}, {"bar", 2}, {"baz", 3}});
{
CHECK(j["foo"] == json(1));
CHECK(j.at("foo") == json(1));
}
{
std::map<std::string, json> m = j;
auto k = j.get<std::map<std::string, json>>();
CHECK(m == k);
}
{
std::unordered_map<std::string, json> m = j;
auto k = j.get<std::unordered_map<std::string, json>>();
CHECK(m == k);
}
}
{
json j = {1, 2, 3, 4, 5};
{
CHECK(j[0] == json(1));
CHECK(j.at(0) == json(1));
}
{
std::vector<json> m = j;
auto k = j.get<std::list<json>>();
CHECK(m == k);
}
{
std::set<json> m = j;
auto k = j.get<std::set<json>>();
CHECK(m == k);
}
}
}
TEST_CASE("null")
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment