2016-01-07 22:35:12 +01:00
|
|
|
// Copyright Benoit Blanchon 2014-2016
|
2015-05-25 15:38:58 +02:00
|
|
|
// MIT License
|
|
|
|
//
|
|
|
|
// Arduino JSON library
|
|
|
|
// https://github.com/bblanchon/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 <gtest/gtest.h>
|
|
|
|
#include <ArduinoJson.h>
|
|
|
|
|
2015-07-25 15:38:12 +02:00
|
|
|
class ArduinoStringTests : public ::testing::Test {
|
|
|
|
protected:
|
|
|
|
static void eraseString(String &str) {
|
|
|
|
char *p = const_cast<char *>(str.c_str());
|
|
|
|
while (*p) *p++ = '*';
|
|
|
|
}
|
|
|
|
|
|
|
|
DynamicJsonBuffer _jsonBuffer;
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(ArduinoStringTests, JsonBuffer_ParseArray) {
|
|
|
|
String json("[\"hello\"]");
|
|
|
|
JsonArray &array = _jsonBuffer.parseArray(json);
|
|
|
|
eraseString(json);
|
2015-05-25 15:38:58 +02:00
|
|
|
ASSERT_TRUE(array.success());
|
2015-07-25 15:38:12 +02:00
|
|
|
ASSERT_STREQ("hello", array[0]);
|
2015-05-25 15:38:58 +02:00
|
|
|
}
|
|
|
|
|
2015-07-25 15:38:12 +02:00
|
|
|
TEST_F(ArduinoStringTests, JsonBuffer_ParseObject) {
|
|
|
|
String json("{\"hello\":\"world\"}");
|
|
|
|
JsonObject &object = _jsonBuffer.parseObject(json);
|
|
|
|
eraseString(json);
|
2015-05-25 15:38:58 +02:00
|
|
|
ASSERT_TRUE(object.success());
|
2015-07-25 15:38:12 +02:00
|
|
|
ASSERT_STREQ("world", object["hello"]);
|
2015-05-25 15:38:58 +02:00
|
|
|
}
|
|
|
|
|
2015-07-25 15:38:12 +02:00
|
|
|
TEST_F(ArduinoStringTests, JsonObject_Subscript) {
|
2015-05-25 15:38:58 +02:00
|
|
|
char json[] = "{\"key\":\"value\"}";
|
2015-07-25 15:38:12 +02:00
|
|
|
JsonObject &object = _jsonBuffer.parseObject(json);
|
2015-05-25 15:38:58 +02:00
|
|
|
ASSERT_STREQ("value", object[String("key")]);
|
|
|
|
}
|
|
|
|
|
2015-07-25 15:38:12 +02:00
|
|
|
TEST_F(ArduinoStringTests, JsonObject_ConstSubscript) {
|
2015-05-25 15:38:58 +02:00
|
|
|
char json[] = "{\"key\":\"value\"}";
|
2015-07-25 15:38:12 +02:00
|
|
|
const JsonObject &object = _jsonBuffer.parseObject(json);
|
2015-05-25 15:38:58 +02:00
|
|
|
ASSERT_STREQ("value", object[String("key")]);
|
|
|
|
}
|
|
|
|
|
2015-07-25 15:38:12 +02:00
|
|
|
TEST_F(ArduinoStringTests, JsonObject_SetKey) {
|
|
|
|
JsonObject &object = _jsonBuffer.createObject();
|
|
|
|
String key("hello");
|
|
|
|
object.set(key, "world");
|
|
|
|
eraseString(key);
|
|
|
|
ASSERT_STREQ("world", object["hello"]);
|
2015-05-25 15:38:58 +02:00
|
|
|
}
|
|
|
|
|
2015-07-25 15:38:12 +02:00
|
|
|
TEST_F(ArduinoStringTests, JsonObject_SetValue) {
|
|
|
|
JsonObject &object = _jsonBuffer.createObject();
|
|
|
|
String value("world");
|
|
|
|
object.set("hello", value);
|
|
|
|
eraseString(value);
|
|
|
|
ASSERT_STREQ("world", object["hello"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ArduinoStringTests, JsonObject_SetKeyValue) {
|
|
|
|
JsonObject &object = _jsonBuffer.createObject();
|
|
|
|
String key("hello");
|
|
|
|
String value("world");
|
|
|
|
object.set(key, value);
|
|
|
|
eraseString(key);
|
|
|
|
eraseString(value);
|
|
|
|
ASSERT_STREQ("world", object["hello"]);
|
|
|
|
}
|
|
|
|
|
2015-09-28 22:14:50 +02:00
|
|
|
TEST_F(ArduinoStringTests, JsonObject_SetToArraySubscript) {
|
|
|
|
JsonArray &arr = _jsonBuffer.createArray();
|
|
|
|
arr.add("world");
|
|
|
|
|
|
|
|
JsonObject &object = _jsonBuffer.createObject();
|
|
|
|
object.set(String("hello"), arr[0]);
|
|
|
|
|
|
|
|
ASSERT_STREQ("world", object["hello"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ArduinoStringTests, JsonObject_SetToObjectSubscript) {
|
|
|
|
JsonObject &arr = _jsonBuffer.createObject();
|
|
|
|
arr.set("x", "world");
|
|
|
|
|
|
|
|
JsonObject &object = _jsonBuffer.createObject();
|
|
|
|
object.set(String("hello"), arr["x"]);
|
|
|
|
|
|
|
|
ASSERT_STREQ("world", object["hello"]);
|
|
|
|
}
|
|
|
|
|
2015-07-25 15:38:12 +02:00
|
|
|
TEST_F(ArduinoStringTests, JsonObject_Get) {
|
2015-05-25 15:38:58 +02:00
|
|
|
char json[] = "{\"key\":\"value\"}";
|
2015-07-25 15:38:12 +02:00
|
|
|
const JsonObject &object = _jsonBuffer.parseObject(json);
|
2015-05-25 15:38:58 +02:00
|
|
|
ASSERT_STREQ("value", object.get(String("key")));
|
|
|
|
}
|
|
|
|
|
2015-07-25 15:38:12 +02:00
|
|
|
TEST_F(ArduinoStringTests, JsonObject_GetT) {
|
2015-05-25 15:38:58 +02:00
|
|
|
char json[] = "{\"key\":\"value\"}";
|
2015-07-25 15:38:12 +02:00
|
|
|
const JsonObject &object = _jsonBuffer.parseObject(json);
|
2015-05-25 15:38:58 +02:00
|
|
|
ASSERT_STREQ("value", object.get<const char *>(String("key")));
|
|
|
|
}
|
|
|
|
|
2015-07-25 15:38:12 +02:00
|
|
|
TEST_F(ArduinoStringTests, JsonObject_IsT) {
|
2015-05-25 15:38:58 +02:00
|
|
|
char json[] = "{\"key\":\"value\"}";
|
2015-07-25 15:38:12 +02:00
|
|
|
const JsonObject &object = _jsonBuffer.parseObject(json);
|
2015-05-25 15:38:58 +02:00
|
|
|
ASSERT_TRUE(object.is<const char *>(String("key")));
|
|
|
|
}
|
|
|
|
|
2015-07-25 15:38:12 +02:00
|
|
|
TEST_F(ArduinoStringTests, JsonObject_CreateNestedObject) {
|
2015-05-25 15:38:58 +02:00
|
|
|
String key = "key";
|
|
|
|
char json[64];
|
2015-07-25 15:38:12 +02:00
|
|
|
JsonObject &object = _jsonBuffer.createObject();
|
2015-05-25 15:38:58 +02:00
|
|
|
object.createNestedObject(key);
|
2015-07-25 15:38:12 +02:00
|
|
|
eraseString(key);
|
2015-05-25 15:38:58 +02:00
|
|
|
object.printTo(json, sizeof(json));
|
|
|
|
ASSERT_STREQ("{\"key\":{}}", json);
|
|
|
|
}
|
|
|
|
|
2015-07-25 15:38:12 +02:00
|
|
|
TEST_F(ArduinoStringTests, JsonObject_CreateNestedArray) {
|
2015-05-25 15:38:58 +02:00
|
|
|
String key = "key";
|
|
|
|
char json[64];
|
2015-07-25 15:38:12 +02:00
|
|
|
JsonObject &object = _jsonBuffer.createObject();
|
2015-05-25 15:38:58 +02:00
|
|
|
object.createNestedArray(key);
|
2015-07-25 15:38:12 +02:00
|
|
|
eraseString(key);
|
2015-05-25 15:38:58 +02:00
|
|
|
object.printTo(json, sizeof(json));
|
|
|
|
ASSERT_STREQ("{\"key\":[]}", json);
|
|
|
|
}
|
|
|
|
|
2015-07-25 15:38:12 +02:00
|
|
|
TEST_F(ArduinoStringTests, JsonObject_ContainsKey) {
|
2015-05-25 15:38:58 +02:00
|
|
|
char json[] = "{\"key\":\"value\"}";
|
2015-07-25 15:38:12 +02:00
|
|
|
const JsonObject &object = _jsonBuffer.parseObject(json);
|
2015-05-25 15:38:58 +02:00
|
|
|
ASSERT_TRUE(object.containsKey(String("key")));
|
|
|
|
}
|
|
|
|
|
2015-07-25 15:38:12 +02:00
|
|
|
TEST_F(ArduinoStringTests, JsonObject_Remove) {
|
2015-05-25 15:38:58 +02:00
|
|
|
char json[] = "{\"key\":\"value\"}";
|
2015-07-25 15:38:12 +02:00
|
|
|
JsonObject &object = _jsonBuffer.parseObject(json);
|
2015-05-25 15:38:58 +02:00
|
|
|
ASSERT_EQ(1, object.size());
|
|
|
|
object.remove(String("key"));
|
|
|
|
ASSERT_EQ(0, object.size());
|
2015-07-25 15:38:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ArduinoStringTests, JsonObjectSubscript_SetKey) {
|
|
|
|
JsonObject &object = _jsonBuffer.createObject();
|
|
|
|
String key("hello");
|
|
|
|
object[key] = "world";
|
|
|
|
eraseString(key);
|
|
|
|
ASSERT_STREQ("world", object["hello"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ArduinoStringTests, JsonObjectSubscript_SetValue) {
|
|
|
|
JsonObject &object = _jsonBuffer.createObject();
|
|
|
|
String value("world");
|
|
|
|
object["hello"] = value;
|
|
|
|
eraseString(value);
|
|
|
|
ASSERT_STREQ("world", object["hello"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ArduinoStringTests, JsonArray_Add) {
|
|
|
|
JsonArray &array = _jsonBuffer.createArray();
|
|
|
|
String value("hello");
|
|
|
|
array.add(value);
|
|
|
|
eraseString(value);
|
|
|
|
ASSERT_STREQ("hello", array[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ArduinoStringTests, JsonArray_Set) {
|
|
|
|
JsonArray &array = _jsonBuffer.createArray();
|
|
|
|
String value("world");
|
|
|
|
array.add("hello");
|
|
|
|
array.set(0, value);
|
|
|
|
eraseString(value);
|
|
|
|
ASSERT_STREQ("world", array[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ArduinoStringTests, JsonArraySubscript) {
|
|
|
|
JsonArray &array = _jsonBuffer.createArray();
|
|
|
|
String value("world");
|
|
|
|
array.add("hello");
|
|
|
|
array[0] = value;
|
|
|
|
eraseString(value);
|
|
|
|
ASSERT_STREQ("world", array[0]);
|
|
|
|
}
|
2015-08-10 17:22:22 +02:00
|
|
|
|
|
|
|
TEST_F(ArduinoStringTests, JsonArray_PrintTo) {
|
|
|
|
JsonArray &array = _jsonBuffer.createArray();
|
|
|
|
array.add(4);
|
|
|
|
array.add(2);
|
|
|
|
String json;
|
|
|
|
array.printTo(json);
|
|
|
|
ASSERT_EQ(String("[4,2]"), json);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ArduinoStringTests, JsonArray_PrettyPrintTo) {
|
|
|
|
JsonArray &array = _jsonBuffer.createArray();
|
|
|
|
array.add(4);
|
|
|
|
array.add(2);
|
|
|
|
String json;
|
|
|
|
array.prettyPrintTo(json);
|
|
|
|
ASSERT_EQ(String("[\r\n 4,\r\n 2\r\n]"), json);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ArduinoStringTests, JsonObject_PrintTo) {
|
|
|
|
JsonObject &object = _jsonBuffer.createObject();
|
|
|
|
object["key"] = "value";
|
|
|
|
String json;
|
|
|
|
object.printTo(json);
|
|
|
|
ASSERT_EQ(String("{\"key\":\"value\"}"), json);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ArduinoStringTests, JsonObject_PrettyPrintTo) {
|
|
|
|
JsonObject &object = _jsonBuffer.createObject();
|
|
|
|
object["key"] = "value";
|
|
|
|
String json;
|
|
|
|
object.prettyPrintTo(json);
|
|
|
|
ASSERT_EQ(String("{\r\n \"key\": \"value\"\r\n}"), json);
|
|
|
|
}
|