Issue10.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Copyright Benoit Blanchon 2014-2017
  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. #include <ArduinoJson.h>
  8. #include <gtest/gtest.h>
  9. struct Person {
  10. int id;
  11. char name[32];
  12. };
  13. class Issue10 : public testing::Test {
  14. protected:
  15. virtual void SetUp() {
  16. Person boss;
  17. boss.id = 1;
  18. strcpy(boss.name, "Jeff");
  19. Person employee;
  20. employee.id = 2;
  21. strcpy(employee.name, "John");
  22. persons[0] = boss;
  23. persons[1] = employee;
  24. }
  25. template <typename T>
  26. void checkJsonString(const T &p) {
  27. char buffer[256];
  28. p.printTo(buffer, sizeof(buffer));
  29. EXPECT_STREQ("[{\"id\":1,\"name\":\"Jeff\"},{\"id\":2,\"name\":\"John\"}]",
  30. buffer);
  31. }
  32. StaticJsonBuffer<JSON_ARRAY_SIZE(2) + 2 * JSON_OBJECT_SIZE(2)> json;
  33. Person persons[2];
  34. };
  35. TEST_F(Issue10, PopulateArrayByAddingAnObject) {
  36. JsonArray &array = json.createArray();
  37. for (int i = 0; i < 2; i++) {
  38. JsonObject &object = json.createObject();
  39. object["id"] = persons[i].id;
  40. object["name"] = persons[i].name;
  41. array.add(object);
  42. }
  43. checkJsonString(array);
  44. }
  45. TEST_F(Issue10, PopulateArrayByCreatingNestedObjects) {
  46. JsonArray &array = json.createArray();
  47. for (int i = 0; i < 2; i++) {
  48. JsonObject &object = array.createNestedObject();
  49. object["id"] = persons[i].id;
  50. object["name"] = persons[i].name;
  51. }
  52. checkJsonString(array);
  53. }