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

Classes

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

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

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

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