JsonObject_Serialization_Tests.cpp 3.4 KB

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