JsonObject_PrintTo_Tests.cpp 3.3 KB

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