Skip to content

Commit c8a0a1d

Browse files
committed
Add tests for xmagics, Add logic to capture stderr
1 parent f4b9593 commit c8a0a1d

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

test/test_interpreter.cpp

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,35 @@
1717
#include "../src/xsystem.hpp"
1818
#include "../src/xmagics/os.hpp"
1919

20+
#include <iostream>
21+
2022
#include <fstream>
2123
#if defined(__GNUC__) && !defined(XEUS_CPP_EMSCRIPTEN_WASM_BUILD)
2224
#include <sys/wait.h>
2325
#include <unistd.h>
2426
#endif
2527

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+
2649
TEST_SUITE("execute_request")
2750
{
2851
TEST_CASE("stl")
@@ -610,6 +633,20 @@ TEST_SUITE("xmagics_contains"){
610633
}
611634
}
612635

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+
613650
TEST_SUITE("xmagics_apply"){
614651
TEST_CASE("bad_status_cell") {
615652
xcpp::xmagics_manager manager;
@@ -626,6 +663,48 @@ TEST_SUITE("xmagics_apply"){
626663
manager.apply("%dummy", kernel_res);
627664
REQUIRE(kernel_res["status"] == "error");
628665
}
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+
}
629708
}
630709

631710
#if defined(__GNUC__) && !defined(XEUS_CPP_EMSCRIPTEN_WASM_BUILD)

0 commit comments

Comments
 (0)