Skip to content

Commit

Permalink
SERVER-18016 added unit test assertion macro to check error code in e…
Browse files Browse the repository at this point in the history
…xception
  • Loading branch information
benety committed May 15, 2015
1 parent 463d334 commit f3d242c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/mongo/db/repl/task_runner_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
9 changes: 9 additions & 0 deletions src/mongo/unittest/unittest.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit f3d242c

Please sign in to comment.