add.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2023
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. TEST_CASE("JsonArray::add()") {
  7. DynamicJsonBuffer _jsonBuffer;
  8. JsonArray& _array = _jsonBuffer.createArray();
  9. SECTION("int") {
  10. _array.add(123);
  11. REQUIRE(123 == _array[0].as<int>());
  12. REQUIRE(_array[0].is<int>());
  13. REQUIRE(_array[0].is<double>());
  14. }
  15. SECTION("double") {
  16. _array.add(123.45);
  17. REQUIRE(123.45 == _array[0].as<double>());
  18. REQUIRE(_array[0].is<double>());
  19. REQUIRE_FALSE(_array[0].is<bool>());
  20. }
  21. SECTION("bool") {
  22. _array.add(true);
  23. REQUIRE(true == _array[0].as<bool>());
  24. REQUIRE(_array[0].is<bool>());
  25. REQUIRE_FALSE(_array[0].is<int>());
  26. }
  27. SECTION("const char*") {
  28. const char* str = "hello";
  29. _array.add(str);
  30. REQUIRE(str == _array[0].as<std::string>());
  31. REQUIRE(_array[0].is<const char*>());
  32. REQUIRE_FALSE(_array[0].is<int>());
  33. }
  34. SECTION("nested array") {
  35. JsonArray& arr = _jsonBuffer.createArray();
  36. _array.add(arr);
  37. REQUIRE(&arr == &_array[0].as<JsonArray&>());
  38. REQUIRE(_array[0].is<JsonArray&>());
  39. REQUIRE_FALSE(_array[0].is<int>());
  40. }
  41. SECTION("nested object") {
  42. JsonObject& obj = _jsonBuffer.createObject();
  43. _array.add(obj);
  44. REQUIRE(&obj == &_array[0].as<JsonObject&>());
  45. REQUIRE(_array[0].is<JsonObject&>());
  46. REQUIRE_FALSE(_array[0].is<int>());
  47. }
  48. SECTION("array subscript") {
  49. const char* str = "hello";
  50. JsonArray& arr = _jsonBuffer.createArray();
  51. arr.add(str);
  52. _array.add(arr[0]);
  53. REQUIRE(str == _array[0]);
  54. }
  55. SECTION("object subscript") {
  56. const char* str = "hello";
  57. JsonObject& obj = _jsonBuffer.createObject();
  58. obj["x"] = str;
  59. _array.add(obj["x"]);
  60. REQUIRE(str == _array[0]);
  61. }
  62. SECTION("should not duplicate const char*") {
  63. _array.add("world");
  64. const size_t expectedSize = JSON_ARRAY_SIZE(1);
  65. REQUIRE(expectedSize == _jsonBuffer.size());
  66. }
  67. SECTION("should duplicate char*") {
  68. _array.add(const_cast<char*>("world"));
  69. const size_t expectedSize = JSON_ARRAY_SIZE(1) + 6;
  70. REQUIRE(expectedSize == _jsonBuffer.size());
  71. }
  72. SECTION("should duplicate std::string") {
  73. _array.add(std::string("world"));
  74. const size_t expectedSize = JSON_ARRAY_SIZE(1) + 6;
  75. REQUIRE(expectedSize == _jsonBuffer.size());
  76. }
  77. SECTION("should not duplicate RawJson(const char*)") {
  78. _array.add(RawJson("{}"));
  79. const size_t expectedSize = JSON_ARRAY_SIZE(1);
  80. REQUIRE(expectedSize == _jsonBuffer.size());
  81. }
  82. SECTION("should duplicate RawJson(char*)") {
  83. _array.add(RawJson(const_cast<char*>("{}")));
  84. const size_t expectedSize = JSON_ARRAY_SIZE(1) + 3;
  85. REQUIRE(expectedSize == _jsonBuffer.size());
  86. }
  87. }