or.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2019
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. TEST_CASE("JsonVariant::operator|()") {
  7. DynamicJsonDocument doc(4096);
  8. JsonVariant variant = doc.to<JsonVariant>();
  9. SECTION("undefined") {
  10. SECTION("undefined | const char*") {
  11. std::string result = variant | "default";
  12. REQUIRE(result == "default");
  13. }
  14. SECTION("undefined | int") {
  15. int result = variant | 42;
  16. REQUIRE(result == 42);
  17. }
  18. SECTION("undefined | bool") {
  19. bool result = variant | true;
  20. REQUIRE(result == true);
  21. }
  22. }
  23. SECTION("null") {
  24. variant.set(static_cast<const char*>(0));
  25. SECTION("null | const char*") {
  26. std::string result = variant | "default";
  27. REQUIRE(result == "default");
  28. }
  29. SECTION("null | int") {
  30. int result = variant | 42;
  31. REQUIRE(result == 42);
  32. }
  33. SECTION("null | bool") {
  34. bool result = variant | true;
  35. REQUIRE(result == true);
  36. }
  37. }
  38. SECTION("int | const char*") {
  39. variant.set(42);
  40. std::string result = variant | "default";
  41. REQUIRE(result == "default");
  42. }
  43. SECTION("int | uint8_t (out of range)") {
  44. variant.set(666);
  45. uint8_t result = variant | static_cast<uint8_t>(42);
  46. REQUIRE(result == 42);
  47. }
  48. SECTION("int | int") {
  49. variant.set(0);
  50. int result = variant | 666;
  51. REQUIRE(result == 0);
  52. }
  53. SECTION("double | int") {
  54. // NOTE: changed the behavior to fix #981
  55. variant.set(666.0);
  56. int result = variant | 42;
  57. REQUIRE(result == 42);
  58. }
  59. SECTION("bool | bool") {
  60. variant.set(false);
  61. bool result = variant | true;
  62. REQUIRE(result == false);
  63. }
  64. SECTION("int | bool") {
  65. variant.set(0);
  66. bool result = variant | true;
  67. REQUIRE(result == true);
  68. }
  69. SECTION("const char* | const char*") {
  70. variant.set("not default");
  71. std::string result = variant | "default";
  72. REQUIRE(result == "not default");
  73. }
  74. SECTION("const char* | int") {
  75. variant.set("not default");
  76. int result = variant | 42;
  77. REQUIRE(result == 42);
  78. }
  79. }