JsonObject_PrintTo_Tests.cpp 3.3 KB

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