subscript.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2017
  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("SizeIncreased_WhenValuesAreAdded") {
  10. _object["hello"] = 1;
  11. REQUIRE(1 == _object.size());
  12. }
  13. SECTION("SizeUntouched_WhenSameValueIsAdded") {
  14. _object["hello"] = 1;
  15. _object["hello"] = 2;
  16. REQUIRE(1 == _object.size());
  17. }
  18. SECTION("int") {
  19. _object["hello"] = 123;
  20. REQUIRE(123 == _object["hello"].as<int>());
  21. REQUIRE(true == _object["hello"].is<int>());
  22. REQUIRE(false == _object["hello"].is<bool>());
  23. }
  24. SECTION("volatile int") { // issue #415
  25. volatile int i = 123;
  26. _object["hello"] = i;
  27. REQUIRE(123 == _object["hello"].as<int>());
  28. REQUIRE(true == _object["hello"].is<int>());
  29. REQUIRE(false == _object["hello"].is<bool>());
  30. }
  31. SECTION("double") {
  32. _object["hello"] = 123.45;
  33. REQUIRE(true == _object["hello"].is<double>());
  34. REQUIRE(false == _object["hello"].is<long>());
  35. REQUIRE(123.45 == _object["hello"].as<double>());
  36. }
  37. SECTION("bool") {
  38. _object["hello"] = true;
  39. REQUIRE(true == _object["hello"].is<bool>());
  40. REQUIRE(false == _object["hello"].is<long>());
  41. REQUIRE(true == _object["hello"].as<bool>());
  42. }
  43. SECTION("const char*") {
  44. _object["hello"] = "h3110";
  45. REQUIRE(true == _object["hello"].is<const char*>());
  46. REQUIRE(false == _object["hello"].is<long>());
  47. REQUIRE(std::string("h3110") == _object["hello"].as<const char*>());
  48. REQUIRE(std::string("h3110") ==
  49. _object["hello"].as<char*>()); // <- short hand
  50. }
  51. SECTION("array") {
  52. JsonArray& arr = _jsonBuffer.createArray();
  53. _object["hello"] = arr;
  54. REQUIRE(&arr == &_object["hello"].as<JsonArray&>());
  55. REQUIRE(&arr == &_object["hello"].as<JsonArray>()); // <- short hand
  56. REQUIRE(&arr == &_object["hello"].as<const JsonArray&>());
  57. REQUIRE(&arr == &_object["hello"].as<const JsonArray>()); // <- short hand
  58. REQUIRE(true == _object["hello"].is<JsonArray&>());
  59. REQUIRE(true == _object["hello"].is<JsonArray>());
  60. REQUIRE(true == _object["hello"].is<const JsonArray&>());
  61. REQUIRE(true == _object["hello"].is<const JsonArray>());
  62. REQUIRE(false == _object["hello"].is<JsonObject&>());
  63. }
  64. SECTION("object") {
  65. JsonObject& obj = _jsonBuffer.createObject();
  66. _object["hello"] = obj;
  67. REQUIRE(&obj == &_object["hello"].as<JsonObject&>());
  68. REQUIRE(&obj == &_object["hello"].as<JsonObject>()); // <- short hand
  69. REQUIRE(&obj == &_object["hello"].as<const JsonObject&>());
  70. REQUIRE(&obj == &_object["hello"].as<const JsonObject>()); // <- short hand
  71. REQUIRE(true == _object["hello"].is<JsonObject&>());
  72. REQUIRE(true == _object["hello"].is<JsonObject>());
  73. REQUIRE(true == _object["hello"].is<const JsonObject&>());
  74. REQUIRE(true == _object["hello"].is<const JsonObject>());
  75. REQUIRE(false == _object["hello"].is<JsonArray&>());
  76. }
  77. SECTION("array subscript") {
  78. JsonArray& arr = _jsonBuffer.createArray();
  79. arr.add(42);
  80. _object["a"] = arr[0];
  81. REQUIRE(42 == _object["a"]);
  82. }
  83. SECTION("object subscript") {
  84. JsonObject& obj = _jsonBuffer.createObject();
  85. obj.set("x", 42);
  86. _object["a"] = obj["x"];
  87. REQUIRE(42 == _object["a"]);
  88. }
  89. SECTION("KeyAsCharArray") { // issue #423
  90. char key[] = "hello";
  91. _object[key] = 42;
  92. REQUIRE(42 == _object[key]);
  93. }
  94. }