JsonObject_Container_Tests.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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.h>
  8. class JsonObject_Container_Tests : public ::testing::Test {
  9. public:
  10. JsonObject_Container_Tests() : object(json.createObject()) {}
  11. protected:
  12. StaticJsonBuffer<256> json;
  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 = json.createArray();
  71. JsonArray& innerarray2 = json.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 = json.createObject();
  79. JsonObject& innerObject2 = json.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. }