Skip to content

Add tests for xsystem and os #111

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

Closed
wants to merge 4 commits into from
Closed
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 include/xeus-cpp/xoptions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ namespace xcpp
using base_type = argparse::ArgumentParser;
using base_type::ArgumentParser;

XEUS_CPP_API
void parse(const std::string& line);
};
}
Expand Down
136 changes: 136 additions & 0 deletions test/test_interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@
#include "xeus-cpp/xholder.hpp"
#include "xeus-cpp/xmanager.hpp"
#include "xeus-cpp/xutils.hpp"
#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 @@ -396,3 +401,134 @@ TEST_SUITE("xbuffer")
REQUIRE(null_stream.good() == true);
}
}

TEST_SUITE("xotions")
{
TEST_CASE("good_status") {
xcpp::argparser parser("test");
parser.add_argument("--verbose").help("increase output verbosity").default_value(false).implicit_value(true);
std::string line = "./main --verbose";

parser.parse(line);

REQUIRE(parser["--verbose"] == true);
}

TEST_CASE("bad_status") {
xcpp::argparser parser("test");
parser.add_argument("--verbose");
std::string line = "./main --verbose";

parser.parse(line);

bool exceptionThrown = false;
try {
bool isVerbose = (parser["--verbose"] == false);
} catch (const std::exception& e) {
exceptionThrown = true;
}
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");
}
}