JsonObject_PrintTo_Tests.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Copyright Benoit Blanchon 2014-2015
  2. // MIT License
  3. //
  4. // Arduino JSON library
  5. // https://github.com/bblanchon/ArduinoJson
  6. #include <gtest/gtest.h>
  7. #include <ArduinoJson.h>
  8. using namespace ArduinoJson::Internals;
  9. class JsonObject_PrintTo_Tests : public testing::Test {
  10. public:
  11. JsonObject_PrintTo_Tests() : object(json.createObject()) {}
  12. protected:
  13. void outputMustBe(const char *expected) {
  14. char actual[256];
  15. int result = object.printTo(actual, sizeof(actual));
  16. EXPECT_STREQ(expected, actual);
  17. EXPECT_EQ(strlen(expected), result);
  18. }
  19. StaticJsonBuffer<JSON_OBJECT_SIZE(2)> json;
  20. JsonObject &object;
  21. };
  22. TEST_F(JsonObject_PrintTo_Tests, EmptyObject) { outputMustBe("{}"); }
  23. TEST_F(JsonObject_PrintTo_Tests, OneString) {
  24. object["key"] = "value";
  25. outputMustBe("{\"key\":\"value\"}");
  26. }
  27. TEST_F(JsonObject_PrintTo_Tests, TwoStrings) {
  28. object["key1"] = "value1";
  29. object["key2"] = "value2";
  30. outputMustBe("{\"key1\":\"value1\",\"key2\":\"value2\"}");
  31. }
  32. TEST_F(JsonObject_PrintTo_Tests, RemoveFirst) {
  33. object["key1"] = "value1";
  34. object["key2"] = "value2";
  35. object.remove("key1");
  36. outputMustBe("{\"key2\":\"value2\"}");
  37. }
  38. TEST_F(JsonObject_PrintTo_Tests, RemoveLast) {
  39. object["key1"] = "value1";
  40. object["key2"] = "value2";
  41. object.remove("key2");
  42. outputMustBe("{\"key1\":\"value1\"}");
  43. }
  44. TEST_F(JsonObject_PrintTo_Tests, RemoveUnexistingKey) {
  45. object["key1"] = "value1";
  46. object["key2"] = "value2";
  47. object.remove("key3");
  48. outputMustBe("{\"key1\":\"value1\",\"key2\":\"value2\"}");
  49. }
  50. TEST_F(JsonObject_PrintTo_Tests, ReplaceExistingKey) {
  51. object["key"] = "value1";
  52. object["key"] = "value2";
  53. outputMustBe("{\"key\":\"value2\"}");
  54. }
  55. TEST_F(JsonObject_PrintTo_Tests, OneStringOverCapacity) {
  56. object["key1"] = "value1";
  57. object["key2"] = "value2";
  58. object["key3"] = "value3";
  59. outputMustBe("{\"key1\":\"value1\",\"key2\":\"value2\"}");
  60. }
  61. TEST_F(JsonObject_PrintTo_Tests, OneInteger) {
  62. object["key"] = 1;
  63. outputMustBe("{\"key\":1}");
  64. }
  65. TEST_F(JsonObject_PrintTo_Tests, OneDoubleFourDigits) {
  66. object["key"].set(3.14159265358979323846, 4);
  67. outputMustBe("{\"key\":3.1416}");
  68. }
  69. TEST_F(JsonObject_PrintTo_Tests, OneDoubleDefaultDigits) {
  70. object["key"] = 3.14159265358979323846;
  71. outputMustBe("{\"key\":3.14}");
  72. }
  73. TEST_F(JsonObject_PrintTo_Tests, OneNull) {
  74. object["key"] = static_cast<char *>(0);
  75. outputMustBe("{\"key\":null}");
  76. }
  77. TEST_F(JsonObject_PrintTo_Tests, OneTrue) {
  78. object["key"] = true;
  79. outputMustBe("{\"key\":true}");
  80. }
  81. TEST_F(JsonObject_PrintTo_Tests, OneFalse) {
  82. object["key"] = false;
  83. outputMustBe("{\"key\":false}");
  84. }
  85. TEST_F(JsonObject_PrintTo_Tests, OneEmptyNestedArrayViaProxy) {
  86. JsonArray &nestedArray = json.createArray();
  87. object["key"] = nestedArray;
  88. outputMustBe("{\"key\":[]}");
  89. }
  90. TEST_F(JsonObject_PrintTo_Tests, OneEmptyNestedObjectViaProxy) {
  91. JsonObject &nestedArray = json.createObject();
  92. object["key"] = nestedArray;
  93. outputMustBe("{\"key\":{}}");
  94. }
  95. TEST_F(JsonObject_PrintTo_Tests, OneEmptyNestedObject) {
  96. object.createNestedObject("key");
  97. outputMustBe("{\"key\":{}}");
  98. }
  99. TEST_F(JsonObject_PrintTo_Tests, OneEmptyNestedArray) {
  100. object.createNestedArray("key");
  101. outputMustBe("{\"key\":[]}");
  102. }