Issue10.cpp 1.4 KB

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