JsonObjectTests.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #include <gtest/gtest.h>
  2. #include <StaticJsonBuffer.h>
  3. #include <JsonValue.h>
  4. class JsonObjectTests : 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(JsonObjectTests, Grow_WhenValuesAreAdded)
  15. {
  16. object["hello"];
  17. EXPECT_EQ(1, object.size());
  18. object["world"];
  19. EXPECT_EQ(2, object.size());
  20. }
  21. TEST_F(JsonObjectTests, DoNotGrow_WhenSameValueIsAdded)
  22. {
  23. object["hello"];
  24. EXPECT_EQ(1, object.size());
  25. object["hello"];
  26. EXPECT_EQ(1, object.size());
  27. }
  28. TEST_F(JsonObjectTests, Shrink_WhenValuesAreRemoved)
  29. {
  30. object["hello"];
  31. object["world"];
  32. object.remove("hello");
  33. EXPECT_EQ(1, object.size());
  34. object.remove("world");
  35. EXPECT_EQ(0, object.size());
  36. }
  37. TEST_F(JsonObjectTests, CanStoreIntegers)
  38. {
  39. object["hello"] = 123;
  40. object["world"] = 456;
  41. EXPECT_EQ(123, (int) object["hello"]);
  42. EXPECT_EQ(456, (int) object["world"]);
  43. }
  44. TEST_F(JsonObjectTests, CanStoreDoubles)
  45. {
  46. object["hello"] = 123.45;
  47. object["world"] = 456.78;
  48. EXPECT_EQ(123.45, (double) object["hello"]);
  49. EXPECT_EQ(456.78, (double) object["world"]);
  50. }
  51. TEST_F(JsonObjectTests, CanStoreBooleans)
  52. {
  53. object["hello"] = true;
  54. object["world"] = false;
  55. EXPECT_TRUE((bool) object["hello"]);
  56. EXPECT_FALSE((bool) object["world"]);
  57. }
  58. TEST_F(JsonObjectTests, CanStoreStrings)
  59. {
  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(JsonObjectTests, CanStoreInnerObjects)
  66. {
  67. JsonObject innerObject1 = json.createObject();
  68. JsonObject innerObject2 = json.createObject();
  69. object["hello"] = innerObject1;
  70. object["world"] = innerObject2;
  71. EXPECT_EQ(innerObject1, (JsonObject) object["hello"]);
  72. EXPECT_EQ(innerObject2, (JsonObject) object["world"]);
  73. }