JsonObject_Container_Tests.cpp 2.8 KB

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