JsonObject_Container_Tests.cpp 3.2 KB

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