Skip to content

Commit 50de569

Browse files
committed
Add tests for xmagics
1 parent f4b9593 commit 50de569

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

test/test_interpreter.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
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>
@@ -610,6 +612,20 @@ TEST_SUITE("xmagics_contains"){
610612
}
611613
}
612614

615+
class MyMagicLine : public xcpp::xmagic_line {
616+
public:
617+
virtual void operator()(const std::string& line) override{
618+
std::cout << line << std::endl;
619+
}
620+
};
621+
622+
class MyMagicCell : public xcpp::xmagic_cell {
623+
public:
624+
virtual void operator()(const std::string& line, const std::string& cell) override{
625+
std::cout << line << cell << std::endl;
626+
}
627+
};
628+
613629
TEST_SUITE("xmagics_apply"){
614630
TEST_CASE("bad_status_cell") {
615631
xcpp::xmagics_manager manager;
@@ -626,6 +642,51 @@ TEST_SUITE("xmagics_apply"){
626642
manager.apply("%dummy", kernel_res);
627643
REQUIRE(kernel_res["status"] == "error");
628644
}
645+
646+
TEST_CASE("good_status_line") {
647+
648+
xcpp::xpreamble_manager preamble_manager;
649+
650+
preamble_manager.register_preamble("magics", new xcpp::xmagics_manager());
651+
652+
preamble_manager["magics"].get_cast<xcpp::xmagics_manager>().register_magic("magic2", MyMagicCell());
653+
654+
nl::json kernel_res;
655+
656+
preamble_manager["magics"].get_cast<xcpp::xmagics_manager>().apply("%%magic2 qwerty", kernel_res);
657+
658+
REQUIRE(kernel_res["status"] == "ok");
659+
}
660+
661+
TEST_CASE("good_status_cell") {
662+
663+
xcpp::xpreamble_manager preamble_manager;
664+
665+
preamble_manager.register_preamble("magics", new xcpp::xmagics_manager());
666+
667+
preamble_manager["magics"].get_cast<xcpp::xmagics_manager>().register_magic("magic1", MyMagicLine());
668+
669+
nl::json kernel_res;
670+
671+
preamble_manager["magics"].get_cast<xcpp::xmagics_manager>().apply("%magic1 qwerty", kernel_res);
672+
673+
REQUIRE(kernel_res["status"] == "ok");
674+
}
675+
676+
TEST_CASE("cell magic with empty cell body") {
677+
xcpp::xmagics_manager manager;
678+
679+
std::stringstream ss;
680+
auto cerr_buff = std::cerr.rdbuf();
681+
std::cerr.rdbuf(ss.rdbuf());
682+
683+
manager.apply("test", "line", "");
684+
685+
std::cerr.rdbuf(cerr_buff);
686+
REQUIRE(ss.str() == "UsageError: %%test is a cell magic, but the cell body is empty.\n"
687+
"If you only intend to display %%test help, please use a double line break to fill in the cell body.\n");
688+
689+
}
629690
}
630691

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

0 commit comments

Comments
 (0)