mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-19 05:22:24 +02:00
Changed unit testing framework from Google Test to Catch
This commit is contained in:
@ -15,5 +15,5 @@ add_executable(MiscTests
|
||||
vla.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(MiscTests gtest)
|
||||
target_link_libraries(MiscTests catch)
|
||||
add_test(Misc MiscTests)
|
||||
|
@ -6,70 +6,37 @@
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <gtest/gtest.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
using namespace ArduinoJson::Internals;
|
||||
|
||||
class StringBuilderTests : public testing::Test {
|
||||
protected:
|
||||
virtual void SetUp() {
|
||||
_stringBuilder = new StaticStringBuilder(_buffer, sizeof(_buffer));
|
||||
TEST_CASE("StringBuilder") {
|
||||
char output[20];
|
||||
StaticStringBuilder sb(output, sizeof(output));
|
||||
|
||||
SECTION("InitialState") {
|
||||
REQUIRE(std::string("") == output);
|
||||
}
|
||||
|
||||
virtual void TearDown() {
|
||||
delete _stringBuilder;
|
||||
SECTION("OverCapacity") {
|
||||
REQUIRE(19 == sb.print("ABCDEFGHIJKLMNOPQRSTUVWXYZ"));
|
||||
REQUIRE(0 == sb.print("ABC"));
|
||||
REQUIRE(std::string("ABCDEFGHIJKLMNOPQRS") == output);
|
||||
}
|
||||
|
||||
void print(const char *value) {
|
||||
_returnValue = _stringBuilder->print(value);
|
||||
SECTION("EmptyString") {
|
||||
REQUIRE(0 == sb.print(""));
|
||||
REQUIRE(std::string("") == output);
|
||||
}
|
||||
|
||||
void outputMustBe(const char *expected) {
|
||||
EXPECT_STREQ(expected, _buffer);
|
||||
SECTION("OneString") {
|
||||
REQUIRE(4 == sb.print("ABCD"));
|
||||
REQUIRE(std::string("ABCD") == output);
|
||||
}
|
||||
|
||||
void resultMustBe(size_t expected) {
|
||||
EXPECT_EQ(expected, _returnValue);
|
||||
SECTION("TwoStrings") {
|
||||
REQUIRE(4 == sb.print("ABCD"));
|
||||
REQUIRE(4 == sb.print("EFGH"));
|
||||
REQUIRE(std::string("ABCDEFGH") == output);
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
|
@ -6,31 +6,37 @@
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <gtest/gtest.h>
|
||||
#include <catch.hpp>
|
||||
#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_CASE("TypeTraits") {
|
||||
SECTION("IsBaseOf") {
|
||||
REQUIRE_FALSE(
|
||||
static_cast<bool>(IsBaseOf<std::istream, std::ostringstream>::value));
|
||||
REQUIRE(
|
||||
static_cast<bool>(IsBaseOf<std::istream, std::istringstream>::value));
|
||||
REQUIRE(static_cast<bool>(
|
||||
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));
|
||||
}
|
||||
SECTION("IsArray") {
|
||||
REQUIRE_FALSE((IsArray<const char*>::value));
|
||||
REQUIRE((IsArray<const char[]>::value));
|
||||
REQUIRE((IsArray<const char[10]>::value));
|
||||
}
|
||||
|
||||
TEST(TypeTraits, IsVariant) {
|
||||
ASSERT_TRUE((IsVariant<JsonObjectSubscript<const char*> >::value));
|
||||
ASSERT_TRUE((IsVariant<JsonVariant>::value));
|
||||
}
|
||||
SECTION("IsVariant") {
|
||||
REQUIRE(
|
||||
static_cast<bool>(IsVariant<JsonObjectSubscript<const char*> >::value));
|
||||
REQUIRE(static_cast<bool>(IsVariant<JsonVariant>::value));
|
||||
}
|
||||
|
||||
TEST(TypeTraits, IsString) {
|
||||
ASSERT_TRUE((IsString<const char*>::value));
|
||||
ASSERT_TRUE((IsString<std::string>::value));
|
||||
ASSERT_FALSE((IsString<double>::value));
|
||||
SECTION("IsString") {
|
||||
REQUIRE((IsString<const char*>::value));
|
||||
REQUIRE((IsString<std::string>::value));
|
||||
REQUIRE_FALSE((IsString<double>::value));
|
||||
}
|
||||
}
|
||||
|
@ -8,7 +8,7 @@
|
||||
#define ARDUINOJSON_ENABLE_DEPRECATED 1
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <gtest/gtest.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
#if defined(__clang__)
|
||||
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
|
||||
@ -18,25 +18,27 @@
|
||||
#pragma warning(disable : 4996)
|
||||
#endif
|
||||
|
||||
TEST(Deprecated, asArray) {
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonVariant variant = jsonBuffer.createArray();
|
||||
ASSERT_TRUE(variant.asArray().success());
|
||||
}
|
||||
TEST_CASE("Deprecated functions") {
|
||||
SECTION("JsonVariant::asArray()") {
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonVariant variant = jsonBuffer.createArray();
|
||||
REQUIRE(variant.asArray().success());
|
||||
}
|
||||
|
||||
TEST(Deprecated, asObject) {
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonVariant variant = jsonBuffer.createObject();
|
||||
ASSERT_TRUE(variant.asObject().success());
|
||||
}
|
||||
SECTION("JsonVariant::asObject()") {
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonVariant variant = jsonBuffer.createObject();
|
||||
REQUIRE(variant.asObject().success());
|
||||
}
|
||||
|
||||
TEST(Deprecated, asString) {
|
||||
JsonVariant variant = "hello";
|
||||
ASSERT_STREQ("hello", variant.asString());
|
||||
}
|
||||
SECTION("JsonVariant::asString()") {
|
||||
JsonVariant variant = "hello";
|
||||
REQUIRE(std::string("hello") == variant.asString());
|
||||
}
|
||||
|
||||
TEST(Deprecated, removeAt) {
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonArray& arr = jsonBuffer.createArray();
|
||||
arr.removeAt(0);
|
||||
SECTION("JsonArray::removeAt()") {
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonArray& arr = jsonBuffer.createArray();
|
||||
arr.removeAt(0);
|
||||
}
|
||||
}
|
||||
|
@ -6,80 +6,82 @@
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <gtest/gtest.h>
|
||||
#include <catch.hpp>
|
||||
#include <sstream>
|
||||
|
||||
TEST(StdStream_Tests, JsonVariantFalse) {
|
||||
std::ostringstream os;
|
||||
JsonVariant variant = false;
|
||||
os << variant;
|
||||
ASSERT_EQ("false", os.str());
|
||||
}
|
||||
TEST_CASE("std::stream") {
|
||||
SECTION("JsonVariantFalse") {
|
||||
std::ostringstream os;
|
||||
JsonVariant variant = false;
|
||||
os << variant;
|
||||
REQUIRE("false" == os.str());
|
||||
}
|
||||
|
||||
TEST(StdStream_Tests, JsonVariantString) {
|
||||
std::ostringstream os;
|
||||
JsonVariant variant = "coucou";
|
||||
os << variant;
|
||||
ASSERT_EQ("\"coucou\"", os.str());
|
||||
}
|
||||
SECTION("JsonVariantString") {
|
||||
std::ostringstream os;
|
||||
JsonVariant variant = "coucou";
|
||||
os << variant;
|
||||
REQUIRE("\"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());
|
||||
}
|
||||
SECTION("JsonObject") {
|
||||
std::ostringstream os;
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& object = jsonBuffer.createObject();
|
||||
object["key"] = "value";
|
||||
os << object;
|
||||
REQUIRE("{\"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());
|
||||
}
|
||||
SECTION("JsonObjectSubscript") {
|
||||
std::ostringstream os;
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& object = jsonBuffer.createObject();
|
||||
object["key"] = "value";
|
||||
os << object["key"];
|
||||
REQUIRE("\"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());
|
||||
}
|
||||
SECTION("JsonArray") {
|
||||
std::ostringstream os;
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonArray& array = jsonBuffer.createArray();
|
||||
array.add("value");
|
||||
os << array;
|
||||
REQUIRE("[\"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());
|
||||
}
|
||||
SECTION("JsonArraySubscript") {
|
||||
std::ostringstream os;
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonArray& array = jsonBuffer.createArray();
|
||||
array.add("value");
|
||||
os << array[0];
|
||||
REQUIRE("\"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]);
|
||||
}
|
||||
SECTION("ParseArray") {
|
||||
std::istringstream json(" [ 42 /* comment */ ] ");
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonArray& arr = jsonBuffer.parseArray(json);
|
||||
REQUIRE(true == arr.success());
|
||||
REQUIRE(1 == arr.size());
|
||||
REQUIRE(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"]);
|
||||
}
|
||||
SECTION("ParseObject") {
|
||||
std::istringstream json(" { hello : world // comment\n }");
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.parseObject(json);
|
||||
REQUIRE(true == obj.success());
|
||||
REQUIRE(1 == obj.size());
|
||||
REQUIRE(std::string("world") == obj["hello"]);
|
||||
}
|
||||
|
||||
TEST(StdStream_Tests, ShouldNotReadPastTheEnd) {
|
||||
std::istringstream json("{}123");
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
jsonBuffer.parseObject(json);
|
||||
ASSERT_EQ('1', json.get());
|
||||
SECTION("ShouldNotReadPastTheEnd") {
|
||||
std::istringstream json("{}123");
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
jsonBuffer.parseObject(json);
|
||||
REQUIRE('1' == json.get());
|
||||
}
|
||||
}
|
||||
|
@ -6,247 +6,248 @@
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <gtest/gtest.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
class StringTests : public ::testing::Test {
|
||||
protected:
|
||||
static void eraseString(std::string &str) {
|
||||
char *p = const_cast<char *>(str.c_str());
|
||||
while (*p) *p++ = '*';
|
||||
static void eraseString(std::string &str) {
|
||||
char *p = const_cast<char *>(str.c_str());
|
||||
while (*p) *p++ = '*';
|
||||
}
|
||||
|
||||
TEST_CASE("std::string") {
|
||||
DynamicJsonBuffer jb;
|
||||
|
||||
SECTION("JsonBuffer_ParseArray") {
|
||||
std::string json("[\"hello\"]");
|
||||
JsonArray &array = jb.parseArray(json);
|
||||
eraseString(json);
|
||||
REQUIRE(true == array.success());
|
||||
REQUIRE(std::string("hello") == array[0]);
|
||||
}
|
||||
|
||||
DynamicJsonBuffer _jsonBuffer;
|
||||
};
|
||||
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"]);
|
||||
}
|
||||
|
||||
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);
|
||||
SECTION("JsonObject_Subscript") {
|
||||
char json[] = "{\"key\":\"value\"}";
|
||||
JsonObject &object = jb.parseObject(json);
|
||||
REQUIRE(std::string("value") == object[std::string("key")]);
|
||||
}
|
||||
|
||||
SECTION("JsonObject_ConstSubscript") {
|
||||
char json[] = "{\"key\":\"value\"}";
|
||||
const JsonObject &object = jb.parseObject(json);
|
||||
REQUIRE(std::string("value") == object[std::string("key")]);
|
||||
}
|
||||
|
||||
SECTION("JsonObject_SetKey") {
|
||||
JsonObject &object = jb.createObject();
|
||||
std::string key("hello");
|
||||
object.set(key, "world");
|
||||
eraseString(key);
|
||||
REQUIRE(std::string("world") == object["hello"]);
|
||||
}
|
||||
|
||||
SECTION("JsonObject_SetValue") {
|
||||
JsonObject &object = jb.createObject();
|
||||
std::string value("world");
|
||||
object.set("hello", value);
|
||||
eraseString(value);
|
||||
REQUIRE(std::string("world") == object["hello"]);
|
||||
}
|
||||
|
||||
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"]);
|
||||
}
|
||||
|
||||
SECTION("JsonObject_SetToArraySubscript") {
|
||||
JsonArray &arr = jb.createArray();
|
||||
arr.add("world");
|
||||
|
||||
JsonObject &object = jb.createObject();
|
||||
object.set(std::string("hello"), arr[0]);
|
||||
|
||||
REQUIRE(std::string("world") == object["hello"]);
|
||||
}
|
||||
|
||||
SECTION("JsonObject_SetToObjectSubscript") {
|
||||
JsonObject &arr = jb.createObject();
|
||||
arr.set("x", "world");
|
||||
|
||||
JsonObject &object = jb.createObject();
|
||||
object.set(std::string("hello"), arr["x"]);
|
||||
|
||||
REQUIRE(std::string("world") == object["hello"]);
|
||||
}
|
||||
|
||||
SECTION("JsonObject_Get") {
|
||||
char json[] = "{\"key\":\"value\"}";
|
||||
const JsonObject &object = jb.parseObject(json);
|
||||
REQUIRE(std::string("value") ==
|
||||
object.get<const char *>(std::string("key")));
|
||||
}
|
||||
|
||||
SECTION("JsonObject_GetT") {
|
||||
char json[] = "{\"key\":\"value\"}";
|
||||
const JsonObject &object = jb.parseObject(json);
|
||||
REQUIRE(std::string("value") ==
|
||||
object.get<const char *>(std::string("key")));
|
||||
}
|
||||
|
||||
SECTION("JsonObject_IsT") {
|
||||
char json[] = "{\"key\":\"value\"}";
|
||||
const JsonObject &object = jb.parseObject(json);
|
||||
REQUIRE(true == object.is<const char *>(std::string("key")));
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
SECTION("JsonObject_ContainsKey") {
|
||||
char json[] = "{\"key\":\"value\"}";
|
||||
const JsonObject &object = jb.parseObject(json);
|
||||
REQUIRE(true == object.containsKey(std::string("key")));
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
SECTION("JsonObjectSubscript_SetKey") {
|
||||
JsonObject &object = jb.createObject();
|
||||
std::string key("hello");
|
||||
object[key] = "world";
|
||||
eraseString(key);
|
||||
REQUIRE(std::string("world") == object["hello"]);
|
||||
}
|
||||
|
||||
SECTION("JsonObjectSubscript_SetValue") {
|
||||
JsonObject &object = jb.createObject();
|
||||
std::string value("world");
|
||||
object["hello"] = value;
|
||||
eraseString(value);
|
||||
REQUIRE(std::string("world") == object["hello"]);
|
||||
}
|
||||
|
||||
SECTION("JsonArray_Add") {
|
||||
JsonArray &array = jb.createArray();
|
||||
std::string value("hello");
|
||||
array.add(value);
|
||||
eraseString(value);
|
||||
REQUIRE(std::string("hello") == array[0]);
|
||||
}
|
||||
|
||||
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]);
|
||||
}
|
||||
|
||||
SECTION("JsonArraySubscript") {
|
||||
JsonArray &array = jb.createArray();
|
||||
std::string value("world");
|
||||
array.add("hello");
|
||||
array[0] = value;
|
||||
eraseString(value);
|
||||
REQUIRE(std::string("world") == array[0]);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
SECTION("JsonObject_PrintTo") {
|
||||
JsonObject &object = jb.createObject();
|
||||
object["key"] = "value";
|
||||
std::string json;
|
||||
object.printTo(json);
|
||||
REQUIRE(std::string("{\"key\":\"value\"}") == json);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
SECTION("JsonBuffer_GrowWhenAddingNewKey") {
|
||||
JsonObject &object = jb.createObject();
|
||||
std::string key1("hello"), key2("world");
|
||||
|
||||
object[key1] = 1;
|
||||
size_t sizeBefore = jb.size();
|
||||
object[key2] = 2;
|
||||
size_t sizeAfter = jb.size();
|
||||
|
||||
REQUIRE(sizeAfter - sizeBefore >= key2.size());
|
||||
}
|
||||
|
||||
SECTION("JsonBuffer_DontGrowWhenReusingKey") {
|
||||
JsonObject &object = jb.createObject();
|
||||
std::string key("hello");
|
||||
|
||||
object[key] = 1;
|
||||
size_t sizeBefore = jb.size();
|
||||
object[key] = 2;
|
||||
size_t sizeAfter = jb.size();
|
||||
|
||||
REQUIRE(sizeBefore == sizeAfter);
|
||||
}
|
||||
|
||||
SECTION("JsonBuffer_strdup") {
|
||||
std::string original("hello");
|
||||
char *copy = jb.strdup(original);
|
||||
original[0] = 'w';
|
||||
REQUIRE(std::string("hello") == copy);
|
||||
}
|
||||
}
|
||||
|
@ -6,278 +6,280 @@
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <gtest/gtest.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
#if defined(__clang__)
|
||||
#define CONFLICTS_WITH_BUILTIN_OPERATOR
|
||||
#endif
|
||||
|
||||
TEST(UnsignedCharArray, ParseArray) {
|
||||
unsigned char json[] = "[42]";
|
||||
TEST_CASE("unsigned char string") {
|
||||
SECTION("JsonBuffer::parseArray") {
|
||||
unsigned char json[] = "[42]";
|
||||
|
||||
StaticJsonBuffer<JSON_ARRAY_SIZE(1)> jsonBuffer;
|
||||
JsonArray& arr = jsonBuffer.parseArray(json);
|
||||
StaticJsonBuffer<JSON_ARRAY_SIZE(1)> jsonBuffer;
|
||||
JsonArray& arr = jsonBuffer.parseArray(json);
|
||||
|
||||
EXPECT_TRUE(arr.success());
|
||||
}
|
||||
REQUIRE(true == arr.success());
|
||||
}
|
||||
|
||||
TEST(UnsignedCharArray, ParseObject) {
|
||||
unsigned char json[] = "{\"a\":42}";
|
||||
SECTION("JsonBuffer::parseObject") {
|
||||
unsigned char json[] = "{\"a\":42}";
|
||||
|
||||
StaticJsonBuffer<JSON_OBJECT_SIZE(1)> jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.parseObject(json);
|
||||
StaticJsonBuffer<JSON_OBJECT_SIZE(1)> jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.parseObject(json);
|
||||
|
||||
EXPECT_TRUE(obj.success());
|
||||
}
|
||||
REQUIRE(true == obj.success());
|
||||
}
|
||||
|
||||
TEST(UnsignedCharArray, JsonVariant_Constructor) {
|
||||
unsigned char value[] = "42";
|
||||
SECTION("JsonVariant constructor") {
|
||||
unsigned char value[] = "42";
|
||||
|
||||
JsonVariant variant(value);
|
||||
JsonVariant variant(value);
|
||||
|
||||
EXPECT_EQ(42, variant.as<int>());
|
||||
}
|
||||
REQUIRE(42 == variant.as<int>());
|
||||
}
|
||||
|
||||
TEST(UnsignedCharArray, JsonVariant_Assign) {
|
||||
unsigned char value[] = "42";
|
||||
SECTION("JsonVariant assignment operator") {
|
||||
unsigned char value[] = "42";
|
||||
|
||||
JsonVariant variant(666);
|
||||
variant = value;
|
||||
JsonVariant variant(666);
|
||||
variant = value;
|
||||
|
||||
EXPECT_EQ(42, variant.as<int>());
|
||||
}
|
||||
REQUIRE(42 == variant.as<int>());
|
||||
}
|
||||
|
||||
#ifndef CONFLICTS_WITH_BUILTIN_OPERATOR
|
||||
TEST(UnsignedCharArray, JsonVariant_Subscript) {
|
||||
unsigned char key[] = "hello";
|
||||
SECTION("JsonVariant::operator[]") {
|
||||
unsigned char key[] = "hello";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonVariant variant = jsonBuffer.parseObject("{\"hello\":\"world\"}");
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonVariant variant = jsonBuffer.parseObject("{\"hello\":\"world\"}");
|
||||
|
||||
EXPECT_STREQ("world", variant[key]);
|
||||
}
|
||||
REQUIRE(std::string("world") == variant[key]);
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef CONFLICTS_WITH_BUILTIN_OPERATOR
|
||||
TEST(UnsignedCharArray, JsonVariant_Subscript_Const) {
|
||||
unsigned char key[] = "hello";
|
||||
SECTION("JsonVariant::operator[] const") {
|
||||
unsigned char key[] = "hello";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
const JsonVariant variant = jsonBuffer.parseObject("{\"hello\":\"world\"}");
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
const JsonVariant variant = jsonBuffer.parseObject("{\"hello\":\"world\"}");
|
||||
|
||||
EXPECT_STREQ("world", variant[key]);
|
||||
}
|
||||
REQUIRE(std::string("world") == variant[key]);
|
||||
}
|
||||
#endif
|
||||
|
||||
TEST(UnsignedCharArray, JsonVariant_Equals) {
|
||||
unsigned char comparand[] = "hello";
|
||||
SECTION("JsonVariant::operator==") {
|
||||
unsigned char comparand[] = "hello";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
const JsonVariant variant = "hello";
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
const JsonVariant variant = "hello";
|
||||
|
||||
EXPECT_TRUE(comparand == variant);
|
||||
EXPECT_TRUE(variant == comparand);
|
||||
EXPECT_FALSE(comparand != variant);
|
||||
EXPECT_FALSE(variant != comparand);
|
||||
}
|
||||
REQUIRE(comparand == variant);
|
||||
REQUIRE(variant == comparand);
|
||||
REQUIRE_FALSE(comparand != variant);
|
||||
REQUIRE_FALSE(variant != comparand);
|
||||
}
|
||||
|
||||
TEST(UnsignedCharArray, JsonVariant_Differs) {
|
||||
unsigned char comparand[] = "hello";
|
||||
SECTION("JsonVariant::operator!=") {
|
||||
unsigned char comparand[] = "hello";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
const JsonVariant variant = "world";
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
const JsonVariant variant = "world";
|
||||
|
||||
EXPECT_TRUE(comparand != variant);
|
||||
EXPECT_TRUE(variant != comparand);
|
||||
EXPECT_FALSE(comparand == variant);
|
||||
EXPECT_FALSE(variant == comparand);
|
||||
}
|
||||
REQUIRE(comparand != variant);
|
||||
REQUIRE(variant != comparand);
|
||||
REQUIRE_FALSE(comparand == variant);
|
||||
REQUIRE_FALSE(variant == comparand);
|
||||
}
|
||||
|
||||
#ifndef CONFLICTS_WITH_BUILTIN_OPERATOR
|
||||
TEST(UnsignedCharArray, JsonObject_Subscript) {
|
||||
unsigned char key[] = "hello";
|
||||
SECTION("JsonObject::operator[]") {
|
||||
unsigned char key[] = "hello";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
obj[key] = "world";
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
obj[key] = "world";
|
||||
|
||||
EXPECT_STREQ("world", obj["hello"]);
|
||||
}
|
||||
REQUIRE(std::string("world") == obj["hello"]);
|
||||
}
|
||||
#endif
|
||||
|
||||
TEST(UnsignedCharArray, JsonObject_Subscript_Assign) { // issue #416
|
||||
unsigned char value[] = "world";
|
||||
SECTION("JsonObjectSubscript::operator=") { // issue #416
|
||||
unsigned char value[] = "world";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
obj["hello"] = value;
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
obj["hello"] = value;
|
||||
|
||||
EXPECT_STREQ("world", obj["hello"]);
|
||||
}
|
||||
REQUIRE(std::string("world") == obj["hello"]);
|
||||
}
|
||||
|
||||
TEST(UnsignedCharArray, JsonObject_Subscript_Set) {
|
||||
unsigned char value[] = "world";
|
||||
SECTION("JsonObjectSubscript::set()") {
|
||||
unsigned char value[] = "world";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
obj["hello"].set(value);
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
obj["hello"].set(value);
|
||||
|
||||
EXPECT_STREQ("world", obj["hello"]);
|
||||
}
|
||||
REQUIRE(std::string("world") == obj["hello"]);
|
||||
}
|
||||
|
||||
#ifndef CONFLICTS_WITH_BUILTIN_OPERATOR
|
||||
TEST(UnsignedCharArray, JsonObject_Subscript_Const) {
|
||||
unsigned char key[] = "hello";
|
||||
SECTION("JsonObject::operator[] const") {
|
||||
unsigned char key[] = "hello";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
const JsonObject& obj = jsonBuffer.parseObject("{\"hello\":\"world\"}");
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
const JsonObject& obj = jsonBuffer.parseObject("{\"hello\":\"world\"}");
|
||||
|
||||
EXPECT_STREQ("world", obj[key]);
|
||||
}
|
||||
REQUIRE(std::string("world") == obj[key]);
|
||||
}
|
||||
#endif
|
||||
|
||||
TEST(UnsignedCharArray, JsonObject_Get) {
|
||||
unsigned char key[] = "hello";
|
||||
SECTION("JsonObject::get()") {
|
||||
unsigned char key[] = "hello";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.parseObject("{\"hello\":\"world\"}");
|
||||
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);
|
||||
REQUIRE(std::string("world") == obj.get<char*>(key));
|
||||
}
|
||||
|
||||
SECTION("JsonObject::set() key") {
|
||||
unsigned char key[] = "hello";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
obj.set(key, "world");
|
||||
|
||||
REQUIRE(std::string("world") == obj["hello"]);
|
||||
}
|
||||
|
||||
SECTION("JsonObject::set() value") {
|
||||
unsigned char value[] = "world";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
obj.set("hello", value);
|
||||
|
||||
REQUIRE(std::string("world") == obj["hello"]);
|
||||
}
|
||||
|
||||
SECTION("JsonObject::set() key with decimals") {
|
||||
unsigned char key[] = "hello";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
obj.set(key, 3.14, 2);
|
||||
|
||||
REQUIRE(3.14 == obj["hello"]);
|
||||
}
|
||||
|
||||
SECTION("JsonObject::set key&value") {
|
||||
unsigned char key[] = "world";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
obj.set(key, key);
|
||||
|
||||
REQUIRE(std::string("world") == obj["world"]);
|
||||
}
|
||||
|
||||
SECTION("JsonObject::containsKey()") {
|
||||
unsigned char key[] = "hello";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
const JsonObject& obj = jsonBuffer.parseObject("{\"hello\":\"world\"}");
|
||||
|
||||
REQUIRE(true == obj.containsKey(key));
|
||||
}
|
||||
|
||||
SECTION("JsonObject::remove()") {
|
||||
unsigned char key[] = "hello";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.parseObject("{\"hello\":\"world\"}");
|
||||
obj.remove(key);
|
||||
|
||||
REQUIRE(0 == obj.size());
|
||||
}
|
||||
|
||||
SECTION("JsonObject::is()") {
|
||||
unsigned char key[] = "hello";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.parseObject("{\"hello\":42}");
|
||||
|
||||
REQUIRE(true == obj.is<int>(key));
|
||||
}
|
||||
|
||||
SECTION("JsonObject::createNestedArray()") {
|
||||
unsigned char key[] = "hello";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
obj.createNestedArray(key);
|
||||
}
|
||||
|
||||
SECTION("JsonObject::createNestedObject()") {
|
||||
unsigned char key[] = "hello";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
obj.createNestedObject(key);
|
||||
}
|
||||
|
||||
SECTION("JsonArray::add()") {
|
||||
unsigned char value[] = "world";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonArray& arr = jsonBuffer.createArray();
|
||||
arr.add(value);
|
||||
|
||||
REQUIRE(std::string("world") == arr[0]);
|
||||
}
|
||||
|
||||
SECTION("JsonArray::set()") {
|
||||
unsigned char value[] = "world";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonArray& arr = jsonBuffer.createArray();
|
||||
arr.add("hello");
|
||||
arr.set(0, value);
|
||||
|
||||
REQUIRE(std::string("world") == arr[0]);
|
||||
}
|
||||
|
||||
SECTION("JsonArraySubscript::set()") {
|
||||
unsigned char value[] = "world";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonArray& arr = jsonBuffer.createArray();
|
||||
arr.add("hello");
|
||||
arr[0].set(value);
|
||||
|
||||
REQUIRE(std::string("world") == arr[0]);
|
||||
}
|
||||
|
||||
SECTION("JsonArraySubscript::operator=") {
|
||||
unsigned char value[] = "world";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonArray& arr = jsonBuffer.createArray();
|
||||
arr.add("hello");
|
||||
arr[0] = value;
|
||||
|
||||
REQUIRE(std::string("world") == arr[0]);
|
||||
}
|
||||
|
||||
SECTION("JsonBuffer::strdup()") {
|
||||
unsigned char value[] = "world";
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
const char* dup = jsonBuffer.strdup(value);
|
||||
|
||||
REQUIRE(static_cast<const void*>(value) != static_cast<const void*>(dup));
|
||||
REQUIRE(std::string("world") == dup);
|
||||
}
|
||||
}
|
||||
|
@ -6,7 +6,7 @@
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <gtest/gtest.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
#if defined(__clang__)
|
||||
#pragma clang diagnostic ignored "-Wvla-extension"
|
||||
@ -19,339 +19,340 @@
|
||||
|
||||
#ifndef VLA_NOT_SUPPORTED
|
||||
|
||||
TEST(VariableLengthArray, ParseArray) {
|
||||
int i = 8;
|
||||
char vla[i];
|
||||
strcpy(vla, "[42]");
|
||||
TEST_CASE("Variable Length Array") {
|
||||
SECTION("ParseArray") {
|
||||
int i = 8;
|
||||
char vla[i];
|
||||
strcpy(vla, "[42]");
|
||||
|
||||
StaticJsonBuffer<JSON_ARRAY_SIZE(1)> jsonBuffer;
|
||||
JsonArray& arr = jsonBuffer.parseArray(vla);
|
||||
StaticJsonBuffer<JSON_ARRAY_SIZE(1)> jsonBuffer;
|
||||
JsonArray& arr = jsonBuffer.parseArray(vla);
|
||||
|
||||
EXPECT_TRUE(arr.success());
|
||||
}
|
||||
REQUIRE(true == arr.success());
|
||||
}
|
||||
|
||||
TEST(VariableLengthArray, ParseObject) {
|
||||
int i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "{\"a\":42}");
|
||||
SECTION("ParseObject") {
|
||||
int i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "{\"a\":42}");
|
||||
|
||||
StaticJsonBuffer<JSON_OBJECT_SIZE(1)> jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.parseObject(vla);
|
||||
StaticJsonBuffer<JSON_OBJECT_SIZE(1)> jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.parseObject(vla);
|
||||
|
||||
EXPECT_TRUE(obj.success());
|
||||
}
|
||||
REQUIRE(true == obj.success());
|
||||
}
|
||||
|
||||
TEST(VariableLengthArray, Parse) {
|
||||
int i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "42");
|
||||
SECTION("Parse") {
|
||||
int i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "42");
|
||||
|
||||
StaticJsonBuffer<1> jsonBuffer;
|
||||
JsonVariant variant = jsonBuffer.parse(vla);
|
||||
StaticJsonBuffer<1> jsonBuffer;
|
||||
JsonVariant variant = jsonBuffer.parse(vla);
|
||||
|
||||
EXPECT_EQ(42, variant.as<int>());
|
||||
}
|
||||
REQUIRE(42 == variant.as<int>());
|
||||
}
|
||||
|
||||
TEST(VariableLengthArray, JsonVariant_Constructor) {
|
||||
int i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "42");
|
||||
SECTION("JsonVariant_Constructor") {
|
||||
int i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "42");
|
||||
|
||||
JsonVariant variant(vla);
|
||||
JsonVariant variant(vla);
|
||||
|
||||
EXPECT_EQ(42, variant.as<int>());
|
||||
}
|
||||
REQUIRE(42 == variant.as<int>());
|
||||
}
|
||||
|
||||
TEST(VariableLengthArray, JsonVariant_Assign) {
|
||||
int i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "42");
|
||||
SECTION("JsonVariant_Assign") {
|
||||
int i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "42");
|
||||
|
||||
JsonVariant variant(666);
|
||||
variant = vla;
|
||||
JsonVariant variant(666);
|
||||
variant = vla;
|
||||
|
||||
EXPECT_EQ(42, variant.as<int>());
|
||||
}
|
||||
REQUIRE(42 == variant.as<int>());
|
||||
}
|
||||
|
||||
#ifndef CONFLICTS_WITH_BUILTIN_OPERATOR
|
||||
TEST(VariableLengthArray, JsonVariant_Subscript) {
|
||||
int i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "hello");
|
||||
SECTION("JsonVariant_Subscript") {
|
||||
int i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "hello");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonVariant variant = jsonBuffer.parseObject("{\"hello\":\"world\"}");
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonVariant variant = jsonBuffer.parseObject("{\"hello\":\"world\"}");
|
||||
|
||||
EXPECT_STREQ("world", variant[vla]);
|
||||
}
|
||||
REQUIRE(std::string("world") == variant[vla]);
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef CONFLICTS_WITH_BUILTIN_OPERATOR
|
||||
TEST(VariableLengthArray, JsonVariant_Subscript_Const) {
|
||||
int i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "hello");
|
||||
SECTION("JsonVariant_Subscript_Const") {
|
||||
int i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "hello");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
const JsonVariant variant = jsonBuffer.parseObject("{\"hello\":\"world\"}");
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
const JsonVariant variant = jsonBuffer.parseObject("{\"hello\":\"world\"}");
|
||||
|
||||
EXPECT_STREQ("world", variant[vla]);
|
||||
}
|
||||
REQUIRE(std::string("world") == variant[vla]);
|
||||
}
|
||||
#endif
|
||||
|
||||
TEST(VariableLengthArray, JsonVariant_Equals) {
|
||||
int i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "hello");
|
||||
SECTION("JsonVariant_Equals") {
|
||||
int i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "hello");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
const JsonVariant variant = "hello";
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
const JsonVariant variant = "hello";
|
||||
|
||||
EXPECT_TRUE(vla == variant);
|
||||
EXPECT_TRUE(variant == vla);
|
||||
EXPECT_FALSE(vla != variant);
|
||||
EXPECT_FALSE(variant != vla);
|
||||
}
|
||||
REQUIRE((vla == variant));
|
||||
REQUIRE((variant == vla));
|
||||
REQUIRE_FALSE((vla != variant));
|
||||
REQUIRE_FALSE((variant != vla));
|
||||
}
|
||||
|
||||
TEST(VariableLengthArray, JsonVariant_Differs) {
|
||||
int i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "hello");
|
||||
SECTION("JsonVariant_Differs") {
|
||||
int i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "hello");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
const JsonVariant variant = "world";
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
const JsonVariant variant = "world";
|
||||
|
||||
EXPECT_TRUE(vla != variant);
|
||||
EXPECT_TRUE(variant != vla);
|
||||
EXPECT_FALSE(vla == variant);
|
||||
EXPECT_FALSE(variant == vla);
|
||||
}
|
||||
REQUIRE((vla != variant));
|
||||
REQUIRE((variant != vla));
|
||||
REQUIRE_FALSE((vla == variant));
|
||||
REQUIRE_FALSE((variant == vla));
|
||||
}
|
||||
|
||||
#ifndef CONFLICTS_WITH_BUILTIN_OPERATOR
|
||||
TEST(VariableLengthArray, JsonObject_Subscript) {
|
||||
int i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "hello");
|
||||
SECTION("JsonObject_Subscript") {
|
||||
int i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "hello");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
obj[vla] = "world";
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
obj[vla] = "world";
|
||||
|
||||
EXPECT_STREQ("world", obj["hello"]);
|
||||
}
|
||||
REQUIRE(std::string("world") == obj["hello"]);
|
||||
}
|
||||
#endif
|
||||
|
||||
TEST(VariableLengthArray, JsonObject_Subscript_Assign) { // issue #416
|
||||
int i = 32;
|
||||
char vla[i];
|
||||
strcpy(vla, "world");
|
||||
SECTION("JsonObject_Subscript_Assign") { // issue #416
|
||||
int i = 32;
|
||||
char vla[i];
|
||||
strcpy(vla, "world");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
obj["hello"] = vla;
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
obj["hello"] = vla;
|
||||
|
||||
EXPECT_STREQ("world", obj["hello"].as<char*>());
|
||||
}
|
||||
REQUIRE(std::string("world") == obj["hello"].as<char*>());
|
||||
}
|
||||
|
||||
TEST(VariableLengthArray, JsonObject_Subscript_Set) {
|
||||
int i = 32;
|
||||
char vla[i];
|
||||
strcpy(vla, "world");
|
||||
SECTION("JsonObject_Subscript_Set") {
|
||||
int i = 32;
|
||||
char vla[i];
|
||||
strcpy(vla, "world");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
obj["hello"].set(vla);
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
obj["hello"].set(vla);
|
||||
|
||||
EXPECT_STREQ("world", obj["hello"].as<char*>());
|
||||
}
|
||||
REQUIRE(std::string("world") == obj["hello"].as<char*>());
|
||||
}
|
||||
|
||||
#ifndef CONFLICTS_WITH_BUILTIN_OPERATOR
|
||||
TEST(VariableLengthArray, JsonObject_Subscript_Const) {
|
||||
int i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "hello");
|
||||
SECTION("JsonObject_Subscript_Const") {
|
||||
int i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "hello");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
const JsonObject& obj = jsonBuffer.parseObject("{\"hello\":\"world\"}");
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
const JsonObject& obj = jsonBuffer.parseObject("{\"hello\":\"world\"}");
|
||||
|
||||
EXPECT_STREQ("world", obj[vla]);
|
||||
}
|
||||
REQUIRE(std::string("world") == obj[vla]);
|
||||
}
|
||||
#endif
|
||||
|
||||
TEST(VariableLengthArray, JsonObject_Get) {
|
||||
int i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "hello");
|
||||
SECTION("JsonObject_Get") {
|
||||
int i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "hello");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.parseObject("{\"hello\":\"world\"}");
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.parseObject("{\"hello\":\"world\"}");
|
||||
|
||||
EXPECT_STREQ("world", obj.get<char*>(vla));
|
||||
REQUIRE(std::string("world") == obj.get<char*>(vla));
|
||||
}
|
||||
|
||||
SECTION("JsonObject_Set_Key") {
|
||||
int i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "hello");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
obj.set(vla, "world");
|
||||
|
||||
REQUIRE(std::string("world") == obj["hello"]);
|
||||
}
|
||||
|
||||
SECTION("JsonObject_Set_Value") {
|
||||
int i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "world");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
obj.set("hello", vla);
|
||||
|
||||
REQUIRE(std::string("world") == obj["hello"]);
|
||||
}
|
||||
|
||||
SECTION("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);
|
||||
|
||||
REQUIRE(3.14 == obj["hello"]);
|
||||
}
|
||||
|
||||
SECTION("JsonObject_Set_KeyAndValue") {
|
||||
int i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "world");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
obj.set(vla, vla);
|
||||
|
||||
REQUIRE(std::string("world") == obj["world"]);
|
||||
}
|
||||
|
||||
SECTION("JsonObject_ContainsKey") {
|
||||
int i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "hello");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
const JsonObject& obj = jsonBuffer.parseObject("{\"hello\":\"world\"}");
|
||||
|
||||
REQUIRE(true == obj.containsKey(vla));
|
||||
}
|
||||
|
||||
SECTION("JsonObject_Remove") {
|
||||
int i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "hello");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.parseObject("{\"hello\":\"world\"}");
|
||||
obj.remove(vla);
|
||||
|
||||
REQUIRE(0 == obj.size());
|
||||
}
|
||||
|
||||
SECTION("JsonObject_Is") {
|
||||
int i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "hello");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.parseObject("{\"hello\":42}");
|
||||
|
||||
REQUIRE(true == obj.is<int>(vla));
|
||||
}
|
||||
|
||||
SECTION("JsonObject_CreateNestedArray") {
|
||||
int i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "hello");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
obj.createNestedArray(vla);
|
||||
}
|
||||
|
||||
SECTION("JsonObject_CreateNestedObject") {
|
||||
int i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "hello");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
obj.createNestedObject(vla);
|
||||
}
|
||||
|
||||
SECTION("JsonArray_Add") {
|
||||
int i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "world");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonArray& arr = jsonBuffer.createArray();
|
||||
arr.add(vla);
|
||||
|
||||
REQUIRE(std::string("world") == arr[0]);
|
||||
}
|
||||
|
||||
SECTION("JsonArray_Set") {
|
||||
int i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "world");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonArray& arr = jsonBuffer.createArray();
|
||||
arr.add("hello");
|
||||
arr.set(0, vla);
|
||||
|
||||
REQUIRE(std::string("world") == arr[0]);
|
||||
}
|
||||
|
||||
SECTION("JsonArraySubscript_Set") {
|
||||
int i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "world");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonArray& arr = jsonBuffer.createArray();
|
||||
arr.add("hello");
|
||||
arr[0].set(vla);
|
||||
|
||||
REQUIRE(std::string("world") == arr[0]);
|
||||
}
|
||||
|
||||
SECTION("JsonArraySubscript_Assign") {
|
||||
int i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "world");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonArray& arr = jsonBuffer.createArray();
|
||||
arr.add("hello");
|
||||
arr[0] = vla;
|
||||
|
||||
REQUIRE(std::string("world") == arr[0]);
|
||||
}
|
||||
|
||||
SECTION("JsonBuffer_strdup") {
|
||||
int i = 16;
|
||||
char vla[i];
|
||||
strcpy(vla, "world");
|
||||
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
const char* dup = jsonBuffer.strdup(vla);
|
||||
|
||||
REQUIRE(static_cast<const void*>(vla) != static_cast<const void*>(dup));
|
||||
REQUIRE(std::string("world") == dup);
|
||||
}
|
||||
}
|
||||
|
||||
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