JsonObject_Container_Tests.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #include <gtest/gtest.h>
  2. #include <ArduinoJson/StaticJsonBuffer.hpp>
  3. #include <ArduinoJson/JsonValue.hpp>
  4. using namespace ArduinoJson;
  5. class JsonObject_Container_Tests : public ::testing::Test
  6. {
  7. protected:
  8. virtual void SetUp()
  9. {
  10. object = json.createObject();
  11. }
  12. StaticJsonBuffer<42> json;
  13. JsonObject object;
  14. };
  15. TEST_F(JsonObject_Container_Tests, InitialSizeIsZero)
  16. {
  17. EXPECT_EQ(0, object.size());
  18. }
  19. TEST_F(JsonObject_Container_Tests, Grow_WhenValuesAreAdded)
  20. {
  21. object["hello"];
  22. EXPECT_EQ(1, object.size());
  23. object["world"];
  24. EXPECT_EQ(2, object.size());
  25. }
  26. TEST_F(JsonObject_Container_Tests, DoNotGrow_WhenSameValueIsAdded)
  27. {
  28. object["hello"];
  29. EXPECT_EQ(1, object.size());
  30. object["hello"];
  31. EXPECT_EQ(1, object.size());
  32. }
  33. TEST_F(JsonObject_Container_Tests, Shrink_WhenValuesAreRemoved)
  34. {
  35. object["hello"];
  36. object["world"];
  37. object.remove("hello");
  38. EXPECT_EQ(1, object.size());
  39. object.remove("world");
  40. EXPECT_EQ(0, object.size());
  41. }
  42. TEST_F(JsonObject_Container_Tests, DoNotShrink_WhenRemoveIsCalledWithAWrongKey)
  43. {
  44. object["hello"];
  45. object["world"];
  46. object.remove(":-P");
  47. EXPECT_EQ(2, object.size());
  48. }
  49. TEST_F(JsonObject_Container_Tests, CanStoreIntegers)
  50. {
  51. object["hello"] = 123;
  52. object["world"] = 456;
  53. EXPECT_EQ(123, (int) object["hello"]);
  54. EXPECT_EQ(456, (int) object["world"]);
  55. }
  56. TEST_F(JsonObject_Container_Tests, CanStoreDoubles)
  57. {
  58. object["hello"] = 123.45;
  59. object["world"] = 456.78;
  60. EXPECT_EQ(123.45, (double) object["hello"]);
  61. EXPECT_EQ(456.78, (double) object["world"]);
  62. }
  63. TEST_F(JsonObject_Container_Tests, CanStoreBooleans)
  64. {
  65. object["hello"] = true;
  66. object["world"] = false;
  67. EXPECT_TRUE((bool) object["hello"]);
  68. EXPECT_FALSE((bool) object["world"]);
  69. }
  70. TEST_F(JsonObject_Container_Tests, CanStoreStrings)
  71. {
  72. object["hello"] = "h3110";
  73. object["world"] = "w0r1d";
  74. EXPECT_STREQ("h3110", (const char*) object["hello"]);
  75. EXPECT_STREQ("w0r1d", (const char*) object["world"]);
  76. }
  77. TEST_F(JsonObject_Container_Tests, CanStoreInnerArrays)
  78. {
  79. JsonArray innerarray1 = json.createArray();
  80. JsonArray innerarray2 = json.createArray();
  81. object["hello"] = innerarray1;
  82. object["world"] = innerarray2;
  83. EXPECT_EQ(innerarray1, (JsonArray) object["hello"]);
  84. EXPECT_EQ(innerarray2, (JsonArray) object["world"]);
  85. }
  86. TEST_F(JsonObject_Container_Tests, CanStoreInnerObjects)
  87. {
  88. JsonObject innerObject1 = json.createObject();
  89. JsonObject innerObject2 = json.createObject();
  90. object["hello"] = innerObject1;
  91. object["world"] = innerObject2;
  92. EXPECT_EQ(innerObject1, (JsonObject) object["hello"]);
  93. EXPECT_EQ(innerObject2, (JsonObject) object["world"]);
  94. }