JsonObject_PrettyPrintTo_Tests.cpp 1.7 KB

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