JsonObject_Container_Tests.cpp 2.8 KB

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