2018-03-10 20:59:10 +00:00
|
|
|
/*
|
|
|
|
__ _____ _____ _____
|
|
|
|
__| | __| | | | JSON for Modern C++ (test suite)
|
2018-08-18 16:46:15 +00:00
|
|
|
| | |__ | | | | | | version 3.2.0
|
2018-03-10 20:59:10 +00:00
|
|
|
|_____|_____|_____|_|___| https://github.com/nlohmann/json
|
|
|
|
|
|
|
|
Licensed under the MIT License <http://opensource.org/licenses/MIT>.
|
2018-05-03 15:41:45 +00:00
|
|
|
SPDX-License-Identifier: MIT
|
2018-03-10 20:59:10 +00:00
|
|
|
Copyright (c) 2018 Vitaliy Manushkin <agri@akamo.info>.
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
|
|
copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "catch.hpp"
|
|
|
|
|
|
|
|
#include <nlohmann/json.hpp>
|
|
|
|
#include <string>
|
|
|
|
#include <utility>
|
|
|
|
|
2018-06-11 17:50:39 +00:00
|
|
|
|
|
|
|
/* forward declarations */
|
|
|
|
class alt_string;
|
|
|
|
bool operator<(const char* op1, const alt_string& op2);
|
|
|
|
|
|
|
|
|
2018-03-10 20:59:10 +00:00
|
|
|
/*
|
|
|
|
* This is virtually a string class.
|
|
|
|
* It covers std::string under the hood.
|
|
|
|
*/
|
|
|
|
class alt_string
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
using value_type = std::string::value_type;
|
|
|
|
|
|
|
|
alt_string(const char* str): str_impl(str) {}
|
2018-03-12 18:15:11 +00:00
|
|
|
alt_string(const char* str, std::size_t count): str_impl(str, count) {}
|
2018-03-10 20:59:10 +00:00
|
|
|
alt_string(size_t count, char chr): str_impl(count, chr) {}
|
|
|
|
alt_string() = default;
|
|
|
|
|
|
|
|
template <typename...TParams>
|
|
|
|
alt_string& append(TParams&& ...params)
|
|
|
|
{
|
|
|
|
str_impl.append(std::forward<TParams>(params)...);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void push_back(char c)
|
|
|
|
{
|
|
|
|
str_impl.push_back(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename op_type>
|
2018-06-13 16:47:29 +00:00
|
|
|
bool operator==(const op_type& op) const
|
2018-03-10 20:59:10 +00:00
|
|
|
{
|
|
|
|
return str_impl == op;
|
|
|
|
}
|
|
|
|
|
2018-06-11 15:03:46 +00:00
|
|
|
bool operator==(const alt_string& op) const
|
|
|
|
{
|
|
|
|
return str_impl == op.str_impl;
|
|
|
|
}
|
|
|
|
|
2018-03-10 20:59:10 +00:00
|
|
|
template <typename op_type>
|
2018-06-13 16:47:29 +00:00
|
|
|
bool operator!=(const op_type& op) const
|
2018-03-10 20:59:10 +00:00
|
|
|
{
|
|
|
|
return str_impl != op;
|
|
|
|
}
|
|
|
|
|
2018-06-18 20:03:46 +00:00
|
|
|
bool operator!=(const alt_string& op) const
|
|
|
|
{
|
2018-06-11 15:03:46 +00:00
|
|
|
return str_impl != op.str_impl;
|
|
|
|
}
|
|
|
|
|
2018-03-12 18:15:11 +00:00
|
|
|
std::size_t size() const noexcept
|
2018-03-10 20:59:10 +00:00
|
|
|
{
|
|
|
|
return str_impl.size();
|
|
|
|
}
|
|
|
|
|
2018-03-12 18:15:11 +00:00
|
|
|
void resize (std::size_t n)
|
2018-03-10 20:59:10 +00:00
|
|
|
{
|
|
|
|
str_impl.resize(n);
|
|
|
|
}
|
|
|
|
|
2018-03-12 18:15:11 +00:00
|
|
|
void resize (std::size_t n, char c)
|
2018-03-10 20:59:10 +00:00
|
|
|
{
|
|
|
|
str_impl.resize(n, c);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename op_type>
|
2018-06-13 16:47:29 +00:00
|
|
|
bool operator<(const op_type& op) const
|
2018-03-10 20:59:10 +00:00
|
|
|
{
|
|
|
|
return str_impl < op;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator<(const alt_string& op) const
|
|
|
|
{
|
|
|
|
return str_impl < op.str_impl;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* c_str() const
|
|
|
|
{
|
|
|
|
return str_impl.c_str();
|
|
|
|
}
|
|
|
|
|
2018-03-12 18:15:11 +00:00
|
|
|
char& operator[](std::size_t index)
|
2018-03-10 20:59:10 +00:00
|
|
|
{
|
|
|
|
return str_impl[index];
|
|
|
|
}
|
|
|
|
|
2018-03-12 18:15:11 +00:00
|
|
|
const char& operator[](std::size_t index) const
|
2018-03-10 20:59:10 +00:00
|
|
|
{
|
|
|
|
return str_impl[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
char& back()
|
|
|
|
{
|
|
|
|
return str_impl.back();
|
|
|
|
}
|
|
|
|
|
|
|
|
const char& back() const
|
|
|
|
{
|
|
|
|
return str_impl.back();
|
|
|
|
}
|
|
|
|
|
2018-03-12 18:15:11 +00:00
|
|
|
void clear()
|
|
|
|
{
|
|
|
|
str_impl.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
const value_type* data()
|
|
|
|
{
|
|
|
|
return str_impl.data();
|
|
|
|
}
|
|
|
|
|
2018-03-10 20:59:10 +00:00
|
|
|
private:
|
|
|
|
std::string str_impl;
|
2018-06-11 16:09:57 +00:00
|
|
|
|
|
|
|
friend bool ::operator<(const char*, const alt_string&);
|
2018-03-10 20:59:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
using alt_json = nlohmann::basic_json <
|
|
|
|
std::map,
|
|
|
|
std::vector,
|
|
|
|
alt_string,
|
|
|
|
bool,
|
|
|
|
std::int64_t,
|
|
|
|
std::uint64_t,
|
|
|
|
double,
|
|
|
|
std::allocator,
|
|
|
|
nlohmann::adl_serializer >;
|
|
|
|
|
|
|
|
|
2018-06-18 20:03:46 +00:00
|
|
|
bool operator<(const char* op1, const alt_string& op2)
|
|
|
|
{
|
2018-06-11 16:09:57 +00:00
|
|
|
return op1 < op2.str_impl;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-10 20:59:10 +00:00
|
|
|
|
|
|
|
TEST_CASE("alternative string type")
|
|
|
|
{
|
|
|
|
SECTION("dump")
|
|
|
|
{
|
2018-03-10 21:29:04 +00:00
|
|
|
{
|
|
|
|
alt_json doc;
|
|
|
|
doc["pi"] = 3.141;
|
|
|
|
alt_string dump = doc.dump();
|
|
|
|
CHECK(dump == R"({"pi":3.141})");
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
alt_json doc;
|
|
|
|
doc["happy"] = true;
|
|
|
|
alt_string dump = doc.dump();
|
|
|
|
CHECK(dump == R"({"happy":true})");
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
alt_json doc;
|
|
|
|
doc["name"] = "I'm Batman";
|
|
|
|
alt_string dump = doc.dump();
|
|
|
|
CHECK(dump == R"({"name":"I'm Batman"})");
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
alt_json doc;
|
|
|
|
doc["nothing"] = nullptr;
|
|
|
|
alt_string dump = doc.dump();
|
|
|
|
CHECK(dump == R"({"nothing":null})");
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
alt_json doc;
|
|
|
|
doc["answer"]["everything"] = 42;
|
|
|
|
alt_string dump = doc.dump();
|
|
|
|
CHECK(dump == R"({"answer":{"everything":42}})");
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
alt_json doc;
|
|
|
|
doc["list"] = { 1, 0, 2 };
|
|
|
|
alt_string dump = doc.dump();
|
|
|
|
CHECK(dump == R"({"list":[1,0,2]})");
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
alt_json doc;
|
|
|
|
doc["list"] = { 1, 0, 2 };
|
|
|
|
alt_string dump = doc.dump();
|
|
|
|
CHECK(dump == R"({"list":[1,0,2]})");
|
|
|
|
}
|
2018-03-10 20:59:10 +00:00
|
|
|
}
|
2018-03-12 18:15:11 +00:00
|
|
|
|
|
|
|
SECTION("parse")
|
|
|
|
{
|
|
|
|
auto doc = alt_json::parse("{\"foo\": \"bar\"}");
|
|
|
|
alt_string dump = doc.dump();
|
|
|
|
CHECK(dump == R"({"foo":"bar"})");
|
|
|
|
}
|
2018-06-11 15:03:46 +00:00
|
|
|
|
|
|
|
SECTION("equality")
|
|
|
|
{
|
|
|
|
alt_json doc;
|
|
|
|
doc["Who are you?"] = "I'm Batman";
|
|
|
|
|
|
|
|
CHECK("I'm Batman" == doc["Who are you?"]);
|
|
|
|
CHECK(doc["Who are you?"] == "I'm Batman");
|
2018-06-13 16:47:29 +00:00
|
|
|
CHECK_FALSE("I'm Batman" != doc["Who are you?"]);
|
|
|
|
CHECK_FALSE(doc["Who are you?"] != "I'm Batman");
|
2018-06-11 15:03:46 +00:00
|
|
|
|
|
|
|
CHECK("I'm Bruce Wayne" != doc["Who are you?"]);
|
|
|
|
CHECK(doc["Who are you?"] != "I'm Bruce Wayne");
|
2018-06-13 16:47:29 +00:00
|
|
|
CHECK_FALSE("I'm Bruce Wayne" == doc["Who are you?"]);
|
|
|
|
CHECK_FALSE(doc["Who are you?"] == "I'm Bruce Wayne");
|
2018-06-11 15:03:46 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
const alt_json& const_doc = doc;
|
|
|
|
|
|
|
|
CHECK("I'm Batman" == const_doc["Who are you?"]);
|
|
|
|
CHECK(const_doc["Who are you?"] == "I'm Batman");
|
2018-06-13 16:47:29 +00:00
|
|
|
CHECK_FALSE("I'm Batman" != const_doc["Who are you?"]);
|
|
|
|
CHECK_FALSE(const_doc["Who are you?"] != "I'm Batman");
|
2018-06-11 15:03:46 +00:00
|
|
|
|
|
|
|
CHECK("I'm Bruce Wayne" != const_doc["Who are you?"]);
|
|
|
|
CHECK(const_doc["Who are you?"] != "I'm Bruce Wayne");
|
2018-06-13 16:47:29 +00:00
|
|
|
CHECK_FALSE("I'm Bruce Wayne" == const_doc["Who are you?"]);
|
|
|
|
CHECK_FALSE(const_doc["Who are you?"] == "I'm Bruce Wayne");
|
2018-06-11 15:03:46 +00:00
|
|
|
}
|
|
|
|
}
|
2018-03-10 20:59:10 +00:00
|
|
|
}
|