Skip to content

Commit

Permalink
Fixed issue #68
Browse files Browse the repository at this point in the history
  • Loading branch information
Benoit Blanchon committed Apr 27, 2015
1 parent 877096d commit 81285f4
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/JsonVariant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ void JsonVariant::set(long value) {

void JsonVariant::set(JsonArray &array) {
if (_type == JSON_INVALID) return;
_type = JSON_ARRAY;
_type = array.success() ? JSON_ARRAY : JSON_INVALID;
_content.asArray = &array;
}

void JsonVariant::set(JsonObject &object) {
if (_type == JSON_INVALID) return;
_type = JSON_OBJECT;
_type = object.success() ? JSON_OBJECT : JSON_INVALID;
_content.asObject = &object;
}

Expand Down
6 changes: 4 additions & 2 deletions test/JsonVariant_Undefined_Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,13 @@ TEST_F(JsonVariant_Undefined_Tests, CanBeSetToBool) {
}

TEST_F(JsonVariant_Undefined_Tests, CanBeSetToArray) {
variant = JsonArray::invalid();
DynamicJsonBuffer jsonBuffer;
variant = jsonBuffer.createArray();
EXPECT_TRUE(variant.success());
}

TEST_F(JsonVariant_Undefined_Tests, CanBeSetToObject) {
variant = JsonObject::invalid();
DynamicJsonBuffer jsonBuffer;
variant = jsonBuffer.createObject();
EXPECT_TRUE(variant.success());
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <gtest/gtest.h>
#include <ArduinoJson.h>

TEST(StaticJsonBuffer_Array_Tests, GrowsWithArray) {
TEST(StaticJsonBuffer_CreateArray_Tests, GrowsWithArray) {
StaticJsonBuffer<JSON_ARRAY_SIZE(2)> json;

JsonArray &array = json.createArray();
Expand All @@ -20,21 +20,21 @@ TEST(StaticJsonBuffer_Array_Tests, GrowsWithArray) {
ASSERT_EQ(JSON_ARRAY_SIZE(2), json.size());
}

TEST(StaticJsonBuffer_Array_Tests, SucceedWhenBigEnough) {
TEST(StaticJsonBuffer_CreateArray_Tests, SucceedWhenBigEnough) {
StaticJsonBuffer<JSON_ARRAY_SIZE(0)> json;

JsonArray &array = json.createArray();
ASSERT_TRUE(array.success());
}

TEST(StaticJsonBuffer_Array_Tests, FailsWhenTooSmall) {
TEST(StaticJsonBuffer_CreateArray_Tests, FailsWhenTooSmall) {
StaticJsonBuffer<JSON_ARRAY_SIZE(0) - 1> json;

JsonArray &array = json.createArray();
ASSERT_FALSE(array.success());
}

TEST(StaticJsonBuffer_Array_Tests, ArrayDoesntGrowWhenFull) {
TEST(StaticJsonBuffer_CreateArray_Tests, ArrayDoesntGrowWhenFull) {
StaticJsonBuffer<JSON_ARRAY_SIZE(1)> json;

JsonArray &array = json.createArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <gtest/gtest.h>
#include <ArduinoJson.h>

TEST(StaticJsonBuffer_Object_Tests, GrowsWithObject) {
TEST(StaticJsonBuffer_CreateObject_Tests, GrowsWithObject) {
StaticJsonBuffer<JSON_OBJECT_SIZE(3)> json;

JsonObject &obj = json.createObject();
Expand All @@ -23,21 +23,21 @@ TEST(StaticJsonBuffer_Object_Tests, GrowsWithObject) {
ASSERT_EQ(JSON_OBJECT_SIZE(2), json.size());
}

TEST(StaticJsonBuffer_Object_Tests, SucceedWhenBigEnough) {
TEST(StaticJsonBuffer_CreateObject_Tests, SucceedWhenBigEnough) {
StaticJsonBuffer<JSON_OBJECT_SIZE(0)> json;

JsonObject &object = json.createObject();
ASSERT_TRUE(object.success());
}

TEST(StaticJsonBuffer_Object_Tests, FailsWhenTooSmall) {
TEST(StaticJsonBuffer_CreateObject_Tests, FailsWhenTooSmall) {
StaticJsonBuffer<JSON_OBJECT_SIZE(0) - 1> json;

JsonObject &object = json.createObject();
ASSERT_FALSE(object.success());
}

TEST(StaticJsonBuffer_Object_Tests, ObjectDoesntGrowWhenFull) {
TEST(StaticJsonBuffer_CreateObject_Tests, ObjectDoesntGrowWhenFull) {
StaticJsonBuffer<JSON_OBJECT_SIZE(1)> json;

JsonObject &obj = json.createObject();
Expand Down
72 changes: 72 additions & 0 deletions test/StaticJsonBuffer_ParseArray_Tests.cpp
Original file line number Diff line number Diff line change
@@ -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 changes: 73 additions & 0 deletions test/StaticJsonBuffer_ParseObject_Tests.cpp
Original file line number Diff line number Diff line change
@@ -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();
}

0 comments on commit 81285f4

Please sign in to comment.