JsonObject_Serialization_Tests.cpp 3.2 KB

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