JsonObject_Subscript_Tests.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 <gtest/gtest.h>
  8. #include <ArduinoJson.h>
  9. class JsonObject_Subscript_Tests : public ::testing::Test {
  10. public:
  11. JsonObject_Subscript_Tests() : _object(_jsonBuffer.createObject()) {}
  12. protected:
  13. DynamicJsonBuffer _jsonBuffer;
  14. JsonObject& _object;
  15. };
  16. #define TEST_(name) TEST_F(JsonObject_Subscript_Tests, name)
  17. TEST_(SizeIncreased_WhenValuesAreAdded) {
  18. _object["hello"] = 1;
  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["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["hello"] = 123.45;
  34. EXPECT_TRUE(_object["hello"].is<double>());
  35. EXPECT_FALSE(_object["hello"].is<long>());
  36. EXPECT_EQ(123.45, _object["hello"].as<double>());
  37. }
  38. TEST_(StoreDoubleWithDigits) {
  39. _object["hello"].set(123.45, 2);
  40. EXPECT_TRUE(_object["hello"].is<double>());
  41. EXPECT_FALSE(_object["hello"].is<long>());
  42. EXPECT_EQ(123.45, _object["hello"].as<double>());
  43. }
  44. TEST_(StoreBoolean) {
  45. _object["hello"] = true;
  46. EXPECT_TRUE(_object["hello"].is<bool>());
  47. EXPECT_FALSE(_object["hello"].is<long>());
  48. EXPECT_TRUE(_object["hello"].as<bool>());
  49. }
  50. TEST_(StoreString) {
  51. _object["hello"] = "h3110";
  52. EXPECT_TRUE(_object["hello"].is<const char*>());
  53. EXPECT_FALSE(_object["hello"].is<long>());
  54. EXPECT_STREQ("h3110", _object["hello"].as<const char*>());
  55. EXPECT_STREQ("h3110", _object["hello"].as<char*>()); // <- short hand
  56. }
  57. TEST_(StoreArray) {
  58. JsonArray& arr = _jsonBuffer.createArray();
  59. _object["hello"] = arr;
  60. EXPECT_EQ(&arr, &_object["hello"].asArray()); // <- DEPRECATED
  61. EXPECT_EQ(&arr, &_object["hello"].as<JsonArray&>());
  62. EXPECT_EQ(&arr, &_object["hello"].as<JsonArray>()); // <- short hand
  63. EXPECT_EQ(&arr, &_object["hello"].as<const JsonArray&>());
  64. EXPECT_EQ(&arr, &_object["hello"].as<const JsonArray>()); // <- short hand
  65. EXPECT_TRUE(_object["hello"].is<JsonArray&>());
  66. EXPECT_TRUE(_object["hello"].is<JsonArray>());
  67. EXPECT_TRUE(_object["hello"].is<const JsonArray&>());
  68. EXPECT_TRUE(_object["hello"].is<const JsonArray>());
  69. EXPECT_FALSE(_object["hello"].is<JsonObject&>());
  70. }
  71. TEST_(StoreObject) {
  72. JsonObject& obj = _jsonBuffer.createObject();
  73. _object["hello"] = obj;
  74. EXPECT_EQ(&obj, &_object["hello"].asObject()); // DEPRECATED
  75. EXPECT_EQ(&obj, &_object["hello"].as<JsonObject&>());
  76. EXPECT_EQ(&obj, &_object["hello"].as<JsonObject>()); // <- short hand
  77. EXPECT_EQ(&obj, &_object["hello"].as<const JsonObject&>());
  78. EXPECT_EQ(&obj, &_object["hello"].as<const JsonObject>()); // <- short hand
  79. EXPECT_TRUE(_object["hello"].is<JsonObject&>());
  80. EXPECT_TRUE(_object["hello"].is<JsonObject>());
  81. EXPECT_TRUE(_object["hello"].is<const JsonObject&>());
  82. EXPECT_TRUE(_object["hello"].is<const JsonObject>());
  83. EXPECT_FALSE(_object["hello"].is<JsonArray&>());
  84. }
  85. TEST_(StoreArraySubscript) {
  86. JsonArray& arr = _jsonBuffer.createArray();
  87. arr.add(42);
  88. _object["a"] = arr[0];
  89. EXPECT_EQ(42, _object["a"]);
  90. }
  91. TEST_(StoreObjectSubscript) {
  92. JsonObject& obj = _jsonBuffer.createObject();
  93. obj.set("x", 42);
  94. _object["a"] = obj["x"];
  95. EXPECT_EQ(42, _object["a"]);
  96. }