Benoit Blanchon před 10 roky
rodič
revize
81285f49fe

+ 2 - 2
src/JsonVariant.cpp

@@ -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;
 }
 

+ 4 - 2
test/JsonVariant_Undefined_Tests.cpp

@@ -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());
 }

+ 4 - 4
test/StaticJsonBuffer_Array_Tests.cpp → test/StaticJsonBuffer_CreateArray_Tests.cpp

@@ -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();
@@ -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();

+ 4 - 4
test/StaticJsonBuffer_Object_Tests.cpp → test/StaticJsonBuffer_CreateObject_Tests.cpp

@@ -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();
@@ -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();

+ 72 - 0
test/StaticJsonBuffer_ParseArray_Tests.cpp

@@ -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 - 0
test/StaticJsonBuffer_ParseObject_Tests.cpp

@@ -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();
+}