diff --git a/CMakeLists.txt b/CMakeLists.txt index 0f958cc6..be5d3928 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -169,14 +169,12 @@ set(XEUS_CPP_HEADERS #src/xinspect.hpp #src/xsystem.hpp #src/xparser.hpp - #src/xmagics/os.hpp ) set(XEUS_CPP_SRC src/xholder.cpp src/xinput.cpp src/xinterpreter.cpp - src/xmagics/os.cpp src/xoptions.cpp src/xparser.cpp src/xutils.cpp @@ -184,8 +182,8 @@ set(XEUS_CPP_SRC if(NOT EMSCRIPTEN) list(APPEND XEUS_CPP_SRC - src/xmagics/xassist.hpp src/xmagics/xassist.cpp + src/xmagics/os.cpp ) endif() diff --git a/src/xinterpreter.cpp b/src/xinterpreter.cpp index 2205ff5c..f099b688 100644 --- a/src/xinterpreter.cpp +++ b/src/xinterpreter.cpp @@ -17,8 +17,8 @@ #include "xinput.hpp" #include "xinspect.hpp" -#include "xmagics/os.hpp" #ifndef EMSCRIPTEN +#include "xmagics/os.hpp" #include "xmagics/xassist.hpp" #endif #include "xparser.hpp" @@ -371,12 +371,14 @@ __get_cxx_version () void interpreter::init_magic() { - // preamble_manager["magics"].get_cast().register_magic("executable", executable(m_interpreter)); - // preamble_manager["magics"].get_cast().register_magic("file", writefile()); - // preamble_manager["magics"].get_cast().register_magic("timeit", timeit(&m_interpreter)); + // preamble_manager["magics"].get_cast().register_magic("executable", + // executable(m_interpreter)); + // preamble_manager["magics"].get_cast().register_magic("timeit", + // timeit(&m_interpreter)); // preamble_manager["magics"].get_cast().register_magic("python", pythonexec()); #ifndef EMSCRIPTEN preamble_manager["magics"].get_cast().register_magic("xassist", xassist()); + preamble_manager["magics"].get_cast().register_magic("file", writefile()); #endif } } diff --git a/test/test_interpreter.cpp b/test/test_interpreter.cpp index 0b785ec4..a4fe3bb1 100644 --- a/test/test_interpreter.cpp +++ b/test/test_interpreter.cpp @@ -1026,4 +1026,63 @@ TEST_SUITE("xassist"){ std::remove("ollama_model.txt"); } -} \ No newline at end of file +} + + +TEST_SUITE("file") { + TEST_CASE("Write") { + xcpp::writefile wf; + std::string line = "%%file testfile.txt"; + std::string cell = "Hello, World!"; + + wf(line, cell); + + std::ifstream infile("testfile.txt"); + std::string content; + std::getline(infile, content); + + REQUIRE(content == "Hello, World!"); + infile.close(); + } + TEST_CASE("Overwrite") { + xcpp::writefile wf; + std::string line = "%%file testfile.txt"; + std::string cell = "Hello, World!"; + + wf(line, cell); + + std::string overwrite_cell = "Overwrite test"; + + wf(line, overwrite_cell); + + std::ifstream infile("testfile.txt"); + std::string content; + std::getline(infile, content); + + REQUIRE(content == overwrite_cell); + infile.close(); + } + TEST_CASE("Append") { + xcpp::writefile wf; + std::string line = "%%file testfile.txt"; + std::string cell = "Hello, World!"; + + wf(line, cell); + + std::string append_line = "%%file -a testfile.txt"; + std::string append_cell = "Hello, again!"; + + wf(append_line, append_cell); + + std::ifstream infile("testfile.txt"); + std::vector lines; + std::string content; + while(std::getline(infile, content)) { + lines.push_back(content); + } + + REQUIRE(lines[0] == "Hello, World!"); + REQUIRE(lines[1] == "Hello, again!"); + infile.close(); + } +}