JsonObject_PrettyPrintTo_Tests.cpp 1.8 KB

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