Skip to content

Commit f98cca3

Browse files
Enable the %%file xmagic (#181)
* 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>
1 parent 859a1b9 commit f98cca3

File tree

3 files changed

+67
-8
lines changed

3 files changed

+67
-8
lines changed

CMakeLists.txt

+1-3
Original file line numberDiff line numberDiff line change
@@ -169,23 +169,21 @@ set(XEUS_CPP_HEADERS
169169
#src/xinspect.hpp
170170
#src/xsystem.hpp
171171
#src/xparser.hpp
172-
#src/xmagics/os.hpp
173172
)
174173

175174
set(XEUS_CPP_SRC
176175
src/xholder.cpp
177176
src/xinput.cpp
178177
src/xinterpreter.cpp
179-
src/xmagics/os.cpp
180178
src/xoptions.cpp
181179
src/xparser.cpp
182180
src/xutils.cpp
183181
)
184182

185183
if(NOT EMSCRIPTEN)
186184
list(APPEND XEUS_CPP_SRC
187-
src/xmagics/xassist.hpp
188185
src/xmagics/xassist.cpp
186+
src/xmagics/os.cpp
189187
)
190188
endif()
191189

src/xinterpreter.cpp

+6-4
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717

1818
#include "xinput.hpp"
1919
#include "xinspect.hpp"
20-
#include "xmagics/os.hpp"
2120
#ifndef EMSCRIPTEN
21+
#include "xmagics/os.hpp"
2222
#include "xmagics/xassist.hpp"
2323
#endif
2424
#include "xparser.hpp"
@@ -371,12 +371,14 @@ __get_cxx_version ()
371371

372372
void interpreter::init_magic()
373373
{
374-
// preamble_manager["magics"].get_cast<xmagics_manager>().register_magic("executable", executable(m_interpreter));
375-
// preamble_manager["magics"].get_cast<xmagics_manager>().register_magic("file", writefile());
376-
// preamble_manager["magics"].get_cast<xmagics_manager>().register_magic("timeit", timeit(&m_interpreter));
374+
// preamble_manager["magics"].get_cast<xmagics_manager>().register_magic("executable",
375+
// executable(m_interpreter));
376+
// preamble_manager["magics"].get_cast<xmagics_manager>().register_magic("timeit",
377+
// timeit(&m_interpreter));
377378
// preamble_manager["magics"].get_cast<xmagics_manager>().register_magic("python", pythonexec());
378379
#ifndef EMSCRIPTEN
379380
preamble_manager["magics"].get_cast<xmagics_manager>().register_magic("xassist", xassist());
381+
preamble_manager["magics"].get_cast<xmagics_manager>().register_magic("file", writefile());
380382
#endif
381383
}
382384
}

test/test_interpreter.cpp

+60-1
Original file line numberDiff line numberDiff line change
@@ -1026,4 +1026,63 @@ TEST_SUITE("xassist"){
10261026
std::remove("ollama_model.txt");
10271027
}
10281028

1029-
}
1029+
}
1030+
1031+
1032+
TEST_SUITE("file") {
1033+
TEST_CASE("Write") {
1034+
xcpp::writefile wf;
1035+
std::string line = "%%file testfile.txt";
1036+
std::string cell = "Hello, World!";
1037+
1038+
wf(line, cell);
1039+
1040+
std::ifstream infile("testfile.txt");
1041+
std::string content;
1042+
std::getline(infile, content);
1043+
1044+
REQUIRE(content == "Hello, World!");
1045+
infile.close();
1046+
}
1047+
TEST_CASE("Overwrite") {
1048+
xcpp::writefile wf;
1049+
std::string line = "%%file testfile.txt";
1050+
std::string cell = "Hello, World!";
1051+
1052+
wf(line, cell);
1053+
1054+
std::string overwrite_cell = "Overwrite test";
1055+
1056+
wf(line, overwrite_cell);
1057+
1058+
std::ifstream infile("testfile.txt");
1059+
std::string content;
1060+
std::getline(infile, content);
1061+
1062+
REQUIRE(content == overwrite_cell);
1063+
infile.close();
1064+
}
1065+
TEST_CASE("Append") {
1066+
xcpp::writefile wf;
1067+
std::string line = "%%file testfile.txt";
1068+
std::string cell = "Hello, World!";
1069+
1070+
wf(line, cell);
1071+
1072+
std::string append_line = "%%file -a testfile.txt";
1073+
std::string append_cell = "Hello, again!";
1074+
1075+
wf(append_line, append_cell);
1076+
1077+
std::ifstream infile("testfile.txt");
1078+
std::vector<std::string> lines;
1079+
std::string content;
1080+
while(std::getline(infile, content)) {
1081+
lines.push_back(content);
1082+
}
1083+
1084+
REQUIRE(lines[0] == "Hello, World!");
1085+
REQUIRE(lines[1] == "Hello, again!");
1086+
infile.close();
1087+
}
1088+
}

0 commit comments

Comments
 (0)