Skip to content

Commit 6692a45

Browse files
authored
Add tests for xsystem and os (#112)
1 parent e788167 commit 6692a45

File tree

2 files changed

+108
-1
lines changed

2 files changed

+108
-1
lines changed

src/xmagics/os.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ namespace xcpp
2020
{
2121
public:
2222

23+
XEUS_CPP_API
2324
virtual void operator()(const std::string& line, const std::string& cell) override;
2425

2526
private:

test/test_interpreter.cpp

Lines changed: 107 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
#include "xeus-cpp/xoptions.hpp"
1515

1616
#include "../src/xparser.hpp"
17+
#include "../src/xsystem.hpp"
18+
#include "../src/xmagics/os.hpp"
19+
20+
#include <fstream>
1721

1822
TEST_SUITE("execute_request")
1923
{
@@ -398,7 +402,7 @@ TEST_SUITE("xbuffer")
398402
}
399403
}
400404

401-
TEST_SUITE("xotions")
405+
TEST_SUITE("xoptions")
402406
{
403407
TEST_CASE("good_status") {
404408
xcpp::argparser parser("test");
@@ -426,3 +430,105 @@ TEST_SUITE("xotions")
426430
REQUIRE(exceptionThrown);
427431
}
428432
}
433+
434+
TEST_SUITE("os")
435+
{
436+
TEST_CASE("write_new_file") {
437+
xcpp::writefile wf;
438+
std::string line = "filename testfile.txt";
439+
std::string cell = "Hello, World!";
440+
441+
wf(line, cell);
442+
443+
std::ifstream infile("testfile.txt");
444+
REQUIRE(infile.good() == true);
445+
infile.close();
446+
447+
std::remove("testfile.txt");
448+
}
449+
450+
TEST_CASE("overwrite_file") {
451+
xcpp::writefile wf;
452+
std::string line = "filename testfile.txt";
453+
std::string cell = "Hello, World!";
454+
455+
wf(line, cell);
456+
457+
std::string overwrite_cell = "Overwrite test";
458+
459+
wf(line, overwrite_cell);
460+
461+
std::ifstream infile("testfile.txt");
462+
std::string content;
463+
std::getline(infile, content);
464+
465+
REQUIRE(content == overwrite_cell);
466+
infile.close();
467+
468+
std::remove("testfile.txt");
469+
}
470+
471+
TEST_CASE("append_file") {
472+
xcpp::writefile wf;
473+
std::string line = "filename testfile.txt";
474+
std::string cell = "Hello, World!";
475+
476+
wf(line, cell);
477+
478+
std::string append_line = "filename testfile.txt --append";
479+
std::string append_cell = "Hello, again!";
480+
481+
wf(append_line, append_cell);
482+
483+
std::ifstream infile("testfile.txt");
484+
std::vector<std::string> lines;
485+
std::string content;
486+
while(std::getline(infile, content)) {
487+
lines.push_back(content);
488+
}
489+
490+
REQUIRE(lines[0] == cell);
491+
REQUIRE(lines[1] == append_cell);
492+
infile.close();
493+
494+
std::remove("testfile.txt");
495+
}
496+
497+
}
498+
499+
TEST_SUITE("xsystem_clone")
500+
{
501+
TEST_CASE("clone_xsystem_not_null")
502+
{
503+
xcpp::xsystem system;
504+
505+
xcpp::xpreamble* clone = system.clone();
506+
507+
REQUIRE(clone != nullptr);
508+
}
509+
510+
TEST_CASE("clone_xsystem_same_type")
511+
{
512+
xcpp::xsystem system;
513+
514+
xcpp::xpreamble* clone = system.clone();
515+
516+
REQUIRE(dynamic_cast<xcpp::xsystem*>(clone) != nullptr);
517+
518+
delete clone;
519+
}
520+
}
521+
522+
TEST_SUITE("xsystem_apply")
523+
{
524+
TEST_CASE("apply_xsystem")
525+
{
526+
xcpp::xsystem system;
527+
std::string code = "!echo Hello, World!";
528+
nl::json kernel_res;
529+
530+
system.apply(code, kernel_res);
531+
532+
REQUIRE(kernel_res["status"] == "ok");
533+
}
534+
}

0 commit comments

Comments
 (0)