mirror of
https://github.com/google/googletest.git
synced 2026-03-20 03:06:39 +00:00
Add string_view overload to AssertHelper
PiperOrigin-RevId: 871382613 Change-Id: I286ccbf8b8622a740e99642b4ed853e9f67cf51c
This commit is contained in:
committed by
Copybara-Service
parent
77f6bd3e75
commit
850cb69efd
@@ -37,6 +37,7 @@
|
||||
#include <iosfwd>
|
||||
#include <ostream>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
|
||||
#include "gtest/internal/gtest-internal.h"
|
||||
@@ -65,10 +66,10 @@ class GTEST_API_ [[nodiscard]] TestPartResult {
|
||||
// C'tor. TestPartResult does NOT have a default constructor.
|
||||
// Always use this constructor (with parameters) to create a
|
||||
// TestPartResult object.
|
||||
TestPartResult(Type a_type, const char* a_file_name, int a_line_number,
|
||||
const char* a_message)
|
||||
TestPartResult(Type a_type, std::string_view a_file_name, int a_line_number,
|
||||
std::string_view a_message)
|
||||
: type_(a_type),
|
||||
file_name_(a_file_name == nullptr ? "" : a_file_name),
|
||||
file_name_(a_file_name),
|
||||
line_number_(a_line_number),
|
||||
summary_(ExtractSummary(a_message)),
|
||||
message_(a_message) {}
|
||||
@@ -112,7 +113,7 @@ class GTEST_API_ [[nodiscard]] TestPartResult {
|
||||
|
||||
// Gets the summary of the failure message by omitting the stack
|
||||
// trace in it.
|
||||
static std::string ExtractSummary(const char* message);
|
||||
static std::string ExtractSummary(std::string_view message);
|
||||
|
||||
// The name of the source file where the test part took place, or
|
||||
// "" if the source file is unknown.
|
||||
|
||||
@@ -57,6 +57,7 @@
|
||||
#include <set>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
|
||||
@@ -1246,7 +1247,7 @@ class GTEST_API_ [[nodiscard]] UnitTest {
|
||||
// eventually call this to report their results. The user code
|
||||
// should use the assertion macros instead of calling this directly.
|
||||
void AddTestPartResult(TestPartResult::Type result_type,
|
||||
const char* file_name, int line_number,
|
||||
std::string_view file_name, int line_number,
|
||||
const std::string& message,
|
||||
const std::string& os_stack_trace)
|
||||
GTEST_LOCK_EXCLUDED_(mutex_);
|
||||
@@ -1619,6 +1620,8 @@ class GTEST_API_ [[nodiscard]] AssertHelper {
|
||||
// Constructor.
|
||||
AssertHelper(TestPartResult::Type type, const char* file, int line,
|
||||
const char* message);
|
||||
AssertHelper(TestPartResult::Type type, std::string_view file, int line,
|
||||
std::string_view message);
|
||||
~AssertHelper();
|
||||
|
||||
// Message assignment is a semantic trick to enable assertion
|
||||
@@ -1632,12 +1635,12 @@ class GTEST_API_ [[nodiscard]] AssertHelper {
|
||||
// re-using stack space even for temporary variables, so every EXPECT_EQ
|
||||
// reserves stack space for another AssertHelper.
|
||||
struct AssertHelperData {
|
||||
AssertHelperData(TestPartResult::Type t, const char* srcfile, int line_num,
|
||||
const char* msg)
|
||||
AssertHelperData(TestPartResult::Type t, std::string_view srcfile,
|
||||
int line_num, std::string_view msg)
|
||||
: type(t), file(srcfile), line(line_num), message(msg) {}
|
||||
|
||||
TestPartResult::Type const type;
|
||||
const char* const file;
|
||||
const std::string_view file;
|
||||
int const line;
|
||||
std::string const message;
|
||||
|
||||
|
||||
@@ -1452,8 +1452,7 @@ class [[nodiscard]] NeverThrown {
|
||||
; \
|
||||
else \
|
||||
fail(::testing::internal::GetBoolAssertionFailureMessage( \
|
||||
gtest_ar_, text, #actual, #expected) \
|
||||
.c_str())
|
||||
gtest_ar_, text, #actual, #expected))
|
||||
|
||||
#define GTEST_TEST_NO_FATAL_FAILURE_(statement, fail) \
|
||||
GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
|
||||
|
||||
@@ -34,7 +34,9 @@
|
||||
|
||||
#include <ostream>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
#include "gtest/internal/gtest-internal.h"
|
||||
#include "gtest/internal/gtest-port.h"
|
||||
#include "src/gtest-internal-inl.h"
|
||||
|
||||
@@ -42,9 +44,9 @@ namespace testing {
|
||||
|
||||
// Gets the summary of the failure message by omitting the stack trace
|
||||
// in it.
|
||||
std::string TestPartResult::ExtractSummary(const char* message) {
|
||||
const char* const stack_trace = strstr(message, internal::kStackTraceMarker);
|
||||
return stack_trace == nullptr ? message : std::string(message, stack_trace);
|
||||
std::string TestPartResult::ExtractSummary(const std::string_view message) {
|
||||
auto stack_trace = message.find(internal::kStackTraceMarker);
|
||||
return std::string(message.substr(0, stack_trace));
|
||||
}
|
||||
|
||||
// Prints a TestPartResult object.
|
||||
|
||||
@@ -58,6 +58,7 @@
|
||||
#include <ostream> // NOLINT
|
||||
#include <set>
|
||||
#include <sstream>
|
||||
#include <string_view>
|
||||
#include <unordered_set>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
@@ -485,6 +486,15 @@ bool ShouldEmitStackTraceForResultType(TestPartResult::Type type) {
|
||||
// AssertHelper constructor.
|
||||
AssertHelper::AssertHelper(TestPartResult::Type type, const char* file,
|
||||
int line, const char* message)
|
||||
: AssertHelper(
|
||||
type, file == nullptr ? std::string_view() : std::string_view(file),
|
||||
line,
|
||||
message == nullptr ? std::string_view() : std::string_view(message)) {
|
||||
}
|
||||
|
||||
AssertHelper::AssertHelper(TestPartResult::Type type,
|
||||
const std::string_view file, int line,
|
||||
const std::string_view message)
|
||||
: data_(new AssertHelperData(type, file, line, message)) {}
|
||||
|
||||
AssertHelper::~AssertHelper() { delete data_; }
|
||||
@@ -2547,8 +2557,9 @@ void ReportFailureInUnknownLocation(TestPartResult::Type result_type,
|
||||
// AddTestPartResult.
|
||||
UnitTest::GetInstance()->AddTestPartResult(
|
||||
result_type,
|
||||
nullptr, // No info about the source file where the exception occurred.
|
||||
-1, // We have no info on which line caused the exception.
|
||||
std::string_view(), // No info about the source file where the exception
|
||||
// occurred.
|
||||
-1, // We have no info on which line caused the exception.
|
||||
message,
|
||||
""); // No stack trace, either.
|
||||
}
|
||||
@@ -5428,8 +5439,8 @@ Environment* UnitTest::AddEnvironment(Environment* env) {
|
||||
// this to report their results. The user code should use the
|
||||
// assertion macros instead of calling this directly.
|
||||
void UnitTest::AddTestPartResult(TestPartResult::Type result_type,
|
||||
const char* file_name, int line_number,
|
||||
const std::string& message,
|
||||
const std::string_view file_name,
|
||||
int line_number, const std::string& message,
|
||||
const std::string& os_stack_trace)
|
||||
GTEST_LOCK_EXCLUDED_(mutex_) {
|
||||
Message msg;
|
||||
|
||||
@@ -45,7 +45,7 @@ class TestPartResultTest : public Test {
|
||||
TestPartResultTest()
|
||||
: r1_(TestPartResult::kSuccess, "foo/bar.cc", 10, "Success!"),
|
||||
r2_(TestPartResult::kNonFatalFailure, "foo/bar.cc", -1, "Failure!"),
|
||||
r3_(TestPartResult::kFatalFailure, nullptr, -1, "Failure!"),
|
||||
r3_(TestPartResult::kFatalFailure, "", -1, "Failure!"),
|
||||
r4_(TestPartResult::kSkip, "foo/bar.cc", 2, "Skipped!") {}
|
||||
|
||||
TestPartResult r1_, r2_, r3_, r4_;
|
||||
|
||||
Reference in New Issue
Block a user