[libc++] Decouple debug mode tests from iostreams

This commit is contained in:
Louis Dionne 2020-10-15 17:54:15 -04:00
parent 95bda510fb
commit a037059577
2 changed files with 22 additions and 28 deletions

View File

@ -14,23 +14,23 @@
// UNSUPPORTED: libcxx-no-debug-mode // UNSUPPORTED: libcxx-no-debug-mode
#include <__debug> #include <__debug>
#include "test_macros.h"
#include "debug_mode_helper.h" #include "debug_mode_helper.h"
#include <cstdio>
#include "test_macros.h"
template <class Func> template <class Func>
inline bool TestDeathTest(const char* stmt, Func&& func, DeathTest::ResultKind ExpectResult, DebugInfoMatcher Matcher = AnyMatcher) { inline bool TestDeathTest(const char* stmt, Func&& func, DeathTest::ResultKind ExpectResult, DebugInfoMatcher Matcher = AnyMatcher) {
DeathTest DT(Matcher); DeathTest DT(Matcher);
DeathTest::ResultKind RK = DT.Run(func); DeathTest::ResultKind RK = DT.Run(func);
auto OnFailure = [&](std::string msg) { auto OnFailure = [&](std::string msg) {
std::cerr << "EXPECT_DEATH( " << stmt << " ) failed! (" << msg << ")\n\n"; std::fprintf(stderr, "EXPECT_DEATH( %s ) failed! (%s)\n\n", stmt, msg.c_str());
if (!DT.getChildStdErr().empty()) { if (!DT.getChildStdErr().empty()) {
std::cerr << "---------- standard err ----------\n"; std::fprintf(stderr, "---------- standard err ----------\n%s\n", DT.getChildStdErr().c_str());
std::cerr << DT.getChildStdErr() << "\n";
} }
if (!DT.getChildStdOut().empty()) { if (!DT.getChildStdOut().empty()) {
std::cerr << "---------- standard out ----------\n"; std::fprintf(stderr, "---------- standard out ----------\n%s\n", DT.getChildStdOut().c_str());
std::cerr << DT.getChildStdOut() << "\n";
} }
return false; return false;
}; };

View File

@ -19,13 +19,13 @@
#endif #endif
#include <__debug> #include <__debug>
#include <utility>
#include <cstddef>
#include <cstdlib>
#include <cassert> #include <cassert>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <string_view> #include <string_view>
#include <sstream> #include <utility>
#include <iostream>
#include <unistd.h> #include <unistd.h>
#include <sys/wait.h> #include <sys/wait.h>
@ -53,19 +53,15 @@ struct DebugInfoMatcher {
return true; return true;
// Write to stdout because that's the file descriptor captured by the parent // Write to stdout because that's the file descriptor captured by the parent
// process. // process.
std::cout << "Failed to match debug info!\n" std::printf("Failed to match debug info!\n%s\nVS\n%s\n", ToString().data(), got.what().data());
<< ToString() << "\n"
<< "VS\n"
<< got.what() << "\n";
return false; return false;
} }
std::string ToString() const { std::string ToString() const {
std::stringstream ss; std::string result = "msg = \""; result += msg; result += "\"\n";
ss << "msg = \"" << msg << "\"\n" result += "line = " + (line == any_line ? "'*'" : std::to_string(line)) + "\n";
<< "line = " << (line == any_line ? "'*'" : std::to_string(line)) << "\n" result += "file = " + (file == any_file ? "'*'" : std::string(any_file));
<< "file = " << (file == any_file ? "'*'" : any_file) << ""; return result;
return ss.str();
} }
bool empty() const { return is_empty; } bool empty() const { return is_empty; }
@ -260,17 +256,15 @@ inline bool ExpectDeath(const char* stmt, Func&& func, DebugInfoMatcher Matcher)
DeathTest DT(Matcher); DeathTest DT(Matcher);
DeathTest::ResultKind RK = DT.Run(func); DeathTest::ResultKind RK = DT.Run(func);
auto OnFailure = [&](const char* msg) { auto OnFailure = [&](const char* msg) {
std::cerr << "EXPECT_DEATH( " << stmt << " ) failed! (" << msg << ")\n\n"; std::fprintf(stderr, "EXPECT_DEATH( %s ) failed! (%s)\n\n", stmt, msg);
if (RK != DeathTest::RK_Unknown) { if (RK != DeathTest::RK_Unknown) {
std::cerr << "child exit code: " << DT.getChildExitCode() << "\n"; std::fprintf(stderr, "child exit code: %d\n", DT.getChildExitCode());
} }
if (!DT.getChildStdErr().empty()) { if (!DT.getChildStdErr().empty()) {
std::cerr << "---------- standard err ----------\n"; std::fprintf(stderr, "---------- standard err ----------\n%s\n", DT.getChildStdErr().c_str());
std::cerr << DT.getChildStdErr() << "\n";
} }
if (!DT.getChildStdOut().empty()) { if (!DT.getChildStdOut().empty()) {
std::cerr << "---------- standard out ----------\n"; std::fprintf(stderr, "---------- standard out ----------\n%s\n", DT.getChildStdOut().c_str());
std::cerr << DT.getChildStdOut() << "\n";
} }
return false; return false;
}; };