forked from bblanchon/ArduinoJson
Templatized all functions using String
or std::string
* Removed `ArduinoJson::String` * Removed `JsonVariant::defaultValue<T>()` * Removed non-template `JsonObject::get()` and `JsonArray.get()` * Fixed support for `StringSumHelper` (issue #184) * Replaced `ARDUINOJSON_USE_ARDUINO_STRING` by `ARDUINOJSON_ENABLE_STD_STRING` and `ARDUINOJSON_ENABLE_ARDUINO_STRING` (issue #378) * Added example `StringExample.ino` to show where `String` can be used
This commit is contained in:
@ -5,8 +5,8 @@
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <ArduinoJson.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
class DynamicJsonBuffer_Basic_Tests : public testing::Test {
|
||||
protected:
|
||||
@ -38,3 +38,16 @@ TEST_F(DynamicJsonBuffer_Basic_Tests, Alignment) {
|
||||
ASSERT_EQ(0, addr & mask);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(DynamicJsonBuffer_Basic_Tests, strdup) {
|
||||
char original[] = "hello";
|
||||
char* copy = buffer.strdup(original);
|
||||
strcpy(original, "world");
|
||||
ASSERT_STREQ("hello", copy);
|
||||
}
|
||||
|
||||
TEST_F(DynamicJsonBuffer_Basic_Tests, strdup_givenNull) {
|
||||
const char* original = NULL;
|
||||
char* copy = buffer.strdup(original);
|
||||
ASSERT_EQ(NULL, copy);
|
||||
}
|
||||
|
26
test/JsonObject_Get_Tests.cpp
Normal file
26
test/JsonObject_Get_Tests.cpp
Normal file
@ -0,0 +1,26 @@
|
||||
// Copyright Benoit Blanchon 2014-2016
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
class JsonObject_Get_Tests : public ::testing::Test {
|
||||
public:
|
||||
JsonObject_Get_Tests() : _object(_jsonBuffer.createObject()) {}
|
||||
|
||||
protected:
|
||||
DynamicJsonBuffer _jsonBuffer;
|
||||
JsonObject& _object;
|
||||
};
|
||||
|
||||
#define TEST_(name) TEST_F(JsonObject_Get_Tests, name)
|
||||
|
||||
TEST_(GetConstCharPointer_GivenStringLiteral) {
|
||||
_object.set("hello", "world");
|
||||
const char* value = _object.get<const char*>("hello");
|
||||
EXPECT_STREQ("world", value);
|
||||
}
|
@ -5,11 +5,11 @@
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <ArduinoJson.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
TEST(JsonObject_Invalid_Tests, SubscriptFails) {
|
||||
ASSERT_FALSE(JsonObject::invalid()[0].success());
|
||||
ASSERT_FALSE(JsonObject::invalid()["key"].success());
|
||||
}
|
||||
|
||||
TEST(JsonObject_Invalid_Tests, AddFails) {
|
||||
|
@ -5,8 +5,8 @@
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <ArduinoJson.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
class JsonObject_Set_Tests : public ::testing::Test {
|
||||
public:
|
||||
@ -112,7 +112,7 @@ TEST_(ShouldReturnTrue_WhenAllocationSucceeds) {
|
||||
StaticJsonBuffer<JSON_OBJECT_SIZE(1) + 15> jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
|
||||
bool result = obj.set(String("hello"), String("world"));
|
||||
bool result = obj.set(std::string("hello"), std::string("world"));
|
||||
|
||||
ASSERT_TRUE(result);
|
||||
}
|
||||
@ -121,7 +121,7 @@ TEST_(ShouldReturnFalse_WhenAllocationFails) {
|
||||
StaticJsonBuffer<JSON_OBJECT_SIZE(1) + 10> jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
|
||||
bool result = obj.set(String("hello"), String("world"));
|
||||
bool result = obj.set(std::string("hello"), std::string("world"));
|
||||
|
||||
ASSERT_FALSE(result);
|
||||
}
|
||||
|
@ -24,7 +24,7 @@ TEST(JsonVariant_As_Tests, DoubleAsCstr) {
|
||||
|
||||
TEST(JsonVariant_As_Tests, DoubleAsString) {
|
||||
JsonVariant variant = 4.2;
|
||||
ASSERT_EQ(String("4.20"), variant.as<String>());
|
||||
ASSERT_EQ(std::string("4.20"), variant.as<std::string>());
|
||||
}
|
||||
|
||||
TEST(JsonVariant_As_Tests, DoubleAsLong) {
|
||||
@ -64,7 +64,7 @@ TEST(JsonVariant_As_Tests, FalseAsLong) {
|
||||
|
||||
TEST(JsonVariant_As_Tests, FalseAsString) {
|
||||
JsonVariant variant = false;
|
||||
ASSERT_EQ(String("false"), variant.as<String>());
|
||||
ASSERT_EQ(std::string("false"), variant.as<std::string>());
|
||||
}
|
||||
|
||||
TEST(JsonVariant_As_Tests, TrueAsBool) {
|
||||
@ -84,7 +84,7 @@ TEST(JsonVariant_As_Tests, TrueAsLong) {
|
||||
|
||||
TEST(JsonVariant_As_Tests, TrueAsString) {
|
||||
JsonVariant variant = true;
|
||||
ASSERT_EQ(String("true"), variant.as<String>());
|
||||
ASSERT_EQ(std::string("true"), variant.as<std::string>());
|
||||
}
|
||||
|
||||
TEST(JsonVariant_As_Tests, LongAsBool) {
|
||||
@ -109,7 +109,7 @@ TEST(JsonVariant_As_Tests, NegativeLongAsDouble) {
|
||||
|
||||
TEST(JsonVariant_As_Tests, LongAsString) {
|
||||
JsonVariant variant = 42L;
|
||||
ASSERT_EQ(String("42"), variant.as<String>());
|
||||
ASSERT_EQ(std::string("42"), variant.as<std::string>());
|
||||
}
|
||||
|
||||
TEST(JsonVariant_As_Tests, LongZeroAsDouble) {
|
||||
@ -134,7 +134,7 @@ TEST(JsonVariant_As_Tests, NullAsLong) {
|
||||
|
||||
TEST(JsonVariant_As_Tests, NullAsString) {
|
||||
JsonVariant variant = null;
|
||||
ASSERT_EQ(String("null"), variant.as<String>());
|
||||
ASSERT_EQ(std::string("null"), variant.as<std::string>());
|
||||
}
|
||||
|
||||
TEST(JsonVariant_As_Tests, NumberStringAsBool) {
|
||||
@ -181,7 +181,7 @@ TEST(JsonVariant_As_Tests, RandomStringAsCharPtr) {
|
||||
|
||||
TEST(JsonVariant_As_Tests, RandomStringAsString) {
|
||||
JsonVariant variant = "hello";
|
||||
ASSERT_EQ(String("hello"), variant.as<String>());
|
||||
ASSERT_EQ(std::string("hello"), variant.as<std::string>());
|
||||
}
|
||||
|
||||
TEST(JsonVariant_As_Tests, TrueStringAsBool) {
|
||||
@ -201,7 +201,7 @@ TEST(JsonVariant_As_Tests, ObjectAsString) {
|
||||
obj["key"] = "value";
|
||||
|
||||
JsonVariant variant = obj;
|
||||
ASSERT_EQ(String("{\"key\":\"value\"}"), variant.as<String>());
|
||||
ASSERT_EQ(std::string("{\"key\":\"value\"}"), variant.as<std::string>());
|
||||
}
|
||||
|
||||
TEST(JsonVariant_As_Tests, ArrayAsString) {
|
||||
@ -212,7 +212,7 @@ TEST(JsonVariant_As_Tests, ArrayAsString) {
|
||||
arr.add(2);
|
||||
|
||||
JsonVariant variant = arr;
|
||||
ASSERT_EQ(String("[4,2]"), variant.as<String>());
|
||||
ASSERT_EQ(std::string("[4,2]"), variant.as<std::string>());
|
||||
}
|
||||
|
||||
TEST(JsonVariant_As_Tests, ArrayAsJsonArray) {
|
||||
|
@ -5,12 +5,12 @@
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <ArduinoJson.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
class ArduinoStringTests : public ::testing::Test {
|
||||
class StringTests : public ::testing::Test {
|
||||
protected:
|
||||
static void eraseString(String &str) {
|
||||
static void eraseString(std::string &str) {
|
||||
char *p = const_cast<char *>(str.c_str());
|
||||
while (*p) *p++ = '*';
|
||||
}
|
||||
@ -18,100 +18,100 @@ class ArduinoStringTests : public ::testing::Test {
|
||||
DynamicJsonBuffer _jsonBuffer;
|
||||
};
|
||||
|
||||
TEST_F(ArduinoStringTests, JsonBuffer_ParseArray) {
|
||||
String json("[\"hello\"]");
|
||||
TEST_F(StringTests, JsonBuffer_ParseArray) {
|
||||
std::string json("[\"hello\"]");
|
||||
JsonArray &array = _jsonBuffer.parseArray(json);
|
||||
eraseString(json);
|
||||
ASSERT_TRUE(array.success());
|
||||
ASSERT_STREQ("hello", array[0]);
|
||||
}
|
||||
|
||||
TEST_F(ArduinoStringTests, JsonBuffer_ParseObject) {
|
||||
String json("{\"hello\":\"world\"}");
|
||||
TEST_F(StringTests, JsonBuffer_ParseObject) {
|
||||
std::string json("{\"hello\":\"world\"}");
|
||||
JsonObject &object = _jsonBuffer.parseObject(json);
|
||||
eraseString(json);
|
||||
ASSERT_TRUE(object.success());
|
||||
ASSERT_STREQ("world", object["hello"]);
|
||||
}
|
||||
|
||||
TEST_F(ArduinoStringTests, JsonObject_Subscript) {
|
||||
TEST_F(StringTests, JsonObject_Subscript) {
|
||||
char json[] = "{\"key\":\"value\"}";
|
||||
JsonObject &object = _jsonBuffer.parseObject(json);
|
||||
ASSERT_STREQ("value", object[String("key")]);
|
||||
ASSERT_STREQ("value", object[std::string("key")]);
|
||||
}
|
||||
|
||||
TEST_F(ArduinoStringTests, JsonObject_ConstSubscript) {
|
||||
TEST_F(StringTests, JsonObject_ConstSubscript) {
|
||||
char json[] = "{\"key\":\"value\"}";
|
||||
const JsonObject &object = _jsonBuffer.parseObject(json);
|
||||
ASSERT_STREQ("value", object[String("key")]);
|
||||
ASSERT_STREQ("value", object[std::string("key")]);
|
||||
}
|
||||
|
||||
TEST_F(ArduinoStringTests, JsonObject_SetKey) {
|
||||
TEST_F(StringTests, JsonObject_SetKey) {
|
||||
JsonObject &object = _jsonBuffer.createObject();
|
||||
String key("hello");
|
||||
std::string key("hello");
|
||||
object.set(key, "world");
|
||||
eraseString(key);
|
||||
ASSERT_STREQ("world", object["hello"]);
|
||||
}
|
||||
|
||||
TEST_F(ArduinoStringTests, JsonObject_SetValue) {
|
||||
TEST_F(StringTests, JsonObject_SetValue) {
|
||||
JsonObject &object = _jsonBuffer.createObject();
|
||||
String value("world");
|
||||
std::string value("world");
|
||||
object.set("hello", value);
|
||||
eraseString(value);
|
||||
ASSERT_STREQ("world", object["hello"]);
|
||||
}
|
||||
|
||||
TEST_F(ArduinoStringTests, JsonObject_SetKeyValue) {
|
||||
TEST_F(StringTests, JsonObject_SetKeyValue) {
|
||||
JsonObject &object = _jsonBuffer.createObject();
|
||||
String key("hello");
|
||||
String value("world");
|
||||
std::string key("hello");
|
||||
std::string value("world");
|
||||
object.set(key, value);
|
||||
eraseString(key);
|
||||
eraseString(value);
|
||||
ASSERT_STREQ("world", object["hello"]);
|
||||
}
|
||||
|
||||
TEST_F(ArduinoStringTests, JsonObject_SetToArraySubscript) {
|
||||
TEST_F(StringTests, JsonObject_SetToArraySubscript) {
|
||||
JsonArray &arr = _jsonBuffer.createArray();
|
||||
arr.add("world");
|
||||
|
||||
JsonObject &object = _jsonBuffer.createObject();
|
||||
object.set(String("hello"), arr[0]);
|
||||
object.set(std::string("hello"), arr[0]);
|
||||
|
||||
ASSERT_STREQ("world", object["hello"]);
|
||||
}
|
||||
|
||||
TEST_F(ArduinoStringTests, JsonObject_SetToObjectSubscript) {
|
||||
TEST_F(StringTests, JsonObject_SetToObjectSubscript) {
|
||||
JsonObject &arr = _jsonBuffer.createObject();
|
||||
arr.set("x", "world");
|
||||
|
||||
JsonObject &object = _jsonBuffer.createObject();
|
||||
object.set(String("hello"), arr["x"]);
|
||||
object.set(std::string("hello"), arr["x"]);
|
||||
|
||||
ASSERT_STREQ("world", object["hello"]);
|
||||
}
|
||||
|
||||
TEST_F(ArduinoStringTests, JsonObject_Get) {
|
||||
TEST_F(StringTests, JsonObject_Get) {
|
||||
char json[] = "{\"key\":\"value\"}";
|
||||
const JsonObject &object = _jsonBuffer.parseObject(json);
|
||||
ASSERT_STREQ("value", object.get(String("key")));
|
||||
ASSERT_STREQ("value", object.get<const char *>(std::string("key")));
|
||||
}
|
||||
|
||||
TEST_F(ArduinoStringTests, JsonObject_GetT) {
|
||||
TEST_F(StringTests, JsonObject_GetT) {
|
||||
char json[] = "{\"key\":\"value\"}";
|
||||
const JsonObject &object = _jsonBuffer.parseObject(json);
|
||||
ASSERT_STREQ("value", object.get<const char *>(String("key")));
|
||||
ASSERT_STREQ("value", object.get<const char *>(std::string("key")));
|
||||
}
|
||||
|
||||
TEST_F(ArduinoStringTests, JsonObject_IsT) {
|
||||
TEST_F(StringTests, JsonObject_IsT) {
|
||||
char json[] = "{\"key\":\"value\"}";
|
||||
const JsonObject &object = _jsonBuffer.parseObject(json);
|
||||
ASSERT_TRUE(object.is<const char *>(String("key")));
|
||||
ASSERT_TRUE(object.is<const char *>(std::string("key")));
|
||||
}
|
||||
|
||||
TEST_F(ArduinoStringTests, JsonObject_CreateNestedObject) {
|
||||
String key = "key";
|
||||
TEST_F(StringTests, JsonObject_CreateNestedObject) {
|
||||
std::string key = "key";
|
||||
char json[64];
|
||||
JsonObject &object = _jsonBuffer.createObject();
|
||||
object.createNestedObject(key);
|
||||
@ -120,8 +120,8 @@ TEST_F(ArduinoStringTests, JsonObject_CreateNestedObject) {
|
||||
ASSERT_STREQ("{\"key\":{}}", json);
|
||||
}
|
||||
|
||||
TEST_F(ArduinoStringTests, JsonObject_CreateNestedArray) {
|
||||
String key = "key";
|
||||
TEST_F(StringTests, JsonObject_CreateNestedArray) {
|
||||
std::string key = "key";
|
||||
char json[64];
|
||||
JsonObject &object = _jsonBuffer.createObject();
|
||||
object.createNestedArray(key);
|
||||
@ -130,99 +130,99 @@ TEST_F(ArduinoStringTests, JsonObject_CreateNestedArray) {
|
||||
ASSERT_STREQ("{\"key\":[]}", json);
|
||||
}
|
||||
|
||||
TEST_F(ArduinoStringTests, JsonObject_ContainsKey) {
|
||||
TEST_F(StringTests, JsonObject_ContainsKey) {
|
||||
char json[] = "{\"key\":\"value\"}";
|
||||
const JsonObject &object = _jsonBuffer.parseObject(json);
|
||||
ASSERT_TRUE(object.containsKey(String("key")));
|
||||
ASSERT_TRUE(object.containsKey(std::string("key")));
|
||||
}
|
||||
|
||||
TEST_F(ArduinoStringTests, JsonObject_Remove) {
|
||||
TEST_F(StringTests, JsonObject_Remove) {
|
||||
char json[] = "{\"key\":\"value\"}";
|
||||
JsonObject &object = _jsonBuffer.parseObject(json);
|
||||
ASSERT_EQ(1, object.size());
|
||||
object.remove(String("key"));
|
||||
object.remove(std::string("key"));
|
||||
ASSERT_EQ(0, object.size());
|
||||
}
|
||||
|
||||
TEST_F(ArduinoStringTests, JsonObjectSubscript_SetKey) {
|
||||
TEST_F(StringTests, JsonObjectSubscript_SetKey) {
|
||||
JsonObject &object = _jsonBuffer.createObject();
|
||||
String key("hello");
|
||||
std::string key("hello");
|
||||
object[key] = "world";
|
||||
eraseString(key);
|
||||
ASSERT_STREQ("world", object["hello"]);
|
||||
}
|
||||
|
||||
TEST_F(ArduinoStringTests, JsonObjectSubscript_SetValue) {
|
||||
TEST_F(StringTests, JsonObjectSubscript_SetValue) {
|
||||
JsonObject &object = _jsonBuffer.createObject();
|
||||
String value("world");
|
||||
std::string value("world");
|
||||
object["hello"] = value;
|
||||
eraseString(value);
|
||||
ASSERT_STREQ("world", object["hello"]);
|
||||
}
|
||||
|
||||
TEST_F(ArduinoStringTests, JsonArray_Add) {
|
||||
TEST_F(StringTests, JsonArray_Add) {
|
||||
JsonArray &array = _jsonBuffer.createArray();
|
||||
String value("hello");
|
||||
std::string value("hello");
|
||||
array.add(value);
|
||||
eraseString(value);
|
||||
ASSERT_STREQ("hello", array[0]);
|
||||
}
|
||||
|
||||
TEST_F(ArduinoStringTests, JsonArray_Set) {
|
||||
TEST_F(StringTests, JsonArray_Set) {
|
||||
JsonArray &array = _jsonBuffer.createArray();
|
||||
String value("world");
|
||||
std::string value("world");
|
||||
array.add("hello");
|
||||
array.set(0, value);
|
||||
eraseString(value);
|
||||
ASSERT_STREQ("world", array[0]);
|
||||
}
|
||||
|
||||
TEST_F(ArduinoStringTests, JsonArraySubscript) {
|
||||
TEST_F(StringTests, JsonArraySubscript) {
|
||||
JsonArray &array = _jsonBuffer.createArray();
|
||||
String value("world");
|
||||
std::string value("world");
|
||||
array.add("hello");
|
||||
array[0] = value;
|
||||
eraseString(value);
|
||||
ASSERT_STREQ("world", array[0]);
|
||||
}
|
||||
|
||||
TEST_F(ArduinoStringTests, JsonArray_PrintTo) {
|
||||
TEST_F(StringTests, JsonArray_PrintTo) {
|
||||
JsonArray &array = _jsonBuffer.createArray();
|
||||
array.add(4);
|
||||
array.add(2);
|
||||
String json;
|
||||
std::string json;
|
||||
array.printTo(json);
|
||||
ASSERT_EQ(String("[4,2]"), json);
|
||||
ASSERT_EQ(std::string("[4,2]"), json);
|
||||
}
|
||||
|
||||
TEST_F(ArduinoStringTests, JsonArray_PrettyPrintTo) {
|
||||
TEST_F(StringTests, JsonArray_PrettyPrintTo) {
|
||||
JsonArray &array = _jsonBuffer.createArray();
|
||||
array.add(4);
|
||||
array.add(2);
|
||||
String json;
|
||||
std::string json;
|
||||
array.prettyPrintTo(json);
|
||||
ASSERT_EQ(String("[\r\n 4,\r\n 2\r\n]"), json);
|
||||
ASSERT_EQ(std::string("[\r\n 4,\r\n 2\r\n]"), json);
|
||||
}
|
||||
|
||||
TEST_F(ArduinoStringTests, JsonObject_PrintTo) {
|
||||
TEST_F(StringTests, JsonObject_PrintTo) {
|
||||
JsonObject &object = _jsonBuffer.createObject();
|
||||
object["key"] = "value";
|
||||
String json;
|
||||
std::string json;
|
||||
object.printTo(json);
|
||||
ASSERT_EQ(String("{\"key\":\"value\"}"), json);
|
||||
ASSERT_EQ(std::string("{\"key\":\"value\"}"), json);
|
||||
}
|
||||
|
||||
TEST_F(ArduinoStringTests, JsonObject_PrettyPrintTo) {
|
||||
TEST_F(StringTests, JsonObject_PrettyPrintTo) {
|
||||
JsonObject &object = _jsonBuffer.createObject();
|
||||
object["key"] = "value";
|
||||
String json;
|
||||
std::string json;
|
||||
object.prettyPrintTo(json);
|
||||
ASSERT_EQ(String("{\r\n \"key\": \"value\"\r\n}"), json);
|
||||
ASSERT_EQ(std::string("{\r\n \"key\": \"value\"\r\n}"), json);
|
||||
}
|
||||
|
||||
TEST_F(ArduinoStringTests, JsonBuffer_GrowWhenAddingNewKey) {
|
||||
TEST_F(StringTests, JsonBuffer_GrowWhenAddingNewKey) {
|
||||
JsonObject &object = _jsonBuffer.createObject();
|
||||
String key1("hello"), key2("world");
|
||||
std::string key1("hello"), key2("world");
|
||||
|
||||
object[key1] = 1;
|
||||
size_t sizeBefore = _jsonBuffer.size();
|
||||
@ -232,9 +232,9 @@ TEST_F(ArduinoStringTests, JsonBuffer_GrowWhenAddingNewKey) {
|
||||
ASSERT_GT(sizeAfter - sizeBefore, key2.size());
|
||||
}
|
||||
|
||||
TEST_F(ArduinoStringTests, JsonBuffer_DontGrowWhenReusingKey) {
|
||||
TEST_F(StringTests, JsonBuffer_DontGrowWhenReusingKey) {
|
||||
JsonObject &object = _jsonBuffer.createObject();
|
||||
String key("hello");
|
||||
std::string key("hello");
|
||||
|
||||
object[key] = 1;
|
||||
size_t sizeBefore = _jsonBuffer.size();
|
||||
@ -243,3 +243,10 @@ TEST_F(ArduinoStringTests, JsonBuffer_DontGrowWhenReusingKey) {
|
||||
|
||||
ASSERT_EQ(sizeBefore, sizeAfter);
|
||||
}
|
||||
|
||||
TEST_F(StringTests, JsonBuffer_strdup) {
|
||||
std::string original("hello");
|
||||
char *copy = _jsonBuffer.strdup(original);
|
||||
original[0] = 'w';
|
||||
ASSERT_STREQ("hello", copy);
|
||||
}
|
Reference in New Issue
Block a user