JsonObject_PrettyPrintTo_Tests.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. class JsonObject_PrettyPrintTo_Tests : public testing::Test {
  12. public:
  13. JsonObject_PrettyPrintTo_Tests() : object(json.createObject()) {}
  14. protected:
  15. StaticJsonBuffer<300> json;
  16. JsonObject &object;
  17. void outputMustBe(const char *expected) {
  18. size_t n = object.prettyPrintTo(buffer, sizeof(buffer));
  19. EXPECT_STREQ(expected, buffer);
  20. EXPECT_EQ(strlen(expected), n);
  21. }
  22. private:
  23. char buffer[256];
  24. };
  25. TEST_F(JsonObject_PrettyPrintTo_Tests, EmptyObject) { outputMustBe("{}"); }
  26. TEST_F(JsonObject_PrettyPrintTo_Tests, OneMember) {
  27. object["key"] = "value";
  28. outputMustBe(
  29. "{\r\n"
  30. " \"key\": \"value\"\r\n"
  31. "}");
  32. }
  33. TEST_F(JsonObject_PrettyPrintTo_Tests, TwoMembers) {
  34. object["key1"] = "value1";
  35. object["key2"] = "value2";
  36. outputMustBe(
  37. "{\r\n"
  38. " \"key1\": \"value1\",\r\n"
  39. " \"key2\": \"value2\"\r\n"
  40. "}");
  41. }
  42. TEST_F(JsonObject_PrettyPrintTo_Tests, EmptyNestedContainers) {
  43. object.createNestedObject("key1");
  44. object.createNestedArray("key2");
  45. outputMustBe(
  46. "{\r\n"
  47. " \"key1\": {},\r\n"
  48. " \"key2\": []\r\n"
  49. "}");
  50. }
  51. TEST_F(JsonObject_PrettyPrintTo_Tests, NestedContainers) {
  52. JsonObject &nested1 = object.createNestedObject("key1");
  53. nested1["a"] = 1;
  54. JsonArray &nested2 = object.createNestedArray("key2");
  55. nested2.add(2);
  56. outputMustBe(
  57. "{\r\n"
  58. " \"key1\": {\r\n"
  59. " \"a\": 1\r\n"
  60. " },\r\n"
  61. " \"key2\": [\r\n"
  62. " 2\r\n"
  63. " ]\r\n"
  64. "}");
  65. }