JsonObject_Container_Tests.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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/JsonValue.hpp>
  10. #include <ArduinoJson/StaticJsonBuffer.hpp>
  11. using namespace ArduinoJson;
  12. class JsonObject_Container_Tests : public ::testing::Test {
  13. public:
  14. JsonObject_Container_Tests() : object(json.createObject()) {}
  15. protected:
  16. StaticJsonBuffer<256> json;
  17. JsonObject& object;
  18. };
  19. TEST_F(JsonObject_Container_Tests, InitialSizeIsZero) {
  20. EXPECT_EQ(0, object.size());
  21. }
  22. TEST_F(JsonObject_Container_Tests, Grow_WhenValuesAreAdded) {
  23. object["hello"];
  24. EXPECT_EQ(1, object.size());
  25. object["world"];
  26. EXPECT_EQ(2, object.size());
  27. }
  28. TEST_F(JsonObject_Container_Tests, DoNotGrow_WhenSameValueIsAdded) {
  29. object["hello"];
  30. EXPECT_EQ(1, object.size());
  31. object["hello"];
  32. EXPECT_EQ(1, object.size());
  33. }
  34. TEST_F(JsonObject_Container_Tests, Shrink_WhenValuesAreRemoved) {
  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,
  43. DoNotShrink_WhenRemoveIsCalledWithAWrongKey) {
  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. object["hello"] = 123;
  51. object["world"] = 456;
  52. EXPECT_EQ(123, object["hello"].as<int>());
  53. EXPECT_EQ(456, object["world"].as<int>());
  54. }
  55. TEST_F(JsonObject_Container_Tests, CanStoreDoubles) {
  56. object["hello"] = 123.45;
  57. object["world"] = 456.78;
  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["world"] = false;
  64. EXPECT_TRUE(object["hello"].as<bool>());
  65. EXPECT_FALSE(object["world"].as<bool>());
  66. }
  67. TEST_F(JsonObject_Container_Tests, CanStoreStrings) {
  68. object["hello"] = "h3110";
  69. object["world"] = "w0r1d";
  70. EXPECT_STREQ("h3110", object["hello"].as<const char*>());
  71. EXPECT_STREQ("w0r1d", object["world"].as<const char*>());
  72. }
  73. TEST_F(JsonObject_Container_Tests, CanStoreInnerArrays) {
  74. JsonArray& innerarray1 = json.createArray();
  75. JsonArray& innerarray2 = json.createArray();
  76. object["hello"] = innerarray1;
  77. object["world"] = innerarray2;
  78. EXPECT_EQ(&innerarray1, &object["hello"].asArray());
  79. EXPECT_EQ(&innerarray2, &object["world"].asArray());
  80. }
  81. TEST_F(JsonObject_Container_Tests, CanStoreInnerObjects) {
  82. JsonObject& innerObject1 = json.createObject();
  83. JsonObject& innerObject2 = json.createObject();
  84. object["hello"] = innerObject1;
  85. object["world"] = innerObject2;
  86. EXPECT_EQ(&innerObject1, &object["hello"].asObject());
  87. EXPECT_EQ(&innerObject2, &object["world"].asObject());
  88. }