subscript.cpp 4.9 KB

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