Browse Source

Test: Add Shell and System test cases

master
Riyyi 3 years ago
parent
commit
875a49332c
  1. 1
      test/testutilargparser.cpp
  2. 39
      test/testutilshell.cpp
  3. 98
      test/testutilsystem.cpp

1
test/testutilargparser.cpp

@ -1,3 +1,4 @@
#include <functional> // function
#include <string>
#include <vector>

39
test/testutilshell.cpp

@ -0,0 +1,39 @@
#include <string>
#include "macro.h"
#include "testcase.h"
#include "testsuite.h"
#include "util/shell.h"
bool runShell(const char* command, std::string* output = nullptr)
{
stdout = Test::TestSuite::the().outputNull();
Util::Shell $;
auto exec = $(command);
if (output) {
*output = exec.output();
}
stdout = Test::TestSuite::the().outputStd();
return !exec.status() ? true : false;
}
// -----------------------------------------
TEST_CASE(ShellCommand)
{
// Pipe test, grep section of the echo command, return true
std::string output = "";
auto result = runShell("echo 'Hello World!' | grep --only-matching 'Hello'", &output);
EXPECT_EQ(result, true);
EXPECT_EQ(output, "Hello\n");
// Return false
result = runShell("exit 1");
EXPECT_EQ(result, false);
// Return false
result = runShell("failure() { return 1; }; failure");
EXPECT_EQ(result, false);
}

98
test/testutilsystem.cpp

@ -0,0 +1,98 @@
#include <functional> // function
#include <string>
#include <vector>
#include "macro.h"
#include "testcase.h"
#include "testsuite.h"
#include "util/system.h"
bool runSystem(std::function<Util::System(Util::System&)> commands, std::string* output = nullptr, std::string* error = nullptr)
{
Util::System system;
auto exec = commands(system);
if (output) {
*output = exec.output();
}
if (error) {
*error = exec.error();
}
return !exec.status() ? true : false;
}
// -----------------------------------------
TEST_CASE(SystemCommand)
{
// Regular echo command, return true
std::string output = "";
std::string error = "";
auto result = runSystem([&](auto& $) {
return $("echo Hello World!")();
}, &output, &error);
EXPECT_EQ(result, true);
EXPECT_EQ(output, "Hello World!\n");
EXPECT_EQ(error, "");
// Apend output of two echo commands, return true
output = "";
error = "";
result = runSystem([&](auto& $) {
return $("echo -n Hello ") + $("echo -n World!");
}, &output, &error);
EXPECT_EQ(result, true);
EXPECT_EQ(output, "Hello World!");
EXPECT_EQ(error, "");
// Pipe test, grep section of the echo command, return true
output = "";
error = "";
result = runSystem([&](auto& $) {
return $("echo Hello World!") | $("grep --only-matching Hello");
}, &output, &error);
EXPECT_EQ(result, true);
EXPECT_EQ(output, "Hello\n");
EXPECT_EQ(error, "");
// Run all commands until first false, return true
output = "";
error = "";
result = runSystem([&](auto& $) {
return $("echo Hello") && $("echo World!");
}, &output, &error);
EXPECT_EQ(result, true);
EXPECT_EQ(output, "Hello\nWorld!\n");
EXPECT_EQ(error, "");
// Run all commands until first true, return true
output = "";
error = "";
result = runSystem([&](auto& $) {
return $("echo Hello") || $("echo World!");
}, &output, &error);
EXPECT_EQ(result, true);
EXPECT_EQ(output, "Hello\n");
EXPECT_EQ(error, "");
// And plus pipe test, grep the middle word, return true
output = "";
error = "";
result = runSystem([&](auto& $) {
return ($("echo -n one ") && $("echo -n two ") && $("echo -n three")) | $("grep --only-matching two");
}, &output, &error);
EXPECT_EQ(result, true);
EXPECT_EQ(output, "two\n");
EXPECT_EQ(error, "");
// FIXME waitpid does not seem to get the right exit status here
// Return false
output = "";
error = "";
result = runSystem([&](auto& $) {
return $("exit 1")();
}, &output, &error);
EXPECT_EQ(result, false);
EXPECT_EQ(output, "");
EXPECT_EQ(error, "");
}
Loading…
Cancel
Save