JsonObject_Container_Tests.cpp 3.2 KB

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