JsonObject_PrettyPrintTo_Tests.cpp 1.7 KB

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