JsonObject_Container_Tests.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #include <gtest/gtest.h>
  2. #include <ArduinoJson/StaticJsonBuffer.hpp>
  3. #include <ArduinoJson/JsonValue.hpp>
  4. using namespace ArduinoJson;
  5. class JsonObject_Container_Tests : public ::testing::Test {
  6. protected:
  7. virtual void SetUp() { object = json.createObject(); }
  8. StaticJsonBuffer<42> json;
  9. JsonObject object;
  10. };
  11. TEST_F(JsonObject_Container_Tests, InitialSizeIsZero) {
  12. EXPECT_EQ(0, object.size());
  13. }
  14. TEST_F(JsonObject_Container_Tests, Grow_WhenValuesAreAdded) {
  15. object["hello"];
  16. EXPECT_EQ(1, object.size());
  17. object["world"];
  18. EXPECT_EQ(2, object.size());
  19. }
  20. TEST_F(JsonObject_Container_Tests, DoNotGrow_WhenSameValueIsAdded) {
  21. object["hello"];
  22. EXPECT_EQ(1, object.size());
  23. object["hello"];
  24. EXPECT_EQ(1, object.size());
  25. }
  26. TEST_F(JsonObject_Container_Tests, Shrink_WhenValuesAreRemoved) {
  27. object["hello"];
  28. object["world"];
  29. object.remove("hello");
  30. EXPECT_EQ(1, object.size());
  31. object.remove("world");
  32. EXPECT_EQ(0, object.size());
  33. }
  34. TEST_F(JsonObject_Container_Tests,
  35. DoNotShrink_WhenRemoveIsCalledWithAWrongKey) {
  36. object["hello"];
  37. object["world"];
  38. object.remove(":-P");
  39. EXPECT_EQ(2, object.size());
  40. }
  41. TEST_F(JsonObject_Container_Tests, CanStoreIntegers) {
  42. object["hello"] = 123;
  43. object["world"] = 456;
  44. EXPECT_EQ(123, (int)object["hello"]);
  45. EXPECT_EQ(456, (int)object["world"]);
  46. }
  47. TEST_F(JsonObject_Container_Tests, CanStoreDoubles) {
  48. object["hello"] = 123.45;
  49. object["world"] = 456.78;
  50. EXPECT_EQ(123.45, (double)object["hello"]);
  51. EXPECT_EQ(456.78, (double)object["world"]);
  52. }
  53. TEST_F(JsonObject_Container_Tests, CanStoreBooleans) {
  54. object["hello"] = true;
  55. object["world"] = false;
  56. EXPECT_TRUE((bool)object["hello"]);
  57. EXPECT_FALSE((bool)object["world"]);
  58. }
  59. TEST_F(JsonObject_Container_Tests, CanStoreStrings) {
  60. object["hello"] = "h3110";
  61. object["world"] = "w0r1d";
  62. EXPECT_STREQ("h3110", (const char *)object["hello"]);
  63. EXPECT_STREQ("w0r1d", (const char *)object["world"]);
  64. }
  65. TEST_F(JsonObject_Container_Tests, CanStoreInnerArrays) {
  66. JsonArray innerarray1 = json.createArray();
  67. JsonArray innerarray2 = json.createArray();
  68. object["hello"] = innerarray1;
  69. object["world"] = innerarray2;
  70. EXPECT_EQ(innerarray1, (JsonArray)object["hello"]);
  71. EXPECT_EQ(innerarray2, (JsonArray)object["world"]);
  72. }
  73. TEST_F(JsonObject_Container_Tests, CanStoreInnerObjects) {
  74. JsonObject innerObject1 = json.createObject();
  75. JsonObject innerObject2 = json.createObject();
  76. object["hello"] = innerObject1;
  77. object["world"] = innerObject2;
  78. EXPECT_EQ(innerObject1, (JsonObject)object["hello"]);
  79. EXPECT_EQ(innerObject2, (JsonObject)object["world"]);
  80. }