observers.cpp 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. #include <catch2/catch.hpp>
  2. #include <tl/expected.hpp>
  3. struct move_detector {
  4. move_detector() = default;
  5. move_detector(move_detector &&rhs) { rhs.been_moved = true; }
  6. bool been_moved = false;
  7. };
  8. TEST_CASE("Observers", "[observers]") {
  9. tl::expected<int,int> o1 = 42;
  10. tl::expected<int,int> o2 {tl::unexpect, 0};
  11. const tl::expected<int,int> o3 = 42;
  12. REQUIRE(*o1 == 42);
  13. REQUIRE(*o1 == o1.value());
  14. REQUIRE(o2.value_or(42) == 42);
  15. REQUIRE(o2.error() == 0);
  16. REQUIRE(o3.value() == 42);
  17. auto success = std::is_same<decltype(o1.value()), int &>::value;
  18. REQUIRE(success);
  19. success = std::is_same<decltype(o3.value()), const int &>::value;
  20. REQUIRE(success);
  21. success = std::is_same<decltype(std::move(o1).value()), int &&>::value;
  22. REQUIRE(success);
  23. #ifndef TL_EXPECTED_NO_CONSTRR
  24. success = std::is_same<decltype(std::move(o3).value()), const int &&>::value;
  25. REQUIRE(success);
  26. #endif
  27. tl::expected<move_detector,int> o4{tl::in_place};
  28. move_detector o5 = std::move(o4).value();
  29. REQUIRE(o4->been_moved);
  30. REQUIRE(!o5.been_moved);
  31. }