Issue10.cpp 1.5 KB

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