Benoit Blanchon 11 лет назад
Родитель
Сommit
cb3c59ec07

+ 2 - 0
srcs/JsonArray.cpp

@@ -0,0 +1,2 @@
+#include "JsonArray.h"
+

+ 22 - 0
srcs/JsonArray.h

@@ -0,0 +1,22 @@
+#pragma once
+
+#include "JsonContainer.h"
+
+class JsonArray : public JsonContainer
+{
+public:
+    JsonArray()
+    {
+    }
+
+    explicit JsonArray(JsonNode* node)
+        : JsonContainer(node)
+    {
+    }
+
+ //   JsonValue operator[](int index);
+
+   // template<typename T>
+   // void add(T value);
+};
+

+ 10 - 10
srcs/JsonBuffer.cpp

@@ -6,16 +6,6 @@
 #include "JsonValue.h"
 #include "Internals/JsonNode.h"
 
-JsonObject JsonBuffer::createObject()
-{
-    JsonNode* node = createNode(JSON_OBJECT);
-
-    if (node)
-        node->content.asContainer.buffer = this;
-
-    return JsonObject(node);
-}
-
 JsonValue JsonBuffer::createValue()
 {
     JsonNode* node = createNode(JSON_UNDEFINED);
@@ -30,4 +20,14 @@ JsonNode* JsonBuffer::createNode(JsonNodeType type)
     memset(node, 0, sizeof(JsonNode));
     node->type = type;
     return node;
+}
+
+JsonNode* JsonBuffer::createContainerNode(JsonNodeType type)
+{
+    JsonNode* node = createNode(type);
+
+    if (node)
+        node->content.asContainer.buffer = this;
+
+    return node;
 }

+ 14 - 8
srcs/JsonBuffer.h

@@ -1,20 +1,25 @@
 #pragma once
 
-#include "Internals/JsonNode.h"
-
-
-class JsonObject;
-class JsonValue;
-struct JsonNode;
+#include "JsonArray.h"
+#include "JsonObject.h"
 
 class JsonBuffer
 {
     friend class JsonContainer;
 
 public:
-//    virtual ~JsonBuffer() = 0;
+    virtual ~JsonBuffer() {};
+
+    JsonArray createArray()
+    {
+        return JsonArray(createContainerNode(JSON_ARRAY));
+    }
+
+    JsonObject createObject()
+    {
+        return JsonObject(createContainerNode(JSON_OBJECT));
+    }
 
-    JsonObject createObject();
     JsonValue createValue();
 
 protected:
@@ -22,5 +27,6 @@ protected:
 
 private:
     JsonNode* createNode(JsonNodeType type);
+    JsonNode* createContainerNode(JsonNodeType type);
 };
 

+ 2 - 0
srcs/srcs.vcxproj

@@ -75,6 +75,7 @@
   </ItemDefinitionGroup>
   <ItemGroup>
     <ClInclude Include="Internals\EscapedString.h" />
+    <ClInclude Include="JsonArray.h" />
     <ClInclude Include="JsonBuffer.h" />
     <ClInclude Include="Internals\JsonNode.h" />
     <ClInclude Include="Internals\JsonNodeSerializer.h" />
@@ -89,6 +90,7 @@
   </ItemGroup>
   <ItemGroup>
     <ClCompile Include="Internals\EscapedString.cpp" />
+    <ClCompile Include="JsonArray.cpp" />
     <ClCompile Include="JsonBuffer.cpp" />
     <ClCompile Include="Internals\JsonNodeSerializer.cpp" />
     <ClCompile Include="JsonContainer.cpp" />

+ 6 - 0
srcs/srcs.vcxproj.filters

@@ -51,6 +51,9 @@
     <ClInclude Include="Internals\JsonNodeIterator.h">
       <Filter>Header Files</Filter>
     </ClInclude>
+    <ClInclude Include="JsonArray.h">
+      <Filter>Header Files</Filter>
+    </ClInclude>
   </ItemGroup>
   <ItemGroup>
     <ClCompile Include="JsonObject.cpp">
@@ -77,5 +80,8 @@
     <ClCompile Include="JsonContainer.cpp">
       <Filter>Source Files</Filter>
     </ClCompile>
+    <ClCompile Include="JsonArray.cpp">
+      <Filter>Source Files</Filter>
+    </ClCompile>
   </ItemGroup>
 </Project>

+ 77 - 0
tests/JsonArray_Container_Tests.cpp

@@ -0,0 +1,77 @@
+#include <gtest/gtest.h>
+#include <StaticJsonBuffer.h>
+#include <JsonValue.h>
+
+class JsonArray_Container_Tests : public ::testing::Test
+{
+protected:
+    virtual void SetUp()
+    {
+        array = json.createArray();
+    }
+
+    StaticJsonBuffer<42> json;
+    JsonArray array;
+};
+
+TEST_F(JsonArray_Container_Tests, InitialSizeIsZero)
+{
+    EXPECT_EQ(0, array.size());
+}
+/*
+TEST_F(JsonArray_Container_Tests, Grow_WhenValuesAreAdded)
+{
+    array.add("hello");
+    EXPECT_EQ(1, array.size());
+
+    array.add("world");
+    EXPECT_EQ(2, array.size());
+}
+
+TEST_F(JsonArray_Container_Tests, CanStoreIntegers)
+{
+    array.add(123);
+    array.add(456);
+
+    EXPECT_EQ(123, (int) array[0]);
+    EXPECT_EQ(456, (int) array[1]);
+}
+
+TEST_F(JsonArray_Container_Tests, CanStoreDoubles)
+{
+    array.add(123.45);
+    array.add(456.78);
+
+    EXPECT_EQ(123.45, (double) array[0]);
+    EXPECT_EQ(456.78, (double) array[1]);
+}
+
+TEST_F(JsonArray_Container_Tests, CanStoreBooleans)
+{
+    array.add(true);
+    array.add(false);
+
+    EXPECT_TRUE((bool) array[0]);
+    EXPECT_FALSE((bool) array[1]);
+}
+
+TEST_F(JsonArray_Container_Tests, CanStoreStrings)
+{
+    array.add("h3110");
+    array.add("w0r1d");
+
+    EXPECT_STREQ("h3110", (const char*) array[0]);
+    EXPECT_STREQ("w0r1d", (const char*) array[1]);
+}
+
+TEST_F(JsonArray_Container_Tests, CanStoreInnerArrays)
+{
+    JsonArray innerarray1 = json.createArray();
+    JsonArray innerarray2 = json.createArray();
+
+    array.add(innerarray1);
+    array.add(innerarray2);
+
+    EXPECT_EQ(innerarray1, (JsonArray) array[0]);
+    EXPECT_EQ(innerarray2, (JsonArray) array[1]);
+}*/

+ 162 - 0
tests/JsonArray_Serialization_Tests.cpp

@@ -0,0 +1,162 @@
+/*
+* Arduino JSON library
+* Benoit Blanchon 2014 - MIT License
+*/
+
+#include <gtest/gtest.h>
+#include <JsonArray.h>
+#include <JsonObject.h>
+#include <StaticJsonBuffer.h>
+
+class JsonArray_Serialization_Tests : testing::Test 
+{
+protected:
+    virtual void SetUp()
+    {
+        array = json.createArray();
+    }
+
+    void outputMustBe(const char* expected)
+    {      
+        size_t n = array.printTo(buffer, sizeof(buffer));
+        EXPECT_STREQ(expected, buffer);
+        EXPECT_EQ(strlen(expected), n);
+    }
+
+private:
+    JsonArray array;
+    char buffer[256];
+    StaticJsonBuffer<32> json;
+};
+/*
+TEST_F(JsonArray_Serialization_Tests, Empty)
+{
+    outputMustBe("[]");
+}
+
+TEST_F(JsonArray_Serialization_Tests, Null)
+{
+    array.add((char*) 0);
+
+    outputMustBe("[null]");
+}
+
+TEST_F(JsonArray_Serialization_Tests, OneString)
+{
+    array.add("hello");
+
+    outputMustBe("[\"hello\"]");
+}
+
+TEST_F(JsonArray_Serialization_Tests, TwoStrings)
+{
+    array.add("hello");
+    array.add("world");
+
+    outputMustBe("[\"hello\",\"world\"]");
+}
+
+TEST_F(JsonArray_Serialization_Tests, OneStringOverCapacity)
+{
+    array.add("hello");
+    array.add("world");
+    array.add("lost");
+
+    outputMustBe("[\"hello\",\"world\"]");
+}
+
+TEST_F(JsonArray_Serialization_Tests, OneDoubleDefaultDigits)
+{
+    array.add(3.14159265358979323846);
+    outputMustBe("[3.14]");
+}
+
+TEST_F(JsonArray_Serialization_Tests, OneDoubleFourDigits)
+{
+    array.add<4>(3.14159265358979323846);
+    outputMustBe("[3.1416]");
+}
+
+TEST_F(JsonArray_Serialization_Tests, OneInteger)
+{
+    array.add(1);
+
+    outputMustBe("[1]");
+}
+
+TEST_F(JsonArray_Serialization_Tests, TwoIntegers)
+{
+    array.add(1);
+    array.add(2);
+
+    outputMustBe("[1,2]");
+}
+
+TEST_F(JsonArray_Serialization_Tests, OneIntegerOverCapacity)
+{
+    array.add(1);
+    array.add(2);
+    array.add(3);
+
+    outputMustBe("[1,2]");
+}
+
+TEST_F(JsonArray_Serialization_Tests, OneTrue)
+{
+    array.add(true);
+
+    outputMustBe("[true]");
+}
+
+TEST_F(JsonArray_Serialization_Tests, OneFalse)
+{
+    array.add(false);
+
+    outputMustBe("[false]");
+}
+
+TEST_F(JsonArray_Serialization_Tests, TwoBooleans)
+{
+    array.add(false);
+    array.add(true);
+
+    outputMustBe("[false,true]");
+}
+
+TEST_F(JsonArray_Serialization_Tests, OneBooleanOverCapacity)
+{
+    array.add(false);
+    array.add(true);
+    array.add(false);
+
+    outputMustBe("[false,true]");
+}
+
+TEST_F(JsonArray_Serialization_Tests, OneEmptyNestedArray)
+{
+    JsonArray<1> nestedArray;
+    
+    array.add(nestedArray);
+
+    outputMustBe("[[]]");
+}
+
+TEST_F(JsonArray_Serialization_Tests, OneEmptyNestedHash)
+{
+    JsonObject<1> nestedObject;
+
+    array.add(nestedObject);
+
+    outputMustBe("[{}]");
+}
+
+TEST_F(JsonArray_Serialization_Tests, OneNestedArrayWithOneInteger)
+{
+    JsonArray<1> nestedArray;
+    nestedArray.add(1);
+
+    array.add(nestedArray);
+
+    outputMustBe("[[1]]");
+}
+*/

+ 11 - 11
tests/JsonObjectTests.cpp → tests/JsonObject_Container_Tests.cpp

@@ -2,7 +2,7 @@
 #include <StaticJsonBuffer.h>
 #include <JsonValue.h>
 
-class JsonObjectTests : public ::testing::Test
+class JsonObject_Container_Tests : public ::testing::Test
 {
 protected:
     virtual void SetUp() 
@@ -14,12 +14,12 @@ protected:
     JsonObject object;
 };
 
-TEST_F(JsonObjectTests, InitialSizeIsZero)
+TEST_F(JsonObject_Container_Tests, InitialSizeIsZero)
 {
     EXPECT_EQ(0, object.size());
 }
 
-TEST_F(JsonObjectTests, Grow_WhenValuesAreAdded)
+TEST_F(JsonObject_Container_Tests, Grow_WhenValuesAreAdded)
 {   
     object["hello"];
     EXPECT_EQ(1, object.size());
@@ -28,7 +28,7 @@ TEST_F(JsonObjectTests, Grow_WhenValuesAreAdded)
     EXPECT_EQ(2, object.size());
 }
 
-TEST_F(JsonObjectTests, DoNotGrow_WhenSameValueIsAdded)
+TEST_F(JsonObject_Container_Tests, DoNotGrow_WhenSameValueIsAdded)
 {
     object["hello"];
     EXPECT_EQ(1, object.size());
@@ -37,7 +37,7 @@ TEST_F(JsonObjectTests, DoNotGrow_WhenSameValueIsAdded)
     EXPECT_EQ(1, object.size());
 }
 
-TEST_F(JsonObjectTests, Shrink_WhenValuesAreRemoved)
+TEST_F(JsonObject_Container_Tests, Shrink_WhenValuesAreRemoved)
 {
     object["hello"];
     object["world"];
@@ -49,7 +49,7 @@ TEST_F(JsonObjectTests, Shrink_WhenValuesAreRemoved)
     EXPECT_EQ(0, object.size());
 }
 
-TEST_F(JsonObjectTests, DoNotShrink_WhenRemoveIsCalledWithAWrongKey)
+TEST_F(JsonObject_Container_Tests, DoNotShrink_WhenRemoveIsCalledWithAWrongKey)
 {
     object["hello"];
     object["world"];
@@ -59,7 +59,7 @@ TEST_F(JsonObjectTests, DoNotShrink_WhenRemoveIsCalledWithAWrongKey)
     EXPECT_EQ(2, object.size());
 }
 
-TEST_F(JsonObjectTests, CanStoreIntegers)
+TEST_F(JsonObject_Container_Tests, CanStoreIntegers)
 {
     object["hello"] = 123;
     object["world"] = 456;
@@ -68,7 +68,7 @@ TEST_F(JsonObjectTests, CanStoreIntegers)
     EXPECT_EQ(456, (int) object["world"]);
 }
 
-TEST_F(JsonObjectTests, CanStoreDoubles)
+TEST_F(JsonObject_Container_Tests, CanStoreDoubles)
 {
     object["hello"] = 123.45;
     object["world"] = 456.78;
@@ -77,7 +77,7 @@ TEST_F(JsonObjectTests, CanStoreDoubles)
     EXPECT_EQ(456.78, (double) object["world"]);
 }
 
-TEST_F(JsonObjectTests, CanStoreBooleans)
+TEST_F(JsonObject_Container_Tests, CanStoreBooleans)
 {
     object["hello"] = true;
     object["world"] = false;
@@ -86,7 +86,7 @@ TEST_F(JsonObjectTests, CanStoreBooleans)
     EXPECT_FALSE((bool) object["world"]);
 }
 
-TEST_F(JsonObjectTests, CanStoreStrings)
+TEST_F(JsonObject_Container_Tests, CanStoreStrings)
 {
     object["hello"] = "h3110";
     object["world"] = "w0r1d";
@@ -95,7 +95,7 @@ TEST_F(JsonObjectTests, CanStoreStrings)
     EXPECT_STREQ("w0r1d", (const char*) object["world"]);
 }
 
-TEST_F(JsonObjectTests, CanStoreInnerObjects)
+TEST_F(JsonObject_Container_Tests, CanStoreInnerObjects)
 {
     JsonObject innerObject1 = json.createObject();
     JsonObject innerObject2 = json.createObject();

+ 17 - 17
tests/JsonObjectSerializationTests.cpp → tests/JsonObject_Serialization_Tests.cpp

@@ -3,7 +3,7 @@
 #include <JsonValue.h>
 #include <StaticJsonBuffer.h>
 
-class JsonObjectSerializationTests : public testing::Test
+class JsonObject_Serialization_Tests : public testing::Test
 {
 protected:
     virtual void SetUp()
@@ -24,19 +24,19 @@ protected:
     StaticJsonBuffer<5> json;
 };
 
-TEST_F(JsonObjectSerializationTests, EmptyObject)
+TEST_F(JsonObject_Serialization_Tests, EmptyObject)
 {
     outputMustBe("{}");
 }
 
-TEST_F(JsonObjectSerializationTests, OneString)
+TEST_F(JsonObject_Serialization_Tests, OneString)
 {
     object["key"] = "value";
 
     outputMustBe("{\"key\":\"value\"}");
 }
 
-TEST_F(JsonObjectSerializationTests, TwoStrings)
+TEST_F(JsonObject_Serialization_Tests, TwoStrings)
 {
     object["key1"] = "value1";
     object["key2"] = "value2";
@@ -44,7 +44,7 @@ TEST_F(JsonObjectSerializationTests, TwoStrings)
     outputMustBe("{\"key1\":\"value1\",\"key2\":\"value2\"}");
 }
 
-TEST_F(JsonObjectSerializationTests, RemoveFirst)
+TEST_F(JsonObject_Serialization_Tests, RemoveFirst)
 {
     object["key1"] = "value1";
     object["key2"] = "value2";
@@ -53,7 +53,7 @@ TEST_F(JsonObjectSerializationTests, RemoveFirst)
     outputMustBe("{\"key2\":\"value2\"}");
 }
 
-TEST_F(JsonObjectSerializationTests, RemoveLast)
+TEST_F(JsonObject_Serialization_Tests, RemoveLast)
 {
     object["key1"] = "value1";
     object["key2"] = "value2";
@@ -62,7 +62,7 @@ TEST_F(JsonObjectSerializationTests, RemoveLast)
     outputMustBe("{\"key1\":\"value1\"}");
 }
 
-TEST_F(JsonObjectSerializationTests, RemoveUnexistingKey)
+TEST_F(JsonObject_Serialization_Tests, RemoveUnexistingKey)
 {
     object["key1"] = "value1";
     object["key2"] = "value2";
@@ -71,7 +71,7 @@ TEST_F(JsonObjectSerializationTests, RemoveUnexistingKey)
     outputMustBe("{\"key1\":\"value1\",\"key2\":\"value2\"}");
 }
 
-TEST_F(JsonObjectSerializationTests, ReplaceExistingKey)
+TEST_F(JsonObject_Serialization_Tests, ReplaceExistingKey)
 {
     object["key"] = "value1";
     object["key"] = "value2";
@@ -79,7 +79,7 @@ TEST_F(JsonObjectSerializationTests, ReplaceExistingKey)
     outputMustBe("{\"key\":\"value2\"}");
 }
 
-TEST_F(JsonObjectSerializationTests, OneStringOverCapacity)
+TEST_F(JsonObject_Serialization_Tests, OneStringOverCapacity)
 {
     object["key1"] = "value1";
     object["key2"] = "value2";
@@ -88,43 +88,43 @@ TEST_F(JsonObjectSerializationTests, OneStringOverCapacity)
     outputMustBe("{\"key1\":\"value1\",\"key2\":\"value2\"}");
 }
 
-TEST_F(JsonObjectSerializationTests, OneInteger)
+TEST_F(JsonObject_Serialization_Tests, OneInteger)
 {
     object["key"] = 1;
     outputMustBe("{\"key\":1}");
 }
 
-TEST_F(JsonObjectSerializationTests, OneDoubleFourDigits)
+TEST_F(JsonObject_Serialization_Tests, OneDoubleFourDigits)
 {
     object["key"].set(3.14159265358979323846, 4);
     outputMustBe("{\"key\":3.1416}");
 }
 
-TEST_F(JsonObjectSerializationTests, OneDoubleDefaultDigits)
+TEST_F(JsonObject_Serialization_Tests, OneDoubleDefaultDigits)
 {
     object["key"] = 3.14159265358979323846;
     outputMustBe("{\"key\":3.14}");
 }
 
-TEST_F(JsonObjectSerializationTests, OneNull)
+TEST_F(JsonObject_Serialization_Tests, OneNull)
 {
     object["key"] = (char*) 0;
     outputMustBe("{\"key\":null}");
 }
 
-TEST_F(JsonObjectSerializationTests, OneTrue)
+TEST_F(JsonObject_Serialization_Tests, OneTrue)
 {
     object["key"] = true;
     outputMustBe("{\"key\":true}");
 }
 
-TEST_F(JsonObjectSerializationTests, OneFalse)
+TEST_F(JsonObject_Serialization_Tests, OneFalse)
 {
     object["key"] = false;
     outputMustBe("{\"key\":false}");
 }
 /*
-TEST_F(JsonObjectSerializationTests, OneEmptyNestedArray)
+TEST_F(JsonObject_Serialization_Tests, OneEmptyNestedArray)
 {
     auto nestedArray = JsonArray<1>();
 
@@ -133,7 +133,7 @@ TEST_F(JsonObjectSerializationTests, OneEmptyNestedArray)
     outputMustBe("{\"key\":[]}");
 }
 */
-TEST_F(JsonObjectSerializationTests, OneEmptyNestedObject)
+TEST_F(JsonObject_Serialization_Tests, OneEmptyNestedObject)
 {
     auto nestedObject = json.createObject();
 

+ 4 - 2
tests/tests.vcxproj

@@ -86,8 +86,10 @@
     <ClCompile Include="..\third-party\gtest-1.7.0\src\gtest-all.cc" />
     <ClCompile Include="..\third-party\gtest-1.7.0\src\gtest_main.cc" />
     <ClCompile Include="EscapedStringTests.cpp" />
-    <ClCompile Include="JsonObjectSerializationTests.cpp" />
-    <ClCompile Include="JsonObjectTests.cpp" />
+    <ClCompile Include="JsonArray_Container_Tests.cpp" />
+    <ClCompile Include="JsonArray_Serialization_Tests.cpp" />
+    <ClCompile Include="JsonObject_Serialization_Tests.cpp" />
+    <ClCompile Include="JsonObject_Container_Tests.cpp" />
     <ClCompile Include="JsonValueTests.cpp" />
     <ClCompile Include="StaticJsonBufferTests.cpp" />
     <ClCompile Include="StringBuilderTests.cpp" />

+ 11 - 5
tests/tests.vcxproj.filters

@@ -27,19 +27,25 @@
     <ClCompile Include="StaticJsonBufferTests.cpp">
       <Filter>Source Files</Filter>
     </ClCompile>
-    <ClCompile Include="JsonObjectTests.cpp">
+    <ClCompile Include="JsonValueTests.cpp">
       <Filter>Source Files</Filter>
     </ClCompile>
-    <ClCompile Include="JsonValueTests.cpp">
+    <ClCompile Include="EscapedStringTests.cpp">
       <Filter>Source Files</Filter>
     </ClCompile>
-    <ClCompile Include="JsonObjectSerializationTests.cpp">
+    <ClCompile Include="StringBuilderTests.cpp">
       <Filter>Source Files</Filter>
     </ClCompile>
-    <ClCompile Include="EscapedStringTests.cpp">
+    <ClCompile Include="JsonArray_Serialization_Tests.cpp">
       <Filter>Source Files</Filter>
     </ClCompile>
-    <ClCompile Include="StringBuilderTests.cpp">
+    <ClCompile Include="JsonObject_Serialization_Tests.cpp">
+      <Filter>Source Files</Filter>
+    </ClCompile>
+    <ClCompile Include="JsonObject_Container_Tests.cpp">
+      <Filter>Source Files</Filter>
+    </ClCompile>
+    <ClCompile Include="JsonArray_Container_Tests.cpp">
       <Filter>Source Files</Filter>
     </ClCompile>
   </ItemGroup>