Skip to content

Commit

Permalink
Enable the %%file xmagic (#181)
Browse files Browse the repository at this point in the history
* Add test for file command

* Uncomment writefile to enable file xmagic

* Move oss header & source to the not emscripten case

* Update xinterpreter.cpp

* Update xinterpreter.cpp

* Update CMakeLists.txt

* Update CMakeLists.txt

* Changes suggested by clang-format

---------

Co-authored-by: Anutosh Bhat <andersonbhat491@gmail.com>
  • Loading branch information
faze-geek and anutosh491 authored Nov 21, 2024
1 parent 859a1b9 commit f98cca3
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 8 deletions.
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();
}
}

0 comments on commit f98cca3

Please sign in to comment.