17
17
#include " ../src/xsystem.hpp"
18
18
#include " ../src/xmagics/os.hpp"
19
19
20
+ #include < iostream>
21
+
20
22
#include < fstream>
21
23
#if defined(__GNUC__) && !defined(XEUS_CPP_EMSCRIPTEN_WASM_BUILD)
22
24
#include < sys/wait.h>
23
25
#include < unistd.h>
24
26
#endif
25
27
28
+ /* *
29
+ * @class StreamRedirectRAII
30
+ * @brief A RAII class to redirect a stream to a stringstream.
31
+ *
32
+ * This class redirects the output of a given std::ostream to a std::stringstream.
33
+ * The original stream is restored when the object is destroyed.
34
+ */
35
+ class StreamRedirectRAII {
36
+ public:
37
+ /* *
38
+ * @brief Constructor that starts redirecting the given stream.
39
+ * @param stream The stream to redirect.
40
+ */
41
+ StreamRedirectRAII (std::ostream& stream) : old_stream_buff(stream.rdbuf()), stream_to_redirect(stream) {
42
+ stream_to_redirect.rdbuf (ss.rdbuf ());
43
+ }
44
+
45
+ /* *
46
+ * @brief Destructor that restores the original stream.
47
+ */
48
+ ~StreamRedirectRAII () {
49
+ stream_to_redirect.rdbuf (old_stream_buff);
50
+ }
51
+
52
+ /* *
53
+ * @brief Get the output that was written to the stream.
54
+ * @return A string containing the output that was written to the stream.
55
+ */
56
+ std::string getCaptured () {
57
+ return ss.str ();
58
+ }
59
+
60
+ private:
61
+ std::streambuf* old_stream_buff; // /< The original buffer of the stream.
62
+ std::ostream& stream_to_redirect; // /< The stream that is being redirected.
63
+ std::stringstream ss; // /< The stringstream that the stream is redirected to.
64
+ };
65
+
26
66
TEST_SUITE (" execute_request" )
27
67
{
28
68
TEST_CASE (" stl" )
@@ -610,6 +650,20 @@ TEST_SUITE("xmagics_contains"){
610
650
}
611
651
}
612
652
653
+ class MyMagicLine : public xcpp ::xmagic_line {
654
+ public:
655
+ virtual void operator ()(const std::string& line) override {
656
+ std::cout << line << std::endl;
657
+ }
658
+ };
659
+
660
+ class MyMagicCell : public xcpp ::xmagic_cell {
661
+ public:
662
+ virtual void operator ()(const std::string& line, const std::string& cell) override {
663
+ std::cout << line << cell << std::endl;
664
+ }
665
+ };
666
+
613
667
TEST_SUITE (" xmagics_apply" ){
614
668
TEST_CASE (" bad_status_cell" ) {
615
669
xcpp::xmagics_manager manager;
@@ -626,6 +680,48 @@ TEST_SUITE("xmagics_apply"){
626
680
manager.apply (" %dummy" , kernel_res);
627
681
REQUIRE (kernel_res[" status" ] == " error" );
628
682
}
683
+
684
+ TEST_CASE (" good_status_line" ) {
685
+
686
+ xcpp::xpreamble_manager preamble_manager;
687
+
688
+ preamble_manager.register_preamble (" magics" , new xcpp::xmagics_manager ());
689
+
690
+ preamble_manager[" magics" ].get_cast <xcpp::xmagics_manager>().register_magic (" magic2" , MyMagicCell ());
691
+
692
+ nl::json kernel_res;
693
+
694
+ preamble_manager[" magics" ].get_cast <xcpp::xmagics_manager>().apply (" %%magic2 qwerty" , kernel_res);
695
+
696
+ REQUIRE (kernel_res[" status" ] == " ok" );
697
+ }
698
+
699
+ TEST_CASE (" good_status_cell" ) {
700
+
701
+ xcpp::xpreamble_manager preamble_manager;
702
+
703
+ preamble_manager.register_preamble (" magics" , new xcpp::xmagics_manager ());
704
+
705
+ preamble_manager[" magics" ].get_cast <xcpp::xmagics_manager>().register_magic (" magic1" , MyMagicLine ());
706
+
707
+ nl::json kernel_res;
708
+
709
+ preamble_manager[" magics" ].get_cast <xcpp::xmagics_manager>().apply (" %magic1 qwerty" , kernel_res);
710
+
711
+ REQUIRE (kernel_res[" status" ] == " ok" );
712
+ }
713
+
714
+ TEST_CASE (" cell magic with empty cell body" ) {
715
+
716
+ xcpp::xmagics_manager manager;
717
+
718
+ StreamRedirectRAII redirect (std::cerr);
719
+
720
+ manager.apply (" test" , " line" , " " );
721
+
722
+ REQUIRE (redirect.getCaptured () == " UsageError: %%test is a cell magic, but the cell body is empty.\n "
723
+ " If you only intend to display %%test help, please use a double line break to fill in the cell body.\n " );
724
+ }
629
725
}
630
726
631
727
#if defined(__GNUC__) && !defined(XEUS_CPP_EMSCRIPTEN_WASM_BUILD)
0 commit comments