JsonObject_Set_Tests.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Copyright Benoit Blanchon 2014-2016
  2. // MIT License
  3. //
  4. // Arduino JSON library
  5. // https://github.com/bblanchon/ArduinoJson
  6. // If you like this project, please add a star!
  7. #include <ArduinoJson.h>
  8. #include <gtest/gtest.h>
  9. class JsonObject_Set_Tests : public ::testing::Test {
  10. public:
  11. JsonObject_Set_Tests() : _object(_jsonBuffer.createObject()) {}
  12. protected:
  13. DynamicJsonBuffer _jsonBuffer;
  14. JsonObject& _object;
  15. };
  16. #define TEST_(name) TEST_F(JsonObject_Set_Tests, name)
  17. TEST_(SizeIncreased_WhenValuesAreAdded) {
  18. _object.set("hello", 42);
  19. EXPECT_EQ(1, _object.size());
  20. }
  21. TEST_(SizeUntouched_WhenSameValueIsAdded) {
  22. _object["hello"] = 1;
  23. _object["hello"] = 2;
  24. EXPECT_EQ(1, _object.size());
  25. }
  26. TEST_(StoreInteger) {
  27. _object.set("hello", 123);
  28. EXPECT_EQ(123, _object["hello"].as<int>());
  29. EXPECT_TRUE(_object["hello"].is<int>());
  30. EXPECT_FALSE(_object["hello"].is<double>());
  31. }
  32. TEST_(StoreDouble) {
  33. _object.set("hello", 123.45);
  34. EXPECT_EQ(123.45, _object["hello"].as<double>());
  35. EXPECT_TRUE(_object["hello"].is<double>());
  36. EXPECT_FALSE(_object["hello"].is<long>());
  37. }
  38. TEST_(StoreDoubleWithDigits) {
  39. _object.set("hello", 123.45, 2);
  40. EXPECT_EQ(123.45, _object["hello"].as<double>());
  41. EXPECT_TRUE(_object["hello"].is<double>());
  42. EXPECT_FALSE(_object["hello"].is<long>());
  43. }
  44. TEST_(StoreBoolean) {
  45. _object.set("hello", true);
  46. EXPECT_TRUE(_object["hello"].as<bool>());
  47. EXPECT_TRUE(_object["hello"].is<bool>());
  48. EXPECT_FALSE(_object["hello"].is<long>());
  49. }
  50. TEST_(StoreString) {
  51. _object.set("hello", "h3110");
  52. EXPECT_STREQ("h3110", _object["hello"].as<const char*>());
  53. EXPECT_TRUE(_object["hello"].is<const char*>());
  54. EXPECT_FALSE(_object["hello"].is<long>());
  55. }
  56. TEST_(StoreArray) {
  57. JsonArray& arr = _jsonBuffer.createArray();
  58. _object.set("hello", arr);
  59. EXPECT_EQ(&arr, &_object["hello"].asArray());
  60. EXPECT_TRUE(_object["hello"].is<JsonArray&>());
  61. EXPECT_FALSE(_object["hello"].is<JsonObject&>());
  62. }
  63. TEST_(StoreObject) {
  64. JsonObject& obj = _jsonBuffer.createObject();
  65. _object.set("hello", obj);
  66. EXPECT_EQ(&obj, &_object["hello"].asObject());
  67. EXPECT_TRUE(_object["hello"].is<JsonObject&>());
  68. EXPECT_FALSE(_object["hello"].is<JsonArray&>());
  69. }
  70. TEST_(StoreArraySubscript) {
  71. JsonArray& arr = _jsonBuffer.createArray();
  72. arr.add(42);
  73. _object.set("a", arr[0]);
  74. EXPECT_EQ(42, _object["a"]);
  75. }
  76. TEST_(StoreObjectSubscript) {
  77. JsonObject& obj = _jsonBuffer.createObject();
  78. obj.set("x", 42);
  79. _object.set("a", obj["x"]);
  80. EXPECT_EQ(42, _object["a"]);
  81. }
  82. TEST_(ShouldReturnTrue_WhenAllocationSucceeds) {
  83. StaticJsonBuffer<JSON_OBJECT_SIZE(1) + 15> jsonBuffer;
  84. JsonObject& obj = jsonBuffer.createObject();
  85. bool result = obj.set(std::string("hello"), std::string("world"));
  86. ASSERT_TRUE(result);
  87. }
  88. TEST_(ShouldReturnFalse_WhenAllocationFails) {
  89. StaticJsonBuffer<JSON_OBJECT_SIZE(1) + 10> jsonBuffer;
  90. JsonObject& obj = jsonBuffer.createObject();
  91. bool result = obj.set(std::string("hello"), std::string("world"));
  92. ASSERT_FALSE(result);
  93. }