kuhnel updated this revision to Diff 355507. kuhnel marked 4 inline comments as done. kuhnel added a comment.
updated code examples based on Sam's review Oh my, this is really simple if you know how it's supposed to work. However my intuition is completely off in trying to understand the error handling. I hope the examples help others avoid the pain. Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D105014/new/ https://reviews.llvm.org/D105014 Files: llvm/include/llvm/Testing/Support/Error.h Index: llvm/include/llvm/Testing/Support/Error.h =================================================================== --- llvm/include/llvm/Testing/Support/Error.h +++ llvm/include/llvm/Testing/Support/Error.h @@ -165,26 +165,24 @@ #define ASSERT_THAT_ERROR(Err, Matcher) \ ASSERT_THAT(llvm::detail::TakeError(Err), Matcher) -/// Helper marcro for checking the result of an 'Expected<T>' +/// Helper macro for checking the result of an 'Expected<T>' /// /// @code{.cpp} /// // function to be tested /// Expected<int> myDivide(int A, int B); /// /// TEST(myDivideTests, GoodAndBad) { -/// // test the good care -/// auto D1 = myDivide(10, 5); -/// // ensure the Error gets consumed in case the function fails by -/// // calling 'toString()'. This also helps in debugging failing tests. -/// EXPECT_THAT_EXPECTED(D1, Succeeded()) << toString(D1.takeError()) << -/// "\n"; -/// EXPECT_THAT(*D1, Eq(2)); +/// // test good case +/// // if you only care about successor failure: +/// EXPECT_THAT_EXPECTED(myDivide(10, 5), Succeeded()); +/// // if you also care about the value: +/// EXPECT_THAT_EXPECTED(myDivide(10, 5), HasValue(2)); /// /// // test the error case -/// auto D2 = myDivide(10, 0); -/// EXPECT_THAT_EXPECTED(D2, Failed()); -/// // In the error case we need to consume the error. -/// consumeError(D2.takeError()); +/// EXPECT_THAT_EXPECTED(myDivide(10, 0), Failed()); +/// // also check the error message +/// EXPECT_THAT_EXPECTED(myDivide(10, 0), +/// FailedWithMessage("B must not be zero!")); /// } /// @endcode
Index: llvm/include/llvm/Testing/Support/Error.h =================================================================== --- llvm/include/llvm/Testing/Support/Error.h +++ llvm/include/llvm/Testing/Support/Error.h @@ -165,26 +165,24 @@ #define ASSERT_THAT_ERROR(Err, Matcher) \ ASSERT_THAT(llvm::detail::TakeError(Err), Matcher) -/// Helper marcro for checking the result of an 'Expected<T>' +/// Helper macro for checking the result of an 'Expected<T>' /// /// @code{.cpp} /// // function to be tested /// Expected<int> myDivide(int A, int B); /// /// TEST(myDivideTests, GoodAndBad) { -/// // test the good care -/// auto D1 = myDivide(10, 5); -/// // ensure the Error gets consumed in case the function fails by -/// // calling 'toString()'. This also helps in debugging failing tests. -/// EXPECT_THAT_EXPECTED(D1, Succeeded()) << toString(D1.takeError()) << -/// "\n"; -/// EXPECT_THAT(*D1, Eq(2)); +/// // test good case +/// // if you only care about successor failure: +/// EXPECT_THAT_EXPECTED(myDivide(10, 5), Succeeded()); +/// // if you also care about the value: +/// EXPECT_THAT_EXPECTED(myDivide(10, 5), HasValue(2)); /// /// // test the error case -/// auto D2 = myDivide(10, 0); -/// EXPECT_THAT_EXPECTED(D2, Failed()); -/// // In the error case we need to consume the error. -/// consumeError(D2.takeError()); +/// EXPECT_THAT_EXPECTED(myDivide(10, 0), Failed()); +/// // also check the error message +/// EXPECT_THAT_EXPECTED(myDivide(10, 0), +/// FailedWithMessage("B must not be zero!")); /// } /// @endcode
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits