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/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. void checkJsonString(JsonPrintable &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. }