Skip to content

Enable the %%file xmagic #181

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -169,23 +169,21 @@ 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
)

if(NOT EMSCRIPTEN)
list(APPEND XEUS_CPP_SRC
src/xmagics/xassist.hpp
src/xmagics/xassist.cpp
src/xmagics/os.cpp
)
endif()

Expand Down
10 changes: 6 additions & 4 deletions src/xinterpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -371,12 +371,14 @@ __get_cxx_version ()

void interpreter::init_magic()
{
// preamble_manager["magics"].get_cast<xmagics_manager>().register_magic("executable", executable(m_interpreter));
// preamble_manager["magics"].get_cast<xmagics_manager>().register_magic("file", writefile());
// preamble_manager["magics"].get_cast<xmagics_manager>().register_magic("timeit", timeit(&m_interpreter));
// preamble_manager["magics"].get_cast<xmagics_manager>().register_magic("executable",
// executable(m_interpreter));
// preamble_manager["magics"].get_cast<xmagics_manager>().register_magic("timeit",
// timeit(&m_interpreter));
// preamble_manager["magics"].get_cast<xmagics_manager>().register_magic("python", pythonexec());
#ifndef EMSCRIPTEN
preamble_manager["magics"].get_cast<xmagics_manager>().register_magic("xassist", xassist());
preamble_manager["magics"].get_cast<xmagics_manager>().register_magic("file", writefile());
#endif
}
}
61 changes: 60 additions & 1 deletion test/test_interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1026,4 +1026,63 @@ TEST_SUITE("xassist"){
std::remove("ollama_model.txt");
}

}
}


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<std::string> 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();
}
}