Skip to content

Add tests for xsystem and os #112

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 2 commits into from
May 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
1 change: 1 addition & 0 deletions src/xmagics/os.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ namespace xcpp
{
public:

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

private:
Expand Down
108 changes: 107 additions & 1 deletion test/test_interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
#include "xeus-cpp/xoptions.hpp"

#include "../src/xparser.hpp"
#include "../src/xsystem.hpp"
#include "../src/xmagics/os.hpp"

#include <fstream>

TEST_SUITE("execute_request")
{
Expand Down Expand Up @@ -398,7 +402,7 @@ TEST_SUITE("xbuffer")
}
}

TEST_SUITE("xotions")
TEST_SUITE("xoptions")
{
TEST_CASE("good_status") {
xcpp::argparser parser("test");
Expand Down Expand Up @@ -426,3 +430,105 @@ TEST_SUITE("xotions")
REQUIRE(exceptionThrown);
}
}

TEST_SUITE("os")
{
TEST_CASE("write_new_file") {
xcpp::writefile wf;
std::string line = "filename testfile.txt";
std::string cell = "Hello, World!";

wf(line, cell);

std::ifstream infile("testfile.txt");
REQUIRE(infile.good() == true);
infile.close();

std::remove("testfile.txt");
}

TEST_CASE("overwrite_file") {
xcpp::writefile wf;
std::string line = "filename 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();

std::remove("testfile.txt");
}

TEST_CASE("append_file") {
xcpp::writefile wf;
std::string line = "filename testfile.txt";
std::string cell = "Hello, World!";

wf(line, cell);

std::string append_line = "filename testfile.txt --append";
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] == cell);
REQUIRE(lines[1] == append_cell);
infile.close();

std::remove("testfile.txt");
}

}

TEST_SUITE("xsystem_clone")
{
TEST_CASE("clone_xsystem_not_null")
{
xcpp::xsystem system;

xcpp::xpreamble* clone = system.clone();

REQUIRE(clone != nullptr);
}

TEST_CASE("clone_xsystem_same_type")
{
xcpp::xsystem system;

xcpp::xpreamble* clone = system.clone();

REQUIRE(dynamic_cast<xcpp::xsystem*>(clone) != nullptr);

delete clone;
}
}

TEST_SUITE("xsystem_apply")
{
TEST_CASE("apply_xsystem")
{
xcpp::xsystem system;
std::string code = "!echo Hello, World!";
nl::json kernel_res;

system.apply(code, kernel_res);

REQUIRE(kernel_res["status"] == "ok");
}
}