2017-01-06 21:07:34 +01:00
|
|
|
// Copyright Benoit Blanchon 2014-2017
|
2015-05-25 15:38:58 +02:00
|
|
|
// MIT License
|
|
|
|
//
|
|
|
|
// Arduino JSON library
|
2017-03-25 22:05:06 +01:00
|
|
|
// https://bblanchon.github.io/ArduinoJson/
|
2016-01-07 22:35:12 +01:00
|
|
|
// If you like this project, please add a star!
|
2015-05-25 15:38:58 +02:00
|
|
|
|
|
|
|
#include <ArduinoJson.h>
|
2017-04-18 18:22:24 +02:00
|
|
|
#include <catch.hpp>
|
2015-05-25 15:38:58 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
static void eraseString(std::string &str) {
|
|
|
|
char *p = const_cast<char *>(str.c_str());
|
|
|
|
while (*p) *p++ = '*';
|
|
|
|
}
|
2015-07-25 15:38:12 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
TEST_CASE("std::string") {
|
|
|
|
DynamicJsonBuffer jb;
|
2015-07-25 15:38:12 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("JsonBuffer_ParseArray") {
|
|
|
|
std::string json("[\"hello\"]");
|
|
|
|
JsonArray &array = jb.parseArray(json);
|
|
|
|
eraseString(json);
|
|
|
|
REQUIRE(true == array.success());
|
|
|
|
REQUIRE(std::string("hello") == array[0]);
|
|
|
|
}
|
2015-05-25 15:38:58 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("JsonBuffer_ParseObject") {
|
|
|
|
std::string json("{\"hello\":\"world\"}");
|
|
|
|
JsonObject &object = jb.parseObject(json);
|
|
|
|
eraseString(json);
|
|
|
|
REQUIRE(true == object.success());
|
|
|
|
REQUIRE(std::string("world") == object["hello"]);
|
|
|
|
}
|
2015-05-25 15:38:58 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("JsonObject_Subscript") {
|
|
|
|
char json[] = "{\"key\":\"value\"}";
|
|
|
|
JsonObject &object = jb.parseObject(json);
|
|
|
|
REQUIRE(std::string("value") == object[std::string("key")]);
|
|
|
|
}
|
2015-05-25 15:38:58 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("JsonObject_ConstSubscript") {
|
|
|
|
char json[] = "{\"key\":\"value\"}";
|
|
|
|
const JsonObject &object = jb.parseObject(json);
|
|
|
|
REQUIRE(std::string("value") == object[std::string("key")]);
|
|
|
|
}
|
2015-05-25 15:38:58 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("JsonObject_SetKey") {
|
|
|
|
JsonObject &object = jb.createObject();
|
|
|
|
std::string key("hello");
|
|
|
|
object.set(key, "world");
|
|
|
|
eraseString(key);
|
|
|
|
REQUIRE(std::string("world") == object["hello"]);
|
|
|
|
}
|
2015-05-25 15:38:58 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("JsonObject_SetValue") {
|
|
|
|
JsonObject &object = jb.createObject();
|
|
|
|
std::string value("world");
|
|
|
|
object.set("hello", value);
|
|
|
|
eraseString(value);
|
|
|
|
REQUIRE(std::string("world") == object["hello"]);
|
|
|
|
}
|
2015-07-25 15:38:12 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("JsonObject_SetKeyValue") {
|
|
|
|
JsonObject &object = jb.createObject();
|
|
|
|
std::string key("hello");
|
|
|
|
std::string value("world");
|
|
|
|
object.set(key, value);
|
|
|
|
eraseString(key);
|
|
|
|
eraseString(value);
|
|
|
|
REQUIRE(std::string("world") == object["hello"]);
|
|
|
|
}
|
2015-07-25 15:38:12 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("JsonObject_SetToArraySubscript") {
|
|
|
|
JsonArray &arr = jb.createArray();
|
|
|
|
arr.add("world");
|
2015-09-28 22:14:50 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
JsonObject &object = jb.createObject();
|
|
|
|
object.set(std::string("hello"), arr[0]);
|
2015-09-28 22:14:50 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
REQUIRE(std::string("world") == object["hello"]);
|
|
|
|
}
|
2015-09-28 22:14:50 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("JsonObject_SetToObjectSubscript") {
|
|
|
|
JsonObject &arr = jb.createObject();
|
|
|
|
arr.set("x", "world");
|
2015-09-28 22:14:50 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
JsonObject &object = jb.createObject();
|
|
|
|
object.set(std::string("hello"), arr["x"]);
|
2015-09-28 22:14:50 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
REQUIRE(std::string("world") == object["hello"]);
|
|
|
|
}
|
2015-09-28 22:14:50 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("JsonObject_Get") {
|
|
|
|
char json[] = "{\"key\":\"value\"}";
|
|
|
|
const JsonObject &object = jb.parseObject(json);
|
|
|
|
REQUIRE(std::string("value") ==
|
|
|
|
object.get<const char *>(std::string("key")));
|
|
|
|
}
|
2015-05-25 15:38:58 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("JsonObject_GetT") {
|
|
|
|
char json[] = "{\"key\":\"value\"}";
|
|
|
|
const JsonObject &object = jb.parseObject(json);
|
|
|
|
REQUIRE(std::string("value") ==
|
|
|
|
object.get<const char *>(std::string("key")));
|
|
|
|
}
|
2015-05-25 15:38:58 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("JsonObject_IsT") {
|
|
|
|
char json[] = "{\"key\":\"value\"}";
|
|
|
|
const JsonObject &object = jb.parseObject(json);
|
|
|
|
REQUIRE(true == object.is<const char *>(std::string("key")));
|
|
|
|
}
|
2015-05-25 15:38:58 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("JsonObject_CreateNestedObject") {
|
|
|
|
std::string key = "key";
|
|
|
|
char json[64];
|
|
|
|
JsonObject &object = jb.createObject();
|
|
|
|
object.createNestedObject(key);
|
|
|
|
eraseString(key);
|
|
|
|
object.printTo(json, sizeof(json));
|
|
|
|
REQUIRE(std::string("{\"key\":{}}") == json);
|
|
|
|
}
|
2015-05-25 15:38:58 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("JsonObject_CreateNestedArray") {
|
|
|
|
std::string key = "key";
|
|
|
|
char json[64];
|
|
|
|
JsonObject &object = jb.createObject();
|
|
|
|
object.createNestedArray(key);
|
|
|
|
eraseString(key);
|
|
|
|
object.printTo(json, sizeof(json));
|
|
|
|
REQUIRE(std::string("{\"key\":[]}") == json);
|
|
|
|
}
|
2015-05-25 15:38:58 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("JsonObject_ContainsKey") {
|
|
|
|
char json[] = "{\"key\":\"value\"}";
|
|
|
|
const JsonObject &object = jb.parseObject(json);
|
|
|
|
REQUIRE(true == object.containsKey(std::string("key")));
|
|
|
|
}
|
2015-05-25 15:38:58 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("JsonObject_Remove") {
|
|
|
|
char json[] = "{\"key\":\"value\"}";
|
|
|
|
JsonObject &object = jb.parseObject(json);
|
|
|
|
REQUIRE(1 == object.size());
|
|
|
|
object.remove(std::string("key"));
|
|
|
|
REQUIRE(0 == object.size());
|
|
|
|
}
|
2015-07-25 15:38:12 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("JsonObjectSubscript_SetKey") {
|
|
|
|
JsonObject &object = jb.createObject();
|
|
|
|
std::string key("hello");
|
|
|
|
object[key] = "world";
|
|
|
|
eraseString(key);
|
|
|
|
REQUIRE(std::string("world") == object["hello"]);
|
|
|
|
}
|
2015-07-25 15:38:12 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("JsonObjectSubscript_SetValue") {
|
|
|
|
JsonObject &object = jb.createObject();
|
|
|
|
std::string value("world");
|
|
|
|
object["hello"] = value;
|
|
|
|
eraseString(value);
|
|
|
|
REQUIRE(std::string("world") == object["hello"]);
|
|
|
|
}
|
2015-07-25 15:38:12 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("JsonArray_Add") {
|
|
|
|
JsonArray &array = jb.createArray();
|
|
|
|
std::string value("hello");
|
|
|
|
array.add(value);
|
|
|
|
eraseString(value);
|
|
|
|
REQUIRE(std::string("hello") == array[0]);
|
|
|
|
}
|
2015-07-25 15:38:12 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("JsonArray_Set") {
|
|
|
|
JsonArray &array = jb.createArray();
|
|
|
|
std::string value("world");
|
|
|
|
array.add("hello");
|
|
|
|
array.set(0, value);
|
|
|
|
eraseString(value);
|
|
|
|
REQUIRE(std::string("world") == array[0]);
|
|
|
|
}
|
2015-07-25 15:38:12 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("JsonArraySubscript") {
|
|
|
|
JsonArray &array = jb.createArray();
|
|
|
|
std::string value("world");
|
|
|
|
array.add("hello");
|
|
|
|
array[0] = value;
|
|
|
|
eraseString(value);
|
|
|
|
REQUIRE(std::string("world") == array[0]);
|
|
|
|
}
|
2015-08-10 17:22:22 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("JsonArray_PrintTo") {
|
|
|
|
JsonArray &array = jb.createArray();
|
|
|
|
array.add(4);
|
|
|
|
array.add(2);
|
|
|
|
std::string json;
|
|
|
|
array.printTo(json);
|
|
|
|
REQUIRE(std::string("[4,2]") == json);
|
|
|
|
}
|
2015-08-10 17:22:22 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("JsonArray_PrettyPrintTo") {
|
|
|
|
JsonArray &array = jb.createArray();
|
|
|
|
array.add(4);
|
|
|
|
array.add(2);
|
|
|
|
std::string json;
|
|
|
|
array.prettyPrintTo(json);
|
|
|
|
REQUIRE(std::string("[\r\n 4,\r\n 2\r\n]") == json);
|
|
|
|
}
|
2015-08-10 17:22:22 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("JsonObject_PrintTo") {
|
|
|
|
JsonObject &object = jb.createObject();
|
|
|
|
object["key"] = "value";
|
|
|
|
std::string json;
|
|
|
|
object.printTo(json);
|
|
|
|
REQUIRE(std::string("{\"key\":\"value\"}") == json);
|
|
|
|
}
|
2015-08-10 17:22:22 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("JsonObject_PrettyPrintTo") {
|
|
|
|
JsonObject &object = jb.createObject();
|
|
|
|
object["key"] = "value";
|
|
|
|
std::string json;
|
|
|
|
object.prettyPrintTo(json);
|
|
|
|
REQUIRE(std::string("{\r\n \"key\": \"value\"\r\n}") == json);
|
|
|
|
}
|
2016-02-23 21:27:52 +01:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("JsonBuffer_GrowWhenAddingNewKey") {
|
|
|
|
JsonObject &object = jb.createObject();
|
|
|
|
std::string key1("hello"), key2("world");
|
2016-02-23 21:27:52 +01:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
object[key1] = 1;
|
|
|
|
size_t sizeBefore = jb.size();
|
|
|
|
object[key2] = 2;
|
|
|
|
size_t sizeAfter = jb.size();
|
2016-02-23 21:27:52 +01:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
REQUIRE(sizeAfter - sizeBefore >= key2.size());
|
|
|
|
}
|
2016-02-23 21:27:52 +01:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("JsonBuffer_DontGrowWhenReusingKey") {
|
|
|
|
JsonObject &object = jb.createObject();
|
|
|
|
std::string key("hello");
|
2016-02-23 21:27:52 +01:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
object[key] = 1;
|
|
|
|
size_t sizeBefore = jb.size();
|
|
|
|
object[key] = 2;
|
|
|
|
size_t sizeAfter = jb.size();
|
2016-02-23 21:27:52 +01:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
REQUIRE(sizeBefore == sizeAfter);
|
|
|
|
}
|
2016-11-06 17:48:32 +01:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("JsonBuffer_strdup") {
|
|
|
|
std::string original("hello");
|
|
|
|
char *copy = jb.strdup(original);
|
|
|
|
original[0] = 'w';
|
|
|
|
REQUIRE(std::string("hello") == copy);
|
|
|
|
}
|
2016-11-06 17:48:32 +01:00
|
|
|
}
|