subscript.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2018
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. TEST_CASE("JsonObject::operator[]") {
  7. DynamicJsonDocument doc;
  8. JsonObject& obj = doc.to<JsonObject>();
  9. SECTION("int") {
  10. obj["hello"] = 123;
  11. REQUIRE(123 == obj["hello"].as<int>());
  12. REQUIRE(true == obj["hello"].is<int>());
  13. REQUIRE(false == obj["hello"].is<bool>());
  14. }
  15. SECTION("volatile int") { // issue #415
  16. volatile int i = 123;
  17. obj["hello"] = i;
  18. REQUIRE(123 == obj["hello"].as<int>());
  19. REQUIRE(true == obj["hello"].is<int>());
  20. REQUIRE(false == obj["hello"].is<bool>());
  21. }
  22. SECTION("double") {
  23. obj["hello"] = 123.45;
  24. REQUIRE(true == obj["hello"].is<double>());
  25. REQUIRE(false == obj["hello"].is<long>());
  26. REQUIRE(123.45 == obj["hello"].as<double>());
  27. }
  28. SECTION("bool") {
  29. obj["hello"] = true;
  30. REQUIRE(true == obj["hello"].is<bool>());
  31. REQUIRE(false == obj["hello"].is<long>());
  32. REQUIRE(true == obj["hello"].as<bool>());
  33. }
  34. SECTION("const char*") {
  35. obj["hello"] = "h3110";
  36. REQUIRE(true == obj["hello"].is<const char*>());
  37. REQUIRE(false == obj["hello"].is<long>());
  38. REQUIRE(std::string("h3110") == obj["hello"].as<const char*>());
  39. REQUIRE(std::string("h3110") == obj["hello"].as<char*>()); // <- short hand
  40. }
  41. SECTION("array") {
  42. DynamicJsonDocument doc2;
  43. JsonArray& arr = doc2.to<JsonArray>();
  44. obj["hello"] = arr;
  45. REQUIRE(&arr == &obj["hello"].as<JsonArray&>());
  46. REQUIRE(&arr == &obj["hello"].as<JsonArray>()); // <- short hand
  47. REQUIRE(&arr == &obj["hello"].as<const JsonArray&>());
  48. REQUIRE(&arr == &obj["hello"].as<const JsonArray>()); // <- short hand
  49. REQUIRE(true == obj["hello"].is<JsonArray&>());
  50. REQUIRE(true == obj["hello"].is<JsonArray>());
  51. REQUIRE(true == obj["hello"].is<const JsonArray&>());
  52. REQUIRE(true == obj["hello"].is<const JsonArray>());
  53. REQUIRE(false == obj["hello"].is<JsonObject&>());
  54. }
  55. SECTION("object") {
  56. DynamicJsonDocument doc2;
  57. JsonObject& obj2 = doc2.to<JsonObject>();
  58. obj["hello"] = obj2;
  59. REQUIRE(&obj2 == &obj["hello"].as<JsonObject&>());
  60. REQUIRE(&obj2 == &obj["hello"].as<JsonObject>()); // <- short hand
  61. REQUIRE(&obj2 == &obj["hello"].as<const JsonObject&>());
  62. REQUIRE(&obj2 == &obj["hello"].as<const JsonObject>()); // <- short hand
  63. REQUIRE(true == obj["hello"].is<JsonObject&>());
  64. REQUIRE(true == obj["hello"].is<JsonObject>());
  65. REQUIRE(true == obj["hello"].is<const JsonObject&>());
  66. REQUIRE(true == obj["hello"].is<const JsonObject>());
  67. REQUIRE(false == obj["hello"].is<JsonArray&>());
  68. }
  69. SECTION("array subscript") {
  70. DynamicJsonDocument doc2;
  71. JsonArray& arr = doc2.to<JsonArray>();
  72. arr.add(42);
  73. obj["a"] = arr[0];
  74. REQUIRE(42 == obj["a"]);
  75. }
  76. SECTION("object subscript") {
  77. DynamicJsonDocument doc2;
  78. JsonObject& obj2 = doc2.to<JsonObject>();
  79. obj2.set("x", 42);
  80. obj["a"] = obj2["x"];
  81. REQUIRE(42 == obj["a"]);
  82. }
  83. SECTION("char key[]") { // issue #423
  84. char key[] = "hello";
  85. obj[key] = 42;
  86. REQUIRE(42 == obj[key]);
  87. }
  88. SECTION("should not duplicate const char*") {
  89. obj["hello"] = "world";
  90. const size_t expectedSize = JSON_OBJECT_SIZE(1);
  91. REQUIRE(expectedSize == doc.memoryUsage());
  92. }
  93. SECTION("should duplicate char* value") {
  94. obj["hello"] = const_cast<char*>("world");
  95. const size_t expectedSize = JSON_OBJECT_SIZE(1) + 6;
  96. REQUIRE(expectedSize == doc.memoryUsage());
  97. }
  98. SECTION("should duplicate char* key") {
  99. obj[const_cast<char*>("hello")] = "world";
  100. const size_t expectedSize = JSON_OBJECT_SIZE(1) + 6;
  101. REQUIRE(expectedSize == doc.memoryUsage());
  102. }
  103. SECTION("should duplicate char* key&value") {
  104. obj[const_cast<char*>("hello")] = const_cast<char*>("world");
  105. const size_t expectedSize = JSON_OBJECT_SIZE(1) + 12;
  106. REQUIRE(expectedSize <= doc.memoryUsage());
  107. }
  108. SECTION("should duplicate std::string value") {
  109. obj["hello"] = std::string("world");
  110. const size_t expectedSize = JSON_OBJECT_SIZE(1) + 6;
  111. REQUIRE(expectedSize == doc.memoryUsage());
  112. }
  113. SECTION("should duplicate std::string key") {
  114. obj[std::string("hello")] = "world";
  115. const size_t expectedSize = JSON_OBJECT_SIZE(1) + 6;
  116. REQUIRE(expectedSize == doc.memoryUsage());
  117. }
  118. SECTION("should duplicate std::string key&value") {
  119. obj[std::string("hello")] = std::string("world");
  120. const size_t expectedSize = JSON_OBJECT_SIZE(1) + 12;
  121. REQUIRE(expectedSize <= doc.memoryUsage());
  122. }
  123. }