Skip to content

Commit

Permalink
Ported tests of issue #10
Browse files Browse the repository at this point in the history
  • Loading branch information
bblanchon committed Oct 13, 2014
1 parent d192a14 commit 10c0a8b
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 1 deletion.
2 changes: 1 addition & 1 deletion srcs/JsonArray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ void JsonArray::add(JsonContainer nestedContainer)
JsonNode* node = createNode();
if (!node) return;

*node = *nestedContainer._node;
node->duplicate(nestedContainer._node);
addChild(node);
}

Expand Down
80 changes: 80 additions & 0 deletions tests/Issue10.cpp
Original file line number Diff line number Diff line change
@@ -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 changes: 3 additions & 0 deletions tests/tests.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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>
Expand All @@ -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" />
Expand Down
3 changes: 3 additions & 0 deletions tests/tests.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -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>

0 comments on commit 10c0a8b

Please sign in to comment.