Ported tests of issue #10

This commit is contained in:
Benoit Blanchon
2014-10-13 17:56:16 +02:00
parent d192a14e2e
commit 10c0a8ba70
4 changed files with 87 additions and 1 deletions

View File

@ -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
tests/Issue10.cpp Normal file
View File

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

View File

@ -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" />

View File

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