我有一个单元测试,它通过读取函数发送的缓冲区来验证函数:
template <typename Manifold>
void print_manifold(Manifold const& manifold)
try
{
std::cout << "Manifold has " << manifold.N0() << " vertices and "
<< manifold.N1() << " edges and " << manifold.N2() << " faces and "
<< manifold.N3() << " simplices.\n";
// fmt::print(
// "Manifold has {} vertices and {} edges and {} faces and {}
// simplices.\n", manifold.N0(), manifold.N1(), manifold.N2(),
// manifold.N3());
}
catch (...)
{
std::cerr << "print_manifold() went wrong ...\n";
throw;
} // print_manifold
和:
SCENARIO("Printing results", "[utility]")
{
// redirect std::cout
stringstream buffer;
cout.rdbuf(buffer.rdbuf());
GIVEN("A Manifold3")
{
Manifold3 const manifold(640, 4);
WHEN("We want to print statistics on a manifold.")
{
THEN("Statistics are successfully printed.")
{
print_manifold(manifold);
CHECK_THAT(buffer.str(), Catch::Contains("Manifold has"));
}
}
}
有没有办法捕获fmt::print
去生成的输出stdout
?
当我注释掉cout
代码并取消注释fmt
代码时,我得到了以前的cout <<
.