Drake
Drake C++ Documentation
expect_throws_message.h File Reference
#include <regex>
#include <stdexcept>
#include <string>
#include "drake/common/drake_assert.h"
Include dependency graph for expect_throws_message.h:

Macros

#define DRAKE_EXPECT_THROWS_MESSAGE(expression, regexp)
 Unit test helper macro for "expecting" an exception to be thrown but also testing the error message against a provided regular expression. More...
 
#define DRAKE_ASSERT_THROWS_MESSAGE(expression, regexp)
 Fatal error version of DRAKE_EXPECT_THROWS_MESSAGE. More...
 
#define DRAKE_EXPECT_THROWS_MESSAGE_IF_ARMED(expression, regexp)
 Same as DRAKE_EXPECT_THROWS_MESSAGE in Debug builds, but doesn't require a throw in Release builds. More...
 
#define DRAKE_ASSERT_THROWS_MESSAGE_IF_ARMED(expression, regexp)
 Same as DRAKE_ASSERT_THROWS_MESSAGE in Debug builds, but doesn't require a throw in Release builds. More...
 

Macro Definition Documentation

◆ DRAKE_ASSERT_THROWS_MESSAGE

#define DRAKE_ASSERT_THROWS_MESSAGE (   expression,
  regexp 
)

Fatal error version of DRAKE_EXPECT_THROWS_MESSAGE.

See also
DRAKE_EXPECT_THROWS_MESSAGE

◆ DRAKE_ASSERT_THROWS_MESSAGE_IF_ARMED

#define DRAKE_ASSERT_THROWS_MESSAGE_IF_ARMED (   expression,
  regexp 
)

Same as DRAKE_ASSERT_THROWS_MESSAGE in Debug builds, but doesn't require a throw in Release builds.

However, if the Release build does throw it must throw the right message. More precisely, the thrown message is required whenever DRAKE_ENABLE_ASSERTS is defined, which Debug builds do by default.

See also
DRAKE_ASSERT_THROWS_MESSAGE

◆ DRAKE_EXPECT_THROWS_MESSAGE

#define DRAKE_EXPECT_THROWS_MESSAGE (   expression,
  regexp 
)

Unit test helper macro for "expecting" an exception to be thrown but also testing the error message against a provided regular expression.

This is like GTest's EXPECT_THROW but does not allow for tested the exception subtype (because checking the message should suffice on its own). Usage example:

StatementUnderTest(), // You expect this statement to throw ...
".*some important.*phrases.*that must appear.*"); // ... this message.

The regular expression must match the entire error message. If there is boilerplate you don't care to match at the beginning and end, surround with .* to ignore in single-line messages or [\s\S]* for multiline messages.

Following GTest's conventions, failure to perform as expected here is a non-fatal test error. An ASSERT variant is provided to make it fatal. There are also *_IF_ARMED variants. These require an exception in Debug builds (or any builds where DRAKE_ENABLE_ASSERTS has been defined). In Release builds, the expression will pass if it doesn't throw or if it throws an exception that would pass the same test as in Debug builds. There is no mechanism for testing exclusive throwing behavior (i.e., only throws in Debug).

See also
DRAKE_ASSERT_THROWS_MESSAGE
DRAKE_EXPECT_THROWS_MESSAGE_IF_ARMED, DRAKE_ASSERT_THROWS_MESSAGE_IF_ARMED

◆ DRAKE_EXPECT_THROWS_MESSAGE_IF_ARMED

#define DRAKE_EXPECT_THROWS_MESSAGE_IF_ARMED (   expression,
  regexp 
)

Same as DRAKE_EXPECT_THROWS_MESSAGE in Debug builds, but doesn't require a throw in Release builds.

However, if the Release build does throw it must throw the right message. More precisely, the thrown message is required whenever DRAKE_ENABLE_ASSERTS is defined, which Debug builds do be default.

See also
DRAKE_EXPECT_THROWS_MESSAGE