Skip to content

Commit 5c59e1d

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

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed

test/test_interpreter.cpp

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,52 @@
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+
/**
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+
2666
TEST_SUITE("execute_request")
2767
{
2868
TEST_CASE("stl")
@@ -610,6 +650,20 @@ TEST_SUITE("xmagics_contains"){
610650
}
611651
}
612652

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+
613667
TEST_SUITE("xmagics_apply"){
614668
TEST_CASE("bad_status_cell") {
615669
xcpp::xmagics_manager manager;
@@ -626,6 +680,48 @@ TEST_SUITE("xmagics_apply"){
626680
manager.apply("%dummy", kernel_res);
627681
REQUIRE(kernel_res["status"] == "error");
628682
}
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+
}
629725
}
630726

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

0 commit comments

Comments
 (0)