From f3d242c808ec6c1b7145dd3f084da438829552cb Mon Sep 17 00:00:00 2001 From: Benety Goh Date: Wed, 13 May 2015 16:16:26 -0400 Subject: SERVER-18016 added unit test assertion macro to check error code in exception --- src/mongo/db/repl/task_runner_test.cpp | 10 ++++++---- src/mongo/unittest/unittest.h | 9 +++++++++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/mongo/db/repl/task_runner_test.cpp b/src/mongo/db/repl/task_runner_test.cpp index 38166cf706f..7de153bb227 100644 --- a/src/mongo/db/repl/task_runner_test.cpp +++ b/src/mongo/db/repl/task_runner_test.cpp @@ -45,12 +45,14 @@ namespace { TEST_F(TaskRunnerTest, InvalidConstruction) { // Null thread pool. - ASSERT_THROWS(TaskRunner(nullptr, []() -> OperationContext* { return nullptr; }), - UserException); + ASSERT_THROWS_CODE(TaskRunner(nullptr, []() -> OperationContext* { return nullptr; }), + UserException, + ErrorCodes::BadValue); // Null function for creating operation contexts. - ASSERT_THROWS(TaskRunner(&getThreadPool(), TaskRunner::CreateOperationContextFn()), - UserException); + ASSERT_THROWS_CODE(TaskRunner(&getThreadPool(), TaskRunner::CreateOperationContextFn()), + UserException, + ErrorCodes::BadValue); } TEST_F(TaskRunnerTest, GetDiagnosticString) { diff --git a/src/mongo/unittest/unittest.h b/src/mongo/unittest/unittest.h index b5f6f3eb850..e22f76c5f1d 100644 --- a/src/mongo/unittest/unittest.h +++ b/src/mongo/unittest/unittest.h @@ -134,6 +134,15 @@ ::mongo::stdx::bind(&EXCEPTION_TYPE::what, \ ::mongo::stdx::placeholders::_1))) +/** + * Behaves like ASSERT_THROWS, above, but also fails if calling getCode() on the thrown exception + * does not return an error code equal to EXPECTED_CODE. + */ +#define ASSERT_THROWS_CODE(STATEMENT, EXCEPTION_TYPE, EXPECTED_CODE) \ + ASSERT_THROWS_PRED(STATEMENT, \ + EXCEPTION_TYPE, \ + ([](const EXCEPTION_TYPE& ex) { return (EXPECTED_CODE) == ex.getCode(); })) + /** * Behaves like ASSERT_THROWS, above, but also fails if PREDICATE(ex) for the throw exception, ex, * is false. -- cgit v1.2.1