|
26 | 26 | #endif
|
27 | 27 |
|
28 | 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 |
| - } |
| 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 | + } |
34 | 34 |
|
35 |
| - ~StdErrRedirect() { |
36 |
| - std::cerr.rdbuf(old_cerr_buff); |
37 |
| - } |
| 35 | + ~StreamRedirectRAII() { |
| 36 | + stream_to_redirect.rdbuf(old_stream_buff); |
| 37 | + } |
38 | 38 |
|
39 |
| - std::string getCaptured() { |
40 |
| - return ss.str(); |
41 |
| - } |
| 39 | + std::string getCaptured() { |
| 40 | + return ss.str(); |
| 41 | + } |
42 | 42 |
|
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; |
46 | 47 | };
|
47 | 48 |
|
48 | 49 | TEST_SUITE("execute_request")
|
@@ -694,16 +695,15 @@ TEST_SUITE("xmagics_apply"){
|
694 | 695 | }
|
695 | 696 |
|
696 | 697 | TEST_CASE("cell magic with empty cell body") {
|
| 698 | + |
697 | 699 | xcpp::xmagics_manager manager;
|
698 | 700 |
|
699 |
| - std::stringstream ss; |
700 |
| - StdErrRedirect redirect(ss); |
701 |
| - |
702 |
| - manager.apply("test", "line", ""); |
| 701 | + StreamRedirectRAII redirect(std::cerr); |
703 | 702 |
|
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", ""); |
706 | 704 |
|
| 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"); |
707 | 707 | }
|
708 | 708 | }
|
709 | 709 |
|
|
0 commit comments