mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-22 06:52:24 +02:00
Organized test files in subfolders
This commit is contained in:
75
test/Misc/StringBuilder.cpp
Normal file
75
test/Misc/StringBuilder.cpp
Normal file
@ -0,0 +1,75 @@
|
||||
// Copyright Benoit Blanchon 2014-2017
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://bblanchon.github.io/ArduinoJson/
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
using namespace ArduinoJson::Internals;
|
||||
|
||||
class StringBuilderTests : public testing::Test {
|
||||
protected:
|
||||
virtual void SetUp() {
|
||||
_stringBuilder = new StaticStringBuilder(_buffer, sizeof(_buffer));
|
||||
}
|
||||
|
||||
virtual void TearDown() {
|
||||
delete _stringBuilder;
|
||||
}
|
||||
|
||||
void print(const char *value) {
|
||||
_returnValue = _stringBuilder->print(value);
|
||||
}
|
||||
|
||||
void outputMustBe(const char *expected) {
|
||||
EXPECT_STREQ(expected, _buffer);
|
||||
}
|
||||
|
||||
void resultMustBe(size_t expected) {
|
||||
EXPECT_EQ(expected, _returnValue);
|
||||
}
|
||||
|
||||
private:
|
||||
char _buffer[20];
|
||||
Print *_stringBuilder;
|
||||
size_t _returnValue;
|
||||
};
|
||||
|
||||
TEST_F(StringBuilderTests, InitialState) {
|
||||
outputMustBe("");
|
||||
}
|
||||
|
||||
TEST_F(StringBuilderTests, OverCapacity) {
|
||||
print("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
|
||||
resultMustBe(19);
|
||||
|
||||
print("ABC");
|
||||
resultMustBe(0);
|
||||
|
||||
outputMustBe("ABCDEFGHIJKLMNOPQRS");
|
||||
}
|
||||
|
||||
TEST_F(StringBuilderTests, EmptyString) {
|
||||
print("");
|
||||
resultMustBe(0);
|
||||
outputMustBe("");
|
||||
}
|
||||
|
||||
TEST_F(StringBuilderTests, OneString) {
|
||||
print("ABCD");
|
||||
resultMustBe(4);
|
||||
outputMustBe("ABCD");
|
||||
}
|
||||
|
||||
TEST_F(StringBuilderTests, TwoStrings) {
|
||||
print("ABCD");
|
||||
resultMustBe(4);
|
||||
|
||||
print("EFGH");
|
||||
resultMustBe(4);
|
||||
|
||||
outputMustBe("ABCDEFGH");
|
||||
}
|
36
test/Misc/TypeTraits.cpp
Normal file
36
test/Misc/TypeTraits.cpp
Normal file
@ -0,0 +1,36 @@
|
||||
// Copyright Benoit Blanchon 2014-2017
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://bblanchon.github.io/ArduinoJson/
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <gtest/gtest.h>
|
||||
#include <sstream>
|
||||
|
||||
using namespace ArduinoJson::TypeTraits;
|
||||
|
||||
TEST(TypeTraits, IsBaseOf) {
|
||||
ASSERT_FALSE((IsBaseOf<std::istream, std::ostringstream>::value));
|
||||
ASSERT_TRUE((IsBaseOf<std::istream, std::istringstream>::value));
|
||||
ASSERT_TRUE((IsBaseOf<JsonVariantBase<JsonObjectSubscript<const char*> >,
|
||||
JsonObjectSubscript<const char*> >::value));
|
||||
}
|
||||
|
||||
TEST(TypeTraits, IsArray) {
|
||||
ASSERT_FALSE((IsArray<const char*>::value));
|
||||
ASSERT_TRUE((IsArray<const char[]>::value));
|
||||
ASSERT_TRUE((IsArray<const char[10]>::value));
|
||||
}
|
||||
|
||||
TEST(TypeTraits, IsVariant) {
|
||||
ASSERT_TRUE((IsVariant<JsonObjectSubscript<const char*> >::value));
|
||||
ASSERT_TRUE((IsVariant<JsonVariant>::value));
|
||||
}
|
||||
|
||||
TEST(TypeTraits, IsString) {
|
||||
ASSERT_TRUE((IsString<const char*>::value));
|
||||
ASSERT_TRUE((IsString<std::string>::value));
|
||||
ASSERT_FALSE((IsString<double>::value));
|
||||
}
|
36
test/Misc/deprecated.cpp
Normal file
36
test/Misc/deprecated.cpp
Normal file
@ -0,0 +1,36 @@
|
||||
// Copyright Benoit Blanchon 2014-2017
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://bblanchon.github.io/ArduinoJson/
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#define ARDUINOJSON_ENABLE_DEPRECATED 1
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#if defined(__clang__)
|
||||
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
|
||||
#elif defined(__GNUC__)
|
||||
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
|
||||
#elif defined(_MSC_VER)
|
||||
#pragma warning(disable : 4996)
|
||||
#endif
|
||||
|
||||
TEST(Deprecated, asArray) {
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonVariant variant = jsonBuffer.createArray();
|
||||
ASSERT_TRUE(variant.asArray().success());
|
||||
}
|
||||
|
||||
TEST(Deprecated, asObject) {
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonVariant variant = jsonBuffer.createObject();
|
||||
ASSERT_TRUE(variant.asObject().success());
|
||||
}
|
||||
|
||||
TEST(Deprecated, asString) {
|
||||
JsonVariant variant = "hello";
|
||||
ASSERT_STREQ("hello", variant.asString());
|
||||
}
|
85
test/Misc/std_stream.cpp
Normal file
85
test/Misc/std_stream.cpp
Normal file
@ -0,0 +1,85 @@
|
||||
// Copyright Benoit Blanchon 2014-2017
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://bblanchon.github.io/ArduinoJson/
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <gtest/gtest.h>
|
||||
#include <sstream>
|
||||
|
||||
TEST(StdStream_Tests, JsonVariantFalse) {
|
||||
std::ostringstream os;
|
||||
JsonVariant variant = false;
|
||||
os << variant;
|
||||
ASSERT_EQ("false", os.str());
|
||||
}
|
||||
|
||||
TEST(StdStream_Tests, JsonVariantString) {
|
||||
std::ostringstream os;
|
||||
JsonVariant variant = "coucou";
|
||||
os << variant;
|
||||
ASSERT_EQ("\"coucou\"", os.str());
|
||||
}
|
||||
|
||||
TEST(StdStream_Tests, JsonObject) {
|
||||
std::ostringstream os;
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& object = jsonBuffer.createObject();
|
||||
object["key"] = "value";
|
||||
os << object;
|
||||
ASSERT_EQ("{\"key\":\"value\"}", os.str());
|
||||
}
|
||||
|
||||
TEST(StdStream_Tests, JsonObjectSubscript) {
|
||||
std::ostringstream os;
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& object = jsonBuffer.createObject();
|
||||
object["key"] = "value";
|
||||
os << object["key"];
|
||||
ASSERT_EQ("\"value\"", os.str());
|
||||
}
|
||||
|
||||
TEST(StdStream_Tests, JsonArray) {
|
||||
std::ostringstream os;
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonArray& array = jsonBuffer.createArray();
|
||||
array.add("value");
|
||||
os << array;
|
||||
ASSERT_EQ("[\"value\"]", os.str());
|
||||
}
|
||||
|
||||
TEST(StdStream_Tests, JsonArraySubscript) {
|
||||
std::ostringstream os;
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonArray& array = jsonBuffer.createArray();
|
||||
array.add("value");
|
||||
os << array[0];
|
||||
ASSERT_EQ("\"value\"", os.str());
|
||||
}
|
||||
|
||||
TEST(StdStream_Tests, ParseArray) {
|
||||
std::istringstream json(" [ 42 /* comment */ ] ");
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonArray& arr = jsonBuffer.parseArray(json);
|
||||
ASSERT_TRUE(arr.success());
|
||||
ASSERT_EQ(1, arr.size());
|
||||
ASSERT_EQ(42, arr[0]);
|
||||
}
|
||||
|
||||
TEST(StdStream_Tests, ParseObject) {
|
||||
std::istringstream json(" { hello : world // comment\n }");
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.parseObject(json);
|
||||
ASSERT_TRUE(obj.success());
|
||||
ASSERT_EQ(1, obj.size());
|
||||
ASSERT_STREQ("world", obj["hello"]);
|
||||
}
|
||||
|
||||
TEST(StdStream_Tests, ShouldNotReadPastTheEnd) {
|
||||
std::istringstream json("{}123");
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
jsonBuffer.parseObject(json);
|
||||
ASSERT_EQ('1', json.get());
|
||||
}
|
252
test/Misc/std_string.cpp
Normal file
252
test/Misc/std_string.cpp
Normal file
@ -0,0 +1,252 @@
|
||||
// Copyright Benoit Blanchon 2014-2017
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://bblanchon.github.io/ArduinoJson/
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
class StringTests : public ::testing::Test {
|
||||
protected:
|
||||
static void eraseString(std::string &str) {
|
||||
char *p = const_cast<char *>(str.c_str());
|
||||
while (*p) *p++ = '*';
|
||||
}
|
||||
|
||||
DynamicJsonBuffer _jsonBuffer;
|
||||
};
|
||||
|
||||
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(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(StringTests, JsonObject_Subscript) {
|
||||
char json[] = "{\"key\":\"value\"}";
|
||||
JsonObject &object = _jsonBuffer.parseObject(json);
|
||||
ASSERT_STREQ("value", object[std::string("key")]);
|
||||
}
|
||||
|
||||
TEST_F(StringTests, JsonObject_ConstSubscript) {
|
||||
char json[] = "{\"key\":\"value\"}";
|
||||
const JsonObject &object = _jsonBuffer.parseObject(json);
|
||||
ASSERT_STREQ("value", object[std::string("key")]);
|
||||
}
|
||||
|
||||
TEST_F(StringTests, JsonObject_SetKey) {
|
||||
JsonObject &object = _jsonBuffer.createObject();
|
||||
std::string key("hello");
|
||||
object.set(key, "world");
|
||||
eraseString(key);
|
||||
ASSERT_STREQ("world", object["hello"]);
|
||||
}
|
||||
|
||||
TEST_F(StringTests, JsonObject_SetValue) {
|
||||
JsonObject &object = _jsonBuffer.createObject();
|
||||
std::string value("world");
|
||||
object.set("hello", value);
|
||||
eraseString(value);
|
||||
ASSERT_STREQ("world", object["hello"]);
|
||||
}
|
||||
|
||||
TEST_F(StringTests, JsonObject_SetKeyValue) {
|
||||
JsonObject &object = _jsonBuffer.createObject();
|
||||
std::string key("hello");
|
||||
std::string value("world");
|
||||
object.set(key, value);
|
||||
eraseString(key);
|
||||
eraseString(value);
|
||||
ASSERT_STREQ("world", object["hello"]);
|
||||
}
|
||||
|
||||
TEST_F(StringTests, JsonObject_SetToArraySubscript) {
|
||||
JsonArray &arr = _jsonBuffer.createArray();
|
||||
arr.add("world");
|
||||
|
||||
JsonObject &object = _jsonBuffer.createObject();
|
||||
object.set(std::string("hello"), arr[0]);
|
||||
|
||||
ASSERT_STREQ("world", object["hello"]);
|
||||
}
|
||||
|
||||
TEST_F(StringTests, JsonObject_SetToObjectSubscript) {
|
||||
JsonObject &arr = _jsonBuffer.createObject();
|
||||
arr.set("x", "world");
|
||||
|
||||
JsonObject &object = _jsonBuffer.createObject();
|
||||
object.set(std::string("hello"), arr["x"]);
|
||||
|
||||
ASSERT_STREQ("world", object["hello"]);
|
||||
}
|
||||
|
||||
TEST_F(StringTests, JsonObject_Get) {
|
||||
char json[] = "{\"key\":\"value\"}";
|
||||
const JsonObject &object = _jsonBuffer.parseObject(json);
|
||||
ASSERT_STREQ("value", object.get<const char *>(std::string("key")));
|
||||
}
|
||||
|
||||
TEST_F(StringTests, JsonObject_GetT) {
|
||||
char json[] = "{\"key\":\"value\"}";
|
||||
const JsonObject &object = _jsonBuffer.parseObject(json);
|
||||
ASSERT_STREQ("value", object.get<const char *>(std::string("key")));
|
||||
}
|
||||
|
||||
TEST_F(StringTests, JsonObject_IsT) {
|
||||
char json[] = "{\"key\":\"value\"}";
|
||||
const JsonObject &object = _jsonBuffer.parseObject(json);
|
||||
ASSERT_TRUE(object.is<const char *>(std::string("key")));
|
||||
}
|
||||
|
||||
TEST_F(StringTests, JsonObject_CreateNestedObject) {
|
||||
std::string key = "key";
|
||||
char json[64];
|
||||
JsonObject &object = _jsonBuffer.createObject();
|
||||
object.createNestedObject(key);
|
||||
eraseString(key);
|
||||
object.printTo(json, sizeof(json));
|
||||
ASSERT_STREQ("{\"key\":{}}", json);
|
||||
}
|
||||
|
||||
TEST_F(StringTests, JsonObject_CreateNestedArray) {
|
||||
std::string key = "key";
|
||||
char json[64];
|
||||
JsonObject &object = _jsonBuffer.createObject();
|
||||
object.createNestedArray(key);
|
||||
eraseString(key);
|
||||
object.printTo(json, sizeof(json));
|
||||
ASSERT_STREQ("{\"key\":[]}", json);
|
||||
}
|
||||
|
||||
TEST_F(StringTests, JsonObject_ContainsKey) {
|
||||
char json[] = "{\"key\":\"value\"}";
|
||||
const JsonObject &object = _jsonBuffer.parseObject(json);
|
||||
ASSERT_TRUE(object.containsKey(std::string("key")));
|
||||
}
|
||||
|
||||
TEST_F(StringTests, JsonObject_Remove) {
|
||||
char json[] = "{\"key\":\"value\"}";
|
||||
JsonObject &object = _jsonBuffer.parseObject(json);
|
||||
ASSERT_EQ(1, object.size());
|
||||
object.remove(std::string("key"));
|
||||
ASSERT_EQ(0, object.size());
|
||||
}
|
||||
|
||||
TEST_F(StringTests, JsonObjectSubscript_SetKey) {
|
||||
JsonObject &object = _jsonBuffer.createObject();
|
||||
std::string key("hello");
|
||||
object[key] = "world";
|
||||
eraseString(key);
|
||||
ASSERT_STREQ("world", object["hello"]);
|
||||
}
|
||||
|
||||
TEST_F(StringTests, JsonObjectSubscript_SetValue) {
|
||||
JsonObject &object = _jsonBuffer.createObject();
|
||||
std::string value("world");
|
||||
object["hello"] = value;
|
||||
eraseString(value);
|
||||
ASSERT_STREQ("world", object["hello"]);
|
||||
}
|
||||
|
||||
TEST_F(StringTests, JsonArray_Add) {
|
||||
JsonArray &array = _jsonBuffer.createArray();
|
||||
std::string value("hello");
|
||||
array.add(value);
|
||||
eraseString(value);
|
||||
ASSERT_STREQ("hello", array[0]);
|
||||
}
|
||||
|
||||
TEST_F(StringTests, JsonArray_Set) {
|
||||
JsonArray &array = _jsonBuffer.createArray();
|
||||
std::string value("world");
|
||||
array.add("hello");
|
||||
array.set(0, value);
|
||||
eraseString(value);
|
||||
ASSERT_STREQ("world", array[0]);
|
||||
}
|
||||
|
||||
TEST_F(StringTests, JsonArraySubscript) {
|
||||
JsonArray &array = _jsonBuffer.createArray();
|
||||
std::string value("world");
|
||||
array.add("hello");
|
||||
array[0] = value;
|
||||
eraseString(value);
|
||||
ASSERT_STREQ("world", array[0]);
|
||||
}
|
||||
|
||||
TEST_F(StringTests, JsonArray_PrintTo) {
|
||||
JsonArray &array = _jsonBuffer.createArray();
|
||||
array.add(4);
|
||||
array.add(2);
|
||||
std::string json;
|
||||
array.printTo(json);
|
||||
ASSERT_EQ(std::string("[4,2]"), json);
|
||||
}
|
||||
|
||||
TEST_F(StringTests, JsonArray_PrettyPrintTo) {
|
||||
JsonArray &array = _jsonBuffer.createArray();
|
||||
array.add(4);
|
||||
array.add(2);
|
||||
std::string json;
|
||||
array.prettyPrintTo(json);
|
||||
ASSERT_EQ(std::string("[\r\n 4,\r\n 2\r\n]"), json);
|
||||
}
|
||||
|
||||
TEST_F(StringTests, JsonObject_PrintTo) {
|
||||
JsonObject &object = _jsonBuffer.createObject();
|
||||
object["key"] = "value";
|
||||
std::string json;
|
||||
object.printTo(json);
|
||||
ASSERT_EQ(std::string("{\"key\":\"value\"}"), json);
|
||||
}
|
||||
|
||||
TEST_F(StringTests, JsonObject_PrettyPrintTo) {
|
||||
JsonObject &object = _jsonBuffer.createObject();
|
||||
object["key"] = "value";
|
||||
std::string json;
|
||||
object.prettyPrintTo(json);
|
||||
ASSERT_EQ(std::string("{\r\n \"key\": \"value\"\r\n}"), json);
|
||||
}
|
||||
|
||||
TEST_F(StringTests, JsonBuffer_GrowWhenAddingNewKey) {
|
||||
JsonObject &object = _jsonBuffer.createObject();
|
||||
std::string key1("hello"), key2("world");
|
||||
|
||||
object[key1] = 1;
|
||||
size_t sizeBefore = _jsonBuffer.size();
|
||||
object[key2] = 2;
|
||||
size_t sizeAfter = _jsonBuffer.size();
|
||||
|
||||
ASSERT_GT(sizeAfter - sizeBefore, key2.size());
|
||||
}
|
||||
|
||||
TEST_F(StringTests, JsonBuffer_DontGrowWhenReusingKey) {
|
||||
JsonObject &object = _jsonBuffer.createObject();
|
||||
std::string key("hello");
|
||||
|
||||
object[key] = 1;
|
||||
size_t sizeBefore = _jsonBuffer.size();
|
||||
object[key] = 2;
|
||||
size_t sizeAfter = _jsonBuffer.size();
|
||||
|
||||
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);
|
||||
}
|
283
test/Misc/unsigned_char.cpp
Normal file
283
test/Misc/unsigned_char.cpp
Normal file
@ -0,0 +1,283 @@
|
||||
// Copyright Benoit Blanchon 2014-2017
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://bblanchon.github.io/ArduinoJson/
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#if defined(__clang__)
|
||||
#define CONFLICTS_WITH_BUILTIN_OPERATOR
|
||||
#endif
|
||||
|
||||
TEST(UnsignedCharArray, ParseArray) {
|
||||
unsigned char json[] = "[42]";
|
||||
|
||||
StaticJsonBuffer<JSON_ARRAY_SIZE(1)> jsonBuffer;
|
||||
JsonArray& arr = jsonBuffer.parseArray(json);
|
||||
|
||||
EXPECT_TRUE(arr.success());
|
||||
}
|
||||
|
||||
TEST(UnsignedCharArray, ParseObject) {
|
||||
unsigned char json[] = "{\"a\":42}";
|
||||
|
||||
StaticJsonBuffer<JSON_OBJECT_SIZE(1)> jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.parseObject(json);
|
||||
|
||||
EXPECT_TRUE(obj.success());
|
||||
}
|
||||
|
||||
TEST(UnsignedCharArray, JsonVariant_Constructor) {
|
||||
unsigned char value[] = "42";
|
||||
|
||||
JsonVariant variant(value);
|
||||
|
||||
EXPECT_EQ(42, variant.as<int>());
|
||||
}
|
||||
|
||||
TEST(UnsignedCharArray, JsonVariant_Assign) {
|
||||
unsigned char value[] = "42";
|
||||
|
||||
JsonVariant variant(666);
|
||||
variant = value;
|
||||
|
||||
EXPECT_EQ(42, variant.as<int>());
|
||||
}
|
||||
|
||||
#ifndef CONFLICTS_WITH_BUILTIN_OPERATOR
|
||||
TEST(UnsignedCharArray, JsonVariant_Subscript) {
|
||||
unsigned char key[] = "hello";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonVariant variant = jsonBuffer.parseObject("{\"hello\":\"world\"}");
|
||||
|
||||
EXPECT_STREQ("world", variant[key]);
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef CONFLICTS_WITH_BUILTIN_OPERATOR
|
||||
TEST(UnsignedCharArray, JsonVariant_Subscript_Const) {
|
||||
unsigned char key[] = "hello";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
const JsonVariant variant = jsonBuffer.parseObject("{\"hello\":\"world\"}");
|
||||
|
||||
EXPECT_STREQ("world", variant[key]);
|
||||
}
|
||||
#endif
|
||||
|
||||
TEST(UnsignedCharArray, JsonVariant_Equals) {
|
||||
unsigned char comparand[] = "hello";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
const JsonVariant variant = "hello";
|
||||
|
||||
EXPECT_TRUE(comparand == variant);
|
||||
EXPECT_TRUE(variant == comparand);
|
||||
EXPECT_FALSE(comparand != variant);
|
||||
EXPECT_FALSE(variant != comparand);
|
||||
}
|
||||
|
||||
TEST(UnsignedCharArray, JsonVariant_Differs) {
|
||||
unsigned char comparand[] = "hello";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
const JsonVariant variant = "world";
|
||||
|
||||
EXPECT_TRUE(comparand != variant);
|
||||
EXPECT_TRUE(variant != comparand);
|
||||
EXPECT_FALSE(comparand == variant);
|
||||
EXPECT_FALSE(variant == comparand);
|
||||
}
|
||||
|
||||
#ifndef CONFLICTS_WITH_BUILTIN_OPERATOR
|
||||
TEST(UnsignedCharArray, JsonObject_Subscript) {
|
||||
unsigned char key[] = "hello";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
obj[key] = "world";
|
||||
|
||||
EXPECT_STREQ("world", obj["hello"]);
|
||||
}
|
||||
#endif
|
||||
|
||||
TEST(UnsignedCharArray, JsonObject_Subscript_Assign) { // issue #416
|
||||
unsigned char value[] = "world";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
obj["hello"] = value;
|
||||
|
||||
EXPECT_STREQ("world", obj["hello"]);
|
||||
}
|
||||
|
||||
TEST(UnsignedCharArray, JsonObject_Subscript_Set) {
|
||||
unsigned char value[] = "world";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
obj["hello"].set(value);
|
||||
|
||||
EXPECT_STREQ("world", obj["hello"]);
|
||||
}
|
||||
|
||||
#ifndef CONFLICTS_WITH_BUILTIN_OPERATOR
|
||||
TEST(UnsignedCharArray, JsonObject_Subscript_Const) {
|
||||
unsigned char key[] = "hello";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
const JsonObject& obj = jsonBuffer.parseObject("{\"hello\":\"world\"}");
|
||||
|
||||
EXPECT_STREQ("world", obj[key]);
|
||||
}
|
||||
#endif
|
||||
|
||||
TEST(UnsignedCharArray, JsonObject_Get) {
|
||||
unsigned char key[] = "hello";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.parseObject("{\"hello\":\"world\"}");
|
||||
|
||||
EXPECT_STREQ("world", obj.get<char*>(key));
|
||||
}
|
||||
|
||||
TEST(UnsignedCharArray, JsonObject_Set_Key) {
|
||||
unsigned char key[] = "hello";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
obj.set(key, "world");
|
||||
|
||||
EXPECT_STREQ("world", obj["hello"]);
|
||||
}
|
||||
|
||||
TEST(UnsignedCharArray, JsonObject_Set_Value) {
|
||||
unsigned char value[] = "world";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
obj.set("hello", value);
|
||||
|
||||
EXPECT_STREQ("world", obj["hello"]);
|
||||
}
|
||||
|
||||
TEST(UnsignedCharArray, JsonObject_Set_Key_WithDecimals) {
|
||||
unsigned char key[] = "hello";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
obj.set(key, 3.14, 2);
|
||||
|
||||
EXPECT_EQ(3.14, obj["hello"]);
|
||||
}
|
||||
|
||||
TEST(UnsignedCharArray, JsonObject_Set_KeyAndValue) {
|
||||
unsigned char key[] = "world";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
obj.set(key, key);
|
||||
|
||||
EXPECT_STREQ("world", obj["world"]);
|
||||
}
|
||||
|
||||
TEST(UnsignedCharArray, JsonObject_ContainsKey) {
|
||||
unsigned char key[] = "hello";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
const JsonObject& obj = jsonBuffer.parseObject("{\"hello\":\"world\"}");
|
||||
|
||||
EXPECT_TRUE(obj.containsKey(key));
|
||||
}
|
||||
|
||||
TEST(UnsignedCharArray, JsonObject_Remove) {
|
||||
unsigned char key[] = "hello";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.parseObject("{\"hello\":\"world\"}");
|
||||
obj.remove(key);
|
||||
|
||||
EXPECT_EQ(0, obj.size());
|
||||
}
|
||||
|
||||
TEST(UnsignedCharArray, JsonObject_Is) {
|
||||
unsigned char key[] = "hello";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.parseObject("{\"hello\":42}");
|
||||
|
||||
EXPECT_TRUE(obj.is<int>(key));
|
||||
}
|
||||
|
||||
TEST(UnsignedCharArray, JsonObject_CreateNestedArray) {
|
||||
unsigned char key[] = "hello";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
obj.createNestedArray(key);
|
||||
}
|
||||
|
||||
TEST(UnsignedCharArray, JsonObject_CreateNestedObject) {
|
||||
unsigned char key[] = "hello";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
obj.createNestedObject(key);
|
||||
}
|
||||
|
||||
TEST(UnsignedCharArray, JsonArray_Add) {
|
||||
unsigned char value[] = "world";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonArray& arr = jsonBuffer.createArray();
|
||||
arr.add(value);
|
||||
|
||||
EXPECT_STREQ("world", arr[0]);
|
||||
}
|
||||
|
||||
TEST(UnsignedCharArray, JsonArray_Set) {
|
||||
unsigned char value[] = "world";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonArray& arr = jsonBuffer.createArray();
|
||||
arr.add("hello");
|
||||
arr.set(0, value);
|
||||
|
||||
EXPECT_STREQ("world", arr[0]);
|
||||
}
|
||||
|
||||
TEST(UnsignedCharArray, JsonArraySubscript_Set) {
|
||||
unsigned char value[] = "world";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonArray& arr = jsonBuffer.createArray();
|
||||
arr.add("hello");
|
||||
arr[0].set(value);
|
||||
|
||||
EXPECT_STREQ("world", arr[0]);
|
||||
}
|
||||
|
||||
TEST(UnsignedCharArray, JsonArraySubscript_Assign) {
|
||||
unsigned char value[] = "world";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonArray& arr = jsonBuffer.createArray();
|
||||
arr.add("hello");
|
||||
arr[0] = value;
|
||||
|
||||
EXPECT_STREQ("world", arr[0]);
|
||||
}
|
||||
|
||||
TEST(UnsignedCharArray, JsonBuffer_strdup) {
|
||||
unsigned char value[] = "world";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
const char* dup = jsonBuffer.strdup(value);
|
||||
|
||||
EXPECT_NE(static_cast<const void*>(value), static_cast<const void*>(dup));
|
||||
EXPECT_STREQ("world", dup);
|
||||
}
|
357
test/Misc/vla.cpp
Normal file
357
test/Misc/vla.cpp
Normal file
@ -0,0 +1,357 @@
|
||||
// Copyright Benoit Blanchon 2014-2017
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://bblanchon.github.io/ArduinoJson/
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#if defined(__clang__)
|
||||
#pragma clang diagnostic ignored "-Wvla-extension"
|
||||
#define CONFLICTS_WITH_BUILTIN_OPERATOR
|
||||
#elif defined(__GNUC__)
|
||||
#pragma GCC diagnostic ignored "-Wvla"
|
||||
#else
|
||||
#define VLA_NOT_SUPPORTED
|
||||
#endif
|
||||
|
||||
#ifndef VLA_NOT_SUPPORTED
|
||||
|
||||
TEST(VariableLengthArray, ParseArray) {
|
||||
int i = 8;
|
||||
char vla[i];
|
||||
strcpy(vla, "[42]");
|
||||
|
||||
StaticJsonBuffer<JSON_ARRAY_SIZE(1)> jsonBuffer;
|
||||
JsonArray& arr = jsonBuffer.parseArray(vla);
|
||||
|
||||
EXPECT_TRUE(arr.success());
|
||||
}
|
||||
|
||||
TEST(VariableLengthArray, ParseObject) {
|
||||
int i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "{\"a\":42}");
|
||||
|
||||
StaticJsonBuffer<JSON_OBJECT_SIZE(1)> jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.parseObject(vla);
|
||||
|
||||
EXPECT_TRUE(obj.success());
|
||||
}
|
||||
|
||||
TEST(VariableLengthArray, Parse) {
|
||||
int i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "42");
|
||||
|
||||
StaticJsonBuffer<1> jsonBuffer;
|
||||
JsonVariant variant = jsonBuffer.parse(vla);
|
||||
|
||||
EXPECT_EQ(42, variant.as<int>());
|
||||
}
|
||||
|
||||
TEST(VariableLengthArray, JsonVariant_Constructor) {
|
||||
int i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "42");
|
||||
|
||||
JsonVariant variant(vla);
|
||||
|
||||
EXPECT_EQ(42, variant.as<int>());
|
||||
}
|
||||
|
||||
TEST(VariableLengthArray, JsonVariant_Assign) {
|
||||
int i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "42");
|
||||
|
||||
JsonVariant variant(666);
|
||||
variant = vla;
|
||||
|
||||
EXPECT_EQ(42, variant.as<int>());
|
||||
}
|
||||
|
||||
#ifndef CONFLICTS_WITH_BUILTIN_OPERATOR
|
||||
TEST(VariableLengthArray, JsonVariant_Subscript) {
|
||||
int i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "hello");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonVariant variant = jsonBuffer.parseObject("{\"hello\":\"world\"}");
|
||||
|
||||
EXPECT_STREQ("world", variant[vla]);
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef CONFLICTS_WITH_BUILTIN_OPERATOR
|
||||
TEST(VariableLengthArray, JsonVariant_Subscript_Const) {
|
||||
int i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "hello");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
const JsonVariant variant = jsonBuffer.parseObject("{\"hello\":\"world\"}");
|
||||
|
||||
EXPECT_STREQ("world", variant[vla]);
|
||||
}
|
||||
#endif
|
||||
|
||||
TEST(VariableLengthArray, JsonVariant_Equals) {
|
||||
int i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "hello");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
const JsonVariant variant = "hello";
|
||||
|
||||
EXPECT_TRUE(vla == variant);
|
||||
EXPECT_TRUE(variant == vla);
|
||||
EXPECT_FALSE(vla != variant);
|
||||
EXPECT_FALSE(variant != vla);
|
||||
}
|
||||
|
||||
TEST(VariableLengthArray, JsonVariant_Differs) {
|
||||
int i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "hello");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
const JsonVariant variant = "world";
|
||||
|
||||
EXPECT_TRUE(vla != variant);
|
||||
EXPECT_TRUE(variant != vla);
|
||||
EXPECT_FALSE(vla == variant);
|
||||
EXPECT_FALSE(variant == vla);
|
||||
}
|
||||
|
||||
#ifndef CONFLICTS_WITH_BUILTIN_OPERATOR
|
||||
TEST(VariableLengthArray, JsonObject_Subscript) {
|
||||
int i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "hello");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
obj[vla] = "world";
|
||||
|
||||
EXPECT_STREQ("world", obj["hello"]);
|
||||
}
|
||||
#endif
|
||||
|
||||
TEST(VariableLengthArray, JsonObject_Subscript_Assign) { // issue #416
|
||||
int i = 32;
|
||||
char vla[i];
|
||||
strcpy(vla, "world");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
obj["hello"] = vla;
|
||||
|
||||
EXPECT_STREQ("world", obj["hello"].as<char*>());
|
||||
}
|
||||
|
||||
TEST(VariableLengthArray, JsonObject_Subscript_Set) {
|
||||
int i = 32;
|
||||
char vla[i];
|
||||
strcpy(vla, "world");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
obj["hello"].set(vla);
|
||||
|
||||
EXPECT_STREQ("world", obj["hello"].as<char*>());
|
||||
}
|
||||
|
||||
#ifndef CONFLICTS_WITH_BUILTIN_OPERATOR
|
||||
TEST(VariableLengthArray, JsonObject_Subscript_Const) {
|
||||
int i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "hello");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
const JsonObject& obj = jsonBuffer.parseObject("{\"hello\":\"world\"}");
|
||||
|
||||
EXPECT_STREQ("world", obj[vla]);
|
||||
}
|
||||
#endif
|
||||
|
||||
TEST(VariableLengthArray, JsonObject_Get) {
|
||||
int i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "hello");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.parseObject("{\"hello\":\"world\"}");
|
||||
|
||||
EXPECT_STREQ("world", obj.get<char*>(vla));
|
||||
}
|
||||
|
||||
TEST(VariableLengthArray, JsonObject_Set_Key) {
|
||||
int i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "hello");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
obj.set(vla, "world");
|
||||
|
||||
EXPECT_STREQ("world", obj["hello"]);
|
||||
}
|
||||
|
||||
TEST(VariableLengthArray, JsonObject_Set_Value) {
|
||||
int i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "world");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
obj.set("hello", vla);
|
||||
|
||||
EXPECT_STREQ("world", obj["hello"]);
|
||||
}
|
||||
|
||||
TEST(VariableLengthArray, JsonObject_Set_Key_WithDecimals) {
|
||||
int i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "hello");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
obj.set(vla, 3.14, 2);
|
||||
|
||||
EXPECT_EQ(3.14, obj["hello"]);
|
||||
}
|
||||
|
||||
TEST(VariableLengthArray, JsonObject_Set_KeyAndValue) {
|
||||
int i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "world");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
obj.set(vla, vla);
|
||||
|
||||
EXPECT_STREQ("world", obj["world"]);
|
||||
}
|
||||
|
||||
TEST(VariableLengthArray, JsonObject_ContainsKey) {
|
||||
int i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "hello");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
const JsonObject& obj = jsonBuffer.parseObject("{\"hello\":\"world\"}");
|
||||
|
||||
EXPECT_TRUE(obj.containsKey(vla));
|
||||
}
|
||||
|
||||
TEST(VariableLengthArray, JsonObject_Remove) {
|
||||
int i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "hello");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.parseObject("{\"hello\":\"world\"}");
|
||||
obj.remove(vla);
|
||||
|
||||
EXPECT_EQ(0, obj.size());
|
||||
}
|
||||
|
||||
TEST(VariableLengthArray, JsonObject_Is) {
|
||||
int i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "hello");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.parseObject("{\"hello\":42}");
|
||||
|
||||
EXPECT_TRUE(obj.is<int>(vla));
|
||||
}
|
||||
|
||||
TEST(VariableLengthArray, JsonObject_CreateNestedArray) {
|
||||
int i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "hello");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
obj.createNestedArray(vla);
|
||||
}
|
||||
|
||||
TEST(VariableLengthArray, JsonObject_CreateNestedObject) {
|
||||
int i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "hello");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
obj.createNestedObject(vla);
|
||||
}
|
||||
|
||||
TEST(VariableLengthArray, JsonArray_Add) {
|
||||
int i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "world");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonArray& arr = jsonBuffer.createArray();
|
||||
arr.add(vla);
|
||||
|
||||
EXPECT_STREQ("world", arr[0]);
|
||||
}
|
||||
|
||||
TEST(VariableLengthArray, JsonArray_Set) {
|
||||
int i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "world");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonArray& arr = jsonBuffer.createArray();
|
||||
arr.add("hello");
|
||||
arr.set(0, vla);
|
||||
|
||||
EXPECT_STREQ("world", arr[0]);
|
||||
}
|
||||
|
||||
TEST(VariableLengthArray, JsonArraySubscript_Set) {
|
||||
int i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "world");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonArray& arr = jsonBuffer.createArray();
|
||||
arr.add("hello");
|
||||
arr[0].set(vla);
|
||||
|
||||
EXPECT_STREQ("world", arr[0]);
|
||||
}
|
||||
|
||||
TEST(VariableLengthArray, JsonArraySubscript_Assign) {
|
||||
int i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "world");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonArray& arr = jsonBuffer.createArray();
|
||||
arr.add("hello");
|
||||
arr[0] = vla;
|
||||
|
||||
EXPECT_STREQ("world", arr[0]);
|
||||
}
|
||||
|
||||
TEST(VariableLengthArray, JsonBuffer_strdup) {
|
||||
int i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "world");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
const char* dup = jsonBuffer.strdup(vla);
|
||||
|
||||
EXPECT_NE(static_cast<const void*>(vla), static_cast<const void*>(dup));
|
||||
EXPECT_STREQ("world", dup);
|
||||
}
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user