I'm trying to make unit tests with criterion for my C++ code but I can't figure out how to test a function that only print and do not return anything. Here's what I tried:
//the function to test
#include <iostream>
#include <fstream>
void my_cat(int ac, char **av)
{
if (ac <= 1)
std::cout << "my_cat: Usage: ./my_cat file [...]" << std::endl;
for (unsigned i = 1; i < ac; i += 1) {
std::ifstream file (av[i]);
if (file.fail()) {
std::cout << "my_cat: ";
std::cout << av[i];
std::cout << ": No such file or directory" << std::endl;
}
else if (file.is_open()) {
std::cout << file.rdbuf() << std::endl;
}
file.close();
}
}
//the test
#include <criterion/criterion.h>
#include <criterion/redirect.h>
void my_cat(int ac, char **av);
Test(mycat, my_cat)
{
char *av[] = {"./my_cat", "text.txt"};
my_cat(2, av);
}
But now that I'm here I don't know what to use to check if the print is correct.