Skip to content

Commit fc3af37

Browse files
committed
UnitTest: expectThrow & expectNoThrow
1 parent 7035e13 commit fc3af37

3 files changed

Lines changed: 101 additions & 0 deletions

File tree

modules/UnitTest/UnitTest.mpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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)

tests/System/Error.mpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
export module CppUtils.UnitTests.System.Error;
2+
3+
import std;
4+
import CppUtils;
5+
6+
export namespace CppUtils::UnitTest::System::Error
7+
{
8+
inline auto _ = TestSuite{"System/Error", {"UnitTest"}, [](auto& suite) {
9+
suite.addTest("throwErrno", [&] {
10+
suite.template expectThrow<std::runtime_error>([&] {
11+
CppUtils::System::throwErrno(-1, "Test error");
12+
});
13+
suite.template expectNoThrow<>([&] {
14+
CppUtils::System::throwErrno(0, "Test success");
15+
});
16+
});
17+
18+
suite.addTest("expectErrno", [&] {
19+
{
20+
auto result = CppUtils::System::expectErrno(-1, "Test error");
21+
suite.expect(not result.has_value());
22+
}
23+
{
24+
auto result = CppUtils::System::expectErrno(0, "Test success");
25+
suite.expect(result.has_value());
26+
suite.expectEqual(result.value(), 0);
27+
}
28+
});
29+
30+
#if defined(OS_WINDOWS)
31+
suite.addTest("throwLastError", [&] {
32+
suite.template expectThrow<std::runtime_error>([&] {
33+
CppUtils::System::throwLastError("Test last error");
34+
});
35+
});
36+
37+
suite.addTest("throwLastErrorIf", [&] {
38+
suite.template expectThrow<std::runtime_error>([&] {
39+
CppUtils::System::throwLastErrorIf(1, "Test last error if");
40+
});
41+
suite.template expectNoThrow<>([&] {
42+
CppUtils::System::throwLastErrorIf(0, "Test last error if");
43+
});
44+
});
45+
46+
suite.addTest("expectNoError", [&] {
47+
auto result = CppUtils::System::expectNoError(0, "Test no error");
48+
suite.expect(result.has_value());
49+
suite.expectEqual(result.value(), 0);
50+
result = CppUtils::System::expectNoError(1, "Test no error");
51+
suite.expect(not result.has_value());
52+
});
53+
#endif
54+
}};
55+
}

tests/UnitTests.mpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export import CppUtils.UnitTests.Network;
2828
export import CppUtils.UnitTests.Random;
2929
export import CppUtils.UnitTests.Stl.Format;
3030
export import CppUtils.UnitTests.String.Utility;
31+
export import CppUtils.UnitTests.System.Error;
3132
export import CppUtils.UnitTests.Thread.SharedLocker;
3233
export import CppUtils.UnitTests.Thread.ThreadLoop;
3334
export import CppUtils.UnitTests.Thread.ThreadPool;

0 commit comments

Comments
 (0)