or.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 | int") {
  44. variant.set(0);
  45. int result = variant | 666;
  46. REQUIRE(result == 0);
  47. }
  48. SECTION("double | int") {
  49. variant.set(42.0);
  50. int result = variant | 666;
  51. REQUIRE(result == 42);
  52. }
  53. SECTION("bool | bool") {
  54. variant.set(false);
  55. bool result = variant | true;
  56. REQUIRE(result == false);
  57. }
  58. SECTION("int | bool") {
  59. variant.set(0);
  60. bool result = variant | true;
  61. REQUIRE(result == true);
  62. }
  63. SECTION("const char* | const char*") {
  64. variant.set("not default");
  65. std::string result = variant | "default";
  66. REQUIRE(result == "not default");
  67. }
  68. SECTION("const char* | int") {
  69. variant.set("not default");
  70. int result = variant | 42;
  71. REQUIRE(result == 42);
  72. }
  73. }