or.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2022, Benoit BLANCHON
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. TEST_CASE("JsonVariant::operator|()") {
  7. DynamicJsonDocument doc(4096);
  8. JsonVariant variant = doc["value"].to<JsonVariant>();
  9. SECTION("null") {
  10. SECTION("null | const char*") {
  11. std::string result = variant | "default";
  12. REQUIRE(result == "default");
  13. }
  14. SECTION("null | int") {
  15. int result = variant | 42;
  16. REQUIRE(result == 42);
  17. }
  18. SECTION("null | bool") {
  19. bool result = variant | true;
  20. REQUIRE(result == true);
  21. }
  22. SECTION("null | ElementProxy") {
  23. doc["array"][0] = 42;
  24. JsonVariantConst result = variant | doc["array"][0];
  25. REQUIRE(result == 42);
  26. }
  27. SECTION("null | MemberProxy") {
  28. doc["other"] = 42;
  29. JsonVariantConst result = variant | doc["other"];
  30. REQUIRE(result == 42);
  31. }
  32. SECTION("ElementProxy | ElementProxy") {
  33. doc["array"][0] = 42;
  34. JsonVariantConst result = doc["array"][1] | doc["array"][0];
  35. REQUIRE(result == 42);
  36. }
  37. }
  38. SECTION("null") {
  39. variant.set(static_cast<const char*>(0));
  40. SECTION("null | const char*") {
  41. std::string result = variant | "default";
  42. REQUIRE(result == "default");
  43. }
  44. SECTION("null | int") {
  45. int result = variant | 42;
  46. REQUIRE(result == 42);
  47. }
  48. SECTION("null | bool") {
  49. bool result = variant | true;
  50. REQUIRE(result == true);
  51. }
  52. SECTION("null | ElementProxy") {
  53. doc["array"][0] = 42;
  54. JsonVariantConst result = variant | doc["array"][0];
  55. REQUIRE(result == 42);
  56. }
  57. SECTION("null | MemberProxy") {
  58. doc["other"] = 42;
  59. JsonVariantConst result = variant | doc["other"];
  60. REQUIRE(result == 42);
  61. }
  62. }
  63. SECTION("int | const char*") {
  64. variant.set(42);
  65. std::string result = variant | "default";
  66. REQUIRE(result == "default");
  67. }
  68. SECTION("int | uint8_t (out of range)") {
  69. variant.set(666);
  70. uint8_t result = variant | static_cast<uint8_t>(42);
  71. REQUIRE(result == 42);
  72. }
  73. SECTION("int | ElementProxy") {
  74. variant.set(42);
  75. doc["array"][0] = 666;
  76. JsonVariantConst result = variant | doc["array"][0];
  77. REQUIRE(result == 42);
  78. }
  79. SECTION("int | MemberProxy") {
  80. variant.set(42);
  81. doc["other"] = 666;
  82. JsonVariantConst result = variant | doc["other"];
  83. REQUIRE(result == 42);
  84. }
  85. SECTION("int | int") {
  86. variant.set(0);
  87. int result = variant | 666;
  88. REQUIRE(result == 0);
  89. }
  90. SECTION("double | int") {
  91. // NOTE: changed the behavior to fix #981
  92. variant.set(666.0);
  93. int result = variant | 42;
  94. REQUIRE(result == 42);
  95. }
  96. SECTION("bool | bool") {
  97. variant.set(false);
  98. bool result = variant | true;
  99. REQUIRE(result == false);
  100. }
  101. SECTION("int | bool") {
  102. variant.set(0);
  103. bool result = variant | true;
  104. REQUIRE(result == true);
  105. }
  106. SECTION("const char* | const char*") {
  107. variant.set("not default");
  108. std::string result = variant | "default";
  109. REQUIRE(result == "not default");
  110. }
  111. SECTION("const char* | char*") {
  112. char dflt[] = "default";
  113. variant.set("not default");
  114. std::string result = variant | dflt;
  115. REQUIRE(result == "not default");
  116. }
  117. SECTION("int | char*") {
  118. char dflt[] = "default";
  119. variant.set(42);
  120. std::string result = variant | dflt;
  121. REQUIRE(result == "default");
  122. }
  123. SECTION("const char* | int") {
  124. variant.set("not default");
  125. int result = variant | 42;
  126. REQUIRE(result == 42);
  127. }
  128. }