0
0
mirror of https://github.com/google/googletest.git synced 2025-03-19 18:33:48 +00:00

Googletest export

I'm not sure how this relates to the GitHub repo. Could you please advise?

PiperOrigin-RevId: 339060470
This commit is contained in:
Abseil Team 2020-10-26 13:01:10 -04:00 committed by vslashg
parent a6dfd3aca7
commit 1845b85a0e

View File

@ -189,11 +189,11 @@ write a predicate function that returns `AssertionResult` instead of `bool`. For
example, if you define `IsEven()` as: example, if you define `IsEven()` as:
```c++ ```c++
::testing::AssertionResult IsEven(int n) { testing::AssertionResult IsEven(int n) {
if ((n % 2) == 0) if ((n % 2) == 0)
return ::testing::AssertionSuccess(); return testing::AssertionSuccess();
else else
return ::testing::AssertionFailure() << n << " is odd"; return testing::AssertionFailure() << n << " is odd";
} }
``` ```
@ -227,11 +227,11 @@ are fine with making the predicate slower in the success case, you can supply a
success message: success message:
```c++ ```c++
::testing::AssertionResult IsEven(int n) { testing::AssertionResult IsEven(int n) {
if ((n % 2) == 0) if ((n % 2) == 0)
return ::testing::AssertionSuccess() << n << " is even"; return testing::AssertionSuccess() << n << " is even";
else else
return ::testing::AssertionFailure() << n << " is odd"; return testing::AssertionFailure() << n << " is odd";
} }
``` ```
@ -262,7 +262,7 @@ a predicate, `(ASSERT|EXPECT)_PRED_FORMAT*` take a *predicate-formatter*
(`pred_formatn`), which is a function or functor with the signature: (`pred_formatn`), which is a function or functor with the signature:
```c++ ```c++
::testing::AssertionResult PredicateFormattern(const char* expr1, testing::AssertionResult PredicateFormattern(const char* expr1,
const char* expr2, const char* expr2,
... ...
const char* exprn, const char* exprn,
@ -287,13 +287,13 @@ used with `EXPECT_PRED2()`:
int SmallestPrimeCommonDivisor(int m, int n) { ... } int SmallestPrimeCommonDivisor(int m, int n) { ... }
// A predicate-formatter for asserting that two integers are mutually prime. // A predicate-formatter for asserting that two integers are mutually prime.
::testing::AssertionResult AssertMutuallyPrime(const char* m_expr, testing::AssertionResult AssertMutuallyPrime(const char* m_expr,
const char* n_expr, const char* n_expr,
int m, int m,
int n) { int n) {
if (MutuallyPrime(m, n)) return ::testing::AssertionSuccess(); if (MutuallyPrime(m, n)) return testing::AssertionSuccess();
return ::testing::AssertionFailure() << m_expr << " and " << n_expr return testing::AssertionFailure() << m_expr << " and " << n_expr
<< " (" << m << " and " << n << ") are not mutually prime, " << " (" << m << " and " << n << ") are not mutually prime, "
<< "as they have a common divisor " << SmallestPrimeCommonDivisor(m, n); << "as they have a common divisor " << SmallestPrimeCommonDivisor(m, n);
} }
@ -362,8 +362,8 @@ that can be used in predicate assertion macros (e.g. `EXPECT_PRED_FORMAT2`,
etc). etc).
```c++ ```c++
EXPECT_PRED_FORMAT2(::testing::FloatLE, val1, val2); EXPECT_PRED_FORMAT2(testing::FloatLE, val1, val2);
EXPECT_PRED_FORMAT2(::testing::DoubleLE, val1, val2); EXPECT_PRED_FORMAT2(testing::DoubleLE, val1, val2);
``` ```
Verifies that `val1` is less than, or almost equal to, `val2`. You can replace Verifies that `val1` is less than, or almost equal to, `val2`. You can replace
@ -433,7 +433,7 @@ its DOM tree matches an
```c++ ```c++
// Currently still in //template/prototemplate/testing:xpath_matcher // Currently still in //template/prototemplate/testing:xpath_matcher
#include "template/prototemplate/testing/xpath_matcher.h" #include "template/prototemplate/testing/xpath_matcher.h"
using prototemplate::testing::MatchesXPath; using ::prototemplate::testing::MatchesXPath;
EXPECT_THAT(html_string, MatchesXPath("//a[text()='click here']")); EXPECT_THAT(html_string, MatchesXPath("//a[text()='click here']"));
``` ```
@ -480,7 +480,7 @@ instantiated. For example, given:
```c++ ```c++
template <typename T> class Foo { template <typename T> class Foo {
public: public:
void Bar() { ::testing::StaticAssertTypeEq<int, T>(); } void Bar() { testing::StaticAssertTypeEq<int, T>(); }
}; };
``` ```
@ -609,7 +609,7 @@ call `::testing::PrintToString(x)`, which returns an `std::string`:
vector<pair<Bar, int> > bar_ints = GetBarIntVector(); vector<pair<Bar, int> > bar_ints = GetBarIntVector();
EXPECT_TRUE(IsCorrectBarIntVector(bar_ints)) EXPECT_TRUE(IsCorrectBarIntVector(bar_ints))
<< "bar_ints = " << ::testing::PrintToString(bar_ints); << "bar_ints = " << testing::PrintToString(bar_ints);
``` ```
## Death Tests ## Death Tests
@ -678,7 +678,7 @@ This expression is `true` if the program exited normally with the given exit
code. code.
```c++ ```c++
::testing::KilledBySignal(signal_number) // Not available on Windows. testing::KilledBySignal(signal_number) // Not available on Windows.
``` ```
This expression is `true` if the program was killed by the given signal. This expression is `true` if the program was killed by the given signal.
@ -711,11 +711,11 @@ TEST(MyDeathTest, Foo) {
} }
TEST(MyDeathTest, NormalExit) { TEST(MyDeathTest, NormalExit) {
EXPECT_EXIT(NormalExit(), ::testing::ExitedWithCode(0), "Success"); EXPECT_EXIT(NormalExit(), testing::ExitedWithCode(0), "Success");
} }
TEST(MyDeathTest, KillMyself) { TEST(MyDeathTest, KillMyself) {
EXPECT_EXIT(KillMyself(), ::testing::KilledBySignal(SIGKILL), EXPECT_EXIT(KillMyself(), testing::KilledBySignal(SIGKILL),
"Sending myself unblockable signal"); "Sending myself unblockable signal");
} }
``` ```
@ -742,7 +742,7 @@ If a test fixture class is shared by normal tests and death tests, you can use
duplicating its code: duplicating its code:
```c++ ```c++
class FooTest : public ::testing::Test { ... }; class FooTest : public testing::Test { ... };
using FooDeathTest = FooTest; using FooDeathTest = FooTest;
@ -802,7 +802,7 @@ limited syntax only.
Under the hood, `ASSERT_EXIT()` spawns a new process and executes the death test Under the hood, `ASSERT_EXIT()` spawns a new process and executes the death test
statement in that process. The details of how precisely that happens depend on statement in that process. The details of how precisely that happens depend on
the platform and the variable ::testing::GTEST_FLAG(death_test_style) (which is the platform and the variable `::testing::GTEST_FLAG(death_test_style)` (which is
initialized from the command-line flag `--gtest_death_test_style`). initialized from the command-line flag `--gtest_death_test_style`).
* On POSIX systems, `fork()` (or `clone()` on Linux) is used to spawn the * On POSIX systems, `fork()` (or `clone()` on Linux) is used to spawn the
@ -867,13 +867,13 @@ restored afterwards, so you need not do that yourself. For example:
```c++ ```c++
int main(int argc, char** argv) { int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv); testing::InitGoogleTest(&argc, argv);
::testing::FLAGS_gtest_death_test_style = "fast"; testing::FLAGS_gtest_death_test_style = "fast";
return RUN_ALL_TESTS(); return RUN_ALL_TESTS();
} }
TEST(MyDeathTest, TestOne) { TEST(MyDeathTest, TestOne) {
::testing::FLAGS_gtest_death_test_style = "threadsafe"; testing::FLAGS_gtest_death_test_style = "threadsafe";
// This test is run in the "threadsafe" style: // This test is run in the "threadsafe" style:
ASSERT_DEATH(ThisShouldDie(), ""); ASSERT_DEATH(ThisShouldDie(), "");
} }
@ -1110,7 +1110,7 @@ If `HasFatalFailure()` is used outside of `TEST()` , `TEST_F()` , or a test
fixture, you must add the `::testing::Test::` prefix, as in: fixture, you must add the `::testing::Test::` prefix, as in:
```c++ ```c++
if (::testing::Test::HasFatalFailure()) return; if (testing::Test::HasFatalFailure()) return;
``` ```
Similarly, `HasNonfatalFailure()` returns `true` if the current test has at Similarly, `HasNonfatalFailure()` returns `true` if the current test has at
@ -1189,7 +1189,7 @@ state to its original value before passing control to the next test.
Here's an example of per-test-suite set-up and tear-down: Here's an example of per-test-suite set-up and tear-down:
```c++ ```c++
class FooTest : public ::testing::Test { class FooTest : public testing::Test {
protected: protected:
// Per-test-suite set-up. // Per-test-suite set-up.
// Called before the first test in this test suite. // Called before the first test in this test suite.
@ -1240,7 +1240,7 @@ First, you subclass the `::testing::Environment` class to define a test
environment, which knows how to set-up and tear-down: environment, which knows how to set-up and tear-down:
```c++ ```c++
class Environment : public ::testing::Environment { class Environment : public testing::Environment {
public: public:
~Environment() override {} ~Environment() override {}
@ -1278,8 +1278,8 @@ probably in `main()`. If you use `gtest_main`, you need to call this before
variable like this: variable like this:
```c++ ```c++
::testing::Environment* const foo_env = testing::Environment* const foo_env =
::testing::AddGlobalTestEnvironment(new FooEnvironment); testing::AddGlobalTestEnvironment(new FooEnvironment);
``` ```
However, we strongly recommend you to write your own `main()` and call However, we strongly recommend you to write your own `main()` and call
@ -1535,7 +1535,7 @@ Remember to derive it from `::testing::Test`:
```c++ ```c++
template <typename T> template <typename T>
class FooTest : public ::testing::Test { class FooTest : public testing::Test {
public: public:
... ...
using List = std::list<T>; using List = std::list<T>;
@ -1603,7 +1603,7 @@ First, define a fixture class template, as we did with typed tests:
```c++ ```c++
template <typename T> template <typename T>
class FooTest : public ::testing::Test { class FooTest : public testing::Test {
... ...
}; };
``` ```
@ -1761,7 +1761,7 @@ To test them, we use the following special techniques:
```c++ ```c++
namespace my_namespace { namespace my_namespace {
class FooTest : public ::testing::Test { class FooTest : public testing::Test {
protected: protected:
... ...
}; };
@ -1856,7 +1856,7 @@ undefined.
Use case example: Use case example:
```c++ ```c++
class MyFixture : public ::testing::Test { class MyFixture : public testing::Test {
public: public:
// All of these optional, just like in regular macro usage. // All of these optional, just like in regular macro usage.
static void SetUpTestSuite() { ... } static void SetUpTestSuite() { ... }
@ -1876,7 +1876,7 @@ class MyTest : public MyFixture {
void RegisterMyTests(const std::vector<int>& values) { void RegisterMyTests(const std::vector<int>& values) {
for (int v : values) { for (int v : values) {
::testing::RegisterTest( testing::RegisterTest(
"MyFixture", ("Test" + std::to_string(v)).c_str(), nullptr, "MyFixture", ("Test" + std::to_string(v)).c_str(), nullptr,
std::to_string(v).c_str(), std::to_string(v).c_str(),
__FILE__, __LINE__, __FILE__, __LINE__,
@ -1921,8 +1921,8 @@ To obtain a `TestInfo` object for the currently running test, call
```c++ ```c++
// Gets information about the currently running test. // Gets information about the currently running test.
// Do NOT delete the returned object - it's managed by the UnitTest class. // Do NOT delete the returned object - it's managed by the UnitTest class.
const ::testing::TestInfo* const test_info = const testing::TestInfo* const test_info =
::testing::UnitTest::GetInstance()->current_test_info(); testing::UnitTest::GetInstance()->current_test_info();
printf("We are in test %s of test suite %s.\n", printf("We are in test %s of test suite %s.\n",
test_info->name(), test_info->name(),
@ -1968,15 +1968,15 @@ interesting information about the event and the test program's state.
Here's an example: Here's an example:
```c++ ```c++
class MinimalistPrinter : public ::testing::EmptyTestEventListener { class MinimalistPrinter : public testing::EmptyTestEventListener {
// Called before a test starts. // Called before a test starts.
virtual void OnTestStart(const ::testing::TestInfo& test_info) { virtual void OnTestStart(const testing::TestInfo& test_info) {
printf("*** Test %s.%s starting.\n", printf("*** Test %s.%s starting.\n",
test_info.test_suite_name(), test_info.name()); test_info.test_suite_name(), test_info.name());
} }
// Called after a failed assertion or a SUCCESS(). // Called after a failed assertion or a SUCCESS().
virtual void OnTestPartResult(const ::testing::TestPartResult& test_part_result) { virtual void OnTestPartResult(const testing::TestPartResult& test_part_result) {
printf("%s in %s:%d\n%s\n", printf("%s in %s:%d\n%s\n",
test_part_result.failed() ? "*** Failure" : "Success", test_part_result.failed() ? "*** Failure" : "Success",
test_part_result.file_name(), test_part_result.file_name(),
@ -1985,7 +1985,7 @@ Here's an example:
} }
// Called after a test ends. // Called after a test ends.
virtual void OnTestEnd(const ::testing::TestInfo& test_info) { virtual void OnTestEnd(const testing::TestInfo& test_info) {
printf("*** Test %s.%s ending.\n", printf("*** Test %s.%s ending.\n",
test_info.test_suite_name(), test_info.name()); test_info.test_suite_name(), test_info.name());
} }
@ -2001,10 +2001,10 @@ the "s" at the end of the name) in your `main()` function, before calling
```c++ ```c++
int main(int argc, char** argv) { int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv); testing::InitGoogleTest(&argc, argv);
// Gets hold of the event listener list. // Gets hold of the event listener list.
::testing::TestEventListeners& listeners = testing::TestEventListeners& listeners =
::testing::UnitTest::GetInstance()->listeners(); testing::UnitTest::GetInstance()->listeners();
// Adds a listener to the end. googletest takes the ownership. // Adds a listener to the end. googletest takes the ownership.
listeners.Append(new MinimalistPrinter); listeners.Append(new MinimalistPrinter);
return RUN_ALL_TESTS(); return RUN_ALL_TESTS();
@ -2149,7 +2149,7 @@ will still be compiled:
// Tests that Foo does Abc. // Tests that Foo does Abc.
TEST(FooTest, DISABLED_DoesAbc) { ... } TEST(FooTest, DISABLED_DoesAbc) { ... }
class DISABLED_BarTest : public ::testing::Test { ... }; class DISABLED_BarTest : public testing::Test { ... };
// Tests that Bar does Xyz. // Tests that Bar does Xyz.
TEST_F(DISABLED_BarTest, DoesXyz) { ... } TEST_F(DISABLED_BarTest, DoesXyz) { ... }