Skip to content

Commit fbef59e

Browse files
committed
add test for unified exception errors
1 parent cbd7708 commit fbef59e

1 file changed

Lines changed: 40 additions & 0 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#ifdef REFLECTCPP_USE_STD_EXPECTED
2+
#include <expected>
3+
#endif
4+
5+
#include <rfl.hpp>
6+
#include <gtest/gtest.h>
7+
8+
namespace test_exceptions {
9+
10+
struct Person {
11+
rfl::Rename<"firstName", std::string> first_name;
12+
rfl::Rename<"lastName", std::string> last_name = "Flanders";
13+
};
14+
15+
TEST(generic, test_exceptions) {
16+
const std::string error_message = "unified bad access message!";
17+
#ifdef REFLECTCPP_USE_STD_EXPECTED
18+
rfl::Result<Person> person = std::unexpected(rfl::Error(error_message));
19+
#else
20+
rfl::Result<Person> person(rfl::error(error_message));
21+
#endif
22+
23+
try {
24+
person.value();
25+
FAIL() << "Expected an exception!";
26+
}
27+
#ifdef REFLECTCPP_USE_STD_EXPECTED
28+
catch (std::bad_expected_access<rfl::Error> const& err) {
29+
EXPECT_EQ(err.what(), error_message);
30+
}
31+
#else
32+
catch (std::runtime_error const& err) {
33+
EXPECT_EQ(err.what(), error_message);
34+
}
35+
#endif
36+
catch (...) {
37+
FAIL() << "Invalid exception type!";
38+
}
39+
}
40+
} // namespace test_exceptions

0 commit comments

Comments
 (0)