subscript.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 <stdint.h>
  9. #include <catch.hpp>
  10. TEST_CASE("JsonArray::operator[]") {
  11. DynamicJsonBuffer _jsonBuffer;
  12. JsonArray& _array = _jsonBuffer.createArray();
  13. _array.add(0);
  14. SECTION("SizeIsUnchanged") {
  15. _array[0] = "hello";
  16. REQUIRE(1U == _array.size());
  17. }
  18. SECTION("int") {
  19. _array[0] = 123;
  20. REQUIRE(123 == _array[0].as<int>());
  21. REQUIRE(true == _array[0].is<int>());
  22. REQUIRE(false == _array[0].is<bool>());
  23. }
  24. #if ARDUINOJSON_USE_LONG_LONG || ARDUINOJSON_USE_INT64
  25. SECTION("long long") {
  26. _array[0] = 9223372036854775807;
  27. REQUIRE(9223372036854775807 == _array[0].as<long long>());
  28. REQUIRE(true == _array[0].is<int>());
  29. REQUIRE(false == _array[0].is<bool>());
  30. }
  31. #endif
  32. SECTION("double") {
  33. _array[0] = 123.45;
  34. REQUIRE(123.45 == _array[0].as<double>());
  35. REQUIRE(true == _array[0].is<double>());
  36. REQUIRE(false == _array[0].is<int>());
  37. }
  38. SECTION("bool") {
  39. _array[0] = true;
  40. REQUIRE(true == _array[0].as<bool>());
  41. REQUIRE(true == _array[0].is<bool>());
  42. REQUIRE(false == _array[0].is<int>());
  43. }
  44. SECTION("const char*") {
  45. const char* str = "hello";
  46. _array[0] = str;
  47. REQUIRE(str == _array[0].as<const char*>());
  48. REQUIRE(str == _array[0].as<char*>()); // <- short hand
  49. REQUIRE(true == _array[0].is<const char*>());
  50. REQUIRE(false == _array[0].is<int>());
  51. }
  52. SECTION("nested array") {
  53. JsonArray& arr = _jsonBuffer.createArray();
  54. _array[0] = arr;
  55. REQUIRE(&arr == &_array[0].as<JsonArray&>());
  56. REQUIRE(&arr == &_array[0].as<JsonArray>()); // <- short hand
  57. REQUIRE(&arr == &_array[0].as<const JsonArray&>());
  58. REQUIRE(&arr == &_array[0].as<const JsonArray>()); // <- short hand
  59. REQUIRE(true == _array[0].is<JsonArray&>());
  60. REQUIRE(false == _array[0].is<int>());
  61. }
  62. SECTION("nested object") {
  63. JsonObject& obj = _jsonBuffer.createObject();
  64. _array[0] = obj;
  65. REQUIRE(&obj == &_array[0].as<JsonObject&>());
  66. REQUIRE(&obj == &_array[0].as<JsonObject>()); // <- short hand
  67. REQUIRE(&obj == &_array[0].as<const JsonObject&>());
  68. REQUIRE(&obj == &_array[0].as<const JsonObject>()); // <- short hand
  69. REQUIRE(true == _array[0].is<JsonObject&>());
  70. REQUIRE(false == _array[0].is<int>());
  71. }
  72. SECTION("array subscript") {
  73. JsonArray& arr = _jsonBuffer.createArray();
  74. const char* str = "hello";
  75. arr.add(str);
  76. _array[0] = arr[0];
  77. REQUIRE(str == _array[0]);
  78. }
  79. SECTION("object subscript") {
  80. JsonObject& obj = _jsonBuffer.createObject();
  81. const char* str = "hello";
  82. obj["x"] = str;
  83. _array[0] = obj["x"];
  84. REQUIRE(str == _array[0]);
  85. }
  86. }