subscript.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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("StoreInteger") {
  19. _array[0] = 123;
  20. REQUIRE(123 == _array[0].as<int>());
  21. REQUIRE(true == _array[0].is<int>());
  22. REQUIRE(false == _array[0].is<double>());
  23. }
  24. #if ARDUINOJSON_USE_LONG_LONG || ARDUINOJSON_USE_INT64
  25. SECTION("StoreLongLong") {
  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<double>());
  30. }
  31. #endif
  32. SECTION("StoreDouble") {
  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("StoreDoubleWithDecimals") {
  39. _array[0].set(123.45, 2);
  40. REQUIRE(123.45 == _array[0].as<double>());
  41. REQUIRE(true == _array[0].is<double>());
  42. REQUIRE(false == _array[0].is<int>());
  43. }
  44. SECTION("StoreBoolean") {
  45. _array[0] = true;
  46. REQUIRE(true == _array[0].as<bool>());
  47. REQUIRE(true == _array[0].is<bool>());
  48. REQUIRE(false == _array[0].is<int>());
  49. }
  50. SECTION("StoreString") {
  51. const char* str = "hello";
  52. _array[0] = str;
  53. REQUIRE(str == _array[0].as<const char*>());
  54. REQUIRE(str == _array[0].as<char*>()); // <- short hand
  55. REQUIRE(true == _array[0].is<const char*>());
  56. REQUIRE(false == _array[0].is<int>());
  57. }
  58. SECTION("StoreNestedArray") {
  59. JsonArray& arr = _jsonBuffer.createArray();
  60. _array[0] = arr;
  61. REQUIRE(&arr == &_array[0].as<JsonArray&>());
  62. REQUIRE(&arr == &_array[0].as<JsonArray>()); // <- short hand
  63. REQUIRE(&arr == &_array[0].as<const JsonArray&>());
  64. REQUIRE(&arr == &_array[0].as<const JsonArray>()); // <- short hand
  65. REQUIRE(true == _array[0].is<JsonArray&>());
  66. REQUIRE(false == _array[0].is<int>());
  67. }
  68. SECTION("StoreNestedObject") {
  69. JsonObject& obj = _jsonBuffer.createObject();
  70. _array[0] = obj;
  71. REQUIRE(&obj == &_array[0].as<JsonObject&>());
  72. REQUIRE(&obj == &_array[0].as<JsonObject>()); // <- short hand
  73. REQUIRE(&obj == &_array[0].as<const JsonObject&>());
  74. REQUIRE(&obj == &_array[0].as<const JsonObject>()); // <- short hand
  75. REQUIRE(true == _array[0].is<JsonObject&>());
  76. REQUIRE(false == _array[0].is<int>());
  77. }
  78. SECTION("StoreArraySubscript") {
  79. JsonArray& arr = _jsonBuffer.createArray();
  80. const char* str = "hello";
  81. arr.add(str);
  82. _array[0] = arr[0];
  83. REQUIRE(str == _array[0]);
  84. }
  85. SECTION("StoreObjectSubscript") {
  86. JsonObject& obj = _jsonBuffer.createObject();
  87. const char* str = "hello";
  88. obj["x"] = str;
  89. _array[0] = obj["x"];
  90. REQUIRE(str == _array[0]);
  91. }
  92. }