Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
googletest
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Chen Yisong
googletest
Commits
6f8a6643
Commit
6f8a6643
authored
Jul 28, 2015
by
kosak
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Introduce FormatForComparison to format values that are operands of comparison…
Introduce FormatForComparison to format values that are operands of comparison assertions (e.g. ASSERT_EQ).
parent
0b10cfd5
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
97 additions
and
97 deletions
+97
-97
gtest-printers.h
include/gtest/gtest-printers.h
+97
-0
gtest.h
include/gtest/gtest.h
+0
-97
No files found.
include/gtest/gtest-printers.h
View file @
6f8a6643
...
@@ -254,6 +254,103 @@ void DefaultPrintNonContainerTo(const T& value, ::std::ostream* os) {
...
@@ -254,6 +254,103 @@ void DefaultPrintNonContainerTo(const T& value, ::std::ostream* os) {
namespace
testing
{
namespace
testing
{
namespace
internal
{
namespace
internal
{
// FormatForComparison<ToPrint, OtherOperand>::Format(value) formats a
// value of type ToPrint that is an operand of a comparison assertion
// (e.g. ASSERT_EQ). OtherOperand is the type of the other operand in
// the comparison, and is used to help determine the best way to
// format the value. In particular, when the value is a C string
// (char pointer) and the other operand is an STL string object, we
// want to format the C string as a string, since we know it is
// compared by value with the string object. If the value is a char
// pointer but the other operand is not an STL string object, we don't
// know whether the pointer is supposed to point to a NUL-terminated
// string, and thus want to print it as a pointer to be safe.
//
// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
// The default case.
template
<
typename
ToPrint
,
typename
OtherOperand
>
class
FormatForComparison
{
public
:
static
::
std
::
string
Format
(
const
ToPrint
&
value
)
{
return
::
testing
::
PrintToString
(
value
);
}
};
// Array.
template
<
typename
ToPrint
,
size_t
N
,
typename
OtherOperand
>
class
FormatForComparison
<
ToPrint
[
N
],
OtherOperand
>
{
public
:
static
::
std
::
string
Format
(
const
ToPrint
*
value
)
{
return
FormatForComparison
<
const
ToPrint
*
,
OtherOperand
>::
Format
(
value
);
}
};
// By default, print C string as pointers to be safe, as we don't know
// whether they actually point to a NUL-terminated string.
#define GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(CharType) \
template <typename OtherOperand> \
class FormatForComparison<CharType*, OtherOperand> { \
public: \
static ::std::string Format(CharType* value) { \
return ::testing::PrintToString(static_cast<const void*>(value)); \
} \
}
GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_
(
char
);
GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_
(
const
char
);
GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_
(
wchar_t
);
GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_
(
const
wchar_t
);
#undef GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_
// If a C string is compared with an STL string object, we know it's meant
// to point to a NUL-terminated string, and thus can print it as a string.
#define GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(CharType, OtherStringType) \
template <> \
class FormatForComparison<CharType*, OtherStringType> { \
public: \
static ::std::string Format(CharType* value) { \
return ::testing::PrintToString(value); \
} \
}
GTEST_IMPL_FORMAT_C_STRING_AS_STRING_
(
char
,
::
std
::
string
);
GTEST_IMPL_FORMAT_C_STRING_AS_STRING_
(
const
char
,
::
std
::
string
);
#if GTEST_HAS_GLOBAL_STRING
GTEST_IMPL_FORMAT_C_STRING_AS_STRING_
(
char
,
::
string
);
GTEST_IMPL_FORMAT_C_STRING_AS_STRING_
(
const
char
,
::
string
);
#endif
#if GTEST_HAS_GLOBAL_WSTRING
GTEST_IMPL_FORMAT_C_STRING_AS_STRING_
(
wchar_t
,
::
wstring
);
GTEST_IMPL_FORMAT_C_STRING_AS_STRING_
(
const
wchar_t
,
::
wstring
);
#endif
#if GTEST_HAS_STD_WSTRING
GTEST_IMPL_FORMAT_C_STRING_AS_STRING_
(
wchar_t
,
::
std
::
wstring
);
GTEST_IMPL_FORMAT_C_STRING_AS_STRING_
(
const
wchar_t
,
::
std
::
wstring
);
#endif
#undef GTEST_IMPL_FORMAT_C_STRING_AS_STRING_
// Formats a comparison assertion (e.g. ASSERT_EQ, EXPECT_LT, and etc)
// operand to be used in a failure message. The type (but not value)
// of the other operand may affect the format. This allows us to
// print a char* as a raw pointer when it is compared against another
// char* or void*, and print it as a C string when it is compared
// against an std::string object, for example.
//
// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
template
<
typename
T1
,
typename
T2
>
std
::
string
FormatForComparisonFailureMessage
(
const
T1
&
value
,
const
T2
&
/* other_operand */
)
{
return
FormatForComparison
<
T1
,
T2
>::
Format
(
value
);
}
// UniversalPrinter<T>::Print(value, ostream_ptr) prints the given
// UniversalPrinter<T>::Print(value, ostream_ptr) prints the given
// value to the given ostream. The caller must ensure that
// value to the given ostream. The caller must ensure that
// 'ostream_ptr' is not NULL, or the behavior is undefined.
// 'ostream_ptr' is not NULL, or the behavior is undefined.
...
...
include/gtest/gtest.h
View file @
6f8a6643
...
@@ -1368,103 +1368,6 @@ GTEST_API_ void InitGoogleTest(int* argc, wchar_t** argv);
...
@@ -1368,103 +1368,6 @@ GTEST_API_ void InitGoogleTest(int* argc, wchar_t** argv);
namespace
internal
{
namespace
internal
{
// FormatForComparison<ToPrint, OtherOperand>::Format(value) formats a
// value of type ToPrint that is an operand of a comparison assertion
// (e.g. ASSERT_EQ). OtherOperand is the type of the other operand in
// the comparison, and is used to help determine the best way to
// format the value. In particular, when the value is a C string
// (char pointer) and the other operand is an STL string object, we
// want to format the C string as a string, since we know it is
// compared by value with the string object. If the value is a char
// pointer but the other operand is not an STL string object, we don't
// know whether the pointer is supposed to point to a NUL-terminated
// string, and thus want to print it as a pointer to be safe.
//
// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
// The default case.
template
<
typename
ToPrint
,
typename
OtherOperand
>
class
FormatForComparison
{
public
:
static
::
std
::
string
Format
(
const
ToPrint
&
value
)
{
return
::
testing
::
PrintToString
(
value
);
}
};
// Array.
template
<
typename
ToPrint
,
size_t
N
,
typename
OtherOperand
>
class
FormatForComparison
<
ToPrint
[
N
],
OtherOperand
>
{
public
:
static
::
std
::
string
Format
(
const
ToPrint
*
value
)
{
return
FormatForComparison
<
const
ToPrint
*
,
OtherOperand
>::
Format
(
value
);
}
};
// By default, print C string as pointers to be safe, as we don't know
// whether they actually point to a NUL-terminated string.
#define GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(CharType) \
template <typename OtherOperand> \
class FormatForComparison<CharType*, OtherOperand> { \
public: \
static ::std::string Format(CharType* value) { \
return ::testing::PrintToString(static_cast<const void*>(value)); \
} \
}
GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_
(
char
);
GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_
(
const
char
);
GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_
(
wchar_t
);
GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_
(
const
wchar_t
);
#undef GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_
// If a C string is compared with an STL string object, we know it's meant
// to point to a NUL-terminated string, and thus can print it as a string.
#define GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(CharType, OtherStringType) \
template <> \
class FormatForComparison<CharType*, OtherStringType> { \
public: \
static ::std::string Format(CharType* value) { \
return ::testing::PrintToString(value); \
} \
}
GTEST_IMPL_FORMAT_C_STRING_AS_STRING_
(
char
,
::
std
::
string
);
GTEST_IMPL_FORMAT_C_STRING_AS_STRING_
(
const
char
,
::
std
::
string
);
#if GTEST_HAS_GLOBAL_STRING
GTEST_IMPL_FORMAT_C_STRING_AS_STRING_
(
char
,
::
string
);
GTEST_IMPL_FORMAT_C_STRING_AS_STRING_
(
const
char
,
::
string
);
#endif
#if GTEST_HAS_GLOBAL_WSTRING
GTEST_IMPL_FORMAT_C_STRING_AS_STRING_
(
wchar_t
,
::
wstring
);
GTEST_IMPL_FORMAT_C_STRING_AS_STRING_
(
const
wchar_t
,
::
wstring
);
#endif
#if GTEST_HAS_STD_WSTRING
GTEST_IMPL_FORMAT_C_STRING_AS_STRING_
(
wchar_t
,
::
std
::
wstring
);
GTEST_IMPL_FORMAT_C_STRING_AS_STRING_
(
const
wchar_t
,
::
std
::
wstring
);
#endif
#undef GTEST_IMPL_FORMAT_C_STRING_AS_STRING_
// Formats a comparison assertion (e.g. ASSERT_EQ, EXPECT_LT, and etc)
// operand to be used in a failure message. The type (but not value)
// of the other operand may affect the format. This allows us to
// print a char* as a raw pointer when it is compared against another
// char* or void*, and print it as a C string when it is compared
// against an std::string object, for example.
//
// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
template
<
typename
T1
,
typename
T2
>
std
::
string
FormatForComparisonFailureMessage
(
const
T1
&
value
,
const
T2
&
/* other_operand */
)
{
return
FormatForComparison
<
T1
,
T2
>::
Format
(
value
);
}
// Separate the error generating code from the code path to reduce the stack
// Separate the error generating code from the code path to reduce the stack
// frame size of CmpHelperEQ. This helps reduce the overhead of some sanitizers
// frame size of CmpHelperEQ. This helps reduce the overhead of some sanitizers
// when calling EXPECT_* in a tight loop.
// when calling EXPECT_* in a tight loop.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment