mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-15 11:36:36 +02:00
Fixed issue #68
This commit is contained in:
@ -64,13 +64,13 @@ void JsonVariant::set(long value) {
|
|||||||
|
|
||||||
void JsonVariant::set(JsonArray &array) {
|
void JsonVariant::set(JsonArray &array) {
|
||||||
if (_type == JSON_INVALID) return;
|
if (_type == JSON_INVALID) return;
|
||||||
_type = JSON_ARRAY;
|
_type = array.success() ? JSON_ARRAY : JSON_INVALID;
|
||||||
_content.asArray = &array;
|
_content.asArray = &array;
|
||||||
}
|
}
|
||||||
|
|
||||||
void JsonVariant::set(JsonObject &object) {
|
void JsonVariant::set(JsonObject &object) {
|
||||||
if (_type == JSON_INVALID) return;
|
if (_type == JSON_INVALID) return;
|
||||||
_type = JSON_OBJECT;
|
_type = object.success() ? JSON_OBJECT : JSON_INVALID;
|
||||||
_content.asObject = &object;
|
_content.asObject = &object;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -62,11 +62,13 @@ TEST_F(JsonVariant_Undefined_Tests, CanBeSetToBool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(JsonVariant_Undefined_Tests, CanBeSetToArray) {
|
TEST_F(JsonVariant_Undefined_Tests, CanBeSetToArray) {
|
||||||
variant = JsonArray::invalid();
|
DynamicJsonBuffer jsonBuffer;
|
||||||
|
variant = jsonBuffer.createArray();
|
||||||
EXPECT_TRUE(variant.success());
|
EXPECT_TRUE(variant.success());
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(JsonVariant_Undefined_Tests, CanBeSetToObject) {
|
TEST_F(JsonVariant_Undefined_Tests, CanBeSetToObject) {
|
||||||
variant = JsonObject::invalid();
|
DynamicJsonBuffer jsonBuffer;
|
||||||
|
variant = jsonBuffer.createObject();
|
||||||
EXPECT_TRUE(variant.success());
|
EXPECT_TRUE(variant.success());
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,7 @@
|
|||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
#include <ArduinoJson.h>
|
#include <ArduinoJson.h>
|
||||||
|
|
||||||
TEST(StaticJsonBuffer_Array_Tests, GrowsWithArray) {
|
TEST(StaticJsonBuffer_CreateArray_Tests, GrowsWithArray) {
|
||||||
StaticJsonBuffer<JSON_ARRAY_SIZE(2)> json;
|
StaticJsonBuffer<JSON_ARRAY_SIZE(2)> json;
|
||||||
|
|
||||||
JsonArray &array = json.createArray();
|
JsonArray &array = json.createArray();
|
||||||
@ -20,21 +20,21 @@ TEST(StaticJsonBuffer_Array_Tests, GrowsWithArray) {
|
|||||||
ASSERT_EQ(JSON_ARRAY_SIZE(2), json.size());
|
ASSERT_EQ(JSON_ARRAY_SIZE(2), json.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(StaticJsonBuffer_Array_Tests, SucceedWhenBigEnough) {
|
TEST(StaticJsonBuffer_CreateArray_Tests, SucceedWhenBigEnough) {
|
||||||
StaticJsonBuffer<JSON_ARRAY_SIZE(0)> json;
|
StaticJsonBuffer<JSON_ARRAY_SIZE(0)> json;
|
||||||
|
|
||||||
JsonArray &array = json.createArray();
|
JsonArray &array = json.createArray();
|
||||||
ASSERT_TRUE(array.success());
|
ASSERT_TRUE(array.success());
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(StaticJsonBuffer_Array_Tests, FailsWhenTooSmall) {
|
TEST(StaticJsonBuffer_CreateArray_Tests, FailsWhenTooSmall) {
|
||||||
StaticJsonBuffer<JSON_ARRAY_SIZE(0) - 1> json;
|
StaticJsonBuffer<JSON_ARRAY_SIZE(0) - 1> json;
|
||||||
|
|
||||||
JsonArray &array = json.createArray();
|
JsonArray &array = json.createArray();
|
||||||
ASSERT_FALSE(array.success());
|
ASSERT_FALSE(array.success());
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(StaticJsonBuffer_Array_Tests, ArrayDoesntGrowWhenFull) {
|
TEST(StaticJsonBuffer_CreateArray_Tests, ArrayDoesntGrowWhenFull) {
|
||||||
StaticJsonBuffer<JSON_ARRAY_SIZE(1)> json;
|
StaticJsonBuffer<JSON_ARRAY_SIZE(1)> json;
|
||||||
|
|
||||||
JsonArray &array = json.createArray();
|
JsonArray &array = json.createArray();
|
@ -7,7 +7,7 @@
|
|||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
#include <ArduinoJson.h>
|
#include <ArduinoJson.h>
|
||||||
|
|
||||||
TEST(StaticJsonBuffer_Object_Tests, GrowsWithObject) {
|
TEST(StaticJsonBuffer_CreateObject_Tests, GrowsWithObject) {
|
||||||
StaticJsonBuffer<JSON_OBJECT_SIZE(3)> json;
|
StaticJsonBuffer<JSON_OBJECT_SIZE(3)> json;
|
||||||
|
|
||||||
JsonObject &obj = json.createObject();
|
JsonObject &obj = json.createObject();
|
||||||
@ -23,21 +23,21 @@ TEST(StaticJsonBuffer_Object_Tests, GrowsWithObject) {
|
|||||||
ASSERT_EQ(JSON_OBJECT_SIZE(2), json.size());
|
ASSERT_EQ(JSON_OBJECT_SIZE(2), json.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(StaticJsonBuffer_Object_Tests, SucceedWhenBigEnough) {
|
TEST(StaticJsonBuffer_CreateObject_Tests, SucceedWhenBigEnough) {
|
||||||
StaticJsonBuffer<JSON_OBJECT_SIZE(0)> json;
|
StaticJsonBuffer<JSON_OBJECT_SIZE(0)> json;
|
||||||
|
|
||||||
JsonObject &object = json.createObject();
|
JsonObject &object = json.createObject();
|
||||||
ASSERT_TRUE(object.success());
|
ASSERT_TRUE(object.success());
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(StaticJsonBuffer_Object_Tests, FailsWhenTooSmall) {
|
TEST(StaticJsonBuffer_CreateObject_Tests, FailsWhenTooSmall) {
|
||||||
StaticJsonBuffer<JSON_OBJECT_SIZE(0) - 1> json;
|
StaticJsonBuffer<JSON_OBJECT_SIZE(0) - 1> json;
|
||||||
|
|
||||||
JsonObject &object = json.createObject();
|
JsonObject &object = json.createObject();
|
||||||
ASSERT_FALSE(object.success());
|
ASSERT_FALSE(object.success());
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(StaticJsonBuffer_Object_Tests, ObjectDoesntGrowWhenFull) {
|
TEST(StaticJsonBuffer_CreateObject_Tests, ObjectDoesntGrowWhenFull) {
|
||||||
StaticJsonBuffer<JSON_OBJECT_SIZE(1)> json;
|
StaticJsonBuffer<JSON_OBJECT_SIZE(1)> json;
|
||||||
|
|
||||||
JsonObject &obj = json.createObject();
|
JsonObject &obj = json.createObject();
|
72
test/StaticJsonBuffer_ParseArray_Tests.cpp
Normal file
72
test/StaticJsonBuffer_ParseArray_Tests.cpp
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
// Copyright Benoit Blanchon 2014-2015
|
||||||
|
// MIT License
|
||||||
|
//
|
||||||
|
// Arduino JSON library
|
||||||
|
// https://github.com/bblanchon/ArduinoJson
|
||||||
|
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
#include <ArduinoJson.h>
|
||||||
|
|
||||||
|
class StaticJsonBuffer_ParseArray_Tests : public testing::Test {
|
||||||
|
protected:
|
||||||
|
void with(JsonBuffer& jsonBuffer) { _jsonBuffer = &jsonBuffer; }
|
||||||
|
|
||||||
|
void whenInputIs(const char* json) { strcpy(_jsonString, json); }
|
||||||
|
|
||||||
|
void parseMustSucceed() {
|
||||||
|
EXPECT_TRUE(_jsonBuffer->parseArray(_jsonString).success());
|
||||||
|
}
|
||||||
|
|
||||||
|
void parseMustFail() {
|
||||||
|
EXPECT_FALSE(_jsonBuffer->parseArray(_jsonString).success());
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
JsonBuffer* _jsonBuffer;
|
||||||
|
char _jsonString[256];
|
||||||
|
};
|
||||||
|
|
||||||
|
TEST_F(StaticJsonBuffer_ParseArray_Tests, TooSmallBufferForEmptyArray) {
|
||||||
|
StaticJsonBuffer<JSON_ARRAY_SIZE(0) - 1> bufferTooSmall;
|
||||||
|
with(bufferTooSmall);
|
||||||
|
whenInputIs("[]");
|
||||||
|
parseMustFail();
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(StaticJsonBuffer_ParseArray_Tests, BufferOfTheRightSizeForEmptyArray) {
|
||||||
|
StaticJsonBuffer<JSON_ARRAY_SIZE(0)> bufferOfRightSize;
|
||||||
|
with(bufferOfRightSize);
|
||||||
|
whenInputIs("[]");
|
||||||
|
parseMustSucceed();
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(StaticJsonBuffer_ParseArray_Tests, TooSmallBufferForArrayWithOneValue) {
|
||||||
|
StaticJsonBuffer<JSON_ARRAY_SIZE(1) - 1> bufferTooSmall;
|
||||||
|
with(bufferTooSmall);
|
||||||
|
whenInputIs("[1]");
|
||||||
|
parseMustFail();
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(StaticJsonBuffer_ParseArray_Tests,
|
||||||
|
BufferOfTheRightSizeForArrayWithOneValue) {
|
||||||
|
StaticJsonBuffer<JSON_ARRAY_SIZE(1)> bufferOfRightSize;
|
||||||
|
with(bufferOfRightSize);
|
||||||
|
whenInputIs("[1]");
|
||||||
|
parseMustSucceed();
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(StaticJsonBuffer_ParseArray_Tests,
|
||||||
|
TooSmallBufferForArrayWithNestedObject) {
|
||||||
|
StaticJsonBuffer<JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(0) - 1> bufferTooSmall;
|
||||||
|
with(bufferTooSmall);
|
||||||
|
whenInputIs("[{}]");
|
||||||
|
parseMustFail();
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(StaticJsonBuffer_ParseArray_Tests,
|
||||||
|
BufferOfTheRightSizeForArrayWithNestedObject) {
|
||||||
|
StaticJsonBuffer<JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(0)> bufferOfRightSize;
|
||||||
|
with(bufferOfRightSize);
|
||||||
|
whenInputIs("[{}]");
|
||||||
|
parseMustSucceed();
|
||||||
|
}
|
73
test/StaticJsonBuffer_ParseObject_Tests.cpp
Normal file
73
test/StaticJsonBuffer_ParseObject_Tests.cpp
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
// Copyright Benoit Blanchon 2014-2015
|
||||||
|
// MIT License
|
||||||
|
//
|
||||||
|
// Arduino JSON library
|
||||||
|
// https://github.com/bblanchon/ArduinoJson
|
||||||
|
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
#include <ArduinoJson.h>
|
||||||
|
|
||||||
|
class StaticJsonBuffer_ParseObject_Tests : public testing::Test {
|
||||||
|
protected:
|
||||||
|
void with(JsonBuffer& jsonBuffer) { _jsonBuffer = &jsonBuffer; }
|
||||||
|
|
||||||
|
void whenInputIs(const char* json) { strcpy(_jsonString, json); }
|
||||||
|
|
||||||
|
void parseMustSucceed() {
|
||||||
|
EXPECT_TRUE(_jsonBuffer->parseObject(_jsonString).success());
|
||||||
|
}
|
||||||
|
|
||||||
|
void parseMustFail() {
|
||||||
|
EXPECT_FALSE(_jsonBuffer->parseObject(_jsonString).success());
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
JsonBuffer* _jsonBuffer;
|
||||||
|
char _jsonString[256];
|
||||||
|
};
|
||||||
|
|
||||||
|
TEST_F(StaticJsonBuffer_ParseObject_Tests, TooSmallBufferForEmptyObject) {
|
||||||
|
StaticJsonBuffer<JSON_OBJECT_SIZE(0) - 1> bufferTooSmall;
|
||||||
|
with(bufferTooSmall);
|
||||||
|
whenInputIs("{}");
|
||||||
|
parseMustFail();
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(StaticJsonBuffer_ParseObject_Tests, BufferOfTheRightSizeForEmptyObject) {
|
||||||
|
StaticJsonBuffer<JSON_OBJECT_SIZE(0)> bufferOfRightSize;
|
||||||
|
with(bufferOfRightSize);
|
||||||
|
whenInputIs("{}");
|
||||||
|
parseMustSucceed();
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(StaticJsonBuffer_ParseObject_Tests,
|
||||||
|
TooSmallBufferForObjectWithOneValue) {
|
||||||
|
StaticJsonBuffer<JSON_OBJECT_SIZE(1) - 1> bufferTooSmall;
|
||||||
|
with(bufferTooSmall);
|
||||||
|
whenInputIs("{\"a\":1}");
|
||||||
|
parseMustFail();
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(StaticJsonBuffer_ParseObject_Tests,
|
||||||
|
BufferOfTheRightSizeForObjectWithOneValue) {
|
||||||
|
StaticJsonBuffer<JSON_OBJECT_SIZE(1)> bufferOfRightSize;
|
||||||
|
with(bufferOfRightSize);
|
||||||
|
whenInputIs("{\"a\":1}");
|
||||||
|
parseMustSucceed();
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(StaticJsonBuffer_ParseObject_Tests,
|
||||||
|
TooSmallBufferForObjectWithNestedObject) {
|
||||||
|
StaticJsonBuffer<JSON_OBJECT_SIZE(1) + JSON_ARRAY_SIZE(0) - 1> bufferTooSmall;
|
||||||
|
with(bufferTooSmall);
|
||||||
|
whenInputIs("{\"a\":[]}");
|
||||||
|
parseMustFail();
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(StaticJsonBuffer_ParseObject_Tests,
|
||||||
|
BufferOfTheRightSizeForObjectWithNestedObject) {
|
||||||
|
StaticJsonBuffer<JSON_OBJECT_SIZE(1) + JSON_ARRAY_SIZE(0)> bufferOfRightSize;
|
||||||
|
with(bufferOfRightSize);
|
||||||
|
whenInputIs("{\"a\":[]}");
|
||||||
|
parseMustSucceed();
|
||||||
|
}
|
Reference in New Issue
Block a user