more test cases
This commit is contained in:
parent
385865c4ec
commit
5ec433604a
2 changed files with 47 additions and 10 deletions
10
src/json.hpp
10
src/json.hpp
|
@ -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")
|
||||
|
|
Loading…
Reference in a new issue