Skip to content

Commit 37cc623

Browse files
committed
Add logic to capture stderr
1 parent 50de569 commit 37cc623

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

test/test_interpreter.cpp

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,26 @@
2525
#include <unistd.h>
2626
#endif
2727

28+
// 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+
}
34+
35+
~StdErrRedirect() {
36+
std::cerr.rdbuf(old_cerr_buff);
37+
}
38+
39+
std::string getCaptured() {
40+
return ss.str();
41+
}
42+
43+
private:
44+
std::streambuf* old_cerr_buff;
45+
std::stringstream& ss;
46+
};
47+
2848
TEST_SUITE("execute_request")
2949
{
3050
TEST_CASE("stl")
@@ -677,14 +697,12 @@ TEST_SUITE("xmagics_apply"){
677697
xcpp::xmagics_manager manager;
678698

679699
std::stringstream ss;
680-
auto cerr_buff = std::cerr.rdbuf();
681-
std::cerr.rdbuf(ss.rdbuf());
700+
StdErrRedirect redirect(ss);
682701

683-
manager.apply("test", "line", "");
702+
manager.apply("test", "line", "");
684703

685-
std::cerr.rdbuf(cerr_buff);
686-
REQUIRE(ss.str() == "UsageError: %%test is a cell magic, but the cell body is empty.\n"
687-
"If you only intend to display %%test help, please use a double line break to fill in the cell body.\n");
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");
688706

689707
}
690708
}

0 commit comments

Comments
 (0)