Skip to content

Commit 2420bbd

Browse files
committed
Fix tests for no-symbols mode
1 parent 571027b commit 2420bbd

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

test/unit/tracing/try_catch.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,49 @@ using namespace std::literals;
1919

2020
namespace {
2121
template<typename E, typename... Args>
22+
CPPTRACE_FORCE_NO_INLINE
2223
void do_throw(Args&&... args) {
2324
throw E(std::forward<Args>(args)...);
2425
}
2526

2627
void check_trace(const cpptrace::stacktrace& trace, std::string_view file, int line) {
28+
#ifndef CPPTRACE_BUILD_NO_SYMBOLS
2729
for(const auto& frame : trace) {
2830
if(frame.filename.find(file) != std::string::npos && frame.line == line) {
2931
SUCCEED();
3032
return;
3133
}
3234
}
3335
FAIL() << "Trace does not contain "<<file<<":"<<line<<"\n"<<trace.to_string();
36+
#endif
37+
}
38+
39+
void check_trace(const cpptrace::stacktrace& trace, std::string_view try_name) {
40+
EXPECT_NE(
41+
std::find_if(
42+
trace.begin(),
43+
trace.end(),
44+
[&] (const cpptrace::stacktrace_frame& frame) {
45+
return frame.symbol.find(try_name) != std::string::npos;
46+
}
47+
),
48+
trace.end()
49+
) << trace;
50+
EXPECT_NE(
51+
std::find_if(
52+
trace.begin(),
53+
trace.end(),
54+
[&] (const cpptrace::stacktrace_frame& frame) {
55+
return frame.symbol.find("do_throw") != std::string::npos;
56+
}
57+
),
58+
trace.end()
59+
) << trace;
3460
}
3561
}
3662

3763
TEST(TryCatch, Basic) {
64+
constexpr std::string_view test_name = __func__;
3865
int line = 0;
3966
bool did_catch = false;
4067
cpptrace::try_catch(
@@ -46,6 +73,7 @@ TEST(TryCatch, Basic) {
4673
did_catch = true;
4774
EXPECT_EQ(e.what(), "foobar"sv);
4875
check_trace(cpptrace::from_current_exception(), "try_catch.cpp", line);
76+
check_trace(cpptrace::from_current_exception(), test_name);
4977
}
5078
);
5179
EXPECT_TRUE(did_catch);
@@ -65,6 +93,7 @@ TEST(TryCatch, NoException) {
6593
}
6694

6795
TEST(TryCatch, Upcast) {
96+
constexpr std::string_view test_name = __func__;
6897
int line = 0;
6998
bool did_catch = false;
7099
cpptrace::try_catch(
@@ -76,6 +105,7 @@ TEST(TryCatch, Upcast) {
76105
did_catch = true;
77106
EXPECT_EQ(e.what(), "foobar"sv);
78107
check_trace(cpptrace::from_current_exception(), "try_catch.cpp", line);
108+
check_trace(cpptrace::from_current_exception(), test_name);
79109
}
80110
);
81111
EXPECT_TRUE(did_catch);
@@ -119,6 +149,7 @@ TEST(TryCatch, NoMatchingHandler) {
119149
}
120150

121151
TEST(TryCatch, CorrectHandler) {
152+
constexpr std::string_view test_name = __func__;
122153
int line = 0;
123154
bool did_catch = false;
124155
cpptrace::try_catch(
@@ -136,6 +167,7 @@ TEST(TryCatch, CorrectHandler) {
136167
did_catch = true;
137168
EXPECT_EQ(e.what(), "foobar"sv);
138169
check_trace(cpptrace::from_current_exception(), "try_catch.cpp", line);
170+
check_trace(cpptrace::from_current_exception(), test_name);
139171
},
140172
[&] (const std::exception&) {
141173
FAIL();
@@ -145,6 +177,7 @@ TEST(TryCatch, CorrectHandler) {
145177
}
146178

147179
TEST(TryCatch, BlanketHandler) {
180+
constexpr std::string_view test_name = __func__;
148181
int line = 0;
149182
bool did_catch = false;
150183
cpptrace::try_catch(
@@ -164,12 +197,14 @@ TEST(TryCatch, BlanketHandler) {
164197
[&] () {
165198
did_catch = true;
166199
check_trace(cpptrace::from_current_exception(), "try_catch.cpp", line);
200+
check_trace(cpptrace::from_current_exception(), test_name);
167201
}
168202
);
169203
EXPECT_TRUE(did_catch);
170204
}
171205

172206
TEST(TryCatch, CatchOrdering) {
207+
constexpr std::string_view test_name = __func__;
173208
int line = 0;
174209
bool did_catch = false;
175210
cpptrace::try_catch(
@@ -186,6 +221,7 @@ TEST(TryCatch, CatchOrdering) {
186221
[&] () {
187222
did_catch = true;
188223
check_trace(cpptrace::from_current_exception(), "try_catch.cpp", line);
224+
check_trace(cpptrace::from_current_exception(), test_name);
189225
},
190226
[&] (const std::runtime_error&) {
191227
FAIL();

0 commit comments

Comments
 (0)