Issue10.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Copyright Benoit Blanchon 2014
  2. // MIT License
  3. //
  4. // Arduino JSON library
  5. // https://github.com/bblanchon/ArduinoJson
  6. #include <gtest/gtest.h>
  7. #include <ArduinoJson/JsonArray.hpp>
  8. #include <ArduinoJson/JsonObject.hpp>
  9. #include <ArduinoJson/JsonValue.hpp>
  10. #include <ArduinoJson/StaticJsonBuffer.hpp>
  11. using namespace ArduinoJson;
  12. struct Person {
  13. int id;
  14. char name[32];
  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. template <typename T>
  29. void checkJsonString(const T &p) {
  30. char buffer[256];
  31. p.printTo(buffer, sizeof(buffer));
  32. EXPECT_STREQ("[{\"id\":1,\"name\":\"Jeff\"},{\"id\":2,\"name\":\"John\"}]",
  33. buffer);
  34. }
  35. StaticJsonBuffer<JSON_ARRAY_SIZE(2) + 2 * JSON_OBJECT_SIZE(2)> json;
  36. Person persons[2];
  37. };
  38. TEST_F(Issue10, PopulateArrayByAddingAnObject) {
  39. JsonArray &array = json.createArray();
  40. for (int i = 0; i < 2; i++) {
  41. JsonObject &object = json.createObject();
  42. object["id"] = persons[i].id;
  43. object["name"] = persons[i].name;
  44. array.add(object);
  45. }
  46. checkJsonString(array);
  47. }
  48. TEST_F(Issue10, PopulateArrayByCreatingNestedObjects) {
  49. JsonArray &array = json.createArray();
  50. for (int i = 0; i < 2; i++) {
  51. JsonObject &object = array.createNestedObject();
  52. object["id"] = persons[i].id;
  53. object["name"] = persons[i].name;
  54. }
  55. checkJsonString(array);
  56. }