From 5c59e1d40e0861989bf2e5244a8ddb941da87846 Mon Sep 17 00:00:00 2001 From: tharun571 Date: Fri, 31 May 2024 22:25:13 +0530 Subject: [PATCH 1/2] Add tests for xmagics, Add logic to capture stderr --- test/test_interpreter.cpp | 96 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) diff --git a/test/test_interpreter.cpp b/test/test_interpreter.cpp index 2d479939..3af0a2b1 100644 --- a/test/test_interpreter.cpp +++ b/test/test_interpreter.cpp @@ -17,12 +17,52 @@ #include "../src/xsystem.hpp" #include "../src/xmagics/os.hpp" +#include + #include #if defined(__GNUC__) && !defined(XEUS_CPP_EMSCRIPTEN_WASM_BUILD) #include #include #endif +/** + * @class StreamRedirectRAII + * @brief A RAII class to redirect a stream to a stringstream. + * + * This class redirects the output of a given std::ostream to a std::stringstream. + * The original stream is restored when the object is destroyed. + */ +class StreamRedirectRAII { +public: + /** + * @brief Constructor that starts redirecting the given stream. + * @param stream The stream to redirect. + */ + StreamRedirectRAII(std::ostream& stream) : old_stream_buff(stream.rdbuf()), stream_to_redirect(stream) { + stream_to_redirect.rdbuf(ss.rdbuf()); + } + + /** + * @brief Destructor that restores the original stream. + */ + ~StreamRedirectRAII() { + stream_to_redirect.rdbuf(old_stream_buff); + } + + /** + * @brief Get the output that was written to the stream. + * @return A string containing the output that was written to the stream. + */ + std::string getCaptured() { + return ss.str(); + } + +private: + std::streambuf* old_stream_buff; ///< The original buffer of the stream. + std::ostream& stream_to_redirect; ///< The stream that is being redirected. + std::stringstream ss; ///< The stringstream that the stream is redirected to. +}; + TEST_SUITE("execute_request") { TEST_CASE("stl") @@ -610,6 +650,20 @@ TEST_SUITE("xmagics_contains"){ } } +class MyMagicLine : public xcpp::xmagic_line { +public: + virtual void operator()(const std::string& line) override{ + std::cout << line << std::endl; + } +}; + +class MyMagicCell : public xcpp::xmagic_cell { +public: + virtual void operator()(const std::string& line, const std::string& cell) override{ + std::cout << line << cell << std::endl; + } +}; + TEST_SUITE("xmagics_apply"){ TEST_CASE("bad_status_cell") { xcpp::xmagics_manager manager; @@ -626,6 +680,48 @@ TEST_SUITE("xmagics_apply"){ manager.apply("%dummy", kernel_res); REQUIRE(kernel_res["status"] == "error"); } + + TEST_CASE("good_status_line") { + + xcpp::xpreamble_manager preamble_manager; + + preamble_manager.register_preamble("magics", new xcpp::xmagics_manager()); + + preamble_manager["magics"].get_cast().register_magic("magic2", MyMagicCell()); + + nl::json kernel_res; + + preamble_manager["magics"].get_cast().apply("%%magic2 qwerty", kernel_res); + + REQUIRE(kernel_res["status"] == "ok"); + } + + TEST_CASE("good_status_cell") { + + xcpp::xpreamble_manager preamble_manager; + + preamble_manager.register_preamble("magics", new xcpp::xmagics_manager()); + + preamble_manager["magics"].get_cast().register_magic("magic1", MyMagicLine()); + + nl::json kernel_res; + + preamble_manager["magics"].get_cast().apply("%magic1 qwerty", kernel_res); + + REQUIRE(kernel_res["status"] == "ok"); + } + + TEST_CASE("cell magic with empty cell body") { + + xcpp::xmagics_manager manager; + + StreamRedirectRAII redirect(std::cerr); + + manager.apply("test", "line", ""); + + REQUIRE(redirect.getCaptured() == "UsageError: %%test is a cell magic, but the cell body is empty.\n" + "If you only intend to display %%test help, please use a double line break to fill in the cell body.\n"); + } } #if defined(__GNUC__) && !defined(XEUS_CPP_EMSCRIPTEN_WASM_BUILD) From ca7401dd85ae16887b2657a4fe3a4c107af92af5 Mon Sep 17 00:00:00 2001 From: tharun571 Date: Sat, 1 Jun 2024 08:31:43 +0530 Subject: [PATCH 2/2] Update documentation --- test/test_interpreter.cpp | 60 ++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 32 deletions(-) diff --git a/test/test_interpreter.cpp b/test/test_interpreter.cpp index 3af0a2b1..df3698db 100644 --- a/test/test_interpreter.cpp +++ b/test/test_interpreter.cpp @@ -25,42 +25,38 @@ #include #endif -/** - * @class StreamRedirectRAII - * @brief A RAII class to redirect a stream to a stringstream. - * - * This class redirects the output of a given std::ostream to a std::stringstream. - * The original stream is restored when the object is destroyed. - */ + +/// A RAII class to redirect a stream to a stringstream. +/// +/// This class redirects the output of a given std::ostream to a std::stringstream. +/// The original stream is restored when the object is destroyed. class StreamRedirectRAII { -public: - /** - * @brief Constructor that starts redirecting the given stream. - * @param stream The stream to redirect. - */ - StreamRedirectRAII(std::ostream& stream) : old_stream_buff(stream.rdbuf()), stream_to_redirect(stream) { - stream_to_redirect.rdbuf(ss.rdbuf()); - } + public: - /** - * @brief Destructor that restores the original stream. - */ - ~StreamRedirectRAII() { - stream_to_redirect.rdbuf(old_stream_buff); - } + /// Constructor that starts redirecting the given stream. + StreamRedirectRAII(std::ostream& stream) : old_stream_buff(stream.rdbuf()), stream_to_redirect(stream) { + stream_to_redirect.rdbuf(ss.rdbuf()); + } - /** - * @brief Get the output that was written to the stream. - * @return A string containing the output that was written to the stream. - */ - std::string getCaptured() { - return ss.str(); - } + /// Destructor that restores the original stream. + ~StreamRedirectRAII() { + stream_to_redirect.rdbuf(old_stream_buff); + } + + /// Get the output that was written to the stream. + std::string getCaptured() { + return ss.str(); + } + + private: + /// The original buffer of the stream. + std::streambuf* old_stream_buff; + + /// The stream that is being redirected. + std::ostream& stream_to_redirect; -private: - std::streambuf* old_stream_buff; ///< The original buffer of the stream. - std::ostream& stream_to_redirect; ///< The stream that is being redirected. - std::stringstream ss; ///< The stringstream that the stream is redirected to. + /// The stringstream that the stream is redirected to. + std::stringstream ss; }; TEST_SUITE("execute_request")