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
+ // Object for capturing stderr
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
+
35
+ ~StreamRedirectRAII () {
36
+ stream_to_redirect.rdbuf (old_stream_buff);
37
+ }
38
+
39
+ std::string getCaptured () {
40
+ return ss.str ();
41
+ }
42
+
43
+ private:
44
+ std::streambuf* old_stream_buff;
45
+ std::ostream& stream_to_redirect;
46
+ std::stringstream ss;
47
+ };
48
+
26
49
TEST_SUITE (" execute_request" )
27
50
{
28
51
TEST_CASE (" stl" )
@@ -610,6 +633,20 @@ TEST_SUITE("xmagics_contains"){
610
633
}
611
634
}
612
635
636
+ class MyMagicLine : public xcpp ::xmagic_line {
637
+ public:
638
+ virtual void operator ()(const std::string& line) override {
639
+ std::cout << line << std::endl;
640
+ }
641
+ };
642
+
643
+ class MyMagicCell : public xcpp ::xmagic_cell {
644
+ public:
645
+ virtual void operator ()(const std::string& line, const std::string& cell) override {
646
+ std::cout << line << cell << std::endl;
647
+ }
648
+ };
649
+
613
650
TEST_SUITE (" xmagics_apply" ){
614
651
TEST_CASE (" bad_status_cell" ) {
615
652
xcpp::xmagics_manager manager;
@@ -626,6 +663,48 @@ TEST_SUITE("xmagics_apply"){
626
663
manager.apply (" %dummy" , kernel_res);
627
664
REQUIRE (kernel_res[" status" ] == " error" );
628
665
}
666
+
667
+ TEST_CASE (" good_status_line" ) {
668
+
669
+ xcpp::xpreamble_manager preamble_manager;
670
+
671
+ preamble_manager.register_preamble (" magics" , new xcpp::xmagics_manager ());
672
+
673
+ preamble_manager[" magics" ].get_cast <xcpp::xmagics_manager>().register_magic (" magic2" , MyMagicCell ());
674
+
675
+ nl::json kernel_res;
676
+
677
+ preamble_manager[" magics" ].get_cast <xcpp::xmagics_manager>().apply (" %%magic2 qwerty" , kernel_res);
678
+
679
+ REQUIRE (kernel_res[" status" ] == " ok" );
680
+ }
681
+
682
+ TEST_CASE (" good_status_cell" ) {
683
+
684
+ xcpp::xpreamble_manager preamble_manager;
685
+
686
+ preamble_manager.register_preamble (" magics" , new xcpp::xmagics_manager ());
687
+
688
+ preamble_manager[" magics" ].get_cast <xcpp::xmagics_manager>().register_magic (" magic1" , MyMagicLine ());
689
+
690
+ nl::json kernel_res;
691
+
692
+ preamble_manager[" magics" ].get_cast <xcpp::xmagics_manager>().apply (" %magic1 qwerty" , kernel_res);
693
+
694
+ REQUIRE (kernel_res[" status" ] == " ok" );
695
+ }
696
+
697
+ TEST_CASE (" cell magic with empty cell body" ) {
698
+
699
+ xcpp::xmagics_manager manager;
700
+
701
+ StreamRedirectRAII redirect (std::cerr);
702
+
703
+ manager.apply (" test" , " line" , " " );
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
+ }
629
708
}
630
709
631
710
#if defined(__GNUC__) && !defined(XEUS_CPP_EMSCRIPTEN_WASM_BUILD)
0 commit comments