Skip to content

Commit

Permalink
Add tests for xsystem and os
Browse files Browse the repository at this point in the history
  • Loading branch information
tharun571 committed May 21, 2024
1 parent e788167 commit 28e2b9a
Showing 1 changed file with 107 additions and 1 deletion.
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");
}
}

0 comments on commit 28e2b9a

Please sign in to comment.