deprecated.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2023
  3. // MIT License
  4. #define ARDUINOJSON_ENABLE_DEPRECATED 1
  5. #include <ArduinoJson.h>
  6. #include <catch.hpp>
  7. #if defined(__clang__)
  8. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  9. #elif defined(__GNUC__)
  10. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  11. #elif defined(_MSC_VER)
  12. #pragma warning(disable : 4996)
  13. #endif
  14. TEST_CASE("Deprecated functions") {
  15. DynamicJsonBuffer jsonBuffer;
  16. SECTION("JsonVariant::asArray()") {
  17. JsonVariant variant = jsonBuffer.createArray();
  18. REQUIRE(variant.asArray().success());
  19. }
  20. SECTION("JsonVariant::asObject()") {
  21. JsonVariant variant = jsonBuffer.createObject();
  22. REQUIRE(variant.asObject().success());
  23. }
  24. SECTION("JsonVariant::asString()") {
  25. JsonVariant variant = "hello";
  26. REQUIRE(std::string("hello") == variant.asString());
  27. }
  28. SECTION("JsonArray::removeAt()") {
  29. JsonArray& arr = jsonBuffer.createArray();
  30. arr.removeAt(0);
  31. }
  32. SECTION("JsonVariant::JsonVariant(float, uint8_t)") {
  33. JsonVariant variant(3.14f, 2);
  34. REQUIRE(variant == 3.14f);
  35. }
  36. SECTION("JsonVariant::JsonVariant(double, uint8_t)") {
  37. JsonVariant variant(3.14, 2);
  38. REQUIRE(variant == 3.14);
  39. }
  40. SECTION("float_with_n_digits()") {
  41. JsonVariant variant = float_with_n_digits(3.14f, 4);
  42. REQUIRE(variant == 3.14f);
  43. }
  44. SECTION("double_with_n_digits()") {
  45. JsonVariant variant = double_with_n_digits(3.14f, 4);
  46. REQUIRE(variant == 3.14f);
  47. }
  48. SECTION("JsonArraySubscript::set(double, uint8_t)") {
  49. JsonArray& arr = jsonBuffer.createArray();
  50. arr.add(666);
  51. arr[0].set(123.45, 2);
  52. REQUIRE(123.45 == arr[0].as<double>());
  53. REQUIRE(true == arr[0].is<double>());
  54. REQUIRE(false == arr[0].is<int>());
  55. }
  56. SECTION("JsonArray::add(double, uint8_t)") {
  57. JsonArray& arr = jsonBuffer.createArray();
  58. arr.add(3.14159265358979323846, 4);
  59. }
  60. SECTION("JsonArray::add(float, uint8_t)") {
  61. JsonArray& arr = jsonBuffer.createArray();
  62. arr.add(3.14159265358979323846f, 4);
  63. }
  64. SECTION("JsonObject::set(unsigned char[], double, uint8_t)") {
  65. unsigned char key[] = "hello";
  66. JsonObject& obj = jsonBuffer.createObject();
  67. obj.set(key, 3.14, 2);
  68. REQUIRE(3.14 == obj["hello"]);
  69. }
  70. SECTION("JsonObject::set(const char*, double, uint8_t)") {
  71. JsonObject& obj = jsonBuffer.createObject();
  72. obj.set("hello", 123.45, 2);
  73. REQUIRE(123.45 == obj["hello"].as<double>());
  74. REQUIRE(obj["hello"].is<double>());
  75. REQUIRE_FALSE(obj["hello"].is<long>());
  76. }
  77. SECTION("JsonObjectSubscript::set(double, uint8_t)") {
  78. JsonObject& obj = jsonBuffer.createObject();
  79. obj["hello"].set(123.45, 2);
  80. REQUIRE(true == obj["hello"].is<double>());
  81. REQUIRE(false == obj["hello"].is<long>());
  82. REQUIRE(123.45 == obj["hello"].as<double>());
  83. }
  84. }
  85. TEST_CASE("DynamicJsonBuffer::strdup()") {
  86. DynamicJsonBuffer buffer;
  87. SECTION("char*") {
  88. char original[] = "hello";
  89. const char* copy = buffer.strdup(original);
  90. strcpy(original, "world");
  91. REQUIRE(std::string("hello") == copy);
  92. }
  93. SECTION("unsigned char*") {
  94. unsigned char value[] = "world";
  95. DynamicJsonBuffer jsonBuffer;
  96. const char* dup = jsonBuffer.strdup(value);
  97. REQUIRE(static_cast<const void*>(value) != static_cast<const void*>(dup));
  98. REQUIRE(std::string("world") == dup);
  99. }
  100. SECTION("std::string") {
  101. std::string original("hello");
  102. const char* copy = buffer.strdup(original);
  103. original[0] = 'w';
  104. REQUIRE(std::string("hello") == copy);
  105. }
  106. SECTION("NULL") {
  107. const char* original = NULL;
  108. const char* copy = buffer.strdup(original);
  109. REQUIRE(0 == copy);
  110. }
  111. }