JsonObject_Container_Tests.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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"] = 1;
  20. EXPECT_EQ(1, _object.size());
  21. _object.set("world", 2);
  22. EXPECT_EQ(2, _object.size());
  23. }
  24. TEST_F(JsonObject_Container_Tests, DoNotGrow_WhenSameValueIsAdded) {
  25. _object["hello"] = 1;
  26. EXPECT_EQ(1, _object.size());
  27. _object["hello"] = 2;
  28. EXPECT_EQ(1, _object.size());
  29. }
  30. TEST_F(JsonObject_Container_Tests, Shrink_WhenValuesAreRemoved) {
  31. _object["hello"] = 1;
  32. _object["world"] = 2;
  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"] = 1;
  41. _object["world"] = 2;
  42. _object.remove(":-P");
  43. EXPECT_EQ(2, _object.size());
  44. }
  45. TEST_F(JsonObject_Container_Tests, CanStoreIntegers) {
  46. _object["hello"] = 123;
  47. _object.set("world", 456);
  48. EXPECT_TRUE(_object["hello"].is<int>());
  49. EXPECT_FALSE(_object["hello"].is<double>());
  50. EXPECT_EQ(123, _object["hello"].as<int>());
  51. EXPECT_EQ(456, _object["world"].as<int>());
  52. }
  53. TEST_F(JsonObject_Container_Tests, CanStoreDoubles) {
  54. _object["hello"] = 123.45;
  55. _object.set("world", 456.78);
  56. EXPECT_TRUE(_object["hello"].is<double>());
  57. EXPECT_FALSE(_object["hello"].is<long>());
  58. EXPECT_EQ(123.45, _object["hello"].as<double>());
  59. EXPECT_EQ(456.78, _object["world"].as<double>());
  60. }
  61. TEST_F(JsonObject_Container_Tests, CanStoreBooleans) {
  62. _object["hello"] = true;
  63. _object.set("world", false);
  64. EXPECT_TRUE(_object["hello"].is<bool>());
  65. EXPECT_FALSE(_object["hello"].is<long>());
  66. EXPECT_TRUE(_object["hello"].as<bool>());
  67. EXPECT_FALSE(_object["world"].as<bool>());
  68. }
  69. TEST_F(JsonObject_Container_Tests, CanStoreStrings) {
  70. _object["hello"] = "h3110";
  71. _object.set("world", "w0r1d");
  72. EXPECT_TRUE(_object["hello"].is<const char*>());
  73. EXPECT_FALSE(_object["hello"].is<long>());
  74. EXPECT_STREQ("h3110", _object["hello"].as<const char*>());
  75. EXPECT_STREQ("w0r1d", _object["world"].as<const char*>());
  76. }
  77. TEST_F(JsonObject_Container_Tests, CanStoreArrays) {
  78. JsonArray& array1 = _jsonBuffer.createArray();
  79. JsonArray& array2 = _jsonBuffer.createArray();
  80. _object["hello"] = array1;
  81. _object.set("world", array2);
  82. EXPECT_TRUE(_object["hello"].is<JsonArray&>());
  83. EXPECT_FALSE(_object["hello"].is<JsonObject&>());
  84. EXPECT_EQ(&array1, &_object["hello"].asArray());
  85. EXPECT_EQ(&array2, &_object["world"].asArray());
  86. }
  87. TEST_F(JsonObject_Container_Tests, CanStoreObjects) {
  88. JsonObject& object1 = _jsonBuffer.createObject();
  89. JsonObject& object2 = _jsonBuffer.createObject();
  90. _object["hello"] = object1;
  91. _object.set("world", object2);
  92. EXPECT_TRUE(_object["hello"].is<JsonObject&>());
  93. EXPECT_FALSE(_object["hello"].is<JsonArray&>());
  94. EXPECT_EQ(&object1, &_object["hello"].asObject());
  95. EXPECT_EQ(&object2, &_object["world"].asObject());
  96. }
  97. TEST_F(JsonObject_Container_Tests, ContainsKeyReturnsFalseForNonExistingKey) {
  98. EXPECT_FALSE(_object.containsKey("hello"));
  99. }
  100. TEST_F(JsonObject_Container_Tests, ContainsKeyReturnsTrueForDefinedValue) {
  101. _object.set("hello", 42);
  102. EXPECT_TRUE(_object.containsKey("hello"));
  103. }