Files
ArduinoJson/test/Issue10.cpp

72 lines
1.5 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);
}
2014-10-30 21:51:59 +01:00
StaticJsonBuffer<JSON_ARRAY_SIZE(2)+2*JSON_OBJECT_SIZE(2)> json;
2014-10-23 19:54:00 +02:00
Person persons[2];
2014-10-16 16:23:24 +02:00
};
2014-10-23 19:54:00 +02:00
TEST_F(Issue10, PopulateArrayByAddingAnObject) {
2014-10-29 21:18:27 +01:00
JsonArray &array = json.createArray();
2014-10-23 19:54:00 +02:00
for (int i = 0; i < 2; i++) {
2014-10-29 21:18:27 +01:00
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-30 21:51:59 +01:00
array.add(object);
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);
2014-10-16 16:23:24 +02:00
}
2014-10-23 19:54:00 +02:00
TEST_F(Issue10, PopulateArrayByCreatingNestedObjects) {
2014-10-29 21:18:27 +01:00
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++) {
2014-10-29 21:18:27 +01:00
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);
2014-10-16 16:23:24 +02:00
}