@@ -178,6 +178,8 @@ namespace CppUtils::UnitTest
178178
179179 export struct TestSuite final
180180 {
181+ using Logger = CppUtils::Logger<"CppUtils">;
182+
181183 inline explicit TestSuite(const std::string& name, std::vector<std::string> prerequisites, std::function<void(TestSuite&)> function)
182184 {
183185 function(*this);
@@ -216,6 +218,49 @@ namespace CppUtils::UnitTest
216218 String::formatValue(rhs))};
217219 }
218220
221+ template<class ExceptionType>
222+ inline auto expectThrow(auto&& function, std::source_location sourceLocation = std::source_location::current()) -> void
223+ {
224+ try
225+ {
226+ function();
227+ throw TestException{std::format("In {}\nAt line {}, column {}\nExpected throw of type {}, but no exception was thrown.",
228+ sourceLocation.file_name(),
229+ sourceLocation.line(),
230+ sourceLocation.column(),
231+ typeid(ExceptionType).name())};
232+ }
233+ catch (const ExceptionType&)
234+ {
235+ Logger::print<"success">("Expected exception caught.");
236+ }
237+ catch (const std::exception& exception)
238+ {
239+ throw TestException{std::format("In {}\nAt line {}, column {}\nExpected throw of type {}, but caught different exception: {}",
240+ sourceLocation.file_name(),
241+ sourceLocation.line(),
242+ sourceLocation.column(),
243+ typeid(ExceptionType).name(),
244+ exception.what())};
245+ }
246+ }
247+
248+ inline auto expectNoThrow(auto&& function, std::source_location sourceLocation = std::source_location::current()) -> void
249+ {
250+ try
251+ {
252+ function();
253+ }
254+ catch (const std::exception& exception)
255+ {
256+ throw TestException{std::format("In {}\nAt line {}, column {}\nExpected no throw, but an exception was thrown: {}",
257+ sourceLocation.file_name(),
258+ sourceLocation.line(),
259+ sourceLocation.column(),
260+ exception.what())};
261+ }
262+ }
263+
219264 inline auto expectCall(ExpectedCall& expectedCall, const Chrono::Duration auto& timeout, std::source_location sourceLocation = std::source_location::current()) -> void
220265 {
221266 if (expectedCall.wait_for(timeout) != std::future_status::ready)
0 commit comments