Benoit Blanchon пре 11 година
родитељ
комит
10c0a8ba70
4 измењених фајлова са 87 додато и 1 уклоњено
  1. 1 1
      srcs/JsonArray.cpp
  2. 80 0
      tests/Issue10.cpp
  3. 3 0
      tests/tests.vcxproj
  4. 3 0
      tests/tests.vcxproj.filters

+ 1 - 1
srcs/JsonArray.cpp

@@ -56,7 +56,7 @@ void JsonArray::add(JsonContainer nestedContainer)
     JsonNode* node = createNode();
     if (!node) return;
 
-    *node = *nestedContainer._node;
+    node->duplicate(nestedContainer._node);
     addChild(node);
 }
 

+ 80 - 0
tests/Issue10.cpp

@@ -0,0 +1,80 @@
+#include <gtest/gtest.h>
+#include <JsonArray.h>
+#include <JsonObject.h>
+#include <JsonValue.h>
+#include <StaticJsonBuffer.h>
+
+using namespace ArduinoJson::Generator;
+
+struct Person 
+{
+    int id;
+    char name[32];
+};
+
+class Issue10 : public testing::Test 
+{
+protected:
+
+    virtual void SetUp()
+    {
+        Person boss;
+        boss.id = 1;
+        strcpy(boss.name, "Jeff");
+        Person employee;
+        employee.id = 2;
+        strcpy(employee.name, "John");
+        persons[0] = boss;
+        persons[1] = employee;
+    }
+
+    void checkJsonString(JsonContainer& p)
+    {
+        char buffer[256];
+        p.printTo(buffer, sizeof(buffer));
+
+        EXPECT_STREQ("[{\"id\":1,\"name\":\"Jeff\"},{\"id\":2,\"name\":\"John\"}]", buffer);
+    }
+
+    void nodeCountMustBe(int expected)
+    {
+        EXPECT_EQ(expected, json.size());
+    }
+
+    Person persons[2];  
+    StaticJsonBuffer<20> json;    
+};
+
+TEST_F(Issue10, PopulateArrayByAddingAnObject)
+{
+    JsonArray array= json.createArray();
+
+    for (int i = 0; i < 2; i++)
+    {
+        JsonObject object = json.createObject();
+        
+        object["id"] = persons[i].id;
+        object["name"] = persons[i].name;
+
+        array.add(object); // <- adds a reference to an existing objet (creates 2 extra proxy nodes)
+    }
+
+    checkJsonString(array);
+    nodeCountMustBe(15);
+}
+
+TEST_F(Issue10, PopulateArrayByCreatingNestedObjects)
+{
+    JsonArray array = json.createArray();
+
+    for (int i = 0; i < 2; i++) 
+    {
+        JsonObject object = array.createNestedObject();
+
+        object["id"] = persons[i].id;
+        object["name"] = persons[i].name;
+    }
+
+    checkJsonString(array);
+    nodeCountMustBe(11);
+}

+ 3 - 0
tests/tests.vcxproj

@@ -58,6 +58,7 @@
       <Optimization>Disabled</Optimization>
       <PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
       <AdditionalIncludeDirectories>$(SolutionDir)\srcs;$(SolutionDir)\third-party\gtest-1.7.0;$(SolutionDir)\third-party\gtest-1.7.0\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
+      <SDLCheck>false</SDLCheck>
     </ClCompile>
     <Link>
       <SubSystem>Console</SubSystem>
@@ -74,6 +75,7 @@
       <IntrinsicFunctions>true</IntrinsicFunctions>
       <PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
       <AdditionalIncludeDirectories>$(SolutionDir)\srcs;$(SolutionDir)\third-party\gtest-1.7.0;$(SolutionDir)\third-party\gtest-1.7.0\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
+      <SDLCheck>false</SDLCheck>
     </ClCompile>
     <Link>
       <SubSystem>Console</SubSystem>
@@ -86,6 +88,7 @@
     <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="Issue10.cpp" />
     <ClCompile Include="JsonArray_Container_Tests.cpp" />
     <ClCompile Include="JsonArray_PrettyPrintTo_Tests.cpp" />
     <ClCompile Include="JsonArray_PrintTo_Tests.cpp" />

+ 3 - 0
tests/tests.vcxproj.filters

@@ -54,5 +54,8 @@
     <ClCompile Include="JsonObject_PrettyPrintTo_Tests.cpp">
       <Filter>Source Files</Filter>
     </ClCompile>
+    <ClCompile Include="Issue10.cpp">
+      <Filter>Source Files</Filter>
+    </ClCompile>
   </ItemGroup>
 </Project>