https://mooseframework.inl.gov
Functions
Moose::UnitUtils Namespace Reference

Functions

template<class ExceptionType = std::exception, class Action = bool>
void assertThrows (const Action &action, const std::optional< std::string > &contains={}, const bool set_throw_on_error=false)
 A helper for asserting that calling something throws an exception. More...
 

Function Documentation

◆ assertThrows()

template<class ExceptionType = std::exception, class Action = bool>
void Moose::UnitUtils::assertThrows ( const Action action,
const std::optional< std::string > &  contains = {},
const bool  set_throw_on_error = false 
)

A helper for asserting that calling something throws an exception.

Parameters
actionA function that calls the thing that should throw
containsOptional argument to check that the assertion message contains this sub string
set_throw_on_errorSet to true to set moose to throw on error

Definition at line 28 of file MooseUnitUtils.h.

29  {},
30  const bool set_throw_on_error = false)
31 {
32  static_assert(std::is_base_of_v<std::exception, ExceptionType>, "Not an exception");
33 
34  std::unique_ptr<Moose::ScopedThrowOnError> scoped_throw_on_error;
35  if (set_throw_on_error)
36  scoped_throw_on_error = std::make_unique<Moose::ScopedThrowOnError>();
37 
38  try
39  {
40  action();
41  FAIL() << "Expected " << MooseUtils::prettyCppType<ExceptionType>() << " not thrown";
42  }
43  catch (std::exception const & e)
44  {
45  if constexpr (!std::is_same_v<std::exception, ExceptionType>)
46  if (!dynamic_cast<const ExceptionType *>(&e))
47  FAIL() << "Threw " << demangle(typeid(e).name()) << " instead of "
48  << MooseUtils::prettyCppType<ExceptionType>() << " with message '" << e.what()
49  << "'";
50 
51  if (contains)
52  {
53  ASSERT_TRUE(std::string(e.what()).find(*contains) != std::string::npos)
54  << "Exception \"" << e.what() << "\" does not contain \"" << *contains << "\"";
55  }
56  }
57 }
std::string name(const ElemQuality q)
KOKKOS_INLINE_FUNCTION const T * find(const T &target, const T *const begin, const T *const end)
Find a value in an array.
Definition: KokkosUtils.h:30
bool contains(std::string_view superstring, std::string_view substring)
std::string demangle(const char *name)