JsonObject_Container_Tests.cpp 2.9 KB

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