[209] | 1 | // Copyright Benoit Blanchon 2014-2016
|
---|
| 2 | // MIT License
|
---|
| 3 | //
|
---|
| 4 | // Arduino JSON library
|
---|
| 5 | // https://github.com/bblanchon/ArduinoJson
|
---|
| 6 | // If you like this project, please add a star!
|
---|
| 7 |
|
---|
| 8 | #include <gtest/gtest.h>
|
---|
| 9 | #include <ArduinoJson.h>
|
---|
| 10 |
|
---|
| 11 | struct Person {
|
---|
| 12 | int id;
|
---|
| 13 | char name[32];
|
---|
| 14 | };
|
---|
| 15 |
|
---|
| 16 | class Issue10 : public testing::Test {
|
---|
| 17 | protected:
|
---|
| 18 | virtual void SetUp() {
|
---|
| 19 | Person boss;
|
---|
| 20 | boss.id = 1;
|
---|
| 21 | strcpy(boss.name, "Jeff");
|
---|
| 22 | Person employee;
|
---|
| 23 | employee.id = 2;
|
---|
| 24 | strcpy(employee.name, "John");
|
---|
| 25 | persons[0] = boss;
|
---|
| 26 | persons[1] = employee;
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | template <typename T>
|
---|
| 30 | void checkJsonString(const T &p) {
|
---|
| 31 | char buffer[256];
|
---|
| 32 | p.printTo(buffer, sizeof(buffer));
|
---|
| 33 |
|
---|
| 34 | EXPECT_STREQ("[{\"id\":1,\"name\":\"Jeff\"},{\"id\":2,\"name\":\"John\"}]",
|
---|
| 35 | buffer);
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | StaticJsonBuffer<JSON_ARRAY_SIZE(2) + 2 * JSON_OBJECT_SIZE(2)> json;
|
---|
| 39 | Person persons[2];
|
---|
| 40 | };
|
---|
| 41 |
|
---|
| 42 | TEST_F(Issue10, PopulateArrayByAddingAnObject) {
|
---|
| 43 | JsonArray &array = json.createArray();
|
---|
| 44 |
|
---|
| 45 | for (int i = 0; i < 2; i++) {
|
---|
| 46 | JsonObject &object = json.createObject();
|
---|
| 47 |
|
---|
| 48 | object["id"] = persons[i].id;
|
---|
| 49 | object["name"] = persons[i].name;
|
---|
| 50 |
|
---|
| 51 | array.add(object);
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | checkJsonString(array);
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | TEST_F(Issue10, PopulateArrayByCreatingNestedObjects) {
|
---|
| 58 | JsonArray &array = json.createArray();
|
---|
| 59 |
|
---|
| 60 | for (int i = 0; i < 2; i++) {
|
---|
| 61 | JsonObject &object = array.createNestedObject();
|
---|
| 62 |
|
---|
| 63 | object["id"] = persons[i].id;
|
---|
| 64 | object["name"] = persons[i].name;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | checkJsonString(array);
|
---|
| 68 | }
|
---|