JsonObject_PrettyPrintTo_Tests.cpp 1.8 KB

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