https://mooseframework.inl.gov
Loading...
Searching...
No Matches
Classes | Functions
Moose::UnitUtils Namespace Reference

Classes

class  ScopedTestDirectory
 Run a unit test from a unique temporary working directory. More...
 
class  TempFile
 Create a temporary file and delete it upon destruction. More...
 

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.
 
template<typename ExceptionType , bool exact, bool assert, bool set_throw_on_error, typename Func >
void throwsWithMessage (Func &&fn, const std::string_view message, const char *file, int line)
 Helper for the [EXPECT,ASSERT]_[MOOSEERROR,THROW]_[MSG,MSG_CONTAINS] macros.
 

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 34 of file MooseUnitUtils.h.

35 {},
36 const bool set_throw_on_error = false)
37{
38 static_assert(std::is_base_of_v<std::exception, ExceptionType>, "Not an exception");
39
40 std::unique_ptr<Moose::ScopedThrowOnError> scoped_throw_on_error;
41 if (set_throw_on_error)
42 scoped_throw_on_error = std::make_unique<Moose::ScopedThrowOnError>();
43
44 try
45 {
46 action();
47 FAIL() << "Expected " << MooseUtils::prettyCppType<ExceptionType>() << " not thrown";
48 }
49 catch (std::exception const & e)
50 {
51 if constexpr (!std::is_same_v<std::exception, ExceptionType>)
52 if (!dynamic_cast<const ExceptionType *>(&e))
53 FAIL() << "Threw " << demangle(typeid(e).name()) << " instead of "
54 << MooseUtils::prettyCppType<ExceptionType>() << " with message '" << e.what()
55 << "'";
56
57 if (contains)
58 {
59 ASSERT_TRUE(std::string(e.what()).find(*contains) != std::string::npos)
60 << "Exception \"" << e.what() << "\" does not contain \"" << *contains << "\"";
61 }
62 }
63}

◆ throwsWithMessage()

template<typename ExceptionType , bool exact, bool assert, bool set_throw_on_error, typename Func >
void Moose::UnitUtils::throwsWithMessage ( Func &&  fn,
const std::string_view  message,
const char *  file,
int  line 
)

Helper for the [EXPECT,ASSERT]_[MOOSEERROR,THROW]_[MSG,MSG_CONTAINS] macros.

Definition at line 70 of file MooseUnitUtils.h.

71{
72 static_assert(std::is_base_of_v<std::exception, ExceptionType>, "Not an exception");
73
74 std::ostringstream error;
75
76 try
77 {
78 std::unique_ptr<Moose::ScopedThrowOnError> scoped_throw_on_error;
79 if constexpr (set_throw_on_error)
80 scoped_throw_on_error = std::make_unique<Moose::ScopedThrowOnError>();
81
82 fn();
83
84 error << "Expected exception of type " << libMesh::demangle(typeid(ExceptionType).name())
85 << " but no exception was thrown.";
86 }
87 catch (const ExceptionType & ex)
88 {
89 // Exact match
90 if constexpr (exact)
91 {
92 if (std::string(ex.what()) == message)
93 return;
94 }
95 // Partial match
96 else
97 {
98 if (std::string(ex.what()).find(message) != std::string::npos)
99 return;
100 }
101
102 error << "Expected" << (exact ? "" : " partial") << " exception message: \"" << message
103 << "\"\n Actual exception message: \"" << ex.what() << "\"";
104 }
105 catch (const std::exception & ex)
106 {
107 error << "Expected exception of type " << libMesh::demangle(typeid(ExceptionType).name())
108 << " but exception of type " << libMesh::demangle(typeid(ex).name()) << " was thrown.";
109 }
110
111 ::testing::internal::AssertHelper(
112 ::testing::TestPartResult::kNonFatalFailure, file, line, error.str().c_str()) =
113 ::testing::Message();
114}
std::string name(const ElemQuality q)
std::string demangle(const char *name)