Skip to content

Commit 8a8deb7

Browse files
committed
Update logic to capture stderr
1 parent 37cc623 commit 8a8deb7

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

test/test_interpreter.cpp

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,24 @@
2626
#endif
2727

2828
// Object for capturing stderr
29-
class StdErrRedirect {
30-
public:
31-
StdErrRedirect(std::ostream& new_stream) : old_cerr_buff(std::cerr.rdbuf()), ss(static_cast<std::stringstream&>(new_stream)) {
32-
std::cerr.rdbuf(ss.rdbuf());
33-
}
29+
class StreamRedirectRAII {
30+
public:
31+
StreamRedirectRAII(std::ostream& stream) : old_stream_buff(stream.rdbuf()), stream_to_redirect(stream) {
32+
stream_to_redirect.rdbuf(ss.rdbuf());
33+
}
3434

35-
~StdErrRedirect() {
36-
std::cerr.rdbuf(old_cerr_buff);
37-
}
35+
~StreamRedirectRAII() {
36+
stream_to_redirect.rdbuf(old_stream_buff);
37+
}
3838

39-
std::string getCaptured() {
40-
return ss.str();
41-
}
39+
std::string getCaptured() {
40+
return ss.str();
41+
}
4242

43-
private:
44-
std::streambuf* old_cerr_buff;
45-
std::stringstream& ss;
43+
private:
44+
std::streambuf* old_stream_buff;
45+
std::ostream& stream_to_redirect;
46+
std::stringstream ss;
4647
};
4748

4849
TEST_SUITE("execute_request")
@@ -694,16 +695,15 @@ TEST_SUITE("xmagics_apply"){
694695
}
695696

696697
TEST_CASE("cell magic with empty cell body") {
698+
697699
xcpp::xmagics_manager manager;
698700

699-
std::stringstream ss;
700-
StdErrRedirect redirect(ss);
701-
702-
manager.apply("test", "line", "");
701+
StreamRedirectRAII redirect(std::cerr);
703702

704-
REQUIRE(redirect.getCaptured() == "UsageError: %%test is a cell magic, but the cell body is empty.\n"
705-
"If you only intend to display %%test help, please use a double line break to fill in the cell body.\n");
703+
manager.apply("test", "line", "");
706704

705+
REQUIRE(redirect.getCaptured() == "UsageError: %%test is a cell magic, but the cell body is empty.\n"
706+
"If you only intend to display %%test help, please use a double line break to fill in the cell body.\n");
707707
}
708708
}
709709

0 commit comments

Comments
 (0)