subscript.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Copyright Benoit Blanchon 2014-2017
  2. // MIT License
  3. //
  4. // Arduino JSON library
  5. // https://bblanchon.github.io/ArduinoJson/
  6. // If you like this project, please add a star!
  7. #include <ArduinoJson.h>
  8. #include <catch.hpp>
  9. TEST_CASE("JsonObject::operator[]") {
  10. DynamicJsonBuffer _jsonBuffer;
  11. JsonObject& _object = _jsonBuffer.createObject();
  12. SECTION("SizeIncreased_WhenValuesAreAdded") {
  13. _object["hello"] = 1;
  14. REQUIRE(1 == _object.size());
  15. }
  16. SECTION("SizeUntouched_WhenSameValueIsAdded") {
  17. _object["hello"] = 1;
  18. _object["hello"] = 2;
  19. REQUIRE(1 == _object.size());
  20. }
  21. SECTION("StoreInteger") {
  22. _object["hello"] = 123;
  23. REQUIRE(123 == _object["hello"].as<int>());
  24. REQUIRE(true == _object["hello"].is<int>());
  25. REQUIRE(false == _object["hello"].is<double>());
  26. }
  27. SECTION("StoreVolatileInteger") { // issue #415
  28. volatile int i = 123;
  29. _object["hello"] = i;
  30. REQUIRE(123 == _object["hello"].as<int>());
  31. REQUIRE(true == _object["hello"].is<int>());
  32. REQUIRE(false == _object["hello"].is<double>());
  33. }
  34. SECTION("StoreDouble") {
  35. _object["hello"] = 123.45;
  36. REQUIRE(true == _object["hello"].is<double>());
  37. REQUIRE(false == _object["hello"].is<long>());
  38. REQUIRE(123.45 == _object["hello"].as<double>());
  39. }
  40. SECTION("StoreDoubleWithDigits") {
  41. _object["hello"].set(123.45, 2);
  42. REQUIRE(true == _object["hello"].is<double>());
  43. REQUIRE(false == _object["hello"].is<long>());
  44. REQUIRE(123.45 == _object["hello"].as<double>());
  45. }
  46. SECTION("StoreBoolean") {
  47. _object["hello"] = true;
  48. REQUIRE(true == _object["hello"].is<bool>());
  49. REQUIRE(false == _object["hello"].is<long>());
  50. REQUIRE(true == _object["hello"].as<bool>());
  51. }
  52. SECTION("StoreString") {
  53. _object["hello"] = "h3110";
  54. REQUIRE(true == _object["hello"].is<const char*>());
  55. REQUIRE(false == _object["hello"].is<long>());
  56. REQUIRE(std::string("h3110") == _object["hello"].as<const char*>());
  57. REQUIRE(std::string("h3110") ==
  58. _object["hello"].as<char*>()); // <- short hand
  59. }
  60. SECTION("StoreArray") {
  61. JsonArray& arr = _jsonBuffer.createArray();
  62. _object["hello"] = arr;
  63. REQUIRE(&arr == &_object["hello"].as<JsonArray&>());
  64. REQUIRE(&arr == &_object["hello"].as<JsonArray>()); // <- short hand
  65. REQUIRE(&arr == &_object["hello"].as<const JsonArray&>());
  66. REQUIRE(&arr == &_object["hello"].as<const JsonArray>()); // <- short hand
  67. REQUIRE(true == _object["hello"].is<JsonArray&>());
  68. REQUIRE(true == _object["hello"].is<JsonArray>());
  69. REQUIRE(true == _object["hello"].is<const JsonArray&>());
  70. REQUIRE(true == _object["hello"].is<const JsonArray>());
  71. REQUIRE(false == _object["hello"].is<JsonObject&>());
  72. }
  73. SECTION("StoreObject") {
  74. JsonObject& obj = _jsonBuffer.createObject();
  75. _object["hello"] = obj;
  76. REQUIRE(&obj == &_object["hello"].as<JsonObject&>());
  77. REQUIRE(&obj == &_object["hello"].as<JsonObject>()); // <- short hand
  78. REQUIRE(&obj == &_object["hello"].as<const JsonObject&>());
  79. REQUIRE(&obj == &_object["hello"].as<const JsonObject>()); // <- short hand
  80. REQUIRE(true == _object["hello"].is<JsonObject&>());
  81. REQUIRE(true == _object["hello"].is<JsonObject>());
  82. REQUIRE(true == _object["hello"].is<const JsonObject&>());
  83. REQUIRE(true == _object["hello"].is<const JsonObject>());
  84. REQUIRE(false == _object["hello"].is<JsonArray&>());
  85. }
  86. SECTION("StoreArraySubscript") {
  87. JsonArray& arr = _jsonBuffer.createArray();
  88. arr.add(42);
  89. _object["a"] = arr[0];
  90. REQUIRE(42 == _object["a"]);
  91. }
  92. SECTION("StoreObjectSubscript") {
  93. JsonObject& obj = _jsonBuffer.createObject();
  94. obj.set("x", 42);
  95. _object["a"] = obj["x"];
  96. REQUIRE(42 == _object["a"]);
  97. }
  98. SECTION("KeyAsCharArray") { // issue #423
  99. char key[] = "hello";
  100. _object[key] = 42;
  101. REQUIRE(42 == _object[key]);
  102. }
  103. }