Files
ArduinoJson/test/Issue10.cpp

77 lines
1.7 KiB
C++
Raw Normal View History

2014-10-23 23:39:22 +02:00
// Copyright Benoit Blanchon 2014
// MIT License
//
// Arduino JSON library
// https://github.com/bblanchon/ArduinoJson
2014-10-16 16:23:24 +02:00
#include <gtest/gtest.h>
#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 {
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;
}
2014-10-27 22:50:50 +01:00
void checkJsonString(JsonPrintable &p) {
2014-10-23 19:54:00 +02:00
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
array.add(object); // <- adds a reference to an existing objet (creates 2
// extra proxy nodes)
2014-10-23 19:54:00 +02:00
}
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
}