Basically I created a shell and I want to using the Google Unit Test to test if my shell does the something of the regular terminal, and I am wondering how can I copy the output of my shell output into a string and compare it. Simply, I have created a buffer which reads the output of a regular terminal, and I don't know how to use a buffer to read my shell output. Here is my code for the Google Unit Test
TEST(lsTest, lsT) {
string bash_cmd = "ls";
std::array<char, 128> buffer;
string result;
FILE* pipe = popen(bash_cmd.c_str(),"r");
while(fgets(buffer.data(),128,pipe)!=NULL){
result += buffer.data();
}
Base* start = parse(bash_command);
start->execute(); // this would output the command of my shell
EXPECT_EQ(result,?output of start->execute()?);}
Since the execute() is a boolean function, I can't using the buffer to convert the output to a string. Is there any way to read to output of my shell into a string? Also, my shell doesn't contains any redirection which are >
, >>
,| tee
, etc. It basically contains ls
, echo
,mkdir
.