Skip to content

Commit

Permalink
Refactor code to avoid GTest GetCapturedStderr
Browse files Browse the repository at this point in the history
it's the same approach as in parser_TEST.cc
  • Loading branch information
Bi0T1N committed Mar 18, 2022
1 parent 11975da commit 625bc4d
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions src/SDF_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,6 @@ TEST(SDF, Version)
TEST(SDF, EmbeddedSpec)
{
std::string result;
std::string output;

result = sdf::SDF::EmbeddedSpec("actor.sdf", false);
EXPECT_NE(result.find("<!-- Actor -->"), std::string::npos);
Expand All @@ -562,21 +561,32 @@ TEST(SDF, EmbeddedSpec)
TEST(SDF, EmbeddedSpecNonExistent)
{
std::string result;
std::string output;

testing::internal::CaptureStderr();
// Capture sdferr output
std::stringstream stderr_buffer;
auto old = std::cerr.rdbuf(stderr_buffer.rdbuf());

#ifdef _WIN32
sdf::Console::Instance()->SetQuiet(false);
#endif

result = sdf::SDF::EmbeddedSpec("unavailable.sdf", false);
output = testing::internal::GetCapturedStderr();
EXPECT_NE(output.find("Unable to find SDF filename"), std::string::npos);
EXPECT_NE(output.find("with version"), std::string::npos);
EXPECT_NE(stderr_buffer.str().find("Unable to find SDF filename"), std::string::npos);
EXPECT_NE(stderr_buffer.str().find("with version"), std::string::npos);
EXPECT_TRUE(result.empty());

output = "";
testing::internal::CaptureStderr();
// clear the contents of the buffer
stderr_buffer.str("");

result = sdf::SDF::EmbeddedSpec("unavailable.sdf", true);
output = testing::internal::GetCapturedStderr();
EXPECT_TRUE(output.empty());
EXPECT_TRUE(stderr_buffer.str().empty());
EXPECT_TRUE(result.empty());

// Revert cerr rdbuf to not interfere with other tests
std::cerr.rdbuf(old);
#ifdef _WIN32
sdf::Console::Instance()->SetQuiet(true);
#endif
}

/////////////////////////////////////////////////
Expand Down

0 comments on commit 625bc4d

Please sign in to comment.