From 10c0a8ba70cd9118656afa2be8495f26ffc65e10 Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Mon, 13 Oct 2014 17:56:16 +0200 Subject: [PATCH] Ported tests of issue #10 --- srcs/JsonArray.cpp | 2 +- tests/Issue10.cpp | 80 +++++++++++++++++++++++++++++++++++++ tests/tests.vcxproj | 3 ++ tests/tests.vcxproj.filters | 3 ++ 4 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 tests/Issue10.cpp diff --git a/srcs/JsonArray.cpp b/srcs/JsonArray.cpp index 57a94f14..bd7d2dd1 100644 --- a/srcs/JsonArray.cpp +++ b/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); } diff --git a/tests/Issue10.cpp b/tests/Issue10.cpp new file mode 100644 index 00000000..e958f1f2 --- /dev/null +++ b/tests/Issue10.cpp @@ -0,0 +1,80 @@ +#include +#include +#include +#include +#include + +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); +} diff --git a/tests/tests.vcxproj b/tests/tests.vcxproj index 25945158..931e03d0 100644 --- a/tests/tests.vcxproj +++ b/tests/tests.vcxproj @@ -58,6 +58,7 @@ Disabled WIN32;_DEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions) $(SolutionDir)\srcs;$(SolutionDir)\third-party\gtest-1.7.0;$(SolutionDir)\third-party\gtest-1.7.0\include;%(AdditionalIncludeDirectories) + false Console @@ -74,6 +75,7 @@ true WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions) $(SolutionDir)\srcs;$(SolutionDir)\third-party\gtest-1.7.0;$(SolutionDir)\third-party\gtest-1.7.0\include;%(AdditionalIncludeDirectories) + false Console @@ -86,6 +88,7 @@ + diff --git a/tests/tests.vcxproj.filters b/tests/tests.vcxproj.filters index 31240bc6..ccbc587c 100644 --- a/tests/tests.vcxproj.filters +++ b/tests/tests.vcxproj.filters @@ -54,5 +54,8 @@ Source Files + + Source Files + \ No newline at end of file