2014-10-16 16:23:24 +02:00
|
|
|
#include <gtest/gtest.h>
|
2014-10-19 15:46:36 +02:00
|
|
|
#include <ArduinoJson/JsonArray.hpp>
|
|
|
|
#include <ArduinoJson/JsonObject.hpp>
|
|
|
|
#include <ArduinoJson/JsonValue.hpp>
|
|
|
|
#include <ArduinoJson/StaticJsonBuffer.hpp>
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-18 23:05:54 +02:00
|
|
|
using namespace ArduinoJson;
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
struct Person {
|
|
|
|
int id;
|
|
|
|
char name[32];
|
2014-10-16 16:23:24 +02:00
|
|
|
};
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
class Issue10 : public testing::Test {
|
2014-10-16 16:23:24 +02:00
|
|
|
protected:
|
2014-10-23 19:54:00 +02:00
|
|
|
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;
|
2014-10-16 16:23:24 +02:00
|
|
|
};
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(Issue10, PopulateArrayByAddingAnObject) {
|
|
|
|
JsonArray array = json.createArray();
|
|
|
|
|
|
|
|
for (int i = 0; i < 2; i++) {
|
|
|
|
JsonObject object = json.createObject();
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
object["id"] = persons[i].id;
|
|
|
|
object["name"] = persons[i].name;
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
array.add(object); // <- adds a reference to an existing objet (creates 2
|
|
|
|
// extra proxy nodes)
|
|
|
|
}
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
checkJsonString(array);
|
|
|
|
nodeCountMustBe(15);
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(Issue10, PopulateArrayByCreatingNestedObjects) {
|
|
|
|
JsonArray array = json.createArray();
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
for (int i = 0; i < 2; i++) {
|
|
|
|
JsonObject object = array.createNestedObject();
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
object["id"] = persons[i].id;
|
|
|
|
object["name"] = persons[i].name;
|
|
|
|
}
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
checkJsonString(array);
|
|
|
|
nodeCountMustBe(11);
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|