Compare commits

...

437 Commits

Author SHA1 Message Date
Abseil Team 373af2e3df Provide a better error message when ASSERT and SKIP macros are used in methods that return values
PiperOrigin-RevId: 788083860
Change-Id: I91583f0c0f816144428d56ab07e58fd035c532d1
2025-07-28 11:13:39 -07:00
Abseil Team 32f9f4c82a Skip the predicate on SIGSEGV in death-test on Android builds with API level <= 23.
PiperOrigin-RevId: 786394374
Change-Id: I5188b55ae8ae6d3188e6492f3865b21ae6d2285e
2025-07-23 13:04:17 -07:00
Abseil Team 7e17b15f15 Fix the sample usage of `ConvertGenerator` by removing the type-id name.
PiperOrigin-RevId: 784561837
Change-Id: I6792fdde81762797c64ac24d297d93938932aef4
2025-07-18 06:24:47 -07:00
Abseil Team 309dab8d4b Automated Code Change
PiperOrigin-RevId: 782277586
Change-Id: Idd7ffd0a585fa8b307ecafb8b3d53abbf124a774
2025-07-12 02:06:50 -07:00
Abseil Team 3983f67e32 Replace internal link in GoogleTest docs
Replace an internal link to AbslStringify's documentation with the equivalent public link.

PiperOrigin-RevId: 779085254
Change-Id: I2cbd2b6262a5ced06f166d4fcc7c08796e5a60af
2025-07-04 01:14:13 -07:00
David Pizzuto c67de11737 gtest: Reword fail_if_no_test_selected_message for sharding and filtering.
Users have shown some confusion about the interaction between this flag,
sharding, and --gtest_filter, so let's provide some more information.

PiperOrigin-RevId: 776734799
Change-Id: Icdcf6aa056988095f15588758994604d326c0567
2025-06-27 15:18:25 -07:00
Copybara-Service a45468c0fc Merge pull request #4694 from chromy:main
PiperOrigin-RevId: 775667059
Change-Id: I07edd44ae368ae640d7b985800ffee8cc663c07f
2025-06-25 06:46:45 -07:00
Abseil Team f8ed0e687c Add documentation for exception matchers.
PiperOrigin-RevId: 775205267
Change-Id: I3ad59ff4b1fa095cbf7dd04dd97a152cb33c7435
2025-06-24 06:22:36 -07:00
Abseil Team 35b75a2cba Although the following paragraph explains there is a better solution, having this technique in the bullet point seems to suggest that this technique is considered as a valid alternative. It would be better to drop it or make it clear that this technique is not recommended.
PiperOrigin-RevId: 771116391
Change-Id: I753560115ed65e8858b749473935df57b6a50488
2025-06-13 09:13:47 -07:00
Abseil Team 175c1b55cf Add UnpackStructImpl for structs with 24, 25 and 26 fields.
PiperOrigin-RevId: 770690821
Change-Id: Ic759e29f46a34d0f2c0ef831e0ddc784290a938f
2025-06-12 09:37:31 -07:00
Copybara-Service 1aeec48a1d Merge pull request #4774 from joel-langlois:FixReadme
PiperOrigin-RevId: 770642738
Change-Id: Ie8e8f12f82737f4eb614862334ff0a89583aeafc
2025-06-12 07:21:58 -07:00
Joël Langlois 0fe21ac6ef README.md: Fixed broken Markdown link formatting. 2025-06-11 12:28:02 -04:00
Abseil Team fd15f51d57 Automated Code Change
PiperOrigin-RevId: 769938700
Change-Id: I3f36d03a5d54f1f2fbeda9fbaa5e205736c3cdbc
2025-06-10 21:03:35 -07:00
Abseil Team 6230d316e1 In MatcherCast, store the input value as its own type rather than as the Matcher type, to avoid dangling references
PiperOrigin-RevId: 769240838
Change-Id: I7b1ac23a0a88ba90b5d1ae6e20b97f679f381f31
2025-06-09 12:07:01 -07:00
David Pizzuto 28e9d1f267 googletest: Add a flag to fail if no tests were selected to run.
There are two cases that prompt this behavior:

- All test cases in the binary are disabled.

- There are more shards defined than test cases, so some shards are empty.

The result in each case is the same: the test runner needs to spin up
additional processes that do nothing, which is wasteful, especially when tests
need expensive resources.

PiperOrigin-RevId: 769176856
Change-Id: Ifa399a0b7b68e4add5a94ca148b32b2938a8666d
2025-06-09 09:31:09 -07:00
Abseil Team 7e2c425db2 Remove "blindly" from the gmock "uninteresting call" message.
PiperOrigin-RevId: 767766090
Change-Id: I9202c1c24a3af8d73806f68ca93025b26704178e
2025-06-05 14:31:20 -07:00
Abseil Team e9092b12dc Fix unified diff headers.
The length part (only) of each range is optional when equal to one.
See http://www.gnu.org/software/diffutils/manual/html_node/Detailed-Unified.html

PiperOrigin-RevId: 765326445
Change-Id: I4aec68e82f889e3b4f01861d3b6a16a8b2785ce6
2025-05-30 13:58:42 -07:00
Abseil Team 7427a6b5e3 Use the provided length in ConditionalPrintAsText
While ConditionalPrintAsText gets a char pointer and the string length, it used to only pass the pointer to operator<<. That does not work for strings that are not zero terminated, because operator<< has to resort to strlen, which happily overflows the string buffer.

This CL wraps the char pointer and the length in a string_view and passes that to operator<< to fix that issue.

PiperOrigin-RevId: 765137769
Change-Id: Ie97067ce9d5b23175a512945fee943a8b95a94ff
2025-05-30 04:47:12 -07:00
Copybara-Service 7da55820cc Merge pull request #4765 from qubka:main
PiperOrigin-RevId: 763814429
Change-Id: Id80dc3c910aa955fda6b9ece1aee17032e28130d
2025-05-27 08:56:42 -07:00
qubka 3abc68be30 Fix extra ';' after member function definition 2025-05-25 11:09:39 +01:00
Abseil Team 09ffd00153 Updates Google Analytics tracker.
PiperOrigin-RevId: 762080294
Change-Id: Id874316dc6360e889f54fa631b56c8c6c783cc7a
2025-05-22 12:37:05 -07:00
Daniel Cheng 6aa03e6774 Print std::basic_string_view<Char> as a string literal
This is a followup to a previous change switching std::u8string,
std::u16string, and std::u32string to print as string literals instead
of an array of characters. Also update the PrintCharsAsStringTo<Char>()
helper to handle cases where the pointer to the string data is null;
unlike std::basic_string<Char>, std::basic_string_view<Char>'s data()
is allowed to return nullptr.

The new PrintTo overloads complicate the PrintTo(internal::StringView)
overload, which needs to be disabled if internal::StringView is an alias
for std::string_view to avoid multiple definitions with the same
overload. Simply omitting the unconditional PrintTo(std::string_view)
overload is a worse option though, as this results in std::string_view
not printing nicely if internal::StringView is not an alias for
std::string_view.
PiperOrigin-RevId: 762020327
Change-Id: I92f5bdbacba89e97bbcc0fef3ca9261ea5a788d3
2025-05-22 10:21:59 -07:00
Derek Mauro 16d4f8eff6 Delete the absl polyfill support for std::any, std::optional
and std::variant now that the absl types are aliases of the
std types

PiperOrigin-RevId: 761136061
Change-Id: I702c3e1e8c58d003b8f4da99e7c84c9e5dbe1863
2025-05-20 10:25:35 -07:00
Abseil Team bac6a8fd8a Add UnpackStructImpl specialization for 23 struct members.
PiperOrigin-RevId: 760700889
Change-Id: I52ea2bc83d218f73504c9dfba82ce0c07e59cbb2
2025-05-19 11:21:57 -07:00
Abseil Team fa8438ae6b Use static_cast instead of ImplicitCast_ for character conversions
Clang has recently added "warnings when mixing different charN_t types" [1].
The rationale is that "charN_t represent code units of different UTF encodings.
Therefore the values of 2 different charN_t objects do not represent the same
characters."

Note that the warning here may be legitimate - from https://github.com/google/googletest/issues/4762:
"[...] This is incorrect for values that do not represent valid codepoints."

For the time being, silence the warning by being more explicit about the
conversion being intentional by using static_cast.

Link: https://github.com/llvm/llvm-project/pull/138708 [1]
PiperOrigin-RevId: 760644157
Change-Id: I2e6cc1871975455cecac8731b2f93fd5beeaf0e1
2025-05-19 09:02:31 -07:00
Derek Mauro 571930618f GoogleTest CI Update
* Use abseil-cpp@20250512.0, which requires C++17 and
 removes any/optional/variant polyfills
* Test C++23
* Support/test GCC15
* Use Bazel vendor mode to reduce reliance on GitHub

PiperOrigin-RevId: 759184924
Change-Id: Ifb866cdd7faf1e5be475b44f69870745e21a3104
2025-05-15 09:42:50 -07:00
Mike Kruskal 9f79a9597a Add [[nodiscard]] to GetParam() to prevent accidental misuse.
This helps avoid a situation where someone sets up a parameterized test but forgets to actually use the parameter.

PiperOrigin-RevId: 758455362
Change-Id: Ie4db03e82b6a4e1787be96f154b3fbb25657ae64
2025-05-13 18:26:40 -07:00
Abseil Team 8b8ef3ff0d Clarify that the return value of `InvokeArgument` is not the return value of the action.
PiperOrigin-RevId: 757918204
Change-Id: I3f3544ca602a3863c3bae67b8fb4c57e692f3373
2025-05-12 14:40:55 -07:00
Abseil Team 90a4152114 gtest: Remove incorrect documentation of private headers as PIMPL idiom
PiperOrigin-RevId: 753626014
Change-Id: I3ef90e13b08d5a22d3b220a4e8fbb78df92deb0a
2025-05-01 09:50:38 -07:00
Derek Mauro 04ee1b4f2a Update documentation for v1.17.0
PiperOrigin-RevId: 753214894
Change-Id: I5ec24399301dcc7e70b179db98e175f45374e3c2
2025-04-30 10:11:59 -07:00
Derek Mauro 54915d462b Update GoogleTest dependencies in preparation for release
PiperOrigin-RevId: 753143607
Change-Id: I3dadd610342d86e3c856327e446e956fab8abf04
2025-04-30 06:31:27 -07:00
Laramie Leavitt 59c924bc47 Use std::memcpy instead of a union to access floating point bits.
PiperOrigin-RevId: 752789717
Change-Id: I20d17677bc75d12fa50328feb2bdc5c25e852cc4
2025-04-29 10:26:44 -07:00
Derek Mauro 00b2154e8e Fix GCC15 warning that <ciso646> is deprecated in C++17
PiperOrigin-RevId: 752706554
Change-Id: I83d35b693efdaabcc63d15169dbf19d63163a563
2025-04-29 06:13:25 -07:00
Abseil Team cd430b47a5 AllOf, AnyOf, Optional: Avoid generating unnecessary match explanations
Previously, those matchers always invoked the child matchers with an IsInterested MatchResultListener, resulting in unnecessary work formatting match results that would be discarded.

PiperOrigin-RevId: 750704295
Change-Id: I1639a3a15d269459f26b3aebc3a6cbdced6896a3
2025-04-23 13:29:56 -07:00
Derek Mauro 155b337c93 Bump RE2 dependency to 2024-07-02.bcr.1
Mirrors the change from https://github.com/bazelbuild/bazel-central-registry/pull/4342

PiperOrigin-RevId: 748665684
Change-Id: I775ff08130046ad7a2dd288e1a58e007e407acd3
2025-04-17 06:50:43 -07:00
Aaron Jacobs e90fe24856 `gmock-actions`: add a missing conversion operator to `WithArgsAction`.
PiperOrigin-RevId: 745802500
Change-Id: I8cf7b69f89af8615e9c405b1e6552d3e12ebf93d
2025-04-09 17:57:30 -07:00
Aaron Jacobs 50a9511f50 `gmock-actions`: run clang-format.
PiperOrigin-RevId: 745757973
Change-Id: I59ca0098a73eaf8a7e68016ba003fc2447e1c702
2025-04-09 15:33:40 -07:00
Andy Getzendanner 52204f78f9 Remove obsolete references to LinuxThreads.
PiperOrigin-RevId: 740860675
Change-Id: I73bf9fd36e6135eb7577da54254254df7eebdf7b
2025-03-26 12:20:16 -07:00
Abseil Team 2ae29b52fd Add UnpackStructImpl for structs with 22 fields.
PiperOrigin-RevId: 739148442
Change-Id: Iae29066daddeea8384f3eb06ff075299156e9579
2025-03-21 05:44:56 -07:00
Tommy Chiang c2ceb2b09b Fix a typo
I have no idea what DLP here means, but ULP make sense.
Assuming DLP is a typo of ULP.

PiperOrigin-RevId: 739113596
Change-Id: Ib8c66d54d95e3b015c79af1285b7008c91b8502e
2025-03-21 03:04:11 -07:00
Abseil Team e7b26b7246 Add a non-'const' overload for the function invoked by the IgnoreArgs action wrapper.
PiperOrigin-RevId: 738865907
Change-Id: Ia43b297692ddca681bf29fa7547a5a4da330e51a
2025-03-20 10:56:05 -07:00
Zhanyong Wan 3af834740f Improve the failure messages of `ElementsAre()`, `ElementsAreArray()`, etc.
NOTE: if you suspect that this change breaks your build, it's likely that your tests depend on the exact messages of `ElementsAre()` and friends. The messages are implementation details of these matcher and are subject to change without notice. Depending on the messages is not supported. In that case, please rewrite your tests to avoid the dependency.

When the array being matched is long, it can be hard for the user to find the mismatched element in the message generated by `ElementsAre()` or `ElementsAreArray()` - even though these matchers print the index of the mismatched element, the user still has to count to find the actual element and its corresponding matcher.

With this change, these matchers will include the actual value and corresponding matcher in the failure message, making it easier for the user.

Also make a small style improvement: now it's advised to write
```
EXPECT_EQ(actual, expected);
```
as opposed to
```
EXPECT_EQ(expected, actual);
```

PiperOrigin-RevId: 738039133
Change-Id: I3b94f7d01a6a4c92e2daf268df8cfb04a21d4294
2025-03-18 09:42:12 -07:00
Zhanyong Wan 4902ea2d7c Clarify the polymorphic matcher concept and explain how to define a composite matcher.
Many gtest users aren't aware that a polymorphic matcher is not actually a `testing::Matcher<>` and thus doesn't know how to describe itself. Clarify this.

Also adds a recipe on how to define a composite matcher. In particular, explain how the composite matcher can describe itself in terms of the description(s) of its sub-matcher(s).

PiperOrigin-RevId: 735840759
Change-Id: I26cff6179349aa739fc7fdd528a2f5308d18c189
2025-03-11 12:04:28 -07:00
Zhanyong Wan 4ee4b17bf5 Allow `DistanceFrom()` to use user-defined `abs()` by default.
`std::abs()` doesn't work on custom types. While one can use the 3-argument version of `DistanceFrom()` to specify how to compute the distance, it's not as convenient as defining `abs()` for the custom type once in the type's namespace and then use the 2-argument version.

PiperOrigin-RevId: 735741409
Change-Id: If8fb668455eb963a2ccf089f7467c64965a2e7a6
2025-03-11 06:58:43 -07:00
Zhanyong Wan 0bdccf4aa2 Add a `DistanceFrom()` matcher for general distance comparison.
We have a bunch of matchers for asserting that a value is near the target value, e.g.
`DoubleNear()` and `FloatNear()`. These matchers only work for specific types (`double` and `float`). They are not flexible enough to support other types that have the notion of a "distance" (e.g. N-dimensional points and vectors, which are commonly used in ML).

In this diff, we generalize the idea to a `DistanceFrom(target, get_distance, m)` matcher that works on arbitrary types that have the "distance" concept (the `get_distance` argument is optional and can be omitted for types that support `-`, and `std::abs()`). What it does:

1. compute the distance between the value and the target using `get_distance(value, target)`; if `get_distance` is omitted, compute the distance as `std::abs(value - target)`.
2. match the distance against matcher `m`; if the match succeeds, the `DistanceFrom()` match succeeds.

Examples:

```
  // 0.5's distance from 0.6 should be <= 0.2.
  EXPECT_THAT(0.5, DistanceFrom(0.6, Le(0.2)));

  Vector2D v1(3.0, 4.0), v2(3.2, 6.0);
  // v1's distance from v2, as computed by EuclideanDistance(v1, v2),
  // should be >= 1.0.
  EXPECT_THAT(v1, DistanceFrom(v2, EuclideanDistance, Ge(1.0)));
```

PiperOrigin-RevId: 734593292
Change-Id: Id6bb7074dc4aa4d8abd78b57ad2426637e590de5
2025-03-07 09:54:00 -08:00
Abseil Team e88cb95b92 Add a `testing::ConvertGenerator` overload that accepts a converting functor. This allows the use of classes that do not have a converting ctor to the desired type.
PiperOrigin-RevId: 733383835
Change-Id: I6fbf79db0509b3d4fe8305a83ed47fceaa820e47
2025-03-04 10:40:25 -08:00
Derek Mauro 24a9e940d4 Try to warn the user when test filters do not match any tests
PiperOrigin-RevId: 732204780
Change-Id: I2c4ccabd123e3b79c3dd8bc768a4cd9a576d282c
2025-02-28 11:36:13 -08:00
pizzud 72189081ca googletest: Add testonly alias of :gtest for testonly cc_libraries to use.
While a testonly library can certainly depend on a non-testonly library, gtest
itself is logically testonly, but is not tagged as such for historical
reasons. Having a testonly target allows gradual migrations towards a
testonly-only world.

PiperOrigin-RevId: 731807655
Change-Id: Ie16f17fb74b80bb6200767a3ac5bafa424162146
2025-02-27 11:49:14 -08:00
Abseil Team 144d335538 Add UnpackStructImpl for structs with 21 fields.
PiperOrigin-RevId: 731737844
Change-Id: I3efeb26b0bf179958c8a3eda0b55dfc3d7778fb9
2025-02-27 08:36:00 -08:00
Derek Mauro e5669fdffc Output to the test warning file if no tests are linked.
Bazel sets the environment variable TEST_WARNINGS_OUTPUT_FILE
https://bazel.build/reference/test-encyclopedia#initial-conditions

If no tests are linked and the new flag --gtest_fail_if_no_test_linked
is not true (which is the current default), we can still warn the user
as this may be a programming error without failing the test (which would
break existing users).

PiperOrigin-RevId: 731402363
Change-Id: Ia481689efd4bd18889feaaa38bc56049a3f651cd
2025-02-26 11:57:12 -08:00
Zhanyong Wan 54501746a6 Adds support for a --gtest_fail_if_no_test_linked flag
to fail the test program if no test case is linked in
(a common programmer mistake).

PiperOrigin-RevId: 730571311
Change-Id: I1dab04adfe35581274d0b4ec79a017014d50e1ea
2025-02-24 13:18:33 -08:00
Derek Mauro 3fbe4db9a3 GoogleTest C++17 cleanups
* Use `[[nodiscard]]` instead of GTEST_MUST_USE_RESULT_
  * Use `[[maybe_unused]]` instead of  GTEST_INTERNAL_ATTRIBUTE_MAYBE_UNUSED
  * Use `[[deprecated]]` instead of `GTEST_INTERNAL_DEPRECATED`
  * Remove `GTEST_INTERNAL_NEED_REDUNDANT_CONSTEXPR_DECL`

PiperOrigin-RevId: 729523519
Change-Id: Ia1b901cf9c0a0e148eec419ada0e0b56aba2dd3d
2025-02-21 07:33:55 -08:00
Jesse Rosenstock a6ce08abf7 Property: Promote note to warning
Property() should not be used against member functions that you do not own.  Make
this warning more prominent in matchers.md and add it to gmock-matchers.h.

PiperOrigin-RevId: 727753777
Change-Id: Idf9155103b04b98efc957ff0d0cf3a8d8662eb72
2025-02-17 01:00:00 -08:00
Derek Mauro c00fd25b71 Require C++17
Policy information:
https://opensource.google/documentation/policies/cplusplus-support#c_language_standard
https://github.com/google/oss-policies-info/blob/main/foundational-cxx-support-matrix.md

Some small fixes are included for C++17 compatibility.

We had no tests for MSVC C++17 or C++20, so those tests that failed
and had no obvious fix are disabled and a tracking bug has been filed.

PiperOrigin-RevId: 726090558
Change-Id: I4d37d47e87c11f85bfd572deb10f67ca3eb2a9b5
2025-02-12 09:44:07 -08:00
Abseil Team 4a00a24fff Add SaveArgByMove
Allows capture of move-only argument types (e.g. AnyInvocable)

PiperOrigin-RevId: 725262899
Change-Id: Idcd46e333a42d99ff05d58a1bc57d8791f6d45a6
2025-02-10 10:22:27 -08:00
Derek Mauro a866428a78 Update docs for v1.16.x branch
Also update the minimum required CMake version
https://github.com/google/oss-policies-info/blob/main/foundational-cxx-support-matrix.md

PiperOrigin-RevId: 724375734
Change-Id: Iefa6c7e2061457bc6149a280d2408085b1fb9d11
2025-02-07 09:41:05 -08:00
Derek Mauro 445e9bd8d0 Update GoogleTest deps in preparation for release
PiperOrigin-RevId: 724135630
Change-Id: I24622387e508d27337769e83f7de8fecae9ad425
2025-02-06 17:40:25 -08:00
Derek Mauro e5443e5c65 Remove empty glob pattern, which is now an error.
It look like this may be a copy-paste bug from the root BUILD file.

PiperOrigin-RevId: 724062332
Change-Id: I8eda2275d996dd14693aee569254db28d9bc4f9d
2025-02-06 13:52:23 -08:00
Abseil Team e235eb34c6 Pull Regexp syntax out of Death test section in advanced.md
Regexps seem to have nothing in common with death tests, yet their description is planted right in the middle of the death test section.

This CL pulls the regexp section one level up and just before death tests.

PiperOrigin-RevId: 721817710
Change-Id: Idc52f450fb10960a590ceb1a70339f86d4478fe4
2025-01-31 10:23:04 -08:00
Abseil Team 66d7401378 Fix extended regular expressions reference URL in advanced.md
The original URL started with https://www.opengroup.org and fails to redirect to the reference page. According to Chrome's dev tools (Network tab), the response to that request is a HTTP 302 Found, with the new location being "balancer://pubs/onlinepubs/009695399/basedefs/xbd_chap09.html". Chrome does not know what to do with that and displays a blank page.

Changing the `www` subdomain to `pubs` results in HTTP 200.

PiperOrigin-RevId: 721804738
Change-Id: I816d08d5baf957c63ac97506d04c7cc49c3a5ce7
2025-01-31 09:45:17 -08:00
Abseil Team b4aae50ce1 Export testsuite properties as XML elements, not attributes.
With this change, arbitrary property names in the testsuite no longer
cause the produced XML output to be ill-formed.

PiperOrigin-RevId: 721549090
Change-Id: Iedffa89bf914478f563c8f3b82cd50557762a665
2025-01-30 16:01:31 -08:00
Abseil Team 2b6b042a77 Cast mutable lvalue references to const from testing::ResultOf
PiperOrigin-RevId: 716343482
Change-Id: I125bc4725886958d026c88f3902a8289e476598b
2025-01-16 12:51:11 -08:00
Abseil Team e4ece4881d Enable safe matcher casts from `Matcher<const T&>` to `Matcher<T>`.
PiperOrigin-RevId: 715826130
Change-Id: Id962fd456f6da21ea2a909f331f92d814f1dad46
2025-01-15 09:16:57 -08:00
Hector Dearman 005254dae2 Default to color output for ghostty 2025-01-10 15:37:52 +00:00
Abseil Team 504ea69cf7 Document unexpected interaction with death test error code and gmock.
If you have a death test that uses mock objects, it's very likely that those mock objects are not properly cleaned up before death. The gMock atexit leak detector will call `_exit(1)`, overriding your expected exit code.

PiperOrigin-RevId: 713722657
Change-Id: I92a326f3923efc9f0e235d825616f3094eca995c
2025-01-09 10:43:08 -08:00
Abseil Team 4bbf80823c Add UnpackStructImpl for structs with 20 fields.
PiperOrigin-RevId: 713272335
Change-Id: I2b289ece4ce3f91834a8c9ba11a4bd18e6e40fca
2025-01-08 06:38:13 -08:00
Abseil Team 7d76a231b0 gtest: Output a canned test case for test suite setup / teardown failures in XML/JSON
This surfaces useful information about the environment failure in a structured form.

As we can see from the updated test, previously unsurfaced information is now present.

PiperOrigin-RevId: 709892315
Change-Id: I2656294d50c33f995bef5c96195a66cff3c4b907
2024-12-26 15:30:52 -08:00
Abseil Team e54519b094 Put the fake Fuchsia SDK in a module extension
This allows users to override the fake SDK with a real one using https://bazel.build/rules/lib/globals/module#override_repo.

Without this change, it is impossible for a project that depends on googletest as a `bazel_dep` to build tests using the "real" Fuchsia SDK, because any references to `@fuchsia_sdk` within googletest `BUILD.bazel` files unconditionally resolve to the "fake" Fuchsia SDK. With this change, if you have the real Fuchsia SDK declared in your `MODULE.bazel`, you can add the following lines to coerce googletest to use the real Fuchsia SDK as well:

    fake_fuchsia_sdk_extension =
    use_extension("@com_google_googletest//:fake_fuchsia_sdk.bzl", "fuchsia_sdk")
    override_repo(fake_fuchsia_sdk_extension, "fuchsia_sdk")

PiperOrigin-RevId: 709139784
Change-Id: I4d10d441c76b7a2481f15723a24f11525dba3878
2024-12-23 13:33:10 -08:00
Abseil Team f3c355f9dd the public version already has the const qualifier
PiperOrigin-RevId: 706721910
Change-Id: I8a76a66d62028176a70678954f095ac70996cc9e
2024-12-16 08:40:16 -08:00
Abseil Team 79219e26e0 Update the links to ISTQB glossary.
PiperOrigin-RevId: 705108072
Change-Id: I209e7fb24bd093412dda1cd5eba10e9ccc2306f4
2024-12-11 08:00:11 -08:00
Abseil Team d122c0d435 Add support for printing C++20 std::*_ordering types to gtest.
Adds feature test macro for C++20 <compare> header, a pretty-printer, and tests.
Inexplicably, these types aren't enums, so can't be handled with a switch.

PiperOrigin-RevId: 704783038
Change-Id: I29688989d18f43520fe610c12a447a20d2f98c95
2024-12-10 11:23:04 -08:00
Copybara-Service 35d0c36560 Merge pull request #4662 from asoffer:patch-1
PiperOrigin-RevId: 698420375
Change-Id: I786e5b50d171c697c21579659a67716442f224df
2024-11-20 09:37:33 -08:00
Andy Soffer 7927f8e93d
Fix documentation bug.
Matchers require the signature of these three functions to be `const`.
2024-11-15 10:19:57 -05:00
Abseil Team d144031940 Fixing a typo in the documentation of IsSubsetOf().
PiperOrigin-RevId: 692957797
Change-Id: Ifd3efe64cd2a3a5623167fa13de61d6e3358662d
2024-11-04 07:26:27 -08:00
Abseil Team 1204d63444 Remove mention of using `ASSERT_...` statements inside custom matcher definitions.
PiperOrigin-RevId: 691994071
Change-Id: I63913152217c99b8c08ae6af8924bc28b02aa9d0
2024-10-31 18:04:00 -07:00
Peter Boström 5ed2186395 Use FAIL() in GTEST_SKIP() documentation
This replaces EXPECT_EQ(0, 1) which would fail if hit.

PiperOrigin-RevId: 690491467
Change-Id: Ieff4e616348254f29200e0ba1d9a6900a2eea130
2024-10-27 23:57:15 -07:00
Abseil Team df1544bcee Avoid creating std::vector<const T> in UnorderedElementsAreArrayMatcher and others.
std::vector<const T> for trivially relocatable types is not allowed by C++ and is rejected by libc++ starting from 4e112e5c1c
PiperOrigin-RevId: 686487841
Change-Id: I3c90c7c0a6e8e23ffa5ebd1702a3f30ebc4a702f
2024-10-16 06:39:54 -07:00
Abseil Team 62df7bdbc1 This change adjusts how `ASSERT_NEAR` and `EXPECT_NEAR` treats infinity, such that `ASSERT_NEAR(inf, inf, 0)` passes. This makes the behavior more consistent with `ASSERT_EQ(inf, inf)` which succeeds.
Some examples of asserts that now pass:
```
ASSERT_NEAR(inf, inf, 0)
ASSERT_NEAR(-inf, inf, inf)
ASSERT_NEAR(inf, x, inf)  // x is any finite floating point value
```

PiperOrigin-RevId: 685748133
Change-Id: I7b3af377773e8e0031e4c6b86830cbbf76bf20c6
2024-10-14 10:13:58 -07:00
Abseil Team 71815bbf7d Automated Code Change
PiperOrigin-RevId: 682549952
Change-Id: Ica81f0020c3001c01543a5851dae7d0b42730c66
2024-10-04 20:54:14 -07:00
Abseil Team a1e255a582 Optional(): Add support for std::optional<>-like types lacking bool conversion.
PiperOrigin-RevId: 681053268
Change-Id: If80ba667fd4c91340e1405a9691f5ca0350fa9eb
2024-10-01 09:35:17 -07:00
Abseil Team 6dae7eb4a5 Use matcher's description in AnyOf when matcher has no explanation.
PiperOrigin-RevId: 675298308
Change-Id: I32d32cafebc7a63fd03e6d957c3a47043d71e5d9
2024-09-16 14:49:27 -07:00
Abseil Team 0953a17a42 Automated Code Change
PiperOrigin-RevId: 668944316
Change-Id: I65aa0a4f3b8a980242849963a3e921ec7fd92b28
2024-08-29 08:17:22 -07:00
Derek Mauro ff233bdd4c Update main to point to 1.15.2 patch release
PiperOrigin-RevId: 658382055
Change-Id: Ia5eed4bec26da8c8cbe29fbd3a41b44048c25e07
2024-08-01 06:34:56 -07:00
Derek Mauro 3e3b44c300 Remove auto-detection of Python toolchain from MODULE.bazel
since it affects downstream users

The correct solution appears to be
https://rules-python.readthedocs.io/en/stable/toolchains.html#library-modules-with-dev-only-python-usage

This change also includes a workaround for the new mechanism creating
paths that are too long for Windows to handle.

Fixes #4581

PiperOrigin-RevId: 657706984
Change-Id: I8ae6b9e5efeb7629d79d14e7d4d3889876282d17
2024-07-30 13:17:40 -07:00
Abseil Team ffa31aec1c Directly include `gmock.h` and `gtest.h` in gmock-matchers tests.
These headers have been included through `gmock-matchers_test.h`

PiperOrigin-RevId: 657612455
Change-Id: I65675f1cfe7789f1821ccacbe60acf90efd5c221
2024-07-30 09:03:18 -07:00
Abseil Team 5bcb2d78a1 Use matcher's description in AllOf if matcher has no explanation.
PiperOrigin-RevId: 655569834
Change-Id: Ia760d74d1cdde766e9719864c5e19c0159da3128
2024-07-24 08:07:57 -07:00
Aaron Jacobs 352788321f gmock-actions: make DoAll convert to OnceAction via custom conversions.
Currently it will refuse to become a `OnceAction` if its component sub-actions
have an `Action` conversion operator but don't know about `OnceAction` in
particular because although `Action` is convertible to `OnceAction`, the
compiler won't follow the chain of conversions.

Instead, teach it explicitly that it can always be a `OnceAction` when it can be
an `Action`.

PiperOrigin-RevId: 655393035
Change-Id: Ib205b518ceef5f256627f4b02cd93ec9bd98343b
2024-07-23 19:43:42 -07:00
Aaron Jacobs 57e107a10e Run clang-format.
PiperOrigin-RevId: 655106304
Change-Id: Ie69b407fce74b31cf71d6dcc1361910d30e86bb5
2024-07-23 03:57:23 -07:00
Derek Mauro cee1ba1f24 Change the Bazel build to use the canonical repo names used by Bzlmod
and the Bazel Central Registry

GoogleTest and its dependencies now use the following names:
GoogleTest: com_google_googletest -> googletest
Abseil: com_google_absl -> abseil-cpp
RE2: com_googlesource_code_re2 -> re2

Bazel users using the WORKSPACE file to specify dependencies may
need to use https://bazel.build/rules/lib/repo/http#http_archive-repo_mapping
to remap repositories to their former names if their dependencies do not
agree on on which name is used.

It is recommended that users migrate to bzlmod to manage Bazel dependencies.

PiperOrigin-RevId: 654430227
Change-Id: Iae8797994d7d87bd4f013dcdc889e7494a6ad2fb
2024-07-20 23:09:44 -07:00
Derek Mauro 9ff2450a56 Update GoogleTest Bazel quickstart for Bzlmod
PiperOrigin-RevId: 652824490
Change-Id: I5e6f57004708e7fa62abb454db9bae81fa265c83
2024-07-16 06:58:45 -07:00
Derek Mauro b62593aceb Update main branch to reflect the release of 1.15.0
PiperOrigin-RevId: 652824317
Change-Id: I39085d9451405c3decb462a0b4682added365d17
2024-07-16 06:57:43 -07:00
Abseil Team d49a665484 Use matcher's description in AllOf if matcher has no explanation.
PiperOrigin-RevId: 652798234
Change-Id: I8e92248a2d9faf2a5719fe220145ea563acc14ff
2024-07-16 05:22:42 -07:00
Abseil Team 417158b8bc Use matcher's description in AllOf if matcher has no explanation.
PiperOrigin-RevId: 652634229
Change-Id: I5fd7eaef4db3dea3d4142e1fb5fc97e46f654358
2024-07-15 16:40:10 -07:00
Derek Mauro 7321f950df Update dependency versions in preparation for release
PiperOrigin-RevId: 652479800
Change-Id: I71574e7dc2ea2e9a2f3071aebbbf0e6b5ce32a35
2024-07-15 07:51:06 -07:00
Derek Mauro 4b21f1abdd README.md: Remove outdated notes about continuous integration and
documentation

PiperOrigin-RevId: 651816538
Change-Id: I3c85b4a26aff8277c627b99b1c5805bad0d29df6
2024-07-12 10:31:17 -07:00
Derek Mauro b4aaf97d8f Workaround GCC 12 -Wrestrict false-positive
Suggested workaround from 751760ad54

Fixes #4570

PiperOrigin-RevId: 651044944
Change-Id: I21f099a15dd3182d335a7891d99b9b1293e5c53e
2024-07-10 09:15:21 -07:00
Derek Mauro 3ef16ef8b3 Fix the documentation that erroneously says you can use short versions
of help flags

Fixes #4564

PiperOrigin-RevId: 650730047
Change-Id: I3a52aca490338066c607e34ab20c827bfde5090d
2024-07-09 12:59:38 -07:00
Abseil Team 34ad51b3dc Add a bounds check to protect against an empty vector from GetArgs(), which
can cause an out of bounds access in GetCurrentExecutableName(). One way this
can happen is if the user forgets to call InitGoogleTest().

PiperOrigin-RevId: 647740658
Change-Id: Id87692aa3d515b8ae0836e474be477d2aafa3871
2024-06-28 11:12:56 -07:00
Abseil Team 1d17ea141d Skip the predicate on SIGSEGV in death-test on Android builds with API level <= 21.
PiperOrigin-RevId: 643076477
Change-Id: Id9486b7d73c3b17851df1eaf8f5ab2085238bf75
2024-06-13 12:00:01 -07:00
Abseil Team a7f443b80b Mention the optional third argument to TYPED_TEST_SUITE
PiperOrigin-RevId: 637896001
Change-Id: Ia3a61ceec4b842e864a0cdfad13e9897bf0ecaaa
2024-05-28 07:42:39 -07:00
Derek Mauro 305e5a238b Update GoogleTest CI Docker image to use Clang 19, GCC 14, and CMake 3.29.3
PiperOrigin-RevId: 636977286
Change-Id: Id9e09398a991c74c2137672948a1eb7dd2d96596
2024-05-24 11:13:43 -07:00
Derek Mauro 9b4993ca7d Change GoogleTest flag removal to not read beyond the end of the array
to the NULL terminator.  #4532 says ASAN complains about this on some
platforms, although it is not clear if ASAN or the platform implementation
is incorrect about accessing the terminating NULL.

Fixes #4532

PiperOrigin-RevId: 635886009
Change-Id: Ibb4237055488c895b1dd09145ab979347bb9a390
2024-05-21 12:08:38 -07:00
Abseil Team c8393f8554 Print mismatches for UnorderedElements() of different sizes.
Changes the behavior of UnorderedElements()/UnorderedElementsAreArray() to print items-without-matchers and matchers-without-items in the case where the actual and expected are different sizes.

PiperOrigin-RevId: 635451316
Change-Id: I2181bb28a14c14cdb577af9268d403e12e942bea
2024-05-20 07:36:47 -07:00
Abseil Team 33af80a883 Make EXPECT_DEATH_IF_SUPPORTED work with matchers
If death tests were not supported the macro would try
to force the second parameter as a string. The second
parameter can be a matcher or a string.

PiperOrigin-RevId: 633199517
Change-Id: If5e71b4be0b569adb273eb4960202197bb264a8f
2024-05-13 07:16:53 -07:00
Copybara-Service fa6de7f438 Merge pull request #4527 from abhina-sree:abhina/add_zos
PiperOrigin-RevId: 631167807
Change-Id: I563883341972bbb82c7407ebf3755d64317b84fc
2024-05-06 13:31:42 -07:00
Abseil Team 2d684efe87 Use std::forward on GoogleTest matcher arguments
This prevents a diagnostic from triggering for a missing std::move.

PiperOrigin-RevId: 631094640
Change-Id: I4ab33964c65f41240fb54a6bf7d0261836fd78e7
2024-05-06 09:48:55 -07:00
Copybara-Service 2d16ed055d Merge pull request #3991 from pgroke-dt:default-enable-thread-support-for-Solaris-AIX-zOS
PiperOrigin-RevId: 630402377
Change-Id: Ie9cafffaa49c2c02220981d665daa672164d35d8
2024-05-03 08:51:23 -07:00
Abseil Team 2954cb8d87 Add example using EXPECT statement in custom matcher
`EXPECT_...` statements can be used inside matcher definitions – this is an important option that is glossed over in this documentation. Users should definitely be aware of this option, since writing custom messages to the `result_listener` can be very cumbersome (and unnecessary) sometimes.

This change adds a relevant example and includes the associated error message it provides on failure.

PiperOrigin-RevId: 630206661
Change-Id: Idee00ba77ce3c1245597aa082f9cd0efff16aceb
2024-05-02 16:07:12 -07:00
Abhina Sreeskantharajan 5035ef3473 add zos for death test support 2024-05-02 09:54:31 -04:00
Paul Groke fbc93e61c4
Merge branch 'main' into default-enable-thread-support-for-Solaris-AIX-zOS 2024-05-02 13:27:54 +02:00
Abseil Team d83fee138a Adds "IWYU pragma: export" to gtest-param-util.h
PiperOrigin-RevId: 628105419
Change-Id: I364e8089b62050c44059e208b610e324eb5a656d
2024-04-25 09:50:29 -07:00
Derek Mauro 5a37b517ad Use `[[maybe_unused]]` when it is available to avoid
-Wused-but-marked-unused warnings

PiperOrigin-RevId: 625430612
Change-Id: Ia9d2e47984e1e6f91966afae8a6750119ae69446
2024-04-16 13:16:40 -07:00
Krzysztof Kosiński 5197b1a8e6 Modify example in the primer to match Testing FAQ.
The CtorVsSetUp section of the FAQ says that constructors and destructors should be preferred over SetUp() and TearDown(), because they will automatically chain up to the fixture's base class, whereas for methods the user must remember to add the chaining manually.

PiperOrigin-RevId: 624273474
Change-Id: Ida41aae193d417eaf996587c7ae1a0099a8cab32
2024-04-12 13:19:21 -07:00
Abseil Team b1a777f319 Fix https://github.com/google/googletest/issues/4423
PiperOrigin-RevId: 623407029
Change-Id: I3559f11e1c18a4f151d521c6ac115c358a11b40c
2024-04-10 01:03:53 -07:00
Copybara-Service 3d73dee972 Merge pull request #4508 from davidmatson:addJsonSkipped
PiperOrigin-RevId: 622929007
Change-Id: Ifaf5a701baee74503e6845f32ebc27425882e950
2024-04-08 13:02:42 -07:00
Abseil Team f10e11fb27 No external change
PiperOrigin-RevId: 622203227
Change-Id: Ic3c24edf917dbda7b02c6ab8ce695398a436dc4c
2024-04-05 09:39:59 -07:00
Lawrence Wolf-Sonkin 0af976647f [gtest] Use `std::index_sequence` and friends instead of rolling our own
* Applies for `std::index_sequence`, `std::make_index_sequence`, and `std::index_sequence_for` replacing `IndexSequence`, `MakeIndexSequence` and IndexSequenceFor`
* Also deleted implementation helper `DoubleSequence`
* The standard interfaces [have been in the standard library since C++14](https://en.cppreference.com/w/cpp/utility/integer_sequence), which [is the minimum supported C++ version by Google Test](https://github.com/google/oss-policies-info/blob/main/foundational-cxx-support-matrix.md)

PiperOrigin-RevId: 621939977
Change-Id: Id264266f08da66c0fa2a6e6fbb8f86fd3cb3a421
2024-04-04 12:38:42 -07:00
Copybara-Service 61db1e1740 Merge pull request #4505 from to01z:add-winapi-partition-games-support
PiperOrigin-RevId: 621631167
Change-Id: I0790f7082ce3c20fef92958c820d40ec854fe91d
2024-04-03 13:59:49 -07:00
Derek Mauro d3a29ff624 Fix a race condition between the Watcher thread and the main thread
during program exit

A race condition exist between the Watcher thread and main(). A case
was found where the Watcher thread does not get execution time before
the main function returns and calls atexit(). At that point the
Watcher thread started runing tls_init() code while the main thread
was shutting down. This resulted in rare crashes and deadlocks.

Fixes #4493
Closes #4494

PiperOrigin-RevId: 621619768
Change-Id: I66f00d8f0f3c37f9937c6d13890f7fa10038256d
2024-04-03 13:21:58 -07:00
David Matson f16770d63d Add skipped messages to JSON output (fixes #4507).
Fix the gap between JSON and XML output.
2024-04-03 08:49:07 -07:00
Derek Mauro ec7b38672c Delete the unofficial GitHub actions tests.
We do not look at these.

Close #4509

PiperOrigin-RevId: 621511191
Change-Id: Icbd3fe0a18444e0888bbec61345c6a957d8899d6
2024-04-03 07:06:06 -07:00
Copybara-Service a2f106c7e2 Merge pull request #4503 from davidmatson:fixTypo
PiperOrigin-RevId: 621334548
Change-Id: I19747f832b51cc5bbb0231bbd9416a8373e4aa77
2024-04-02 16:50:56 -07:00
Copybara-Service c4fec74514 Merge pull request #4502 from davidmatson:fixStreamHelpTextOs
PiperOrigin-RevId: 621172364
Change-Id: Ibaacb17c60b5ee0e58c3689b79aa2cd6b49dc1c4
2024-04-02 07:53:31 -07:00
Abseil Team 77afe8e014 Automated Code Change
PiperOrigin-RevId: 620448229
Change-Id: I487a0d8a8f89ebe82c9ec66fbb60cbe2203188c9
2024-03-30 01:54:42 -07:00
Troels Gram a7678dd0db Revert changes to googletest-port-test.cc
GoogleTest's tests do not currently compile when WINAPI_FAMILY is set to
anything else than WINAPI_FAMILY_DESKTOP_APP on Windows (if WINAPI_FAMILY is not
set explicitly, it will default to WINAPI_FAMILY_DESKTOP_APP).
2024-03-28 22:05:08 -07:00
David Matson f7a6bba24c Fix typo in comment. 2024-03-28 17:34:21 -07:00
David Matson 53ee0fc0b2 Fix help text for stream_result_to flag.
Support for Mac was previously added, in addition to Linux. Fix the help
text to match the code.
2024-03-28 16:59:37 -07:00
Troels Gram f1269cc220 Add support for compiling GoogleTest for Xbox
Support for Xbox platforms, requires the code to support compilation with the
WINAPI_FAMILY_GAMES subset of the Win32 API.

- Add support for WINAPI_FAMILY_GAMES to enable GTEST_OS_WINDOWS_GAMES platform.
- Disable stream redirection (GTEST_HAS_STREAM_REDIRECTION = 0) and colored TTY
  printing for GTEST_OS_WINDOWS_GAMES platform. Both features currently require
  Win32 functions that don't exist in the WINAPI_FAMILY_GAMES subset.

Misc fixes:
- gtest-port.cc: Move GTEST_DISABLE_MSC_DEPRECATED_PUSH_ into
  GTEST_HAS_STREAM_REDIRECTION conditional section where the corresponding
  GTEST_DISABLE_MSC_DEPRECATED_POP_ is located.
- googletest-port-test.cc: Switch stream redirection tests to be conditional on
  GTEST_HAS_STREAM_REDIRECTION instead of !defined(GTEST_OS_WINDOWS_MOBILE).
2024-03-23 22:53:35 -07:00
Copybara-Service eff443c6ef Merge pull request #4490 from memdo:main
PiperOrigin-RevId: 616931521
Change-Id: Iffbb24e3f9add4e7acf8f1988a03afc8628b0733
2024-03-18 13:34:04 -07:00
Mustafa Berkay Düzenli a1a608348e Add colored output support for Alacritty 2024-03-16 00:28:30 +03:00
Abseil Team c231e6f5b1 Add test for move-only type in `Action` signature
PiperOrigin-RevId: 616031018
Change-Id: Ie724f9562174387eab866a824d28106f344c558d
2024-03-15 00:00:12 -07:00
Daniel Cheng b479e7a3c1 Guard Abseil flags usage in googlemock with GTEST_NO_ABSL_FLAGS.
googletest avoids using the Abseil flag library, so googlemock should
do the same for consistency.

PiperOrigin-RevId: 614713968
Change-Id: I0925804b8644ddc6fd3ad07a320d94829b11bb8e
2024-03-11 10:31:56 -07:00
Abseil Team e1a38bc370 Reland: Optimize Google Test process startup
Google Test performs hidden test registration during process
startup. For test binaries that contain a large number of tests, this
registration can be costly. In this CL, we reduce the overhead of
registration via several tactics:

- Treat CodeLocation and FilePath as value types, using std::move to
  pass them around.
- Reduce string copies in various places by either passing std::string
  values via std::move, or passing const-refs to std::string instances.
- Use std::to_string to stringify an int in DefaultParamName rather than
  a std::stringstream.
- Pull some std::string instances out of nested loops in
  ParameterizedTestSuiteInfo::RegisterTests so as to reuse some
  allocations, and replace stringstream with ordinary string appends.
- Use std::unordered_map in UnitTestImpl::GetTestSuite and
  ParameterizedTestSuiteRegistry::GetTestSuitePatternHolder to spend a
  little memory to turn O(N) lookups into constant time lookpus.
- Use range-based for loops in a few places.
- Use emplace-ish methods to add to containers where appropriate.

All together, these changes reduce the overall runtime of a series of 50
death tests in a single Chromium test executable by ~38% due to the
fact that the registration costs are paid in every death test's child
process.

PiperOrigin-RevId: 613833210
Change-Id: I51a262a770edff98ffa1e3b60c4d78a8308f9a9f
2024-03-07 23:53:47 -08:00
Abseil Team 31993dfa6b Revert Optimize Google Test process startup
PiperOrigin-RevId: 612878184
Change-Id: Ia8e23da1ad09c2e0ce635a855f0c250f368f6878
2024-03-05 09:59:16 -08:00
Abseil Team b9059aaa4c Optimize Google Test process startup
Google Test performs hidden test registration during process
startup. For test binaries that contain a large number of tests, this
registration can be costly. In this CL, we reduce the overhead of
registration via several tactics:

- Treat CodeLocation and FilePath as value types, using std::move to
  pass them around.
- Reduce string copies in various places by either passing std::string
  values via std::move, or passing const-refs to std::string instances.
- Use std::to_string to stringify an int in DefaultParamName rather than
  a std::stringstream.
- Pull some std::string instances out of nested loops in
  ParameterizedTestSuiteInfo::RegisterTests so as to reuse some
  allocations, and replace stringstream with ordinary string appends.
- Use std::unordered_map in UnitTestImpl::GetTestSuite and
  ParameterizedTestSuiteRegistry::GetTestSuitePatternHolder to spend a
  little memory to turn O(N) lookups into constant time lookpus.
- Use range-based for loops in various places.
- Use emplace-ish methods to add to containers where appropriate.

All together, these changes reduce the overall runtime of a series of 50
death tests in a single Chromium test executable by ~38% due to the
fact that the registration costs are paid in every death test's child
process.

PiperOrigin-RevId: 612763676
Change-Id: I1f46e012ccb9004c009e1027e4f7c38780ffb9e2
2024-03-05 02:37:09 -08:00
Copybara-Service 926f681a90 Merge pull request #4466 from danfabo:fix-fetch-content-uses-gtest
PiperOrigin-RevId: 612560830
Change-Id: Ib639603d34258909cf0e0d6fceb297dd8c983d54
2024-03-04 13:15:25 -08:00
Abseil Team e4fdb87e76 Accept one-shot callables in InvokeArgument.
PiperOrigin-RevId: 611467660
Change-Id: Ic89ffc986141bee61f835cb60088aee92eb8bad9
2024-02-29 07:42:26 -08:00
Copybara-Service e15c5a51b8 Merge pull request #4477 from tmiguelf:feature/soup_cleanup
PiperOrigin-RevId: 611133343
Change-Id: Ieef806f51fcbc2877805dae415f76d03547b8dfa
2024-02-28 09:48:31 -08:00
Abseil Team 3b6d48e8d5 Reland 9756ee7cba
Since Fuchsia engineers rarely work within this repo, initialize a lightweight fake @fuchsia_sdk repo rather than distributing the Fuchsia SDK here.

Tested locally via `bazel query --[no]enable_bzlmod "deps(set(//googletest/test:gtest_all_test))"` (#4472)

PiperOrigin-RevId: 610826859
Change-Id: I7d41b1dbe9e7f133fe535d7337dc5bff5bf97d3a
2024-02-27 11:54:30 -08:00
Abseil Team 814ba36338 Make sure that current_test_suite and current_test_info are mutex-protected while writing for thread-safety.
PiperOrigin-RevId: 610810340
Change-Id: I37f33510373dff04b8e9c9e8a9f32d30fcce46ff
2024-02-27 11:04:37 -08:00
Tiago Freire 4c676b94b8
Fixed gcc linker error 58 2024-02-23 13:23:25 +01:00
Abseil Team 9d43b27f7a Revert incorrect update.
PiperOrigin-RevId: 609387046
Change-Id: I03e42c39011fc7e9715f896591a44ebede1933b2
2024-02-22 08:48:24 -08:00
Derek Mauro db38b59fa0 Rollback 9756ee7cba
The commit breaks some bazel commands without @fuchsia_sdk being listed as a dependency.

Fixes #4472

PiperOrigin-RevId: 609057667
Change-Id: I32ea8237862d7c10add55304ecc4547a7304ce36
2024-02-21 11:06:13 -08:00
Abseil Team 5df0241ea4 gtest.h: add IWYU export pragmas
PiperOrigin-RevId: 607136416
Change-Id: Ia29dd3156d4d455194745e58501eaee9d77045a1
2024-02-14 16:11:33 -08:00
Abseil Team 9756ee7cba Support Fuchsia target builds.
The Fuchsia team has been using either fork branches
(https://fuchsia.googlesource.com/third_
party/github.com/google/googletest/+/refs/heads/sandbox/fuchsia_bazel_sdk)
or patch files
(https://cs.opensource.google/fuchsia/fuchsia/+/main:build/bazel/patches/googletest/fuchsia-support.bundle)
to support googletest-based tests that run against a Fuchsia target device.

As our SDK is maturing and @platforms//os:fuchsia constraint has been added
for a while now, upstream Fuchsia gtest support to reduce technical debt and
overhead.

This change is noop for non-fuchsia platform builds.

PiperOrigin-RevId: 606843739
Change-Id: I61e0eb9c641f288d7ae57354fc0b484fce013223
2024-02-13 20:56:40 -08:00
Abseil Team 6eb225cb88 Add anchor for expectation ordering note
PiperOrigin-RevId: 606736565
Change-Id: Ifad0b34e1deeec1374377d733375c6210896d0d3
2024-02-13 13:55:04 -08:00
danfabo f5448b3d71 Get include dirs from target rather than global variables.
The global variable gtest_SOURCE_DIR is replaced by CMake when fetch-content is used with the name "GTest" and points to the root directory. The include directories from the gtest target are always valid.
2024-02-11 18:50:28 +01:00
Matt Kulukundis b75ecf1bed Switch rank structs to be consistent with written guidance in go/ranked-overloads
PiperOrigin-RevId: 605110251
Change-Id: I304f3863333cb9ef0b85c5bab5277e757ef9950a
2024-02-07 15:06:10 -08:00
Abseil Team 96519a4019 Use _Exit instead of _exit in GoogleTest
_Exit is standardized since C99, whereas _exit is POSIX-only.

Fixes: #4447
PiperOrigin-RevId: 605000352
Change-Id: Ibfa84edaa043bd003a21383e8148bf45be7217f6
2024-02-07 09:01:25 -08:00
Abseil Team 64be1c79fa Destroy installed environments in normal code, not in static teardown.
Destruction in static teardown causes issues for Environments which own threads and try to join them in their destruction.

This may be a breaking change for users who call RUN_ALL_TESTS multiple times in the same main function if they also install environments, or those who access registered environments after RUN_ALL_TESTS.

The easiest fix is to only call RUN_ALL_TESTS once as the last line of the main function. Another potential fix is to re-register new instances of the Environment once before each call to RUN_ALL_TESTS.

PiperOrigin-RevId: 604800795
Change-Id: I37c44d4aca4a238052649f45a4b6b9cfb5355b71
2024-02-06 16:31:00 -08:00
Copybara-Service 48729681ad Merge pull request #4458 from kaswhy:patch-1
PiperOrigin-RevId: 604402563
Change-Id: If99952449430161609572dab998774fac4653f01
2024-02-05 12:41:31 -08:00
Abseil Team 456574145c Modifications to improve portability of googletest tests.
PiperOrigin-RevId: 603034597
Change-Id: I4d716ed67f80f41075bfa266d975460d2ac27eb6
2024-01-31 06:51:14 -08:00
Soyeon Kim 3cb7ce90b3
Add myself to Contributors 2024-01-31 21:11:21 +09:00
Abseil Team 6fdb4c303f Do not emit stack traces for messages generated by SUCCEED()
Stack traces in assertion failures are an extremely useful tool for
developers tasked with investigating failing tests. It's difficult to
understate this. In contrast to ordinary test assertions (e.g.,
ASSERT_TRUE or EXPECT_FALSE), SUCCEED() is a developer-authored
directive that indicates a success codepath. In fact, the documentation
states that this directive doesn't generate any output. Generating stack
traces for uses of SUCCEED() is wasted work since they are never
printed. If this were to change one day in the future, they still would
not be useful since any emitted message would include the file and line
number where SUCCEED was used. In addition to being noise in the output
in this case, symbolization of stack traces is not free. In some
Chromium configurations, symbolization for use of SUCCEED() can incur a
cost in excess of 25 seconds for a test that otherwise takes 0-1ms; see
https://crbug.com/1517343.

In this CL, we suppress generation and emission of stack traces for
kSuccess messages to reduce the overhead of SUCCEED().

PiperOrigin-RevId: 602832162
Change-Id: I557dd6a1d3e6ed6562daf727d69fd01fe914827b
2024-01-30 13:55:46 -08:00
Copybara-Service fc0076ffc4 Merge pull request #4435 from kaswhy:main
PiperOrigin-RevId: 602385832
Change-Id: I755dae5707e98b6157930a74de90a8e19ef17603
2024-01-29 07:50:37 -08:00
Derek Mauro 6a5938233b Add support for Bzlmod for the next release
https://bazel.build/external/overview#bzlmod

Bzlmod will be the default in a future Bazel release.
By default it requires projects to be registered with the
Bazel Central Registry (https://registry.bazel.build/) and
thus uses regular releases by default.

Users that want to "live-at-head" can still do this through
with overrides (https://bazel.build/external/module#overrides).

This change updates GoogleTest dependencies to use released versions.
CI uses Bzlmod except in the case of linux_gcc-floor, which will
keep testing the old WORKSPACE-based dependency system.

PiperOrigin-RevId: 601489729
Change-Id: I6be52034eba0d0e5fe12110e5e82879305cf73ff
2024-01-25 10:27:52 -08:00
Abseil Team 563ebf1769 Fix double-promotion warnings in AppropriateResolution()
When -Wdouble-promotion is enabled, the templatized function AppropriateResolution fails to compile since its float instantiation promotes floats to doubles when doing arithmetic and comparisons. Add static casts to resolve these errors.

PiperOrigin-RevId: 600776333
Change-Id: Ia530b4bbca6ddce27caf0a817196d87efef711cb
2024-01-23 07:02:53 -08:00
kaswhy 1cab76c7b8 Docs: Add mention of `gtest_recreate_environments_when_repeating` 2024-01-23 21:39:40 +09:00
Copybara-Service bd30c39d61 Merge pull request #4444 from michaeldleslie:only_apply_lregex_for_qnx710_and_newer
PiperOrigin-RevId: 600548775
Change-Id: Ia693791321602e728aef9135b349602c790a058a
2024-01-22 13:13:03 -08:00
Dino Radakovic ffc477e705 googletest: Fix incorrect comment about `value_param` of `internal::MakeAndRegisterTestInfo`
This was probably a copy-paste from the comment about `type_param`.

PiperOrigin-RevId: 600493462
Change-Id: I4a41c2673dd6560db0f68856aab3d32b103164b1
2024-01-22 10:12:16 -08:00
Abseil Team 96cd50c082 If GTEST_NO_ABSL_FLAGS is #defined, then the Abseil flag library
will not be used by googletest, even if GTEST_HAS_ABSL is #defined.

PiperOrigin-RevId: 599625032
Change-Id: Ieb994a15683dec89e88578120071eca8ac9fead1
2024-01-18 14:24:26 -08:00
Abseil Team f6f920d2ba Make posix::FileNo available under !GTEST_HAS_FILE_SYSTEM
This enables automatic color output without filesystem support, as that only requires testing to see if the output is a terminal.

Fixes: #4439
PiperOrigin-RevId: 598929397
Change-Id: Idca7490e6e090951a78cd1cdd710f41d756a68b4
2024-01-16 12:56:41 -08:00
Abseil Team cfe5076a8b Do not emit stack traces for messages generated by GTEST_SKIP()
Stack traces in assertion failures are an extremely useful tool for
developers tasked with investigating failing tests. It's difficult to
understate this. In contrast to ordinary test assertions (e.g.,
ASSERT_TRUE or EXPECT_FALSE), GTEST_SKIP is a developer-authored
directive to skip one or more tests. Stack traces emitted in skip
messages do not give the developer useful information, as the skip
message itself contains the code location where GTEST_SKIP was used. In
addition to being noise in the output, symbolization of stack traces is
not free. In some Chromium configurations, symbolization in a skipped
test can incur a cost in excess of 25 seconds for a test that otherwise
takes 0-1ms; see https://crbug.com/1517343#c9.

In this CL, we suppress generation and emission of stack traces for
kSkip messages to reduce the output noise and overhead of GTEST_SKIP().

PiperOrigin-RevId: 598899010
Change-Id: I46926fed452c8d7edcb3d636d8fed42cb6c0a9e9
2024-01-16 11:19:24 -08:00
kaswhy 778badf63a Docs: add conditions for calling SetUp and TearDown() 2024-01-12 01:06:00 +09:00
Michael Leslie f57505314b only apply -lregex for qnx710 and newer 2024-01-10 11:48:14 -08:00
Derek Mauro 7c07a86369 Update CI builds to use Bazel 7.0.0
--features=external_include_paths has been removed from Windows builds
since it appears cause build command errors currently
PiperOrigin-RevId: 597020418
Change-Id: Ie37be0d05f3a154ab0d3c3f7d39b4e2c0ed650a3
2024-01-09 13:02:32 -08:00
Abseil Team 7f409cb3c0 Add a note about argv requiring NULL termination.
Fixes: #4434
PiperOrigin-RevId: 596960654
Change-Id: I1f70cc0801764fe0328030c46254f82eb9893a49
2024-01-09 09:39:16 -08:00
Krzysztof Kosiński 3b26f7e53b Disable -Wfloat-equal in AppropriateResolution().
This function makes exact floating point that are correct in this context, but trigger the compiler warning.

PiperOrigin-RevId: 596944060
Change-Id: I4bba3c2abcf3ac189107530978961955ad47d6f0
2024-01-09 08:39:28 -08:00
kaswhy dda72ef321 Docs: add conditions for calling TearDown() 2023-12-29 02:05:18 +09:00
Abseil Team dddb219c3e Accept move-only callables in `InvokeArguments`
PiperOrigin-RevId: 594223533
Change-Id: I491fae7d851d4e0df07fb3627416949071fec8d6
2023-12-28 04:04:00 -08:00
Abseil Team 96eadf659f Minor documentation correction.
PiperOrigin-RevId: 593126348
Change-Id: I78e12ab5dd2e5acc69b21250bdb04e62990b6309
2023-12-22 08:51:16 -08:00
Derek Mauro cde9a745a6 Fix broken links in primer.md
Closes #4432

PiperOrigin-RevId: 592335698
Change-Id: I9859451981f58f1426255067d702a0767718b1d5
2023-12-19 13:56:36 -08:00
Abseil Team 8495449f07 Fix data race in leak detection
TSAN identified a data race between updates to the states_ map (ex. in Mock::UnregisterLocked) and the iteration done in this destructor. Writes to the map use g_gmock_mutex, but the destructor does not acquire it. Acquiring the lock here fixes the data race.

It should only be possible to trigger this TSAN finding in cases where a mock object is deleted by a thread other than the main thread.

PiperOrigin-RevId: 591935393
Change-Id: I9dd1faa40058d78e165a91333346514b4b73365c
2023-12-18 10:14:49 -08:00
Abseil Team 530d5c8c84 Add `FAIL_AT` macro variant of `FAIL` matching `ADD_FAILURE`, `ADD_FAILURE_AT`
`FAIL_AT` is shorthand for `GTEST_FAIL_AT` like `FAIL` is for `GTEST_FAIL`.

PiperOrigin-RevId: 590393926
Change-Id: I68263af8fa2f98ca0bbef509d475c84e22068018
2023-12-12 17:40:45 -08:00
Tom Hughes eb80f759d5 Remove unnecessary conversion
`GetAbsolutePathToOutputFile` returns a `std::string` and `OpenFileForWriting`
takes a `std::string&`.

PiperOrigin-RevId: 589984409
Change-Id: I75be9cb105f49b3a279a5d33b1b82dfcfc912cfd
2023-12-11 16:17:28 -08:00
Copybara-Service b3a9ba2b8e Merge pull request #4426 from tamaskenez:cmake_external_absl_re2
PiperOrigin-RevId: 587836393
Change-Id: Ia8895898bd0a826b35dc0ebedd8c76503bc8cd43
2023-12-04 14:11:54 -08:00
Derek Mauro 8760db154a Skip find_package(Python3) when not building tests
#4424 claims this saves several seconds of build time
Closes #4424

PiperOrigin-RevId: 587827426
Change-Id: I207779a6539f9af16a39d6b40887770dc930b74f
2023-12-04 13:42:25 -08:00
Tamas Kenez 48b373f6e0 Allow using external absl and re2. 2023-12-01 23:09:53 +01:00
Abseil Team 76bb2afb8b Implement `testing::Rethrow` to throw exceptions more easily via `std::exception_ptr`
We avoid overloading or specializing `testing::Throw` as this is fundamentally a different operation than throwing the object.
However, we disable the corresponding overload of `testing::Throw` to prevent likely mistakes in the usage.

Fixes: #4412
PiperOrigin-RevId: 585745469
Change-Id: I03bb585427ce51983d914e88f2bf65a13545c920
2023-11-27 13:32:49 -08:00
Copybara-Service b10fad38c4 Merge pull request #4397 from botovq:raise
PiperOrigin-RevId: 577958594
Change-Id: I8a27f67dc5b6817b741bfd2fc0f27c6302291a00
2023-10-30 14:33:13 -07:00
Abseil Team 5b7fd63d6d Export gmock-spec-builders.
gmock.h is the umbrella header to be used for rest of the library, and it also
enables users to export certain details. This wasn't working for some interfaces
like EXPECT_CALL because gmock-spec-builders wasn't explicitly exported.

PiperOrigin-RevId: 576966583
Change-Id: Ie050430cf11384977cd95f4ed6e73235d6857057
2023-10-26 13:15:23 -07:00
Theo Buehler 7f036c5563 Add missing include for raise(3)
If SIGTRAP is defined, this file may end up using raise(3), which is
defined in csignal, leading to a compilation failure on at least
OpenBSD/sparc64 with gcc 8.
2023-10-25 07:52:23 +02:00
Dino Radakovic 518387203b StartsWith: Explicitly construct matcher-typed strings from matchee parameter
The current implementation breaks for absl::string_view on gcc, c++14: https://godbolt.org/z/Tzd3q1fqx

Closes #4391

PiperOrigin-RevId: 575853981
Change-Id: I7b782598add480eb69d4ca27ea4a4bf5f758f6a3
2023-10-23 10:11:36 -07:00
Abseil Team 116b7e5528 Improve error message for invalid parameterized test names.
PiperOrigin-RevId: 574992011
Change-Id: Id6030a9e5f317966186cc48ef2c09ad97fa15d3e
2023-10-19 13:55:15 -07:00
Abseil Team 9bb354fa83 s/::testing::/testing::/ in test documentation outside of using statements to align with best practice
PiperOrigin-RevId: 574377544
Change-Id: I0ca69a3bf14cc1aab75784eba220a48bf50cef04
2023-10-17 23:50:43 -07:00
Dino Radakovic 829c19901d gtest-death-test-internal: Delete obsolete string constants
These are not used anywhere in googletest and they are in namespace `testing::internal`

PiperOrigin-RevId: 574171727
Change-Id: I5f668157a81ba3efaed77c1302b40cf07eeda52b
2023-10-17 09:18:52 -07:00
Abseil Team 2dd1c13195 Fix RE::Init for Android and NetBSD.
This is a somewhat recent change for Android (I'm not clear on whether it's a recent change for NetBSD, or if Android was just very behind on its implementation), so while this worked fine as recently as API 32 devices, REG_GNU is required for API 34 (API 33 untested).

A test actually caught this, but https://github.com/google/googletest/pull/4334 "fixed" the test rather than the implementation. This CL also reverts the test change so it can catch the failure.

PiperOrigin-RevId: 571126374
Change-Id: I420dfcedea58f2c8b605f699515d744006c0a9d9
2023-10-05 14:13:04 -07:00
Dino Radakovic beb552fb47 gmock_cook_book: Document `DoAll`'s return type requirement
The requirement is vaguely  documented by "Only the return value of the last action in the sequence will be used.".
However, this can be misleading, as users could potentially expect default-constructed values to be returned in absence of a matching return type.

PiperOrigin-RevId: 570450839
Change-Id: Ibd98a6e6b2aaf2a8cfc15ed6aeab442526eab98e
2023-10-03 11:41:48 -07:00
Copybara-Service a6d7fa8c0c Merge pull request #4382 from idzm:fix_cmake_comments
PiperOrigin-RevId: 570161165
Change-Id: Idf7eafb163bb067b0031e25a183d5c9cc3e3f378
2023-10-02 13:30:23 -07:00
Dzmitry Ivaniuk 2d09ed35cf cmake: Fix comments in cmake files
Remove extra spaces. Fix so that the comment line starts with a capital letter and ends with a dot.
2023-09-29 17:26:23 +03:00
Abseil Team e47544ad31 Resolve `-Wundef` triggering on `GTEST_CREATE_SHARED_LIBRARY` and `GTEST_LINKED_AS_SHARED_LIBRARY` with shared libraries in GoogleTest
Fixes: #4372
PiperOrigin-RevId: 568327612
Change-Id: Ifc47f1a2a2648c29858a22966331557cc928cc47
2023-09-25 14:38:01 -07:00
Copybara-Service 5bd0f08e0d Merge pull request #4374 from masbug:main
PiperOrigin-RevId: 568317621
Change-Id: Icf7fb519f96f5e88eb0df491eed143134c8ac6eb
2023-09-25 14:00:54 -07:00
Abseil Team e40661d89b Update C++ feature detection in `gtest-port.h` to rely on feature test macros where possible.
This also avoids conflating C++ language versions with standard library versions, which don't always align.

PiperOrigin-RevId: 567662118
Change-Id: I7c023bd043c81c540c9430eaeb7b450feaadb206
2023-09-22 10:46:16 -07:00
Abseil Team 80306a7b5e Use `absl::HasAbslStringify`, instead of the internal version.
PiperOrigin-RevId: 567349341
Change-Id: I35f2679901aecbe9cb90d2e78ff28c5e383e7257
2023-09-21 10:32:25 -07:00
Dino Radakovic 167a2255ae googletest: Update absl to version with HasAbslStringify
9e1789ffea

PiperOrigin-RevId: 567324946
Change-Id: I8adc5803a81075a635dad79aa0312d4455e1ad63
2023-09-21 09:08:09 -07:00
Mitja Spes e27b194c27 Fix compile warnings in gmock-function-mocker.h
Template type int changed to size_t.
This fixes compile warning `conversion to ‘long unsigned int’ from ‘int’ may change the sign of the result` in gmock-function-mocker.h.
2023-09-21 10:06:21 +02:00
Abseil Team adc5145386 Update code with IWYU annotations.
PiperOrigin-RevId: 566424331
Change-Id: I0e16d979b9d79643c882c5082e154842983a5317
2023-09-18 15:04:08 -07:00
Abseil Team 8be20cce69 Use the `empty()` method to check for emptiness instead of `length()`
PiperOrigin-RevId: 566247438
Change-Id: I8199ef53310a057abbe23f8f4295507b60d6b707
2023-09-18 03:20:19 -07:00
Michael Hirshleifer d1467f5813 GoogleTest FAQ: minor punctuation fixes
PiperOrigin-RevId: 565411290
Change-Id: I57e94c679183e39eec2a2835f330b52fc9302767
2023-09-14 10:37:04 -07:00
Michael Hirshleifer 728ec52d21 Remove Googletest FAQ entry for obsolete `ProtocolMessageEquals` and `ProtocolMessageEquiv`
* These long-dead variants of the proto matchers don't exist in the current version of Googletest.
* No evidence of external usage: [the only external references I see](https://www.google.com/search?q=%22protocolmessageequals%22+OR+%22protocolmessageequals%22) are copies of this guide. Possibly they were already removed by the time Googletest was publicly released.

PiperOrigin-RevId: 565358401
Change-Id: I61379b7333fa8ee19cd5520caedf2c539f54c2d7
2023-09-14 07:13:20 -07:00
Copybara-Service af5ddbde4d Merge pull request #4342 from tanzislam:prefer-tmpdir-on-android
PiperOrigin-RevId: 565230380
Change-Id: I6e91eea46d05413d4d87e73a11941786604d9f27
2023-09-13 19:36:52 -07:00
Copybara-Service eab0e7e289 Merge pull request #4365 from tanzislam:fix-thread-count-after-thread-creation
PiperOrigin-RevId: 564472305
Change-Id: I8db9bc0ba0768e7ab9c67f36d9db87edbbf14f10
2023-09-11 12:46:26 -07:00
Tanzinul Islam b86bf840d1 Count threads after thread-creation while still holding mutex lock
The `Mutex` is locked with the `MutexLock` before spawning the thread, so that the thread is prevented from completing (by being blocked on `Mutex`) before the new thread count is obtained. However, the existing bug (introduced in 22e6055) releases `Mutex` before obtaining the new thread count, which allows the thread to run to completion in the meantime.

Also, since the `(thread_count_after_create != starting_count + 1)` condition (line 328) skips the remainder of the `for`-loop body on every iteration, `thread_count_after_join` stays uninitialized.

I believe this is why [this test failed][1] on the macOS CI with this trace:

```
[----------] 1 test from GetThreadCountTest
[ RUN      ] GetThreadCountTest.ReturnsCorrectValue
googletest/test/googletest-port-test.cc:350: Failure
Expected equality of these values:
  thread_count_after_create
    Which is: 1
  starting_count + 1
    Which is: 2

googletest/test/googletest-port-test.cc:351: Failure
Expected equality of these values:
  thread_count_after_join
    Which is: 140493185949400
  starting_count
    Which is: 1

[  FAILED  ] GetThreadCountTest.ReturnsCorrectValue (2 ms)
[----------] 1 test from GetThreadCountTest (2 ms total)
```

[1]: https://github.com/google/googletest/actions/runs/6064919420/job/16453860690?pr=3049
2023-09-03 16:46:02 +01:00
Tanzinul Islam edf9b4964d Reuse TempDir() function 2023-08-26 08:53:45 +01:00
Dino Radakovic 8a6feabf04 googletest: Add universal printer for `std::span`
Fixes #4318

PiperOrigin-RevId: 560089120
Change-Id: I9d0d098140033520266747a1689e953ee8307c47
2023-08-25 07:46:02 -07:00
Copybara-Service 460ae98267 Merge pull request #4349 from sthd:httpToHttps
PiperOrigin-RevId: 559452348
Change-Id: I6f72001fd6b8e5a739c34121c8847c281d563b0f
2023-08-23 09:30:13 -07:00
sthd f0eae4b399 changed http to https 2023-08-22 20:58:34 +03:00
Copybara-Service cb5cd96bca Merge pull request #4343 from sthd:http-to-https
PiperOrigin-RevId: 559151399
Change-Id: I8eb2c7c5222d0fd0935db531a0abe5792633fe7e
2023-08-22 10:24:02 -07:00
Dino Radakovic c374da15c5 googletest: Replace http with https in links to docs
This is a prerequisite for #4341

PiperOrigin-RevId: 559132807
Change-Id: Iadc961913e0ff107c5333dae17be5f8638663836
2023-08-22 09:21:04 -07:00
Derek Mauro 61332bd7e8 CI: Update the Linux hybrid-latest docker container used for testing
The following are the major updates
  * LLVM 17 branch (https://github.com/llvm/llvm-project b744f4c99cf91155c74a3c92db6f1335232ff3d)
  * GCC 13.2
  * CMake 3.27.1
  * Bazel 6.2.1

PiperOrigin-RevId: 558818264
Change-Id: Ib08d8331e2a8b2d68a702670451beaaac5d266f4
2023-08-21 09:50:40 -07:00
Abseil Team 762d3a7f66 Clean up typos: Exhaused => Exhausted
PiperOrigin-RevId: 558801066
Change-Id: Ia225d12014748db87639414f4c8c28a0d0e9e489
2023-08-21 08:47:54 -07:00
Elior Schneider c759a58a2d
Merge branch 'google:main' into http-to-https 2023-08-20 09:39:17 +03:00
Elior Schneider 722daa3da6
Update advanced.md
http to https
2023-08-20 09:38:57 +03:00
Copybara-Service 9fce548044 Merge pull request #4322 from pgawro:ansi_colors
PiperOrigin-RevId: 558224853
Change-Id: Ib2d8ad41fcc4d38f58aa5702b537467d8867d82e
2023-08-18 12:59:42 -07:00
Tanzinul Islam 14d05f4708 Prefer $TMPDIR to /data/local/tmp on Android
Newer devices can have the latter location read-only. (I observed this with Termux on a non-rooted Pixel 6.)
2023-08-18 19:44:48 +01:00
Elior Schneider 5e47c767ff
Merge branch 'google:main' into http-to-https 2023-08-18 16:34:00 +03:00
Abseil Team f42da0e443 Improve error message for invalid parameterized test names.
PiperOrigin-RevId: 557910190
Change-Id: Ia965a6c96e4cc5997d8af2611abc62c42e81653e
2023-08-17 12:56:29 -07:00
Patryk Gawroński e24cced08d googletest: ansi color fix
Adds prevents from returning
nullptr by choosing default color.

Issue: #4321
2023-08-17 00:11:28 +02:00
Copybara-Service 987e225614 Merge pull request #4333 from ngie-eign:gtest_help_test-fix-FreeBSD
PiperOrigin-RevId: 557197748
Change-Id: I55b86353f5351bbcbdf8e6bca70e82d7383a5080
2023-08-15 11:39:35 -07:00
Dino Radakovic dd9a956904 gtest_help_test: Make method names `snake_case`, conforming with [the style guide](https://google.github.io/styleguide/pyguide#316-naming)
PiperOrigin-RevId: 557133618
Change-Id: I27202ee91ee81b3d2e4c28102190d2bde8efba05
2023-08-15 08:00:31 -07:00
Dino Radakovic 6513d0272d gtest_help_test: Inline test helper functions
`TestNonHelpFlag` is only a few asserts with no logic, which is easier to read in line, and helper `TestHelpFlag` is used in a single test case.

PiperOrigin-RevId: 557122793
Change-Id: I7367424abfbb883c10c260fae066a2071e5dfa0e
2023-08-15 07:11:58 -07:00
Copybara-Service fc234d6d18 Merge pull request #4334 from ngie-eign:fix-RETest-non-ABSL
PiperOrigin-RevId: 557122083
Change-Id: I77fb7fe99baf9cbf341ad37d4b651a0ac606b549
2023-08-15 07:08:30 -07:00
Dino Radakovic d76e9e0dd9 gtest_help_test: Delete obsolete helper `TestUnknownFlagWithAbseil`
PiperOrigin-RevId: 557116814
Change-Id: I91e06b0d6001952366c50201b67491475a1f98af
2023-08-15 06:42:46 -07:00
sthd 8ca57f194a Changed 2 public links from http to https 2023-08-11 11:26:59 +03:00
sthd 5b5ef29950 Changed 3 public links from http to https 2023-08-11 11:21:47 +03:00
sthd 622ee5cf29 Changed 2 public links from http to https 2023-08-11 11:06:25 +03:00
sthd 619601c5e2 Changed 3 public links from http to https 2023-08-11 11:01:14 +03:00
Abseil Team 7e33b6a1c4 Specify SetUpTestSuite is required to be public.
PiperOrigin-RevId: 555578256
Change-Id: I9366fc99ae953c29e468fcddb4be203c9c05661b
2023-08-10 11:27:05 -07:00
Copybara-Service 89b25572db Merge pull request #4330 from IncludeGuardian:remove-iomanip
PiperOrigin-RevId: 554867591
Change-Id: Ib32da50384951532419cb54fb70f8ab0920178d7
2023-08-08 10:26:39 -07:00
Enji Cooper 77485ff004 Fix RETest/1.ImplicitConstructorWorks on non-ABSL platforms
The last regular expression specified in the test is not technically
POSIX compatible. Use `[[:alnum:]_]` instead of `\w+`; the latter is a
Perl-compatible regular expression.

Signed-off-by: Enji Cooper <yaneurabeya@gmail.com>
2023-08-07 21:37:54 -07:00
Enji Cooper 22eb2de1ef Fix GTestHelpTest.TestHelpFlag on FreeBSD
The test supported a variety of BSDs, including kFreeBSD, but not FreeBSD.

Move the BSD checks to a separate function and support checking for
FreeBSD, in addition to kFreeBSD.

Signed-off-by: Enji Cooper <yaneurabeya@gmail.com>
2023-08-07 20:18:01 -07:00
Copybara-Service 46db91ef6f Merge pull request #4328 from robert-shade:suppress_unreachable_warning
PiperOrigin-RevId: 554578443
Change-Id: Ib5b03605c30fc2974b9597860577ff89532eedcd
2023-08-07 13:39:47 -07:00
Dino Radakovic 6abc9e3d79 Make references to `#include`s consistent across docs
PiperOrigin-RevId: 554561504
Change-Id: Ia02ab6ac646bf6637d6f500a4aaedd14e0a04798
2023-08-07 12:44:53 -07:00
Elliot Goodrich 9ef5e82269 Remove public includes of `<iomanip>`
This commit removes `<iomanip>` from public GoogleTest header files.

As `<iomanip>` is not a common included file, its content is unlikely
to be included in translation units other than through GoogleTest
includes.

By reducing the number of include directives public headers in
GoogleTest, this may reduce the time taken to compile tests as it would
reduce the amount of work that the preprocessor and compiler front-end
need to do.
2023-08-03 22:14:38 +01:00
Copybara-Service 455fcb7773 Merge pull request #4323 from pgawro:gtest_ExpectThrowTest_DoesNotGenerateUnreachableCodeWarning
PiperOrigin-RevId: 553485739
Change-Id: I6581215e6db514397177af39381eea4d121f32bd
2023-08-03 08:32:42 -07:00
Robert Shade 429432e341 Avoid unreachable code warning 2023-08-02 14:09:54 -04:00
Derek Mauro 843976e4f5 Update documentation to refer to v1.14
PiperOrigin-RevId: 553172719
Change-Id: Ie09afa3788c8ed5c95913d8ca0b436f1df28241a
2023-08-02 09:45:10 -07:00
Derek Mauro f8d7d77c06 Bump version to v1.14 in preparation for release
PiperOrigin-RevId: 553152072
Change-Id: I0bfbb2da6b3902fc3e41a8e1c4aacb291ffdd098
2023-08-02 08:24:53 -07:00
Derek Mauro 96683ee668 Remove the GTEST_HAS_DOWNCAST_ customization point.
PiperOrigin-RevId: 553150809
Change-Id: I10d19a45a85c5f63a5e65dc322413307116e1c25
2023-08-02 08:19:45 -07:00
Derek Mauro 58e37f7e99 Add googletest-message-test to the Bazel tests
It appears to have been unintentionally left out

PiperOrigin-RevId: 553141410
Change-Id: I8adac55a3df0ec12d6fe03446f71858fc702e178
2023-08-02 07:40:55 -07:00
Phoebe Liang e7fd109b53 Make testing::Message support streamed AbslStringify values
This allows types that provide an AbslStringify definition to be streamed into GoogleTest macros.

PiperOrigin-RevId: 552914482
Change-Id: I5fb386980d4d24873f95f0a8ef83067a6a3c86ac
2023-08-01 14:01:41 -07:00
Derek Mauro 717d8ab5e0 Update GoogleTest dependencies
PiperOrigin-RevId: 552912819
Change-Id: If1b6278f7fe5a8fc41fc3bc390356a94387d0091
2023-08-01 13:56:57 -07:00
Copybara-Service 6f6ab4212a Merge pull request #4325 from juliencombattelli:main
PiperOrigin-RevId: 552577282
Change-Id: I2b4d20d155ad5746e36711c039293d5c996a332e
2023-07-31 13:32:19 -07:00
Julien Combattelli efe6bb1db8 Use #if and not #ifdef to check filesystem support 2023-07-29 15:54:54 +02:00
Abseil Team c875c4e224 Adjust includes to use <> instead of "", consistent with quickstart pages.
Right now, gtest documentation uses different #include syntax. The quickstart
pages (e.g., http://google.github.io/googletest/quickstart-bazel.html#create-and-run-a-binary) are checked in with `#include <gtest/gtest.h>` However, other
documentation (such as the primer) uses `#include "gtest/gtest.h"` (e.g.,
https://google.github.io/googletest/primer.html#writing-the-main-function).

PiperOrigin-RevId: 551878641
Change-Id: Iab93cc1da3ef4870a07b624071b75d6e9d3568c1
2023-07-28 09:51:41 -07:00
Patryk Gawroński 2f83a6e8f8 gtest: Supress warning about set unused variable
Modified test ExpectThrowTest.DoesNotGenerateUnreachableCodeWarning
2023-07-27 01:49:57 +02:00
Copybara-Service 40412d8512 Merge pull request #4317 from mrfeod:patch-1
PiperOrigin-RevId: 550585850
Change-Id: I727f74aeb6f1da5bbbf5a5f49f06c4cfea40a73b
2023-07-24 09:31:31 -07:00
Abseil Team 01e18376ef Make `AbslStringify` usage public in GoogleTest
Fixes #4314

PiperOrigin-RevId: 549986457
Change-Id: Iff74f02ab1c106696f288540e9c623d56b76e3f7
2023-07-21 10:36:25 -07:00
Derek Mauro 1ed6a8c67a Remove unused cast implementation
PiperOrigin-RevId: 549456180
Change-Id: I10862e6de981087a5c590cccf6152255e9a176a0
2023-07-19 16:21:55 -07:00
Anton Sosnin f014396910
Fix typo in gmock_cook_book.md 2023-07-19 18:08:41 +03:00
Copybara-Service d66ce58510 Merge pull request #4313 from kimvaleen:main
PiperOrigin-RevId: 549039222
Change-Id: I96bea310beede5ba0ed6160155251ffd9d7a2103
2023-07-18 10:18:14 -07:00
Copybara-Service c541e7c110 Merge pull request #4302 from ciband:feat/nrf52
PiperOrigin-RevId: 549006105
Change-Id: Ifbe2bf57e10df4c71e51dbfaf1687f4f621f5106
2023-07-18 08:16:57 -07:00
Copybara-Service cc366710bb Merge pull request #4308 from smr99:fix-float-cast
PiperOrigin-RevId: 547250378
Change-Id: I084c30e45f331cb296535822923da1cb7e848e11
2023-07-11 11:37:29 -07:00
kimvaleen c88e0b4673 Fixed variables that could be declared 'const' 2023-07-11 12:05:05 +03:00
Abseil Team 4a1a299b20 Update docstring of PrintWithFallback(..) to reflect the recently changed ordering.
PiperOrigin-RevId: 546373360
Change-Id: I2538b45d8c7710592071cc352da6771480c324e6
2023-07-07 13:43:30 -07:00
Steve Robbins 3044657e7a Use template type FloatType in the cast. 2023-07-03 21:23:18 -05:00
Mike Kruskal be03d00f5f Fix C++20 compatibility bug.
This was shown to work for C++14, C++17, and C++20 after patched into googletest for the protobuf repo's CI.

Closes #3659

PiperOrigin-RevId: 544795507
Change-Id: I3e0a94f675e78a6ee9aeccae86c23d940efed8eb
2023-06-30 18:50:27 -07:00
Abseil Team 1f531be3a1 Make GoogleTest handle SEH exceptions before stack unwinding rather than afterward
This ensure the erroring stack frame is visible and accessible when the handler is invoked.

Fixes #4298

PiperOrigin-RevId: 544692549
Change-Id: Ia165a8c293e8edc820da5f5ad4416546fffe2493
2023-06-30 10:32:57 -07:00
Abseil Team 251e720391 Change `::testing` to `testing` in Testing Reference doc
PiperOrigin-RevId: 544466397
Change-Id: Icb4d5fae38361cd75d47f908886831696eb2b1c9
2023-06-29 14:40:26 -07:00
Abseil Team 687c589949 Print stack traces on SEH exceptions on Windows
Also tidies up a couple of things:
- Prevent handling of stack overflows, which cannot be done safely
- `exception_code` is a macro, so we rename it
- The `std::string` heap allocation was unnecessary

Fixes #4298

PiperOrigin-RevId: 544117790
Change-Id: I8ba61f87119d5fbdb1f653700d9867ca6f8c28ce
2023-06-28 11:45:07 -07:00
Derek Mauro 2acd538244 On platforms without a file system, don't log an error when no
alternative output format is requested.

Fixes #4299

PiperOrigin-RevId: 543932266
Change-Id: Ide78c313ecf6829e4910f4a8407275c81edb3848
2023-06-27 20:47:07 -07:00
Abseil Team f269e15c5c Resolve an issue where the resolution of `operator<<` overloads would attempt to instantiate the incomplete `testing::internal::Secret` type.
PiperOrigin-RevId: 543799815
Change-Id: Ic0a4f48d825bef26cb8cc74d8a0117b3a5ef3f14
2023-06-27 11:17:00 -07:00
Chris Johnson 091d7eb897 add support for nrf52
Added support for Nordic nRF52 series of MCUs
2023-06-27 08:29:23 -05:00
Copybara-Service 8e32de89cf Merge pull request #4300 from eltociear:patch-1
PiperOrigin-RevId: 543586460
Change-Id: I87ee05c4b77fb643a3381018568a4765d0a12b18
2023-06-26 17:24:42 -07:00
Copybara-Service 372e81d814 Merge pull request #4301 from ciband:feat/qn9090
PiperOrigin-RevId: 543523833
Change-Id: I602373a321e2b8080264e3f418ef5b2254f615fb
2023-06-26 13:23:58 -07:00
Chris Johnson b2a2d36f3a
Merge branch 'google:main' into feat/qn9090 2023-06-22 12:01:48 -05:00
Chris Johnson e3d405f62b add support for nxp qn9090 mcu
Added additional defines to correctly detect the NXP QN9090 MCU platform
and allow for native googletest compability.
2023-06-22 11:56:59 -05:00
Ikko Eltociear Ashimine 1361c77c4d
Fix typo in googletest-catch-exceptions-test.py
FITLER_OUT_SEH_TESTS_FLAG -> FILTER_OUT_SEH_TESTS_FLAG
2023-06-23 01:20:10 +09:00
Abseil Team ec4fed9321 Update code examples in the gMock Cookbook following C++ best practices.
PiperOrigin-RevId: 542564354
Change-Id: Ia3307f13f845c662c88fb7303112f41ef8c56b28
2023-06-22 08:15:15 -07:00
Copybara-Service af39146b45 Merge pull request #4293 from juan-lunarg:juan/fix_remaining_cmake_version
PiperOrigin-RevId: 542299863
Change-Id: I51cf4a4f8d6a97e39e6c46f48fa6acf3bd0ab6b5
2023-06-21 10:40:58 -07:00
Juan Ramos 812f35b26b cmake: Remove remaining checks for CMAKE_VERSION
Remove conditional code that doesn't need to exist anymore.
2023-06-20 19:09:04 -06:00
Copybara-Service fb11778f43 Merge pull request #4290 from juan-lunarg:juan/cmake_version_cleanup
PiperOrigin-RevId: 542045766
Change-Id: Ib6e0fffdbf1f41fb990603fe2af7c6d5df6b5640
2023-06-20 14:15:01 -07:00
Juan Ramos efd8db1627 cmake: Clean up policy code
Now that the min is 3.13 these policies don't need to be set
manually anymore.

CMP0054 - 3.1
CMP0063 - 3.3
CMP0069 - 3.9
CMP0077 - 3.13
2023-06-20 13:03:00 -06:00
Copybara-Service 5924e5b355 Merge pull request #4282 from zencatalyst:patch-1
PiperOrigin-RevId: 541940311
Change-Id: I953d347f0041dfcacc2bff9cc1b19e086fdd9ac4
2023-06-20 08:59:17 -07:00
Copybara-Service 29836977ef Merge pull request #4288 from juan-lunarg:juan/cmake_min_3_dot_6
PiperOrigin-RevId: 541929012
Change-Id: I90423820611c2b6a6f81fe3f9ec2d23992ffbed1
2023-06-20 08:20:43 -07:00
Copybara-Service 6c08816416 Merge pull request #4286 from ryandesign:patch-1
PiperOrigin-RevId: 541928566
Change-Id: I72437cb3a06f2af7220b247c6c136a38c5f1089c
2023-06-20 08:18:54 -07:00
Juan Ramos 4fed5f2850 cmake: Raise min to 3.6
From the CMake 3.27 release notes:
Compatibility with versions of CMake older than 3.5 is now
deprecated and will be removed from a future version. Calls to
cmake_minimum_required() or cmake_policy() that set the policy
version to an older value now issue a deprecation diagnostic.

This PR also removes manually setting policy CMP0048. This is
redundant since the CMake min is already 3.X
2023-06-19 11:21:44 -06:00
Ryan Schmidt 124bc587f0
Change C++11 requirement to C++14
Version 1.13.x and later require C++14.

Also fix missing closing backtick.
2023-06-19 06:18:12 -05:00
Copybara-Service 9b12f749fa Merge pull request #4267 from niranjan-nilakantan:niranjan-nilakantan/issue4266
PiperOrigin-RevId: 540901148
Change-Id: Ifd4c4a6d37e9bf7ff63470be401f94d2bb530c22
2023-06-16 09:18:27 -07:00
Kasra Hashemi 148b327ffa
Update README.md
Fixed punctuation issues
2023-06-16 13:59:16 +03:30
Abseil Team 18fa6a4db3 Allow clients to un-suppress output from gUnit EXPECT_EXIT tests.
This is useful for running individual tests in a separate process, which is
useful for testing e.g. flag changes which have a process-global effect.

PiperOrigin-RevId: 540580573
Change-Id: I18a5d24d79425a9d595be3369efc44e2f655f6f8
2023-06-15 07:52:45 -07:00
Dino Radakovic 4c7aee827e Skip entire test suite with `GTEST_SKIP()` in `SetUpTestSuite`
Fixes #4273

PiperOrigin-RevId: 540254167
Change-Id: I2555740d10284223539035bf73f88554fcf73f8a
2023-06-14 06:41:11 -07:00
Copybara-Service e9078161e6 Merge pull request #4188 from Mizzrym1:component
PiperOrigin-RevId: 539684886
Change-Id: Ie7f4175ad413fdb82d265374a4aca9fad23571f5
2023-06-12 09:58:37 -07:00
Abseil Team 65cfeca1a1 internal g3doc documentation change.
PiperOrigin-RevId: 539134110
Change-Id: I28041f77e18bc67aa0ffabdc7205e350deed22c8
2023-06-09 11:29:42 -07:00
Copybara-Service 334704df26 Merge pull request #4269 from elupus:patch-3
PiperOrigin-RevId: 538253216
Change-Id: Ib4bc90974f92939f884d6b5927f8bf37aac4d2c7
2023-06-06 12:04:10 -07:00
Copybara-Service 23f642ab23 Merge pull request #4260 from pratyush3757:readme_table_fix
PiperOrigin-RevId: 537887202
Change-Id: I4a52ec0da24f043eba0aa1a9d7945066c799ce8a
2023-06-05 08:47:33 -07:00
Joakim Plate 001f281926
Check for file system for current directory
Check for current directory instead of death test to get original directory.

A port may support filesystems but not death tests.
2023-06-02 12:17:43 +02:00
Pro3757 5ca3ab8331 change table to unordered list 2023-06-02 01:09:32 +05:30
Pratyush Choudhary ad1b28b4a9
Merge branch 'google:main' into readme_table_fix 2023-06-02 00:56:36 +05:30
Niranjan Nilakantan a5b94f6819 Ignore the .cache directory create by VSCode.
This is populated by the clangd language service provider.

Also ignore the cmake-variants.yaml file.
This can be created locally to select various build/test configurations.

Fixes #4266
2023-06-01 09:48:54 -07:00
Dino Radakovic 06f44bc951 Copy supported platforms from README onto https://google.github.io/googletest/platforms.html
PiperOrigin-RevId: 537016664
Change-Id: Ie62891a1061465a009b90feba0ccb91d85591160
2023-06-01 07:28:42 -07:00
Dino Radakovic 04cf298916 Provide example for setting C++ language standard in GoogleTest's Bazel quickstart and readme.
An equivalent for CMake was merged in aa99ce5a0d

Fixes #4254

PiperOrigin-RevId: 536759641
Change-Id: I8400064a24e0d78f17a0720046f505efa1167b4f
2023-05-31 10:45:43 -07:00
Paul Wankadia a7833a19bd Update GoogleTest to RE2 release `2023-06-01`.
Note that RE2 has taken a dependency on Abseil, so the `main` branch
should be used from now on. The `abseil` branch will go away soon...

PiperOrigin-RevId: 536737543
Change-Id: I0e595ee8e754b924881f997f286cdec3822763bf
2023-05-31 09:33:31 -07:00
Copybara-Service f625681bc4 Merge pull request #4256 from niranjan-nilakantan:niranjan-nilakantan/issue4255
PiperOrigin-RevId: 536480185
Change-Id: I9bdd65cd18253703abf3cc92b3f354c1f798aa3e
2023-05-30 12:40:46 -07:00
Pro3757 ded275d75e fix README table 2023-05-30 13:50:23 +05:30
niranjan 029d3dddd4 Disable some warnings for IntelLLVM on Windows.
Use /fp:precise to override IntelLLVM's default of /fp:fast.
This makes IsInf and IsNan work as expected by googletest.

```
[build]...\googletest\googlemock\test\gmock-function-mocker_test.cc(143,21): error: 'VoidReturning' overrides a member function but is not marked 'override' [-Werror,-Winconsistent-missing-override]
[build]   MOCK_METHOD(void, VoidReturning, (int n));  // NOLINT
```

```
[build] ...\googletest\googlemock\test\gmock-function-mocker_test.cc(182,20): error: exception specification of overriding function is more lax than base version [-Werror,-Wmicrosoft-exception-spec]
[build]   MOCK_METHOD(int, CTNullary, (), (Calltype(STDMETHODCALLTYPE)));
```

```
[build] ...\googletest\googletest\test\googletest-death-test-test.cc(209,5): error: unused function 'DieInCRTDebugElse12' [-Werror,-Wunused-function]
[build] int DieInCRTDebugElse12(int* sideeffect) {
[build]     ^
```

```
[build] ...\googletest\googletest\test\gtest_unittest.cc(4096,7): error: variable 'n' set but not used [-Werror,-Wunused-but-set-variable]
[build]   int n = 0;
```
2023-05-24 17:33:46 -07:00
Niranjan Nilakantan e032d57642 Build googletest with IntelLLVM compilers.
Use the same flags as Clang if the compiler id is IntelLLVM.

IntelLLVM warns if a double constant is assigned to a float.

```
[build] .../googletest/googletest/include/gtest/gtest-printers.h:516:17: warning: implicit conversion between floating point types of different sizes [-Wimplicit-float-size-conversion]
[build]       mulfor6 = 1e1;
```

IntelLLVM uses fp-model=fast by default, breaking IsNan and IsInf tests.
Use -ffp-model=precise to fix this.

IntelLLVM does not support -Wchar-subscripts

Fixes #4255
2023-05-24 12:48:51 -07:00
Copybara-Service 4580469122 Merge pull request #4250 from shlomnissan:cmake-quickstart-update
PiperOrigin-RevId: 534150776
Change-Id: Ib6c07c6abd06c259494928102e2cb800b6652612
2023-05-22 12:41:47 -07:00
Shlomi Nissan aa99ce5a0d Add CXX_STANDARD_REQUIRED to CMake quickstart 2023-05-21 16:40:06 -07:00
Dino Radakovic cb455a71fb Fix typo in version number example in README
PiperOrigin-RevId: 532796425
Change-Id: If81029216cf12b9b05f8e2d79671ba106ebd28fb
2023-05-17 08:31:17 -07:00
Dino Radakovic d6fb5e3bf7 Explicitly document googletest release tag format
Fixes #4235

PiperOrigin-RevId: 532446854
Change-Id: If7e1dade02a80d9a30813c136bba3f20c796f0fa
2023-05-16 07:05:45 -07:00
Copybara-Service 60c3602aaa Merge pull request #4231 from yagneshprajapati:main
PiperOrigin-RevId: 532204016
Change-Id: Ifa309f31bb959b43faa54c5ce631a823e6a2888a
2023-05-15 13:17:55 -07:00
Copybara-Service e42cc2294f Merge pull request #4234 from pateldeev:missing_dep
PiperOrigin-RevId: 532143036
Change-Id: I3dd57e975006478eb6b16b8ea289531738a8a0b5
2023-05-15 09:54:08 -07:00
Abseil Team bb2941fcc6 Give CreateArgvFromArgs internal linkage
It isn't declared in any header or referenced from any other file.

PiperOrigin-RevId: 531209642
Change-Id: I9e7df37f737b00994f8845ed38c3b775879cb25c
2023-05-11 08:28:20 -07:00
yagneshprajapati a8e067ae5f
Update README.md
xml report gan removed
2023-05-11 14:57:47 +05:30
yagneshprajapati 7345a66cbd
Update README.md
xUnit link
2023-05-11 14:54:04 +05:30
yagneshprajapati 88295d64a7
Update README.md
xunit link
2023-05-11 14:52:16 +05:30
pateldeev 0eb33cfa5c Add missing absl dependency from build. 2023-05-08 13:02:56 -07:00
yagneshprajapati a7fa9b3708
Update README.md 2023-05-07 15:22:54 +05:30
Abseil Team bc860af087 This trips up when compiling with -Wvla otherwise.
PiperOrigin-RevId: 529762901
Change-Id: I6ce4d630191bf265f847aef2d5dcc12a712faa60
2023-05-05 11:05:02 -07:00
Copybara-Service a3580180d1 Merge pull request #4146 from lygstate:fixes_std_pair_diff
PiperOrigin-RevId: 528781910
Change-Id: I4038332a6255921792bfb4a8098aa84243d48e15
2023-05-02 07:52:02 -07:00
Andy Soffer 76bce79a34
Merge branch 'main' into fixes_std_pair_diff 2023-05-01 14:27:38 -04:00
Copybara-Service f345b2ca6a Merge pull request #4227 from Vertexwahn:fix-spelling
PiperOrigin-RevId: 528474013
Change-Id: I60cd509aded8d8b02423ea1b07c2c27e3709a808
2023-05-01 08:05:40 -07:00
Dino Radakovic 797b0ad2a3 Use GTEST_INTERNAL_CPLUSPLUS_LANG instead of __cplusplus
Some versions of MSVC provide incorrect values for the latter and rely on _MSVC_LANG instead.
Fixes #4226.

PiperOrigin-RevId: 527919195
Change-Id: Ifcca4612074f5ebc5337094426866a187f79f90a
2023-04-28 10:16:07 -07:00
Tom Hughes 0bdaac5a14 Add qualifier to avoid argument dependent lookup
PiperOrigin-RevId: 527740306
Change-Id: I61308cc232c346d5ff8b0548d4c552a59633fffc
2023-04-27 18:17:59 -07:00
Abseil Team dea0484e4d Use Abseil Flag public API for flag parsing.
This change brings InitGoogleTest semantic in accordance with the official documentation: only GoogleTest flags are removed from argc/argv. The rest of the flags remains in place. We do nothing special for flags with unrecognized gunit_/gtest_ prefix and we do not report them.

PiperOrigin-RevId: 527257221
Change-Id: Ibb29a1bda1a44251a4ee579c0fb5bbdfd9965c21
2023-04-26 07:13:51 -07:00
Vertexwahn 97408cae55 Fix spelling 2023-04-25 20:20:01 +02:00
Copybara-Service ccdeec888e Merge pull request #4225 from TurboGawron:non_moveable_struct_fix
PiperOrigin-RevId: 526972449
Change-Id: I77d8bd37807f6c6ab5256f1e6c4abc64cceee740
2023-04-25 08:21:31 -07:00
Patryk Gawroński 51eeae5a55 gmock: fix issue #4222
Rename 'Result' struct to 'NonMoveableStruct' in
gmock-spec-builders_test.cc in ExpectCallTest.NonMoveableType test
2023-04-25 01:45:26 +02:00
Tom Hughes 783d00fd19 Use '=default' to define trivial constructor/destructors
https://clang.llvm.org/extra/clang-tidy/checks/modernize/use-equals-default.html

PiperOrigin-RevId: 526079054
Change-Id: Ia4db21e3e5f58b90de05d52fd94b291ed06d785d
2023-04-21 10:41:25 -07:00
Tom Hughes baf182e006 Use the empty method to check for emptiness
PiperOrigin-RevId: 526046714
Change-Id: I6ad0deff2a7f6dfef75a643c95ad914c83b9ba38
2023-04-21 08:42:42 -07:00
Tom Hughes a5308bb992 Add missing std includes
PiperOrigin-RevId: 525850646
Change-Id: I64387f5b933beb79cd05636dca81b7a75213383e
2023-04-20 14:29:09 -07:00
Tom Hughes 9c1efac2a1 Use std::make_unique
PiperOrigin-RevId: 525828947
Change-Id: I66f47c835bc93ba171eba0da8df09eff89c6c77b
2023-04-20 13:09:18 -07:00
Tom Hughes d7fb5e1af4 Fix spelling
PiperOrigin-RevId: 525820426
Change-Id: Ib5baa8da4a54ebb17b6ace12240ed03eedb46091
2023-04-20 12:36:18 -07:00
Tom Hughes d8a5fb7ee6 Add missing std includes
PiperOrigin-RevId: 525794940
Change-Id: Ib2639058610c91cbffbb0f22a5e71573e0cbd651
2023-04-20 11:01:56 -07:00
Tom Hughes 01bcf8c70d Make parameter names in function declaration match the names in the
definitions

https://clang.llvm.org/extra/clang-tidy/checks/readability/inconsistent-declaration-parameter-name.html

PiperOrigin-RevId: 525752102
Change-Id: Ibf9d3d1dbae8f95cfc7c6ad29fe4b677f4ee19cf
2023-04-20 08:22:57 -07:00
Copybara-Service d25e625364 Merge pull request #4219 from jerylvaz:main
PiperOrigin-RevId: 524932412
Change-Id: Ie70282f7e82cb3b9442bd15bbe49bb9b00e56eb9
2023-04-17 13:23:38 -07:00
Abseil Team 922e0b7d80 Update gMock Cookbook to reflect deprecation of testing::ByMove
PiperOrigin-RevId: 524868227
Change-Id: I702ede27570e3d3f06d534d6ccf8b39689105d07
2023-04-17 09:31:34 -07:00
Jeryl Vaz 952560d3ec Suppress a clang-tidy warning in the MATCHER_P macro 2023-04-14 11:15:21 +02:00
Copybara-Service 12a5852e45 Merge pull request #3993 from pgroke-dt:work-around-GCC-11-ADL-bug
PiperOrigin-RevId: 523706897
Change-Id: If6dcc97c81a20f8fe675241518ae1d6cf23ebf39
2023-04-12 08:28:57 -07:00
Abseil Team 8fa9461cc2 Fix FunctionMocker compilation slowdown in 9d21db9e0a
The slowdown appears to be due to an implicit conversion of distinct (yet semantically identical) lambdas to `std::function`. Lifting out the lambdas into functors that don't get re-instantiated reduces compilation times by nearly half.

Fixes #4156

PiperOrigin-RevId: 523447948
Change-Id: Ib0ae0761a54d7b1f2b706b14b2858eedf47e2297
2023-04-11 10:55:11 -07:00
Abseil Team 7f6f9c12ad Support --gtest_stream_result_to on macOS
This seems to just have been an oversight. POSIX socket APIs work just
fine on macOS.

Fixes https://github.com/google/googletest/issues/4214.

PiperOrigin-RevId: 523398386
Change-Id: I9d56cd9c6933318c1f0b0024f7fef44122fd0c83
2023-04-11 07:41:50 -07:00
Abseil Team 057b4e904f gtest.cc: run tests within a test suite in a deterministic order.
Ensure that tests are run in the order specified in the source code, even if
they are registered manually using RegisterTest. There should be no behavior
change for the common case.

PiperOrigin-RevId: 522136303
Change-Id: If155e2666780af0e514fbbf5ff2b157d5fe2fef1
2023-04-05 13:27:21 -07:00
Abseil Team 7ee260c549 Rolled back due to breaking existing tests.
PiperOrigin-RevId: 521555658
Change-Id: I09742faceb82b2b7ceb423e850a8b50d532ad6ff
2023-04-03 14:01:45 -07:00
Aaron Jacobs b5fd99bbd5 gtest.cc: run tests within a test suite in a deterministic order.
Ensure that tests are run in the order specified in the source code, even if
they are registered manually using RegisterTest. There should be no behavior
change for the common case.

PiperOrigin-RevId: 520729483
Change-Id: I400c78400c6929fccae0676214d993251f31888f
2023-03-30 13:35:22 -07:00
Abseil Team 0cd05c6ea9 Update naming to "GoogleTest" in the GoogleTest Primer.
PiperOrigin-RevId: 520409541
Change-Id: I75a0b8615ece923137d4f5dcdb915f38e4c3b1b2
2023-03-29 12:25:22 -07:00
Copybara-Service ca0d46e95d Merge pull request #4103 from gonzalobg:bugfix/nvcxx_support
PiperOrigin-RevId: 520017251
Change-Id: Ib2be28787b739344c80a5d937f875737ba44a0ec
2023-03-28 07:04:28 -07:00
Derek Mauro 3656c2713e Fix redundant redeclaration warning
Fixes #4200

PiperOrigin-RevId: 520017094
Change-Id: Id707a1c0489edde083771ccd412d7035612474dc
2023-03-28 07:03:07 -07:00
Derek Mauro 13b1900717 Remove the Win64 arch from the CMake Generator since
this can't be used with MSVC 2022

We also have to use Python 3.4 since that is what the base image has,
however, I will create a new image soon that has a modern version
of Python.

PiperOrigin-RevId: 520010732
Change-Id: Icf2420fd97d2bbc310382a17793045b6e16d62bb
2023-03-28 06:28:10 -07:00
Derek Mauro 88af49efa7 Migrate CI builds to MSVC 2022
PiperOrigin-RevId: 519792199
Change-Id: Ic821b264bf1aef5e03f22fb0e288dd9d56fd536b
2023-03-27 12:10:12 -07:00
Abseil Team e9fb5c7bac Replace `const char*` with `absl::string_view` as the latter is preferred.
PiperOrigin-RevId: 519122695
Change-Id: I7dcf969d15d26ccc4b376e3ab6db7f4c08c7386f
2023-03-24 05:58:11 -07:00
Abseil Team a0ced33ac6 Internal Code Change
PiperOrigin-RevId: 519071084
Change-Id: I6459d60606e93bf658e505544538367508722857
2023-03-24 00:23:16 -07:00
Abseil Team 6f01e3dc12 Internal Code Change
PiperOrigin-RevId: 518810140
Change-Id: Id3f9471f827894761080bc9199b0a092dc829b5f
2023-03-23 03:08:22 -07:00
gonzalobg 5d6ba6d3b0 Add doc comment 2023-03-23 10:28:12 +01:00
Aaron Jacobs 5fce13091d gtest.cc: add a newline after a failure when there is no OS stack trace.
This makes the behavior consistent when GTEST_STACK_TRACE_DEPTH is set to zero
and not: there is always vertical whitespace separating failure messages.

PiperOrigin-RevId: 518744611
Change-Id: I5b4af40633849850660504c3f497a76601d4311d
2023-03-22 20:19:44 -07:00
Abseil Team 974e18ee6f Fix minor bug -- if ExecDeathTestChildMain() ever returns, we should exit
with a non-zero status.

PiperOrigin-RevId: 518306642
Change-Id: I72fb2764e477acb0362593c63f7748c4f64db0c6
2023-03-21 10:07:29 -07:00
Abseil Team e9faae170f Internal Code Change
PiperOrigin-RevId: 518213370
Change-Id: I88d0acc195dadf8d03cd15c8534c721141db14ea
2023-03-21 02:36:37 -07:00
Copybara-Service fa87209829 Merge pull request #4157 from dpogue:patch-1
PiperOrigin-RevId: 518065122
Change-Id: I3a71c85626050b9aecebd4aa10d668ea3c48c82c
2023-03-20 13:46:39 -07:00
Phoebe Liang 3288c4deae Revise documentation to include AbslStringify
PiperOrigin-RevId: 518039451
Change-Id: Ife548d0f83266746ce1981bea31e70e6b43027c9
2023-03-20 12:11:55 -07:00
Abseil Team 471087fbfc Introduce std::make_unique and bool literals where possible
PiperOrigin-RevId: 517910526
Change-Id: I398704f4b2ca0a55c86a06ca8b47d34c8670ddd7
2023-03-20 02:43:48 -07:00
Abseil Team 9fd3fb00ff Remove unused using-declarations in sample unit tests
PiperOrigin-RevId: 517910369
Change-Id: I87f29fb09bc83e7d1c7cffdf267b2f691d261c26
2023-03-20 02:42:55 -07:00
Abseil Team 28219b169c Fix typo in test case name.
PiperOrigin-RevId: 517814929
Change-Id: I2bc498ba048d4deab1cf2a021487f63fe44c466d
2023-03-19 14:30:57 -07:00
Copybara-Service 837f2229a9 Merge pull request #4182 from venik:venik-char8-lib-fix
PiperOrigin-RevId: 517470997
Change-Id: I12b079dc1536f136dd0514871fe79f9678b1fd6a
2023-03-17 11:58:34 -07:00
Copybara-Service e49c6b946a Merge pull request #4180 from sergio-nsk:patch-3
PiperOrigin-RevId: 517200256
Change-Id: I2ad5d4644f9e2095909ef646a7207c42a38082cf
2023-03-16 12:46:37 -07:00
Deanna Garcia f53219cdcb Move transitive dependencies of googletest to googletest_deps.bzl to allow other bazel projects to depend on that rule instead of googletests' transitive dependencies directly.
PiperOrigin-RevId: 517166391
Change-Id: I9158e24d3f9613f3bcc811c028e1a15f213b7c40
2023-03-16 10:48:24 -07:00
Maciej Sroczyński abad8af018 Add COMPONENT to install 2023-03-14 23:48:08 +01:00
Abseil Team 391ce627de Internal Code Change
PiperOrigin-RevId: 515855852
Change-Id: I8016eefd45c36089e189bbd1ebbe9fdabe68255b
2023-03-11 02:48:36 -08:00
Lawrence Wolf-Sonkin 038e392ebd [gtest] Drop custom-rolled heterogeneous comparator functors in favor of C++ standard ones
* Standard heterogeneous comparator functors such as `std::equal_to<>` and `std::less<>` [have been available since C++14](https://en.cppreference.com/w/cpp/utility/functional/less_void)
* Now that [C++14 is the minimum supported version of C++ in Googletest](https://github.com/google/oss-policies-info/blob/main/foundational-cxx-support-matrix.md), let's delete these duplications of the standard library

PiperOrigin-RevId: 515743068
Change-Id: I1563a2f94039c3a6688429298555545a922f6d7e
2023-03-10 14:43:13 -08:00
Sergey 2d3b36d40a Environment variables are accessible in UWP/WinRT apps
Environment variables, for example `GTEST_FILTER`, `GTEST_OUTPUT` can be set
before getting WinRT app run. GoogleTest can read environment variables and use
them. It's easier than setting and passing command line parameters to WinRT app.
2023-03-10 09:30:49 -07:00
Abseil Team 50e07d1c92 Apply clang-tidy fixes
PiperOrigin-RevId: 515265927
Change-Id: Iea11668fa4bbf08f6d418a3823e836fb5b874dcc
2023-03-09 01:19:58 -08:00
Abseil Team 48a1b11058 Add a comment to clarify Fuchsia process launcher requirement.
PiperOrigin-RevId: 515154129
Change-Id: I3dd9e912e160d09d1f74a467336212701d4e1b7d
2023-03-08 15:10:45 -08:00
Abseil Team 82a570453c Apply clang-tidy fixes
PiperOrigin-RevId: 514936218
Change-Id: I24c443a2ca75c875052b0cf2d0a48e808d03ae43
2023-03-07 22:33:56 -08:00
Tom Hughes a798c2f102 Add -Wundef to the CI scripts when building with Bazel
The CMake build already adds -Wundef for gcc/clang. This change makes sure that
the gcc/clang Bazel builds also compile correctly with -Wundef (#3267).

PiperOrigin-RevId: 514864451
Change-Id: I7798a4a4c68d037e23625db24ee29df454367734
2023-03-07 15:58:12 -08:00
Tom Hughes 678c1c73de Fix compilation of googletest-printers-test.cc when using -Wundef
#3267

PiperOrigin-RevId: 514858420
Change-Id: Ic712aafad25f5e63ae48f647557de95cef890978
2023-03-07 15:34:41 -08:00
Abseil Team cf4f4400db Suppress std::string DLL interface warning introduced in commit f063cd25c9
Fixes #4171

PiperOrigin-RevId: 514777144
Change-Id: I6f4b309c407684522fc1bc94dcc980ea1fe09cd9
2023-03-07 10:40:39 -08:00
Abseil Team 1a727c27aa Internal Code Change
PiperOrigin-RevId: 514678702
Change-Id: I5b0089d905152ccb85022be395ed340e42586234
2023-03-07 03:06:06 -08:00
Alexander Nikforov 92f0d72507 __cpp_char8_t does not cover std::u8string implementation, but __cpp_lib_char8_t does 2023-03-06 16:34:55 -08:00
Dino Radakovic e1ee0fa3e1 Fix indentation syntax error in feature request issue template
Fixes #4166

PiperOrigin-RevId: 514427586
Change-Id: I947b2a0f3687430f27e37fcd2b3fff8b1cb0b16e
2023-03-06 09:04:22 -08:00
Tom Hughes d92a270d2d Fix compilation with -Wundef.
Fixes #3267

PiperOrigin-RevId: 513946600
Change-Id: I0dd1daa04aeb735a238c4c0af6676565d64cbc21
2023-03-06 07:19:04 -08:00
Tom Hughes 2ddba26691 Always specify definitions for internal macros
These macros should only be used within googletest, so changing them will not
affect external users.

This allows compiling with -Wundef (#3267).

PiperOrigin-RevId: 513946162
Change-Id: I2f2b7df9123adeba4147593b2b55fde349ccce4f
2023-03-06 07:18:52 -08:00
Tom Hughes 0bdd45085a Remove GTEST_FOR_GOOGLE_ macro
The "more details" warning message printed by this macro is no longer
needed.

PiperOrigin-RevId: 513945729
Change-Id: I644910216dbef2fe92eee3a648f2078e705cc7a1
2023-03-06 07:18:26 -08:00
Tom Hughes 16feffa8fa Replace "#if GTEST_HAS_ABSL" with "#ifdef GTEST_HAS_ABSL"
This allows compilation with "-Wundef" (#3267).

PiperOrigin-RevId: 513945230
Change-Id: I45ef19c7ff3d20e97216bd031d406a03365471da
2023-03-06 07:16:32 -08:00
Tom Hughes dc10c3b5e5 Use "#ifdef" with public macros
This allows compilation with "-Wundef" (#3267).

PiperOrigin-RevId: 513944726
Change-Id: I1a3854bb2333d5dec6c0ff91ee1eddd9a766ab91
2023-03-06 07:15:44 -08:00
Tom Hughes 7b1ced0682 Only define GTEST_IS_THREADSAFE to 1, not 0
According to the comments, "Feature-indicating macros",
such as GTEST_IS_THREADSAFE should be defined to 1 when supported
and otherwise undefined (never 0).

PiperOrigin-RevId: 513944266
Change-Id: I0f5c8bed107a5f20e957ec7c70339540ca2fe831
2023-03-06 07:15:32 -08:00
Tom Hughes a08c03c5f5 Always specify definitions for internal macros
These macros should only be used within googletest, so changing them will not
affect external users.

This allows compiling with -Wundef (#3267).

PiperOrigin-RevId: 513943800
Change-Id: I697b1005c29b0d5af06f583f202d86db48b567b9
2023-03-06 07:15:17 -08:00
Tom Hughes 23142843f7 Use "#ifdef GTEST_OS_..." instead of "#if GTEST_OS_..."
This is compatible with compiling with "-Wundef" (#3267).

PiperOrigin-RevId: 513943378
Change-Id: I47cf5fabbb77be061c4483a0adc54511af6b191c
2023-03-06 07:14:21 -08:00
Abseil Team cead3d57c9 Internal Code Change
PiperOrigin-RevId: 513770561
Change-Id: I82fbed177c6ba4e2e5b776ae3e0255c868e32e25
2023-03-03 02:25:22 -08:00
Copybara-Service 2d4f208765 Merge pull request #4158 from VuPhamVan:main
PiperOrigin-RevId: 512959074
Change-Id: Ifbc63077aad573d4496a837f2f57584192573820
2023-02-28 09:40:00 -08:00
Abseil Team 555486f408 Rephrase the description of TEST_F() arguments for clarity.
PiperOrigin-RevId: 512937964
Change-Id: Ifa6369a80dc7d8efe60511417496d58317cfc28d
2023-02-28 08:17:16 -08:00
Yonggang Luo 6f1c4b3d7b Fixes the test gmock_output_test.py with MSVC
For MSVC, gmock_output_test.py output struct std::pair<int,bool>, for GCC, it's output
std::pair<int, bool>, it's not the same, my intention is getting these to be same by removing
struct  for MSVC's outptu, and strip redundant space for GCC.

As a by-product,
```
#ifdef _MSC_VER
#define ERROR_DESC "class std::runtime_error"
#else
#define ERROR_DESC "std::runtime_error"
#endif
```
can be simplified to

```
#define ERROR_DESC "std::runtime_error"
```

Signed-off-by: Yonggang Luo <luoyonggang@gmail.com>
2023-03-01 00:13:32 +08:00
Abseil Team 8aa75fa771 Eliminate argv list action parameter.
PiperOrigin-RevId: 512791992
Change-Id: Ie7fc37ea06ea7d9f595268c9ec84a0e144c297bb
2023-02-27 18:47:21 -08:00
Copybara-Service c7d0bc8309 Merge pull request #4164 from sergio-nsk:patch-2
PiperOrigin-RevId: 512753220
Change-Id: I3f7ad6c62c738d1d7405c50924b36deac8a9ac85
2023-02-27 15:49:24 -08:00
Phoebe Liang 6882aa0987 Reorder printers list.
PiperOrigin-RevId: 512708763
Change-Id: I1f24f2e1d17359aee5aa4cdf614c9357872ca03b
2023-02-27 13:03:15 -08:00
Copybara-Service 2ce822ac55 Merge pull request #4143 from iwsfutcmd:patch-1
PiperOrigin-RevId: 512696294
Change-Id: I6c135ca295e663df48db5b32bad4639d0c0c0774
2023-02-27 12:15:43 -08:00
Abseil Team 95d8c7ac43 Reformat to current g3doc style guide. No content changes.
PiperOrigin-RevId: 512681427
Change-Id: I88b22d82430ed145b8342747adf300f3e734d43b
2023-02-27 11:21:55 -08:00
Abseil Team 3d787f5a0d Add `const` qualifier to `gtest_sentinel` which doesn't change.
PiperOrigin-RevId: 512094429
Change-Id: I29b116da65bc7471c43021d1650d90f7b2a685c2
2023-02-24 09:50:32 -08:00
Abseil Team d0ba3ccc76 Added a missing semicolon for GTEST_FLAG_SET code snippet.
PiperOrigin-RevId: 511858980
Change-Id: I0f9e6ffdeb8ae809d662eb3ff46eab5a109a8d93
2023-02-23 12:40:25 -08:00
Dino Radakovic 39a26e12d6 Avoid redundant declaration of static constexpr members in c++17
Keep declarations in c++ < 17 using new macro, GTEST_INTERNAL_NEED_REDUNDANT_CONSTEXPR_DECL.

Fixes #4148.

PiperOrigin-RevId: 511510401
Change-Id: I76c3f2fccf07a0978adcbe5f8f0203b9d0c33872
2023-02-22 09:02:55 -08:00
Dino Radakovic d9a4bbcaf2 Update googletest's test docker containers
PiperOrigin-RevId: 511500508
Change-Id: Ib686f93d317b95ac9b9bcb0a5566500316ba1929
2023-02-22 08:18:38 -08:00
Sergey 10493e3854
Fix error in_death_test_child_process: undeclared identifier
The error occurs if !GTEST_HAS_DEATH_TEST on Windows.
2023-02-21 13:29:20 -07:00
Tom Hughes 750d67d809 Remove int64_t cast in RecordProperty
Historically, calls to RecordProperty with values that are convertible to
int64_t have been casted to int64_t. The result was that types like float or
double would be truncated when printed (e.g., 4.75 -> 4). This change removes
the cast so that the types are printed in a more appropriate manner.

PiperOrigin-RevId: 511238685
Change-Id: I80de5db14462da2a3e1f476086025ae514383a17
2023-02-21 10:25:39 -08:00
Vũ Phạm 810fb81153
Fix typo 2023-02-19 21:59:23 +07:00
Darryl Pogue fe5b7ef71e
Set CMP0069 policy to avoid warnings
When googletest and googlemock are included as a git submodule and referenced
as part of an existing CMake project, multiple warnings are printed out due to
not setting a value for the CMP0069 policy.
2023-02-17 23:22:27 -08:00
Abseil Team 7a7231c442 Fix link in ReportUninterestingCall message
This CL changes the link in the ReportUninterestingCall message from
.../gmock_cook_book.md#knowing-when-to-expect to
.../gmock_cook_book.md#knowing-when-to-expect-useoncall. This is necessary
following https://github.com/google/googletest/commit/31ff597.

PiperOrigin-RevId: 510138974
Change-Id: Ic98c84b07751d27dfc95eddbe7874f76d68b456f
2023-02-16 07:33:48 -08:00
Abseil Team f063cd25c9 Remove strdup usage
PiperOrigin-RevId: 509947007
Change-Id: I31e1274afa889776829c877c40c9af589298dcf2
2023-02-15 15:22:10 -08:00
Abseil Team 2057566e4e Remove some filesystem APIs and tests under !GTEST_HAS_FILE_SYSTEM
PiperOrigin-RevId: 509537606
Change-Id: I68f7054e34b1fe76c1fd85099fffa4ee3c2b00c0
2023-02-14 08:33:42 -08:00
Abseil Team b80a07ffe6 Avoid reliance on <any> header without RTTI on MSVC
Fixes: #4144
PiperOrigin-RevId: 509322023
Change-Id: I52bdf6c25ff433327e174d3f9583b3d50b872d58
2023-02-13 13:46:03 -08:00
Yonggang Luo 097f64e986 Revert "Fix gmock_output_test when using MSVC"
This reverts commit 0a3b403fe0.
2023-02-09 15:29:01 +08:00
Tom Hughes b73f27fd16 Fix _MSC_VER check
Use "#if defined(_MSC_VER)" instead of "#if _MSC_VER" to be consistent
with other usages in googletest and to work with the "-Wundef" warning.

PiperOrigin-RevId: 508087630
Change-Id: I29c16fd2fa51a9dfecd55e10362a020318318956
2023-02-08 08:29:54 -08:00
Tom Hughes 0570e2d930 Remove GTEST_USES_PCRE references
Nothing defines GTEST_USES_PCRE anymore. It was only meant for
internal use, so nothing public should be relying on it:
https://github.com/google/googletest/issues/2735#issuecomment-644849438.

Found when compiling with "-Wundef".

Fixes #2735.

PiperOrigin-RevId: 507823660
Change-Id: Ie19e576ff01dc3b16381338578ece92adccfc09b
2023-02-07 10:38:08 -08:00
Martijn Vels ee33b34bec Fix GTEST_HAS_ABSL define check for [-Werror=undef] compilations
PiperOrigin-RevId: 507788664
Change-Id: Ib8cfbf3102a38e210fdae8a548fd84f0723ccc10
2023-02-07 08:28:15 -08:00
Abseil Team 3d568bdda5 Add support for the alternative base64 encoding in RFC 4648 section 5 to `WhenBase64Unescaped`.
PiperOrigin-RevId: 507527786
Change-Id: Ie5e088b1814981f6c760d7e25418a430172705ec
2023-02-06 11:00:05 -08:00
Ben Yang 3f9c7fcaa3
Update testing.md
Fixed Parameter Generator table
2023-02-02 13:14:43 -08:00
Tom Hughes 2f2e72bae9 Fix compiler flags in Linux presubmit
CXX_FLAGS should be CXXFLAGS and the quoting was wrong.
As a result, "-Werror -Wdeprecated" was not being applied.

https://cmake.org/cmake/help/latest/envvar/CXXFLAGS.html

PiperOrigin-RevId: 506656655
Change-Id: Ic5e861be3b9c32257eb9aabb845c931f3cba7122
2023-02-02 10:16:20 -08:00
Tom Hughes deaf5615f1 Fix -Wsign-conversion warnings
googletest/test/gtest_xml_outfile2_test_.cc:48:39:
warning: implicit conversion turns floating-point number into integer:
'float' to 'int64_t' (aka 'long') [-Wfloat-conversion]
  RecordProperty("TestFloatProperty", float_prop);
  ~~~~~~~~~~~~~~                      ^~~~~~~~~~

googletest/test/gtest_xml_outfile2_test_.cc:51:40:
warning: implicit conversion turns floating-point number into integer:
'double' to 'int64_t' (aka 'long') [-Wfloat-conversion]
  RecordProperty("TestDoubleProperty", double_prop);
  ~~~~~~~~~~~~~~                       ^~~~~~~~~~~

googletest/test/gtest_xml_outfile2_test_.cc:57:39:
warning: implicit conversion changes signedness:
'size_t' (aka 'unsigned long') to 'int64_t' (aka 'long') [-Wsign-conversion]
  RecordProperty("TestSizetProperty", size_t_prop);
  ~~~~~~~~~~~~~~                      ^~~~~~~~~~~
PiperOrigin-RevId: 506644143
Change-Id: I9c2cd5f52daebe25e73bb97f696687797ed2cabf
2023-02-02 09:31:44 -08:00
Tom Hughes 4f7c63d991 Remove unneccessary "#if _MSC_VER"
The GTEST_DISABLE_MSC_WARNINGS macros already have an _MSC_VER check.
This change also adds a missing GTEST_DISABLE_MSC_WARNINGS_POP_ in
gtest-typed-test_test.cc.

PiperOrigin-RevId: 506636248
Change-Id: Ifdc044528f5448fbf0175887d1671f1e1f3040b9
2023-02-02 09:02:17 -08:00
Abseil Team 6f21175f57 Add a trailing decimal point to FormatTimeInMillisAsSeconds() output when input
is an exact N seconds.

PiperOrigin-RevId: 506610898
Change-Id: Idcd705c719e0e721148c350c8a14f27b9eb5c4f7
2023-02-02 07:15:23 -08:00
Tom Hughes ebedaa18c7 Fix include order
PiperOrigin-RevId: 506424617
Change-Id: If7f0beb92022589523db204a6b5cbe9249cebf62
2023-02-01 14:32:28 -08:00
Tom Hughes 394a8a568c Remove GMOCK_RENAME_MAIN
GMOCK_RENAME_MAIN appears unused.

PiperOrigin-RevId: 506387823
Change-Id: I732c1f64f9038991a5c9aea1f2ad6fff07622afa
2023-02-01 12:14:47 -08:00
Tom Hughes fd36851c8d Build gmock Python tests when building with cmake
Fixes #4124.

PiperOrigin-RevId: 506379032
Change-Id: Ibc44ffce1012bbf313565728a3d9e5e6e56ba660
2023-02-01 11:44:56 -08:00
Tom Hughes 0a3b403fe0 Fix gmock_output_test when using MSVC
std::pair is printed as "struct std::pair<int,bool>" when using MSVC
vs "std::pair<int,bool>" with other compilers. Switch to "std::tuple", which
is the same for all compilers.

See
https://learn.microsoft.com/en-us/cpp/standard-library/pair-structure
https://learn.microsoft.com/en-us/cpp/standard-library/tuple-class

PiperOrigin-RevId: 506340295
Change-Id: Ib4ce2f74d54888a4e4173f42da1b55cc5583f7d4
2023-02-01 09:33:23 -08:00
Tom Hughes d9251171f5 Fix sign conversion warning from clang:
googletest/samples/prime_tables.h:81:57: error:
implicit conversion changes signedness: 'int' to 'unsigned long'
[-Werror,-Wsign-conversion]
      : is_prime_size_(max + 1), is_prime_(new bool[max + 1]) {
                                           ~~~      ~~~~^~~

PiperOrigin-RevId: 506065360
Change-Id: Ida4550562531012c089e2f9fcf530b3a78889fa3
2023-01-31 11:37:10 -08:00
Tom Hughes 4fb7039fda Use GTEST_DISABLE_MSC_WARNINGS macros to disable warnings
Prior to this change we had a mixture of pragmas and
GTEST_DISABLE_MSC_WARNINGS; this change consolidates all instances
to use the macros.

PiperOrigin-RevId: 505786926
Change-Id: I2be8f6304387393995081af42ed32c2ad1bba5a7
2023-01-30 13:45:46 -08:00
Tom Hughes f1c05d4544 Use explicit short to fix MSVC compiler warning
warning C4244: 'initializing': conversion from 'int' to 'short', possible
loss of data

PiperOrigin-RevId: 505774670
Change-Id: I3524040334a4b265bae12cfacdd2b615cbb1cfc8
2023-01-30 12:58:34 -08:00
Copybara-Service e3dbee4b35 Merge pull request #4115 from SunBlack:codespell
PiperOrigin-RevId: 505740434
Change-Id: I963f6a6b667a030c87f211173f578f7c35733e68
2023-01-30 10:55:07 -08:00
Tom Hughes b72202078d Test current behavior of doubles and floats in RecordProperty
PiperOrigin-RevId: 505104193
Change-Id: I4c2758f22ee1321ed6b6662ab2668f6c5b6aa661
2023-01-27 06:38:52 -08:00
Tom Hughes 403a968d3c Fix formatting in Python files
PiperOrigin-RevId: 504890435
Change-Id: Ia9a89d0d7a07fe70c12f7f0202c8256c94d2f118
2023-01-26 11:06:14 -08:00
Tom Hughes 0194f90d0e Fix Python docstrings to match style guide
https://google.github.io/styleguide/pyguide.html#s3.8.3-functions-and-methods

PiperOrigin-RevId: 504857416
Change-Id: I7815ce27e454a120b3974ae7e2bea952108b836c
2023-01-26 09:04:40 -08:00
Abseil Team 4b4c56aff6 Make document and example code more clear in the caveat section for potential memory leak with `SetUpTestSuite`.
PiperOrigin-RevId: 504706717
Change-Id: I5842a4569f81f6c0d636099b5cdaabe778996949
2023-01-25 17:43:53 -08:00
Tom Hughes b495f72f1f Use more specific assertLess instead of assertTrue
PiperOrigin-RevId: 504670738
Change-Id: Ib68b676403204098e89f536f9b8317f9f717c24d
2023-01-25 15:01:21 -08:00
Tom Hughes f47d75faf3 Fix pylint warning
Possible unbalanced tuple unpacking with sequence defined at line N:
left side has 1 label(s), right side has 0 value(s)
[unbalanced-tuple-unpacking]

PiperOrigin-RevId: 504640001
Change-Id: If8d6038a9acf74a409cb6b6ee30cea7745b4b303
2023-01-25 13:03:31 -08:00
Tom Hughes 137dcd18cc Remove pylint disable comments that no longer trigger warnings
PiperOrigin-RevId: 504633130
Change-Id: I3fdbc83975fea97aa53f7325d323adead0a30e24
2023-01-25 12:35:34 -08:00
Abseil Team 643862d8ad Mention `DescribeMatcher` in matchers.md
PiperOrigin-RevId: 504591836
Change-Id: I48c93490d0c3b7c6bfaceceb1cef5eedeacfe6c6
2023-01-25 10:04:20 -08:00
Tom Hughes d1ad27e0a4 Fix formatting in subset of Python files
These files were formatted with automated tools. The remaining Python
files require some manual fix ups, so they will be fixed separately.

PiperOrigin-RevId: 504579820
Change-Id: I3923bd414bffe3ded6163ec496cd09ace3951928
2023-01-25 09:14:26 -08:00
Tom Hughes 6c65a1ca35 Replace numeric lints with their string equivalent
PiperOrigin-RevId: 504570278
Change-Id: Iab9c988b3a781eaafbdd97c924a74c2269125799
2023-01-25 08:32:19 -08:00
Tom Hughes 2491710524 Fix formatting of C++ files
PiperOrigin-RevId: 504325204
Change-Id: Iaa1d6d0ab1dccaaeef26f9cb109d530835499240
2023-01-24 11:10:09 -08:00
Tom Hughes 408471e20c Fix formatting of Markdown files
PiperOrigin-RevId: 504308485
Change-Id: Ia4ae97b2173b44b89aa5d987ddefd6e0c1488386
2023-01-24 10:13:03 -08:00
Derek Mauro 544c96ed5b Remove FloatingPoint::Max() in favor of std::numeric_limits::max()
In #4113 a user says Max clashes with a macro. Since it is only used in
a test, use std::numeric_limits::max() instead.

Note that in headers, the macro issue can be mitigated with
parenthesis like this: `(std::numeric_limits<T>::max)()`

PiperOrigin-RevId: 504284906
Change-Id: Ibf430caec1a6afdf6b303534fec6a4fd00a6373f
2023-01-24 08:41:35 -08:00
Abseil Team ec25eea8f8 Mention using MockFunction as a way to mock free functions.
PiperOrigin-RevId: 502901538
Change-Id: I0cf351b9ad1411ae9c45e09550c24e006a24e179
2023-01-18 09:43:56 -08:00
Tom Hughes bdb3b0a493 Replace deprecated python calls
assert_ -> assertTrue/assertFalse/assertIn/assertNotIn
assertEquals -> assertEqual

PiperOrigin-RevId: 502654909
Change-Id: I25d30095a83c3806606cb80d676b3c979495e6bd
2023-01-17 12:16:29 -08:00
Dino Radakovic bba28fa8b2 Announce that GitHub actions are not GoogleTest's canonical CI
PiperOrigin-RevId: 502654465
Change-Id: I51269e8c389ad63f3737132feec1b6d20dc4c314
2023-01-17 12:14:32 -08:00
Tom Hughes 5cd81a7848 Fix -Wimplicit-int-float-conversion warning
We're intentionally losing precision in this case, so add a cast.

googletest/googletest/include/gtest/gtest-printers.h:532:9: error:
implicit conversion from 'int32_t' (aka 'int') to 'float' may lose precision
[-Werror,-Wimplicit-int-float-conversion]
    if (static_cast<int32_t>(val * mulfor6 + 0.5) / mulfor6 == val) return 6;
        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~

googletest/googletest/include/gtest/gtest-printers.h:544:9: error:
implicit conversion from 'int32_t' (aka 'int') to 'float' may lose precision
[-Werror,-Wimplicit-int-float-conversion]
    if (static_cast<int32_t>(val / divfor6 + 0.5) * divfor6 == val) return 6;
        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~

PiperOrigin-RevId: 502646042
Change-Id: I05989ee42675b531a9907616c9582a5a7c77bed6
2023-01-17 11:44:46 -08:00
Derek Mauro 9d697cc81c Update documentation for v1.13.0
PiperOrigin-RevId: 502644180
Change-Id: Id20b5e4eec22035da278f1767002657f66dbe79c
2023-01-17 11:38:05 -08:00
SunBlack 64e0945546 Fix typos found by codespell 2023-01-04 17:36:11 +01:00
Gonzalo Brito Gadeschi ab669cad0e Fix warnings with nvc++ as the compiler
Closes #3849.
2022-12-28 06:00:31 -08:00
Paul Groke 096014a45d work around GCC 6~11 ADL bug
see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=51577
ADL seems to work properly when we do the SFINAE check via the return type, but not when using a dummy template parameter

fix #3992
2022-09-15 13:36:49 +02:00
Paul Groke b42fa26690 extend GTEST_HAS_PTHREAD default to enable threading on Solaris, AIX and z/OS
fix #3990
2022-08-31 15:02:46 +02:00
165 changed files with 8703 additions and 5190 deletions

View File

@ -1,43 +0,0 @@
name: ci
on:
push:
pull_request:
env:
BAZEL_CXXOPTS: -std=c++14
jobs:
Linux:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Tests
run: bazel test --cxxopt=-std=c++14 --features=external_include_paths --test_output=errors ...
macOS:
runs-on: macos-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Tests
run: bazel test --cxxopt=-std=c++14 --features=external_include_paths --test_output=errors ...
Windows:
runs-on: windows-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Tests
run: bazel test --cxxopt=/std:c++14 --features=external_include_paths --test_output=errors ...

5
.gitignore vendored
View File

@ -8,6 +8,7 @@ bazel-genfiles
bazel-googletest bazel-googletest
bazel-out bazel-out
bazel-testlogs bazel-testlogs
MODULE.bazel.lock
# python # python
*.pyc *.pyc
@ -24,6 +25,10 @@ Win32-Release/
x64-Debug/ x64-Debug/
x64-Release/ x64-Release/
# VSCode files
.cache/
cmake-variants.yaml
# Ignore autoconf / automake files # Ignore autoconf / automake files
Makefile.in Makefile.in
aclocal.m4 aclocal.m4

View File

@ -56,6 +56,12 @@ config_setting(
constraint_values = ["@platforms//os:openbsd"], constraint_values = ["@platforms//os:openbsd"],
) )
# NOTE: Fuchsia is not an officially supported platform.
config_setting(
name = "fuchsia",
constraint_values = ["@platforms//os:fuchsia"],
)
config_setting( config_setting(
name = "msvc_compiler", name = "msvc_compiler",
flag_values = { flag_values = {
@ -77,6 +83,10 @@ cc_library(
) )
# Google Test including Google Mock # Google Test including Google Mock
# For an actual test, use `gtest` and also `gtest_main` if you depend on gtest's
# main(). For a library, use `gtest_for_library` instead if the library can be
# testonly.
cc_library( cc_library(
name = "gtest", name = "gtest",
srcs = glob( srcs = glob(
@ -132,23 +142,42 @@ cc_library(
}), }),
deps = select({ deps = select({
":has_absl": [ ":has_absl": [
"@com_google_absl//absl/debugging:failure_signal_handler", "@abseil-cpp//absl/container:flat_hash_set",
"@com_google_absl//absl/debugging:stacktrace", "@abseil-cpp//absl/debugging:failure_signal_handler",
"@com_google_absl//absl/debugging:symbolize", "@abseil-cpp//absl/debugging:stacktrace",
"@com_google_absl//absl/flags:flag", "@abseil-cpp//absl/debugging:symbolize",
"@com_google_absl//absl/flags:parse", "@abseil-cpp//absl/flags:flag",
"@com_google_absl//absl/flags:reflection", "@abseil-cpp//absl/flags:parse",
"@com_google_absl//absl/flags:usage", "@abseil-cpp//absl/flags:reflection",
"@com_google_absl//absl/strings", "@abseil-cpp//absl/flags:usage",
"@com_google_absl//absl/types:any", "@abseil-cpp//absl/strings",
"@com_google_absl//absl/types:optional", "@re2",
"@com_google_absl//absl/types:variant", ],
"@com_googlesource_code_re2//:re2", "//conditions:default": [],
}) + select({
# `gtest-death-test.cc` has `EXPECT_DEATH` that spawns a process,
# expects it to crash and inspects its logs with the given matcher,
# so that's why these libraries are needed.
# Otherwise, builds targeting Fuchsia would fail to compile.
":fuchsia": [
"@fuchsia_sdk//pkg/fdio",
"@fuchsia_sdk//pkg/syslog",
"@fuchsia_sdk//pkg/zx",
], ],
"//conditions:default": [], "//conditions:default": [],
}), }),
) )
# `gtest`, but testonly. See guidance on `gtest` for when to use this.
alias(
name = "gtest_for_library",
testonly = True,
actual = ":gtest",
)
# Implements main() for tests using gtest. Prefer to depend on `gtest` as well
# to ensure compliance with the layering_check Bazel feature where only the
# direct hdrs values are available.
cc_library( cc_library(
name = "gtest_main", name = "gtest_main",
srcs = ["googlemock/src/gmock_main.cc"], srcs = ["googlemock/src/gmock_main.cc"],

View File

@ -1,22 +1,10 @@
# Note: CMake support is community-based. The maintainers do not use CMake # Note: CMake support is community-based. The maintainers do not use CMake
# internally. # internally.
cmake_minimum_required(VERSION 3.5) cmake_minimum_required(VERSION 3.16)
if (POLICY CMP0048)
cmake_policy(SET CMP0048 NEW)
endif (POLICY CMP0048)
if (POLICY CMP0069)
cmake_policy(SET CMP0069 NEW)
endif (POLICY CMP0069)
if (POLICY CMP0077)
cmake_policy(SET CMP0077 NEW)
endif (POLICY CMP0077)
project(googletest-distribution) project(googletest-distribution)
set(GOOGLETEST_VERSION 1.13.0) set(GOOGLETEST_VERSION 1.16.0)
if(NOT CYGWIN AND NOT MSYS AND NOT ${CMAKE_SYSTEM_NAME} STREQUAL QNX) if(NOT CYGWIN AND NOT MSYS AND NOT ${CMAKE_SYSTEM_NAME} STREQUAL QNX)
set(CMAKE_CXX_EXTENSIONS OFF) set(CMAKE_CXX_EXTENSIONS OFF)
@ -27,11 +15,20 @@ enable_testing()
include(CMakeDependentOption) include(CMakeDependentOption)
include(GNUInstallDirs) include(GNUInstallDirs)
#Note that googlemock target already builds googletest # Note that googlemock target already builds googletest.
option(BUILD_GMOCK "Builds the googlemock subproject" ON) option(BUILD_GMOCK "Builds the googlemock subproject" ON)
option(INSTALL_GTEST "Enable installation of googletest. (Projects embedding googletest may want to turn this OFF.)" ON) option(INSTALL_GTEST "Enable installation of googletest. (Projects embedding googletest may want to turn this OFF.)" ON)
option(GTEST_HAS_ABSL "Use Abseil and RE2. Requires Abseil and RE2 to be separately added to the build." OFF) option(GTEST_HAS_ABSL "Use Abseil and RE2. Requires Abseil and RE2 to be separately added to the build." OFF)
if(GTEST_HAS_ABSL)
if(NOT TARGET absl::base)
find_package(absl REQUIRED)
endif()
if(NOT TARGET re2::re2)
find_package(re2 REQUIRED)
endif()
endif()
if(BUILD_GMOCK) if(BUILD_GMOCK)
add_subdirectory( googlemock ) add_subdirectory( googlemock )
else() else()

View File

@ -47,11 +47,11 @@ PR is acceptable as an alternative.
## The Google Test and Google Mock Communities ## The Google Test and Google Mock Communities
The Google Test community exists primarily through the The Google Test community exists primarily through the
[discussion group](http://groups.google.com/group/googletestframework) and the [discussion group](https://groups.google.com/group/googletestframework) and the
GitHub repository. Likewise, the Google Mock community exists primarily through GitHub repository. Likewise, the Google Mock community exists primarily through
their own [discussion group](http://groups.google.com/group/googlemock). You are their own [discussion group](https://groups.google.com/group/googlemock). You
definitely encouraged to contribute to the discussion and you can also help us are definitely encouraged to contribute to the discussion and you can also help
to keep the effectiveness of the group high by following and promoting the us to keep the effectiveness of the group high by following and promoting the
guidelines listed here. guidelines listed here.
### Please Be Friendly ### Please Be Friendly
@ -88,7 +88,7 @@ check your formatting.
If you plan to contribute a patch, you need to build Google Test, Google Mock, If you plan to contribute a patch, you need to build Google Test, Google Mock,
and their own tests from a git checkout, which has further requirements: and their own tests from a git checkout, which has further requirements:
* [Python](https://www.python.org/) v2.3 or newer (for running some of the * [Python](https://www.python.org/) v3.6 or newer (for running some of the
tests and re-generating certain source files from templates) tests and re-generating certain source files from templates)
* [CMake](https://cmake.org/) v2.8.12 or newer * [CMake](https://cmake.org/) v2.8.12 or newer
@ -102,30 +102,40 @@ To make sure your changes work as intended and don't break existing
functionality, you'll want to compile and run Google Test and GoogleMock's own functionality, you'll want to compile and run Google Test and GoogleMock's own
tests. For that you can use CMake: tests. For that you can use CMake:
mkdir mybuild ```
cd mybuild mkdir mybuild
cmake -Dgtest_build_tests=ON -Dgmock_build_tests=ON ${GTEST_REPO_DIR} cd mybuild
cmake -Dgtest_build_tests=ON -Dgmock_build_tests=ON ${GTEST_REPO_DIR}
```
To choose between building only Google Test or Google Mock, you may modify your To choose between building only Google Test or Google Mock, you may modify your
cmake command to be one of each cmake command to be one of each
cmake -Dgtest_build_tests=ON ${GTEST_DIR} # sets up Google Test tests ```
cmake -Dgmock_build_tests=ON ${GMOCK_DIR} # sets up Google Mock tests cmake -Dgtest_build_tests=ON ${GTEST_DIR} # sets up Google Test tests
cmake -Dgmock_build_tests=ON ${GMOCK_DIR} # sets up Google Mock tests
```
Make sure you have Python installed, as some of Google Test's tests are written Make sure you have Python installed, as some of Google Test's tests are written
in Python. If the cmake command complains about not being able to find Python in Python. If the cmake command complains about not being able to find Python
(`Could NOT find PythonInterp (missing: PYTHON_EXECUTABLE)`), try telling it (`Could NOT find PythonInterp (missing: PYTHON_EXECUTABLE)`), try telling it
explicitly where your Python executable can be found: explicitly where your Python executable can be found:
cmake -DPYTHON_EXECUTABLE=path/to/python ... ```
cmake -DPYTHON_EXECUTABLE=path/to/python ...
```
Next, you can build Google Test and / or Google Mock and all desired tests. On Next, you can build Google Test and / or Google Mock and all desired tests. On
\*nix, this is usually done by \*nix, this is usually done by
make ```
make
```
To run the tests, do To run the tests, do
make test ```
make test
```
All tests should pass. All tests should pass.

View File

@ -55,6 +55,7 @@ Russ Cox <rsc@google.com>
Russ Rufer <russ@pentad.com> Russ Rufer <russ@pentad.com>
Sean Mcafee <eefacm@gmail.com> Sean Mcafee <eefacm@gmail.com>
Sigurður Ásgeirsson <siggi@google.com> Sigurður Ásgeirsson <siggi@google.com>
Soyeon Kim <sxshx818@naver.com>
Sverre Sundsdal <sundsdal@gmail.com> Sverre Sundsdal <sundsdal@gmail.com>
Szymon Sobik <sobik.szymon@gmail.com> Szymon Sobik <sobik.szymon@gmail.com>
Takeshi Yoshino <tyoshino@google.com> Takeshi Yoshino <tyoshino@google.com>

76
MODULE.bazel Normal file
View File

@ -0,0 +1,76 @@
# Copyright 2024 Google Inc.
# All Rights Reserved.
#
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
# * Neither the name of Google Inc. nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# https://bazel.build/external/overview#bzlmod
module(
name = "googletest",
version = "head",
compatibility_level = 1,
)
# Only direct dependencies need to be listed below.
# Please keep the versions in sync with the versions in the WORKSPACE file.
bazel_dep(
name = "abseil-cpp",
version = "20250512.0",
)
bazel_dep(
name = "platforms",
version = "0.0.11",
)
bazel_dep(
name = "re2",
version = "2024-07-02.bcr.1",
)
bazel_dep(
name = "rules_python",
version = "1.3.0",
dev_dependency = True,
)
# https://rules-python.readthedocs.io/en/stable/toolchains.html#library-modules-with-dev-only-python-usage
python = use_extension(
"@rules_python//python/extensions:python.bzl",
"python",
dev_dependency = True,
)
python.toolchain(
ignore_root_user_error = True,
is_default = True,
python_version = "3.12",
)
# See fake_fuchsia_sdk.bzl for instructions on how to override this with a real SDK, if needed.
fuchsia_sdk = use_extension("//:fake_fuchsia_sdk.bzl", "fuchsia_sdk")
fuchsia_sdk.create_fake()
use_repo(fuchsia_sdk, "fuchsia_sdk")

View File

@ -2,32 +2,28 @@
### Announcements ### Announcements
#### Live at Head
GoogleTest now follows the
[Abseil Live at Head philosophy](https://abseil.io/about/philosophy#upgrade-support).
We recommend
[updating to the latest commit in the `main` branch as often as possible](https://github.com/abseil/abseil-cpp/blob/master/FAQ.md#what-is-live-at-head-and-how-do-i-do-it).
#### Documentation Updates #### Documentation Updates
Our documentation is now live on GitHub Pages at Our documentation is now live on GitHub Pages at
https://google.github.io/googletest/. We recommend browsing the documentation on https://google.github.io/googletest/. We recommend browsing the documentation on
GitHub Pages rather than directly in the repository. GitHub Pages rather than directly in the repository.
#### Release 1.12.1 #### Release 1.17.0
[Release 1.12.1](https://github.com/google/googletest/releases/tag/release-1.12.1) [Release 1.17.0](https://github.com/google/googletest/releases/tag/v1.17.0) is
is now available. now available.
The 1.12.x branch will be the last to support C++11. Future releases will The 1.17.x branch
require at least C++14. [requires at least C++17](https://opensource.google/documentation/policies/cplusplus-support#c_language_standard).
#### Continuous Integration
We use Google's internal systems for continuous integration.
#### Coming Soon #### Coming Soon
* We are planning to take a dependency on * We are planning to take a dependency on
[Abseil](https://github.com/abseil/abseil-cpp). [Abseil](https://github.com/abseil/abseil-cpp).
* More documentation improvements are planned.
## Welcome to **GoogleTest**, Google's C++ test framework! ## Welcome to **GoogleTest**, Google's C++ test framework!
@ -46,16 +42,37 @@ More information about building GoogleTest can be found at
## Features ## Features
* An [xUnit](https://en.wikipedia.org/wiki/XUnit) test framework. * xUnit test framework: \
* Test discovery. Googletest is based on the [xUnit](https://en.wikipedia.org/wiki/XUnit)
* A rich set of assertions. testing framework, a popular architecture for unit testing
* User-defined assertions. * Test discovery: \
* Death tests. Googletest automatically discovers and runs your tests, eliminating the need
* Fatal and non-fatal failures. to manually register your tests
* Value-parameterized tests. * Rich set of assertions: \
* Type-parameterized tests. Googletest provides a variety of assertions, such as equality, inequality,
* Various options for running the tests. exceptions, and more, making it easy to test your code
* XML test report generation. * User-defined assertions: \
You can define your own assertions with Googletest, making it simple to
write tests that are specific to your code
* Death tests: \
Googletest supports death tests, which verify that your code exits in a
certain way, making it useful for testing error-handling code
* Fatal and non-fatal failures: \
You can specify whether a test failure should be treated as fatal or
non-fatal with Googletest, allowing tests to continue running even if a
failure occurs
* Value-parameterized tests: \
Googletest supports value-parameterized tests, which run multiple times with
different input values, making it useful for testing functions that take
different inputs
* Type-parameterized tests: \
Googletest also supports type-parameterized tests, which run with different
data types, making it useful for testing functions that work with different
data types
* Various options for running tests: \
Googletest provides many options for running tests including running
individual tests, running tests in a specific order and running tests in
parallel
## Supported Platforms ## Supported Platforms
@ -63,7 +80,7 @@ GoogleTest follows Google's
[Foundational C++ Support Policy](https://opensource.google/documentation/policies/cplusplus-support). [Foundational C++ Support Policy](https://opensource.google/documentation/policies/cplusplus-support).
See See
[this table](https://github.com/google/oss-policies-info/blob/main/foundational-cxx-support-matrix.md) [this table](https://github.com/google/oss-policies-info/blob/main/foundational-cxx-support-matrix.md)
for a list of currently supported versions compilers, platforms, and build for a list of currently supported versions of compilers, platforms, and build
tools. tools.
## Who Is Using GoogleTest? ## Who Is Using GoogleTest?
@ -71,12 +88,12 @@ tools.
In addition to many internal projects at Google, GoogleTest is also used by the In addition to many internal projects at Google, GoogleTest is also used by the
following notable projects: following notable projects:
* The [Chromium projects](http://www.chromium.org/) (behind the Chrome browser * The [Chromium projects](https://www.chromium.org/) (behind the Chrome
and Chrome OS). browser and Chrome OS).
* The [LLVM](http://llvm.org/) compiler. * The [LLVM](https://llvm.org/) compiler.
* [Protocol Buffers](https://github.com/google/protobuf), Google's data * [Protocol Buffers](https://github.com/google/protobuf), Google's data
interchange format. interchange format.
* The [OpenCV](http://opencv.org/) computer vision library. * The [OpenCV](https://opencv.org/) computer vision library.
## Related Open Source Projects ## Related Open Source Projects

View File

@ -1,40 +1,61 @@
workspace(name = "com_google_googletest") # Copyright 2024 Google Inc.
# All Rights Reserved.
#
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
# * Neither the name of Google Inc. nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
workspace(name = "googletest")
load("//:googletest_deps.bzl", "googletest_deps")
googletest_deps()
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive( http_archive(
name = "com_google_absl", # 2023-01-10T21:08:25Z name = "rules_python",
sha256 = "f9a4e749f42c386a32a90fddf0e2913ed408d10c42f7f33ccf4c59ac4f0d1d05", sha256 = "2cc26bbd53854ceb76dd42a834b1002cd4ba7f8df35440cf03482e045affc244",
strip_prefix = "abseil-cpp-52835439ca90d86b27bf8cd1708296e95604d724", strip_prefix = "rules_python-1.3.0",
urls = ["https://github.com/abseil/abseil-cpp/archive/52835439ca90d86b27bf8cd1708296e95604d724.zip"], url = "https://github.com/bazelbuild/rules_python/releases/download/1.3.0/rules_python-1.3.0.tar.gz",
) )
# https://github.com/bazelbuild/rules_python/releases/tag/1.1.0
load("@rules_python//python:repositories.bzl", "py_repositories")
py_repositories()
# Note this must use a commit from the `abseil` branch of the RE2 project.
# https://github.com/google/re2/tree/abseil
http_archive( http_archive(
name = "com_googlesource_code_re2", # 2022-12-21T14:29:10Z name = "bazel_skylib",
sha256 = "b9ce3a51beebb38534d11d40f8928d40509b9e18a735f6a4a97ad3d014c87cb5", sha256 = "cd55a062e763b9349921f0f5db8c3933288dc8ba4f76dd9416aac68acee3cb94",
strip_prefix = "re2-d0b1f8f2ecc2ea74956c7608b6f915175314ff0e", urls = ["https://github.com/bazelbuild/bazel-skylib/releases/download/1.5.0/bazel-skylib-1.5.0.tar.gz"],
urls = ["https://github.com/google/re2/archive/d0b1f8f2ecc2ea74956c7608b6f915175314ff0e.zip"],
) )
http_archive( http_archive(
name = "rules_python", # 2023-01-10T22:00:51Z name = "platforms",
sha256 = "5de54486a60ad8948dabe49605bb1c08053e04001a431ab3e96745b4d97a4419", urls = [
strip_prefix = "rules_python-70cce26432187a60b4e950118791385e6fb3c26f", "https://mirror.bazel.build/github.com/bazelbuild/platforms/releases/download/0.0.11/platforms-0.0.11.tar.gz",
urls = ["https://github.com/bazelbuild/rules_python/archive/70cce26432187a60b4e950118791385e6fb3c26f.zip"], "https://github.com/bazelbuild/platforms/releases/download/0.0.11/platforms-0.0.11.tar.gz",
) ],
sha256 = "29742e87275809b5e598dc2f04d86960cc7a55b3067d97221c9abbc9926bff0f",
http_archive(
name = "bazel_skylib", # 2022-11-16T18:29:32Z
sha256 = "a22290c26d29d3ecca286466f7f295ac6cbe32c0a9da3a91176a90e0725e3649",
strip_prefix = "bazel-skylib-5bfcb1a684550626ce138fe0fe8f5f702b3764c3",
urls = ["https://github.com/bazelbuild/bazel-skylib/archive/5bfcb1a684550626ce138fe0fe8f5f702b3764c3.zip"],
)
http_archive(
name = "platforms", # 2022-11-09T19:18:22Z
sha256 = "b4a3b45dc4202e2b3e34e3bc49d2b5b37295fc23ea58d88fb9e01f3642ad9b55",
strip_prefix = "platforms-3fbc687756043fb58a407c2ea8c944bc2fe1d922",
urls = ["https://github.com/bazelbuild/platforms/archive/3fbc687756043fb58a407c2ea8c944bc2fe1d922.zip"],
) )

35
WORKSPACE.bzlmod Normal file
View File

@ -0,0 +1,35 @@
# Copyright 2024 Google Inc.
# All Rights Reserved.
#
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
# * Neither the name of Google Inc. nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# https://bazel.build/external/migration#workspace.bzlmod
#
# This file is intentionally empty. When bzlmod is enabled and this
# file exists, the content of WORKSPACE is ignored. This prevents
# bzlmod builds from unintentionally depending on the WORKSPACE file.

View File

@ -31,31 +31,60 @@
set -euox pipefail set -euox pipefail
readonly LINUX_LATEST_CONTAINER="gcr.io/google.com/absl-177019/linux_hybrid-latest:20220217" readonly LINUX_LATEST_CONTAINER="gcr.io/google.com/absl-177019/linux_hybrid-latest:20250430"
readonly LINUX_GCC_FLOOR_CONTAINER="gcr.io/google.com/absl-177019/linux_gcc-floor:20220621" readonly LINUX_GCC_FLOOR_CONTAINER="gcr.io/google.com/absl-177019/linux_gcc-floor:20250430"
if [[ -z ${GTEST_ROOT:-} ]]; then if [[ -z ${GTEST_ROOT:-} ]]; then
GTEST_ROOT="$(realpath $(dirname ${0})/..)" GTEST_ROOT="$(realpath $(dirname ${0})/..)"
fi fi
if [[ -z ${STD:-} ]]; then # Use Bazel Vendor mode to reduce reliance on external dependencies.
STD="c++14 c++17 c++20" # See https://bazel.build/external/vendor and the Dockerfile for
# an explaination of how this works.
if [[ ${KOKORO_GFILE_DIR:-} ]] && [[ -f "${KOKORO_GFILE_DIR}/distdir/googletest_vendor.tar.gz" ]]; then
DOCKER_EXTRA_ARGS="--mount type=bind,source=${KOKORO_GFILE_DIR}/distdir,target=/distdir,readonly --env=BAZEL_VENDOR_ARCHIVE=/distdir/googletest_vendor.tar.gz ${DOCKER_EXTRA_ARGS:-}"
BAZEL_EXTRA_ARGS="--vendor_dir=/googletest_vendor ${BAZEL_EXTRA_ARGS:-}"
fi fi
# Test the CMake build if [[ -z ${STD:-} ]]; then
for cc in /usr/local/bin/gcc /opt/llvm/clang/bin/clang; do STD="c++17 c++20 c++23"
for cmake_off_on in OFF ON; do fi
# Test CMake + GCC
for cmake_off_on in OFF ON; do
time docker run \ time docker run \
--volume="${GTEST_ROOT}:/src:ro" \ --volume="${GTEST_ROOT}:/src:ro" \
--tmpfs="/build:exec" \ --tmpfs="/build:exec" \
--workdir="/build" \ --workdir="/build" \
--rm \ --rm \
--env="CC=${cc}" \ --env="CC=/usr/local/bin/gcc" \
--env="CXX_FLAGS=\"-Werror -Wdeprecated\"" \ --env=CXXFLAGS="-Werror -Wdeprecated" \
${LINUX_LATEST_CONTAINER} \ ${LINUX_LATEST_CONTAINER} \
/bin/bash -c " /bin/bash -c "
cmake /src \ cmake /src \
-DCMAKE_CXX_STANDARD=14 \ -DCMAKE_CXX_STANDARD=17 \
-Dgtest_build_samples=ON \
-Dgtest_build_tests=ON \
-Dgmock_build_tests=ON \
-Dcxx_no_exception=${cmake_off_on} \
-Dcxx_no_rtti=${cmake_off_on} && \
make -j$(nproc) && \
ctest -j$(nproc) --output-on-failure"
done
# Test CMake + Clang
for cmake_off_on in OFF ON; do
time docker run \
--volume="${GTEST_ROOT}:/src:ro" \
--tmpfs="/build:exec" \
--workdir="/build" \
--rm \
--env="CC=/opt/llvm/clang/bin/clang" \
--env=CXXFLAGS="-Werror -Wdeprecated --gcc-toolchain=/usr/local" \
${LINUX_LATEST_CONTAINER} \
/bin/bash -c "
cmake /src \
-DCMAKE_CXX_STANDARD=17 \
-Dgtest_build_samples=ON \ -Dgtest_build_samples=ON \
-Dgtest_build_tests=ON \ -Dgtest_build_tests=ON \
-Dgmock_build_tests=ON \ -Dgmock_build_tests=ON \
@ -63,7 +92,6 @@ for cc in /usr/local/bin/gcc /opt/llvm/clang/bin/clang; do
-Dcxx_no_rtti=${cmake_off_on} && \ -Dcxx_no_rtti=${cmake_off_on} && \
make -j$(nproc) && \ make -j$(nproc) && \
ctest -j$(nproc) --output-on-failure" ctest -j$(nproc) --output-on-failure"
done
done done
# Do one test with an older version of GCC # Do one test with an older version of GCC
@ -72,18 +100,22 @@ time docker run \
--workdir="/src" \ --workdir="/src" \
--rm \ --rm \
--env="CC=/usr/local/bin/gcc" \ --env="CC=/usr/local/bin/gcc" \
--env="BAZEL_CXXOPTS=-std=c++14" \ --env="BAZEL_CXXOPTS=-std=c++17" \
${DOCKER_EXTRA_ARGS:-} \
${LINUX_GCC_FLOOR_CONTAINER} \ ${LINUX_GCC_FLOOR_CONTAINER} \
/bin/bash --login -c "
/usr/local/bin/bazel test ... \ /usr/local/bin/bazel test ... \
--copt="-Wall" \ --copt=\"-Wall\" \
--copt="-Werror" \ --copt=\"-Werror\" \
--copt="-Wuninitialized" \ --copt=\"-Wuninitialized\" \
--copt="-Wno-error=pragmas" \ --copt=\"-Wundef\" \
--distdir="/bazel-distdir" \ --copt=\"-Wno-error=pragmas\" \
--enable_bzlmod=false \
--features=external_include_paths \ --features=external_include_paths \
--keep_going \ --keep_going \
--show_timestamps \ --show_timestamps \
--test_output=errors --test_output=errors \
${BAZEL_EXTRA_ARGS:-}"
# Test GCC # Test GCC
for std in ${STD}; do for std in ${STD}; do
@ -94,17 +126,21 @@ for std in ${STD}; do
--rm \ --rm \
--env="CC=/usr/local/bin/gcc" \ --env="CC=/usr/local/bin/gcc" \
--env="BAZEL_CXXOPTS=-std=${std}" \ --env="BAZEL_CXXOPTS=-std=${std}" \
${DOCKER_EXTRA_ARGS:-} \
${LINUX_LATEST_CONTAINER} \ ${LINUX_LATEST_CONTAINER} \
/bin/bash --login -c "
/usr/local/bin/bazel test ... \ /usr/local/bin/bazel test ... \
--copt="-Wall" \ --copt=\"-Wall\" \
--copt="-Werror" \ --copt=\"-Werror\" \
--copt="-Wuninitialized" \ --copt=\"-Wuninitialized\" \
--define="absl=${absl}" \ --copt=\"-Wundef\" \
--distdir="/bazel-distdir" \ --define=\"absl=${absl}\" \
--enable_bzlmod=true \
--features=external_include_paths \ --features=external_include_paths \
--keep_going \ --keep_going \
--show_timestamps \ --show_timestamps \
--test_output=errors --test_output=errors \
${BAZEL_EXTRA_ARGS:-}"
done done
done done
@ -117,18 +153,22 @@ for std in ${STD}; do
--rm \ --rm \
--env="CC=/opt/llvm/clang/bin/clang" \ --env="CC=/opt/llvm/clang/bin/clang" \
--env="BAZEL_CXXOPTS=-std=${std}" \ --env="BAZEL_CXXOPTS=-std=${std}" \
${DOCKER_EXTRA_ARGS:-} \
${LINUX_LATEST_CONTAINER} \ ${LINUX_LATEST_CONTAINER} \
/bin/bash --login -c "
/usr/local/bin/bazel test ... \ /usr/local/bin/bazel test ... \
--copt="--gcc-toolchain=/usr/local" \ --copt=\"--gcc-toolchain=/usr/local\" \
--copt="-Wall" \ --copt=\"-Wall\" \
--copt="-Werror" \ --copt=\"-Werror\" \
--copt="-Wuninitialized" \ --copt=\"-Wuninitialized\" \
--define="absl=${absl}" \ --copt=\"-Wundef\" \
--distdir="/bazel-distdir" \ --define=\"absl=${absl}\" \
--enable_bzlmod=true \
--features=external_include_paths \ --features=external_include_paths \
--keep_going \ --keep_going \
--linkopt="--gcc-toolchain=/usr/local" \ --linkopt=\"--gcc-toolchain=/usr/local\" \
--show_timestamps \ --show_timestamps \
--test_output=errors --test_output=errors \
${BAZEL_EXTRA_ARGS:-}"
done done
done done

View File

@ -31,6 +31,9 @@
set -euox pipefail set -euox pipefail
# Use Xcode 16.0
sudo xcode-select -s /Applications/Xcode_16.0.app/Contents/Developer
if [[ -z ${GTEST_ROOT:-} ]]; then if [[ -z ${GTEST_ROOT:-} ]]; then
GTEST_ROOT="$(realpath $(dirname ${0})/..)" GTEST_ROOT="$(realpath $(dirname ${0})/..)"
fi fi
@ -40,20 +43,20 @@ for cmake_off_on in OFF ON; do
BUILD_DIR=$(mktemp -d build_dir.XXXXXXXX) BUILD_DIR=$(mktemp -d build_dir.XXXXXXXX)
cd ${BUILD_DIR} cd ${BUILD_DIR}
time cmake ${GTEST_ROOT} \ time cmake ${GTEST_ROOT} \
-DCMAKE_CXX_STANDARD=14 \ -DCMAKE_CXX_STANDARD=17 \
-Dgtest_build_samples=ON \ -Dgtest_build_samples=ON \
-Dgtest_build_tests=ON \ -Dgtest_build_tests=ON \
-Dgmock_build_tests=ON \ -Dgmock_build_tests=ON \
-Dcxx_no_exception=${cmake_off_on} \ -Dcxx_no_exception=${cmake_off_on} \
-Dcxx_no_rtti=${cmake_off_on} -Dcxx_no_rtti=${cmake_off_on}
time make time make -j$(nproc)
time ctest -j$(nproc) --output-on-failure time ctest -j$(nproc) --output-on-failure
done done
# Test the Bazel build # Test the Bazel build
# If we are running on Kokoro, check for a versioned Bazel binary. # If we are running on Kokoro, check for a versioned Bazel binary.
KOKORO_GFILE_BAZEL_BIN="bazel-5.1.1-darwin-x86_64" KOKORO_GFILE_BAZEL_BIN="bazel-8.2.1-darwin-x86_64"
if [[ ${KOKORO_GFILE_DIR:-} ]] && [[ -f ${KOKORO_GFILE_DIR}/${KOKORO_GFILE_BAZEL_BIN} ]]; then if [[ ${KOKORO_GFILE_DIR:-} ]] && [[ -f ${KOKORO_GFILE_DIR}/${KOKORO_GFILE_BAZEL_BIN} ]]; then
BAZEL_BIN="${KOKORO_GFILE_DIR}/${KOKORO_GFILE_BAZEL_BIN}" BAZEL_BIN="${KOKORO_GFILE_DIR}/${KOKORO_GFILE_BAZEL_BIN}"
chmod +x ${BAZEL_BIN} chmod +x ${BAZEL_BIN}
@ -61,15 +64,24 @@ else
BAZEL_BIN="bazel" BAZEL_BIN="bazel"
fi fi
# Use Bazel Vendor mode to reduce reliance on external dependencies.
if [[ ${KOKORO_GFILE_DIR:-} ]] && [[ -f "${KOKORO_GFILE_DIR}/distdir/googletest_vendor.tar.gz" ]]; then
tar -xf "${KOKORO_GFILE_DIR}/distdir/googletest_vendor.tar.gz" -C "${TMP}/"
BAZEL_EXTRA_ARGS="--vendor_dir=\"${TMP}/googletest_vendor\" ${BAZEL_EXTRA_ARGS:-}"
fi
cd ${GTEST_ROOT} cd ${GTEST_ROOT}
for absl in 0 1; do for absl in 0 1; do
${BAZEL_BIN} test ... \ ${BAZEL_BIN} test ... \
--copt="-Wall" \ --copt="-Wall" \
--copt="-Werror" \ --copt="-Werror" \
--cxxopt="-std=c++14" \ --copt="-Wundef" \
--cxxopt="-std=c++17" \
--define="absl=${absl}" \ --define="absl=${absl}" \
--enable_bzlmod=true \
--features=external_include_paths \ --features=external_include_paths \
--keep_going \ --keep_going \
--show_timestamps \ --show_timestamps \
--test_output=errors --test_output=errors \
${BAZEL_EXTRA_ARGS:-}
done done

View File

@ -1,29 +1,28 @@
SETLOCAL ENABLEDELAYEDEXPANSION SETLOCAL ENABLEDELAYEDEXPANSION
SET BAZEL_EXE=%KOKORO_GFILE_DIR%\bazel-5.1.1-windows-x86_64.exe SET BAZEL_EXE=%KOKORO_GFILE_DIR%\bazel-8.2.1-windows-x86_64.exe
SET PATH=C:\Python37;%PATH% SET PATH=C:\Python34;%PATH%
SET BAZEL_PYTHON=C:\python37\python.exe SET BAZEL_PYTHON=C:\python34\python.exe
SET BAZEL_SH=C:\tools\msys64\usr\bin\bash.exe SET BAZEL_SH=C:\tools\msys64\usr\bin\bash.exe
SET CMAKE_BIN="C:\Program Files\CMake\bin\cmake.exe" SET CMAKE_BIN="cmake.exe"
SET CTEST_BIN="C:\Program Files\CMake\bin\ctest.exe" SET CTEST_BIN="ctest.exe"
SET CTEST_OUTPUT_ON_FAILURE=1 SET CTEST_OUTPUT_ON_FAILURE=1
SET CMAKE_BUILD_PARALLEL_LEVEL=16
SET CTEST_PARALLEL_LEVEL=16
IF EXIST git\googletest ( SET GTEST_ROOT=%~dp0\..
CD git\googletest
) ELSE IF EXIST github\googletest (
CD github\googletest
)
IF %errorlevel% neq 0 EXIT /B 1 IF %errorlevel% neq 0 EXIT /B 1
:: ---------------------------------------------------------------------------- :: ----------------------------------------------------------------------------
:: CMake Visual Studio 15 2017 Win64 :: CMake
MKDIR cmake_msvc2017 SET CMAKE_BUILD_PATH=cmake_msvc2022
CD cmake_msvc2017 MKDIR %CMAKE_BUILD_PATH%
CD %CMAKE_BUILD_PATH%
%CMAKE_BIN% .. ^ %CMAKE_BIN% %GTEST_ROOT% ^
-G "Visual Studio 15 2017 Win64" ^ -G "Visual Studio 17 2022" ^
-DCMAKE_CXX_STANDARD=17 ^
-DPYTHON_EXECUTABLE:FILEPATH=c:\python37\python.exe ^ -DPYTHON_EXECUTABLE:FILEPATH=c:\python37\python.exe ^
-DPYTHON_INCLUDE_DIR:PATH=c:\python37\include ^ -DPYTHON_INCLUDE_DIR:PATH=c:\python37\include ^
-DPYTHON_LIBRARY:FILEPATH=c:\python37\lib\site-packages\pip ^ -DPYTHON_LIBRARY:FILEPATH=c:\python37\lib\site-packages\pip ^
@ -38,19 +37,49 @@ IF %errorlevel% neq 0 EXIT /B 1
%CTEST_BIN% -C Debug --timeout 600 %CTEST_BIN% -C Debug --timeout 600
IF %errorlevel% neq 0 EXIT /B 1 IF %errorlevel% neq 0 EXIT /B 1
CD .. CD %GTEST_ROOT%
RMDIR /S /Q cmake_msvc2017 RMDIR /S /Q %CMAKE_BUILD_PATH%
:: ---------------------------------------------------------------------------- :: ----------------------------------------------------------------------------
:: Bazel Visual Studio 15 2017 Win64 :: Bazel
SET BAZEL_VC=C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\VC :: The default home directory on Kokoro is a long path which causes errors
%BAZEL_EXE% test ... ^ :: because of Windows limitations on path length.
:: --output_user_root=C:\tmp causes Bazel to use a shorter path.
SET BAZEL_VS=C:\Program Files\Microsoft Visual Studio\2022\Community
:: Use Bazel Vendor mode to reduce reliance on external dependencies.
IF EXIST "%KOKORO_GFILE_DIR%\distdir\googletest_vendor.tar.gz" (
tar --force-local -xf "%KOKORO_GFILE_DIR%\distdir\googletest_vendor.tar.gz" -C c:
SET VENDOR_FLAG=--vendor_dir=c:\googletest_vendor
) ELSE (
SET VENDOR_FLAG=
)
:: C++17
%BAZEL_EXE% ^
--output_user_root=C:\tmp ^
test ... ^
--compilation_mode=dbg ^ --compilation_mode=dbg ^
--copt=/std:c++14 ^ --copt=/std:c++17 ^
--copt=/WX ^ --copt=/WX ^
--features=external_include_paths ^ --enable_bzlmod=true ^
--keep_going ^ --keep_going ^
--test_output=errors ^ --test_output=errors ^
--test_tag_filters=-no_test_msvc2017 --test_tag_filters=-no_test_msvc2017 ^
%VENDOR_FLAG%
IF %errorlevel% neq 0 EXIT /B 1
:: C++20
%BAZEL_EXE% ^
--output_user_root=C:\tmp ^
test ... ^
--compilation_mode=dbg ^
--copt=/std:c++20 ^
--copt=/WX ^
--enable_bzlmod=true ^
--keep_going ^
--test_output=errors ^
--test_tag_filters=-no_test_msvc2017 ^
%VENDOR_FLAG%
IF %errorlevel% neq 0 EXIT /B 1 IF %errorlevel% neq 0 EXIT /B 1

View File

@ -7,15 +7,15 @@
{% seo %} {% seo %}
<link rel="stylesheet" href="{{ "/assets/css/style.css?v=" | append: site.github.build_revision | relative_url }}"> <link rel="stylesheet" href="{{ "/assets/css/style.css?v=" | append: site.github.build_revision | relative_url }}">
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-9PTP6FW1M5"></script>
<script> <script>
window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date; window.dataLayer = window.dataLayer || [];
ga('create', 'UA-197576187-1', { 'storage': 'none' }); function gtag(){dataLayer.push(arguments);}
ga('set', 'referrer', document.referrer.split('?')[0]); gtag('js', new Date());
ga('set', 'location', window.location.href.split('?')[0]);
ga('set', 'anonymizeIp', true); gtag('config', 'G-9PTP6FW1M5');
ga('send', 'pageview');
</script> </script>
<script async src='https://www.google-analytics.com/analytics.js'></script>
</head> </head>
<body> <body>
<div class="sidebar"> <div class="sidebar">

View File

@ -1,9 +1,9 @@
# Advanced googletest Topics # Advanced GoogleTest Topics
## Introduction ## Introduction
Now that you have read the [googletest Primer](primer.md) and learned how to Now that you have read the [GoogleTest Primer](primer.md) and learned how to
write tests using googletest, it's time to learn some new tricks. This document write tests using GoogleTest, it's time to learn some new tricks. This document
will show you more assertions as well as how to construct complex failure will show you more assertions as well as how to construct complex failure
messages, propagate fatal failures, reuse and speed up your test fixtures, and messages, propagate fatal failures, reuse and speed up your test fixtures, and
use various flags with your tests. use various flags with your tests.
@ -25,7 +25,7 @@ Reference.
### Predicate Assertions for Better Error Messages ### Predicate Assertions for Better Error Messages
Even though googletest has a rich set of assertions, they can never be complete, Even though GoogleTest has a rich set of assertions, they can never be complete,
as it's impossible (nor a good idea) to anticipate all scenarios a user might as it's impossible (nor a good idea) to anticipate all scenarios a user might
run into. Therefore, sometimes a user has to use `EXPECT_TRUE()` to check a run into. Therefore, sometimes a user has to use `EXPECT_TRUE()` to check a
complex expression, for lack of a better macro. This has the problem of not complex expression, for lack of a better macro. This has the problem of not
@ -35,7 +35,7 @@ failure message by themselves, streaming it into `EXPECT_TRUE()`. However, this
is awkward especially when the expression has side-effects or is expensive to is awkward especially when the expression has side-effects or is expensive to
evaluate. evaluate.
googletest gives you three different options to solve this problem: GoogleTest gives you three different options to solve this problem:
#### Using an Existing Boolean Function #### Using an Existing Boolean Function
@ -286,7 +286,7 @@ For example:
```c++ ```c++
TEST(SkipTest, DoesSkip) { TEST(SkipTest, DoesSkip) {
GTEST_SKIP() << "Skipping single test"; GTEST_SKIP() << "Skipping single test";
EXPECT_EQ(0, 1); // Won't fail; it won't be executed FAIL(); // Won't fail; it won't be executed
} }
class SkipFixture : public ::testing::Test { class SkipFixture : public ::testing::Test {
@ -298,15 +298,15 @@ class SkipFixture : public ::testing::Test {
// Tests for SkipFixture won't be executed. // Tests for SkipFixture won't be executed.
TEST_F(SkipFixture, SkipsOneTest) { TEST_F(SkipFixture, SkipsOneTest) {
EXPECT_EQ(5, 7); // Won't fail FAIL(); // Won't fail; it won't be executed
} }
``` ```
As with assertion macros, you can stream a custom message into `GTEST_SKIP()`. As with assertion macros, you can stream a custom message into `GTEST_SKIP()`.
## Teaching googletest How to Print Your Values ## Teaching GoogleTest How to Print Your Values
When a test assertion such as `EXPECT_EQ` fails, googletest prints the argument When a test assertion such as `EXPECT_EQ` fails, GoogleTest prints the argument
values to help you debug. It does this using a user-extensible value printer. values to help you debug. It does this using a user-extensible value printer.
This printer knows how to print built-in C++ types, native arrays, STL This printer knows how to print built-in C++ types, native arrays, STL
@ -315,73 +315,143 @@ prints the raw bytes in the value and hopes that you the user can figure it out.
As mentioned earlier, the printer is *extensible*. That means you can teach it As mentioned earlier, the printer is *extensible*. That means you can teach it
to do a better job at printing your particular type than to dump the bytes. To to do a better job at printing your particular type than to dump the bytes. To
do that, define `<<` for your type: do that, define an `AbslStringify()` overload as a `friend` function template
for your type:
```c++
#include <ostream>
```cpp
namespace foo { namespace foo {
class Bar { // We want googletest to be able to print instances of this. class Point { // We want GoogleTest to be able to print instances of this.
... ...
// Create a free inline friend function. // Provide a friend overload.
friend std::ostream& operator<<(std::ostream& os, const Bar& bar) { template <typename Sink>
return os << bar.DebugString(); // whatever needed to print bar to os friend void AbslStringify(Sink& sink, const Point& point) {
absl::Format(&sink, "(%d, %d)", point.x, point.y);
} }
int x;
int y;
}; };
// If you can't declare the function in the class it's important that the // If you can't declare the function in the class it's important that the
// << operator is defined in the SAME namespace that defines Bar. C++'s look-up // AbslStringify overload is defined in the SAME namespace that defines Point.
// rules rely on that. // C++'s look-up rules rely on that.
std::ostream& operator<<(std::ostream& os, const Bar& bar) { enum class EnumWithStringify { kMany = 0, kChoices = 1 };
return os << bar.DebugString(); // whatever needed to print bar to os
template <typename Sink>
void AbslStringify(Sink& sink, EnumWithStringify e) {
absl::Format(&sink, "%s", e == EnumWithStringify::kMany ? "Many" : "Choices");
} }
} // namespace foo } // namespace foo
``` ```
Sometimes, this might not be an option: your team may consider it bad style to {: .callout .note}
have a `<<` operator for `Bar`, or `Bar` may already have a `<<` operator that Note: `AbslStringify()` utilizes a generic "sink" buffer to construct its
doesn't do what you want (and you cannot change it). If so, you can instead string. For more information about supported operations on `AbslStringify()`'s
define a `PrintTo()` function like this: sink, see
[the `AbslStringify()` documentation](https://abseil.io/docs/cpp/guides/abslstringify).
`AbslStringify()` can also use `absl::StrFormat`'s catch-all `%v` type specifier
within its own format strings to perform type deduction. `Point` above could be
formatted as `"(%v, %v)"` for example, and deduce the `int` values as `%d`.
Sometimes, `AbslStringify()` might not be an option: your team may wish to print
types with extra debugging information for testing purposes only. If so, you can
instead define a `PrintTo()` function like this:
```c++ ```c++
#include <ostream> #include <ostream>
namespace foo { namespace foo {
class Bar { class Point {
... ...
friend void PrintTo(const Bar& bar, std::ostream* os) { friend void PrintTo(const Point& point, std::ostream* os) {
*os << bar.DebugString(); // whatever needed to print bar to os *os << "(" << point.x << "," << point.y << ")";
} }
int x;
int y;
}; };
// If you can't declare the function in the class it's important that PrintTo() // If you can't declare the function in the class it's important that PrintTo()
// is defined in the SAME namespace that defines Bar. C++'s look-up rules rely // is defined in the SAME namespace that defines Point. C++'s look-up rules
// on that. // rely on that.
void PrintTo(const Bar& bar, std::ostream* os) { void PrintTo(const Point& point, std::ostream* os) {
*os << bar.DebugString(); // whatever needed to print bar to os *os << "(" << point.x << "," << point.y << ")";
} }
} // namespace foo } // namespace foo
``` ```
If you have defined both `<<` and `PrintTo()`, the latter will be used when If you have defined both `AbslStringify()` and `PrintTo()`, the latter will be
googletest is concerned. This allows you to customize how the value appears in used by GoogleTest. This allows you to customize how the value appears in
googletest's output without affecting code that relies on the behavior of its GoogleTest's output without affecting code that relies on the behavior of
`<<` operator. `AbslStringify()`.
If you want to print a value `x` using googletest's value printer yourself, just If you have an existing `<<` operator and would like to define an
`AbslStringify()`, the latter will be used for GoogleTest printing.
If you want to print a value `x` using GoogleTest's value printer yourself, just
call `::testing::PrintToString(x)`, which returns an `std::string`: call `::testing::PrintToString(x)`, which returns an `std::string`:
```c++ ```c++
vector<pair<Bar, int> > bar_ints = GetBarIntVector(); vector<pair<Point, int> > point_ints = GetPointIntVector();
EXPECT_TRUE(IsCorrectBarIntVector(bar_ints)) EXPECT_TRUE(IsCorrectPointIntVector(point_ints))
<< "bar_ints = " << testing::PrintToString(bar_ints); << "point_ints = " << testing::PrintToString(point_ints);
``` ```
For more details regarding `AbslStringify()` and its integration with other
libraries, see
[the documentation](https://abseil.io/docs/cpp/guides/abslstringify).
## Regular Expression Syntax
When built with Bazel and using Abseil, GoogleTest uses the
[RE2](https://github.com/google/re2/wiki/Syntax) syntax. Otherwise, for POSIX
systems (Linux, Cygwin, Mac), GoogleTest uses the
[POSIX extended regular expression](https://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap09.html#tag_09_04)
syntax. To learn about POSIX syntax, you may want to read this
[Wikipedia entry](https://en.wikipedia.org/wiki/Regular_expression#POSIX_extended).
On Windows, GoogleTest uses its own simple regular expression implementation. It
lacks many features. For example, we don't support union (`"x|y"`), grouping
(`"(xy)"`), brackets (`"[xy]"`), and repetition count (`"x{5,7}"`), among
others. Below is what we do support (`A` denotes a literal character, period
(`.`), or a single `\\ ` escape sequence; `x` and `y` denote regular
expressions.):
Expression | Meaning
---------- | --------------------------------------------------------------
`c` | matches any literal character `c`
`\\d` | matches any decimal digit
`\\D` | matches any character that's not a decimal digit
`\\f` | matches `\f`
`\\n` | matches `\n`
`\\r` | matches `\r`
`\\s` | matches any ASCII whitespace, including `\n`
`\\S` | matches any character that's not a whitespace
`\\t` | matches `\t`
`\\v` | matches `\v`
`\\w` | matches any letter, `_`, or decimal digit
`\\W` | matches any character that `\\w` doesn't match
`\\c` | matches any literal character `c`, which must be a punctuation
`.` | matches any single character except `\n`
`A?` | matches 0 or 1 occurrences of `A`
`A*` | matches 0 or many occurrences of `A`
`A+` | matches 1 or many occurrences of `A`
`^` | matches the beginning of a string (not that of each line)
`$` | matches the end of a string (not that of each line)
`xy` | matches `x` followed by `y`
To help you determine which capability is available on your system, GoogleTest
defines macros to govern which regular expression it is using. The macros are:
`GTEST_USES_SIMPLE_RE=1` or `GTEST_USES_POSIX_RE=1`. If you want your death
tests to work in all cases, you can either `#if` on these macros or use the more
limited syntax only.
## Death Tests ## Death Tests
In many applications, there are assertions that can cause application failure if In many applications, there are assertions that can cause application failure if
@ -393,7 +463,7 @@ corruption, security holes, or worse. Hence it is vitally important to test that
such assertion statements work as expected. such assertion statements work as expected.
Since these precondition checks cause the processes to die, we call such tests Since these precondition checks cause the processes to die, we call such tests
_death tests_. More generally, any test that checks that a program terminates *death tests*. More generally, any test that checks that a program terminates
(except by throwing an exception) in an expected fashion is also a death test. (except by throwing an exception) in an expected fashion is also a death test.
Note that if a piece of code throws an exception, we don't consider it "death" Note that if a piece of code throws an exception, we don't consider it "death"
@ -439,6 +509,12 @@ verifies that:
exit with exit code 0, and exit with exit code 0, and
* calling `KillProcess()` kills the process with signal `SIGKILL`. * calling `KillProcess()` kills the process with signal `SIGKILL`.
{: .callout .warning}
Warning: If your death test contains mocks and is expecting a specific exit
code, then you must allow the mock objects to be leaked via `Mock::AllowLeak`.
This is because the mock leak detector will exit with its own error code if it
detects a leak.
The test function body may contain other assertions and statements as well, if The test function body may contain other assertions and statements as well, if
necessary. necessary.
@ -451,7 +527,7 @@ Note that a death test only cares about three things:
3. does the stderr output match `matcher`? 3. does the stderr output match `matcher`?
In particular, if `statement` generates an `ASSERT_*` or `EXPECT_*` failure, it In particular, if `statement` generates an `ASSERT_*` or `EXPECT_*` failure, it
will **not** cause the death test to fail, as googletest assertions don't abort will **not** cause the death test to fail, as GoogleTest assertions don't abort
the process. the process.
### Death Test Naming ### Death Test Naming
@ -480,51 +556,6 @@ TEST_F(FooDeathTest, DoesThat) {
} }
``` ```
### Regular Expression Syntax
When built with Bazel and using Abseil, googletest uses the
[RE2](https://github.com/google/re2/wiki/Syntax) syntax. Otherwise, for POSIX
systems (Linux, Cygwin, Mac), googletest uses the
[POSIX extended regular expression](http://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap09.html#tag_09_04)
syntax. To learn about POSIX syntax, you may want to read this
[Wikipedia entry](http://en.wikipedia.org/wiki/Regular_expression#POSIX_extended).
On Windows, googletest uses its own simple regular expression implementation. It
lacks many features. For example, we don't support union (`"x|y"`), grouping
(`"(xy)"`), brackets (`"[xy]"`), and repetition count (`"x{5,7}"`), among
others. Below is what we do support (`A` denotes a literal character, period
(`.`), or a single `\\ ` escape sequence; `x` and `y` denote regular
expressions.):
Expression | Meaning
---------- | --------------------------------------------------------------
`c` | matches any literal character `c`
`\\d` | matches any decimal digit
`\\D` | matches any character that's not a decimal digit
`\\f` | matches `\f`
`\\n` | matches `\n`
`\\r` | matches `\r`
`\\s` | matches any ASCII whitespace, including `\n`
`\\S` | matches any character that's not a whitespace
`\\t` | matches `\t`
`\\v` | matches `\v`
`\\w` | matches any letter, `_`, or decimal digit
`\\W` | matches any character that `\\w` doesn't match
`\\c` | matches any literal character `c`, which must be a punctuation
`.` | matches any single character except `\n`
`A?` | matches 0 or 1 occurrences of `A`
`A*` | matches 0 or many occurrences of `A`
`A+` | matches 1 or many occurrences of `A`
`^` | matches the beginning of a string (not that of each line)
`$` | matches the end of a string (not that of each line)
`xy` | matches `x` followed by `y`
To help you determine which capability is available on your system, googletest
defines macros to govern which regular expression it is using. The macros are:
`GTEST_USES_SIMPLE_RE=1` or `GTEST_USES_POSIX_RE=1`. If you want your death
tests to work in all cases, you can either `#if` on these macros or use the more
limited syntax only.
### How It Works ### How It Works
See [Death Assertions](reference/assertions.md#death) in the Assertions See [Death Assertions](reference/assertions.md#death) in the Assertions
@ -539,7 +570,7 @@ arrange that kind of environment. For example, statically-initialized modules
may start threads before main is ever reached. Once threads have been created, may start threads before main is ever reached. Once threads have been created,
it may be difficult or impossible to clean them up. it may be difficult or impossible to clean them up.
googletest has three features intended to raise awareness of threading issues. GoogleTest has three features intended to raise awareness of threading issues.
1. A warning is emitted if multiple threads are running when a death test is 1. A warning is emitted if multiple threads are running when a death test is
encountered. encountered.
@ -562,7 +593,7 @@ The automated testing framework does not set the style flag. You can choose a
particular style of death tests by setting the flag programmatically: particular style of death tests by setting the flag programmatically:
```c++ ```c++
GTEST_FLAG_SET(death_test_style, "threadsafe") GTEST_FLAG_SET(death_test_style, "threadsafe");
``` ```
You can do this in `main()` to set the style for all death tests in the binary, You can do this in `main()` to set the style for all death tests in the binary,
@ -592,7 +623,7 @@ TEST(MyDeathTest, TestTwo) {
The `statement` argument of `ASSERT_EXIT()` can be any valid C++ statement. If The `statement` argument of `ASSERT_EXIT()` can be any valid C++ statement. If
it leaves the current function via a `return` statement or by throwing an it leaves the current function via a `return` statement or by throwing an
exception, the death test is considered to have failed. Some googletest macros exception, the death test is considered to have failed. Some GoogleTest macros
may return from the current function (e.g. `ASSERT_TRUE()`), so be sure to avoid may return from the current function (e.g. `ASSERT_TRUE()`), so be sure to avoid
them in `statement`. them in `statement`.
@ -704,7 +735,7 @@ Some tips on using `SCOPED_TRACE`:
### Propagating Fatal Failures ### Propagating Fatal Failures
A common pitfall when using `ASSERT_*` and `FAIL*` is not understanding that A common pitfall when using `ASSERT_*` and `FAIL*` is not understanding that
when they fail they only abort the _current function_, not the entire test. For when they fail they only abort the *current function*, not the entire test. For
example, the following test will segfault: example, the following test will segfault:
```c++ ```c++
@ -726,7 +757,7 @@ TEST(FooTest, Bar) {
} }
``` ```
To alleviate this, googletest provides three different solutions. You could use To alleviate this, GoogleTest provides three different solutions. You could use
either exceptions, the `(ASSERT|EXPECT)_NO_FATAL_FAILURE` assertions or the either exceptions, the `(ASSERT|EXPECT)_NO_FATAL_FAILURE` assertions or the
`HasFatalFailure()` function. They are described in the following two `HasFatalFailure()` function. They are described in the following two
subsections. subsections.
@ -760,7 +791,7 @@ in it, the test will continue after the subroutine returns. This may not be what
you want. you want.
Often people want fatal failures to propagate like exceptions. For that Often people want fatal failures to propagate like exceptions. For that
googletest offers the following macros: GoogleTest offers the following macros:
Fatal assertion | Nonfatal assertion | Verifies Fatal assertion | Nonfatal assertion | Verifies
------------------------------------- | ------------------------------------- | -------- ------------------------------------- | ------------------------------------- | --------
@ -852,7 +883,7 @@ will output XML like this:
> needs to be prefixed with `::testing::Test::` if used outside of the > needs to be prefixed with `::testing::Test::` if used outside of the
> `TEST` body and the test fixture class. > `TEST` body and the test fixture class.
> * *`key`* must be a valid XML attribute name, and cannot conflict with the > * *`key`* must be a valid XML attribute name, and cannot conflict with the
> ones already used by googletest (`name`, `status`, `time`, `classname`, > ones already used by GoogleTest (`name`, `status`, `time`, `classname`,
> `type_param`, and `value_param`). > `type_param`, and `value_param`).
> * Calling `RecordProperty()` outside of the lifespan of a test is allowed. > * Calling `RecordProperty()` outside of the lifespan of a test is allowed.
> If it's called outside of a test but between a test suite's > If it's called outside of a test but between a test suite's
@ -863,25 +894,25 @@ will output XML like this:
## Sharing Resources Between Tests in the Same Test Suite ## Sharing Resources Between Tests in the Same Test Suite
googletest creates a new test fixture object for each test in order to make GoogleTest creates a new test fixture object for each test in order to make
tests independent and easier to debug. However, sometimes tests use resources tests independent and easier to debug. However, sometimes tests use resources
that are expensive to set up, making the one-copy-per-test model prohibitively that are expensive to set up, making the one-copy-per-test model prohibitively
expensive. expensive.
If the tests don't change the resource, there's no harm in their sharing a If the tests don't change the resource, there's no harm in their sharing a
single resource copy. So, in addition to per-test set-up/tear-down, googletest single resource copy. So, in addition to per-test set-up/tear-down, GoogleTest
also supports per-test-suite set-up/tear-down. To use it: also supports per-test-suite set-up/tear-down. To use it:
1. In your test fixture class (say `FooTest` ), declare as `static` some member 1. In your test fixture class (say `FooTest` ), declare as `static` some member
variables to hold the shared resources. variables to hold the shared resources.
2. Outside your test fixture class (typically just below it), define those 2. Outside your test fixture class (typically just below it), define those
member variables, optionally giving them initial values. member variables, optionally giving them initial values.
3. In the same test fixture class, define a `static void SetUpTestSuite()` 3. In the same test fixture class, define a public member function `static void
function (remember not to spell it as **`SetupTestSuite`** with a small SetUpTestSuite()` (remember not to spell it as **`SetupTestSuite`** with a
`u`!) to set up the shared resources and a `static void TearDownTestSuite()` small `u`!) to set up the shared resources and a `static void
function to tear them down. TearDownTestSuite()` function to tear them down.
That's it! googletest automatically calls `SetUpTestSuite()` before running the That's it! GoogleTest automatically calls `SetUpTestSuite()` before running the
*first test* in the `FooTest` test suite (i.e. before creating the first *first test* in the `FooTest` test suite (i.e. before creating the first
`FooTest` object), and calls `TearDownTestSuite()` after running the *last test* `FooTest` object), and calls `TearDownTestSuite()` after running the *last test*
in it (i.e. after deleting the last `FooTest` object). In between, the tests can in it (i.e. after deleting the last `FooTest` object). In between, the tests can
@ -896,7 +927,8 @@ Note that `SetUpTestSuite()` may be called multiple times for a test fixture
class that has derived classes, so you should not expect code in the function class that has derived classes, so you should not expect code in the function
body to be run only once. Also, derived classes still have access to shared body to be run only once. Also, derived classes still have access to shared
resources defined as static members, so careful consideration is needed when resources defined as static members, so careful consideration is needed when
managing shared resources to avoid memory leaks. managing shared resources to avoid memory leaks if shared resources are not
properly cleaned up in `TearDownTestSuite()`.
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:
@ -907,10 +939,15 @@ class FooTest : public testing::Test {
// Called before the first test in this test suite. // Called before the first test in this test suite.
// Can be omitted if not needed. // Can be omitted if not needed.
static void SetUpTestSuite() { static void SetUpTestSuite() {
// Avoid reallocating static objects if called in subclasses of FooTest.
if (shared_resource_ == nullptr) {
shared_resource_ = new ...; shared_resource_ = new ...;
}
// If `shared_resource_` is **not deleted** in `TearDownTestSuite()`,
// reallocation should be prevented because `SetUpTestSuite()` may be called
// in subclasses of FooTest and lead to memory leak.
//
// if (shared_resource_ == nullptr) {
// shared_resource_ = new ...;
// }
} }
// Per-test-suite tear-down. // Per-test-suite tear-down.
@ -968,24 +1005,34 @@ class Environment : public ::testing::Environment {
}; };
``` ```
Then, you register an instance of your environment class with googletest by Then, you register an instance of your environment class with GoogleTest by
calling the `::testing::AddGlobalTestEnvironment()` function: calling the `::testing::AddGlobalTestEnvironment()` function:
```c++ ```c++
Environment* AddGlobalTestEnvironment(Environment* env); Environment* AddGlobalTestEnvironment(Environment* env);
``` ```
Now, when `RUN_ALL_TESTS()` is called, it first calls the `SetUp()` method of Now, when `RUN_ALL_TESTS()` is invoked, it first calls the `SetUp()` method. The
each environment object, then runs the tests if none of the environments tests are then executed, provided that none of the environments have reported
reported fatal failures and `GTEST_SKIP()` was not called. `RUN_ALL_TESTS()` fatal failures and `GTEST_SKIP()` has not been invoked. Finally, `TearDown()` is
always calls `TearDown()` with each environment object, regardless of whether or called.
not the tests were run.
Note that `SetUp()` and `TearDown()` are only invoked if there is at least one
test to be performed. Importantly, `TearDown()` is executed even if the test is
not run due to a fatal failure or `GTEST_SKIP()`.
Calling `SetUp()` and `TearDown()` for each iteration depends on the flag
`gtest_recreate_environments_when_repeating`. `SetUp()` and `TearDown()` are
called for each environment object when the object is recreated for each
iteration. However, if test environments are not recreated for each iteration,
`SetUp()` is called only on the first iteration, and `TearDown()` is called only
on the last iteration.
It's OK to register multiple environment objects. In this suite, their `SetUp()` It's OK to register multiple environment objects. In this suite, their `SetUp()`
will be called in the order they are registered, and their `TearDown()` will be will be called in the order they are registered, and their `TearDown()` will be
called in the reverse order. called in the reverse order.
Note that googletest takes ownership of the registered environment objects. Note that GoogleTest takes ownership of the registered environment objects.
Therefore **do not delete them** by yourself. Therefore **do not delete them** by yourself.
You should call `AddGlobalTestEnvironment()` before `RUN_ALL_TESTS()` is called, You should call `AddGlobalTestEnvironment()` before `RUN_ALL_TESTS()` is called,
@ -1037,7 +1084,7 @@ they must be declared **public** rather than **protected** in order to use
```c++ ```c++
class FooTest : class FooTest :
public testing::TestWithParam<const char*> { public testing::TestWithParam<absl::string_view> {
// You can implement all the usual fixture class members here. // You can implement all the usual fixture class members here.
// To access the test parameter, call GetParam() from class // To access the test parameter, call GetParam() from class
// TestWithParam<T>. // TestWithParam<T>.
@ -1048,7 +1095,7 @@ class BaseTest : public testing::Test {
... ...
}; };
class BarTest : public BaseTest, class BarTest : public BaseTest,
public testing::WithParamInterface<const char*> { public testing::WithParamInterface<absl::string_view> {
... ...
}; };
``` ```
@ -1119,8 +1166,8 @@ with parameter values `"cat"` and `"dog"` using the
[`ValuesIn`](reference/testing.md#param-generators) parameter generator: [`ValuesIn`](reference/testing.md#param-generators) parameter generator:
```c++ ```c++
const char* pets[] = {"cat", "dog"}; constexpr absl::string_view kPets[] = {"cat", "dog"};
INSTANTIATE_TEST_SUITE_P(Pets, FooTest, testing::ValuesIn(pets)); INSTANTIATE_TEST_SUITE_P(Pets, FooTest, testing::ValuesIn(kPets));
``` ```
The tests from the instantiation above will have these names: The tests from the instantiation above will have these names:
@ -1403,17 +1450,19 @@ are two cases to consider:
To test them, we use the following special techniques: To test them, we use the following special techniques:
* Both static functions and definitions/declarations in an unnamed namespace * Both static functions and definitions/declarations in an unnamed namespace
are only visible within the same translation unit. To test them, you can are only visible within the same translation unit. To test them, move the
`#include` the entire `.cc` file being tested in your `*_test.cc` file. private code into the `foo::internal` namespace, where `foo` is the
(#including `.cc` files is not a good way to reuse code - you should not do namespace your project normally uses, and put the private declarations in a
this in production code!) `*-internal.h` file. Your production `.cc` files and your tests are allowed
to include this internal header, but your clients are not. This way, you can
fully test your internal implementation without leaking it to your clients.
However, a better approach is to move the private code into the {: .callout .note}
`foo::internal` namespace, where `foo` is the namespace your project NOTE: It is also technically *possible* to `#include` the entire `.cc` file
normally uses, and put the private declarations in a `*-internal.h` file. being tested in your `*_test.cc` file to test static functions and
Your production `.cc` files and your tests are allowed to include this definitions/declarations in an unnamed namespace. However, this technique is
internal header, but your clients are not. This way, you can fully test your **not recommended** by this documentation and it is only presented here for the
internal implementation without leaking it to your clients. sake of completeness.
* Private class members are only accessible from within the class or by * Private class members are only accessible from within the class or by
friends. To access a class' private members, you can declare your test friends. To access a class' private members, you can declare your test
@ -1426,10 +1475,7 @@ To test them, we use the following special techniques:
Another way to test private members is to refactor them into an Another way to test private members is to refactor them into an
implementation class, which is then declared in a `*-internal.h` file. Your implementation class, which is then declared in a `*-internal.h` file. Your
clients aren't allowed to include this header but your tests can. Such is clients aren't allowed to include this header but your tests can.
called the
[Pimpl](https://www.gamedev.net/articles/programming/general-and-gameplay-programming/the-c-pimpl-r1794/)
(Private Implementation) idiom.
Or, you can declare an individual test as a friend of your class by adding Or, you can declare an individual test as a friend of your class by adding
this line in the class body: this line in the class body:
@ -1495,12 +1541,12 @@ To test them, we use the following special techniques:
## "Catching" Failures ## "Catching" Failures
If you are building a testing utility on top of googletest, you'll want to test If you are building a testing utility on top of GoogleTest, you'll want to test
your utility. What framework would you use to test it? googletest, of course. your utility. What framework would you use to test it? GoogleTest, of course.
The challenge is to verify that your testing utility reports failures correctly. The challenge is to verify that your testing utility reports failures correctly.
In frameworks that report a failure by throwing an exception, you could catch In frameworks that report a failure by throwing an exception, you could catch
the exception and assert on it. But googletest doesn't use exceptions, so how do the exception and assert on it. But GoogleTest doesn't use exceptions, so how do
we test that a piece of code generates an expected failure? we test that a piece of code generates an expected failure?
`"gtest/gtest-spi.h"` contains some constructs to do this. `"gtest/gtest-spi.h"` contains some constructs to do this.
@ -1643,9 +1689,9 @@ particular, you cannot find the test suite name in `SetUpTestSuite()`,
`TearDownTestSuite()` (where you know the test suite name implicitly), or `TearDownTestSuite()` (where you know the test suite name implicitly), or
functions called from them. functions called from them.
## Extending googletest by Handling Test Events ## Extending GoogleTest by Handling Test Events
googletest provides an **event listener API** to let you receive notifications GoogleTest provides an **event listener API** to let you receive notifications
about the progress of a test program and test failures. The events you can about the progress of a test program and test failures. The events you can
listen to include the start and end of the test program, a test suite, or a test listen to include the start and end of the test program, a test suite, or a test
method, among others. You may use this API to augment or replace the standard method, among others. You may use this API to augment or replace the standard
@ -1706,7 +1752,7 @@ Here's an example:
### Using Event Listeners ### Using Event Listeners
To use the event listener you have defined, add an instance of it to the To use the event listener you have defined, add an instance of it to the
googletest event listener list (represented by class GoogleTest event listener list (represented by class
[`TestEventListeners`](reference/testing.md#TestEventListeners) - note the "s" [`TestEventListeners`](reference/testing.md#TestEventListeners) - note the "s"
at the end of the name) in your `main()` function, before calling at the end of the name) in your `main()` function, before calling
`RUN_ALL_TESTS()`: `RUN_ALL_TESTS()`:
@ -1717,7 +1763,7 @@ int main(int argc, char** 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();
} }
@ -1769,13 +1815,13 @@ See [sample10_unittest.cc] for an example of a failure-raising listener.
## Running Test Programs: Advanced Options ## Running Test Programs: Advanced Options
googletest test programs are ordinary executables. Once built, you can run them GoogleTest test programs are ordinary executables. Once built, you can run them
directly and affect their behavior via the following environment variables directly and affect their behavior via the following environment variables
and/or command line flags. For the flags to work, your programs must call and/or command line flags. For the flags to work, your programs must call
`::testing::InitGoogleTest()` before calling `RUN_ALL_TESTS()`. `::testing::InitGoogleTest()` before calling `RUN_ALL_TESTS()`.
To see a list of supported flags and their usage, please run your test program To see a list of supported flags and their usage, please run your test program
with the `--help` flag. You can also use `-h`, `-?`, or `/?` for short. with the `--help` flag.
If an option is specified both by an environment variable and by a flag, the If an option is specified both by an environment variable and by a flag, the
latter takes precedence. latter takes precedence.
@ -1802,10 +1848,10 @@ corresponding environment variable for this flag.
#### Running a Subset of the Tests #### Running a Subset of the Tests
By default, a googletest program runs all tests the user has defined. Sometimes, By default, a GoogleTest program runs all tests the user has defined. Sometimes,
you want to run only a subset of the tests (e.g. for debugging or quickly you want to run only a subset of the tests (e.g. for debugging or quickly
verifying a change). If you set the `GTEST_FILTER` environment variable or the verifying a change). If you set the `GTEST_FILTER` environment variable or the
`--gtest_filter` flag to a filter string, googletest will only run the tests `--gtest_filter` flag to a filter string, GoogleTest will only run the tests
whose full names (in the form of `TestSuiteName.TestName`) match the filter. whose full names (in the form of `TestSuiteName.TestName`) match the filter.
The format of a filter is a '`:`'-separated list of wildcard patterns (called The format of a filter is a '`:`'-separated list of wildcard patterns (called
@ -1836,7 +1882,7 @@ For example:
#### Stop test execution upon first failure #### Stop test execution upon first failure
By default, a googletest program runs all tests the user has defined. In some By default, a GoogleTest program runs all tests the user has defined. In some
cases (e.g. iterative test development & execution) it may be desirable stop cases (e.g. iterative test development & execution) it may be desirable stop
test execution upon first failure (trading improved latency for completeness). test execution upon first failure (trading improved latency for completeness).
If `GTEST_FAIL_FAST` environment variable or `--gtest_fail_fast` flag is set, If `GTEST_FAIL_FAST` environment variable or `--gtest_fail_fast` flag is set,
@ -1853,7 +1899,7 @@ If you need to disable all tests in a test suite, you can either add `DISABLED_`
to the front of the name of each test, or alternatively add it to the front of to the front of the name of each test, or alternatively add it to the front of
the test suite name. the test suite name.
For example, the following tests won't be run by googletest, even though they For example, the following tests won't be run by GoogleTest, even though they
will still be compiled: will still be compiled:
```c++ ```c++
@ -1868,7 +1914,7 @@ TEST_F(DISABLED_BarTest, DoesXyz) { ... }
{: .callout .note} {: .callout .note}
NOTE: This feature should only be used for temporary pain-relief. You still have NOTE: This feature should only be used for temporary pain-relief. You still have
to fix the disabled tests at a later date. As a reminder, googletest will print to fix the disabled tests at a later date. As a reminder, GoogleTest will print
a banner warning you if a test program contains any disabled tests. a banner warning you if a test program contains any disabled tests.
{: .callout .tip} {: .callout .tip}
@ -1884,6 +1930,35 @@ the `--gtest_also_run_disabled_tests` flag or set the
You can combine this with the `--gtest_filter` flag to further select which You can combine this with the `--gtest_filter` flag to further select which
disabled tests to run. disabled tests to run.
### Enforcing Having At Least One Test Case
A not uncommon programmer mistake is to write a test program that has no test
case linked in. This can happen, for example, when you put test case definitions
in a library and the library is not marked as "always link".
To catch such mistakes, run the test program with the
`--gtest_fail_if_no_test_linked` flag or set the `GTEST_FAIL_IF_NO_TEST_LINKED`
environment variable to a value other than `0`. Now the program will fail if no
test case is linked in.
Note that *any* test case linked in makes the program valid for the purpose of
this check. In particular, even a disabled test case suffices.
### Enforcing Running At Least One Test Case
In addition to enforcing that tests are defined in the binary with
`--gtest_fail_if_no_test_linked`, it is also possible to enforce that a test
case was actually executed to ensure that resources are not consumed by tests
that do nothing.
To catch such optimization opportunities, run the test program with the
`--gtest_fail_if_no_test_selected` flag or set the
`GTEST_FAIL_IF_NO_TEST_SELECTED` environment variable to a value other than `0`.
A test is considered selected if it begins to run, even if it is later skipped
via `GTEST_SKIP`. Thus, `DISABLED` tests do not count as selected and neither do
tests that are not matched by `--gtest_filter`.
### Repeating the Tests ### Repeating the Tests
Once in a while you'll run into a test whose result is hit-or-miss. Perhaps it Once in a while you'll run into a test whose result is hit-or-miss. Perhaps it
@ -1926,16 +2001,16 @@ You can specify the `--gtest_shuffle` flag (or set the `GTEST_SHUFFLE`
environment variable to `1`) to run the tests in a program in a random order. environment variable to `1`) to run the tests in a program in a random order.
This helps to reveal bad dependencies between tests. This helps to reveal bad dependencies between tests.
By default, googletest uses a random seed calculated from the current time. By default, GoogleTest uses a random seed calculated from the current time.
Therefore you'll get a different order every time. The console output includes Therefore you'll get a different order every time. The console output includes
the random seed value, such that you can reproduce an order-related test failure the random seed value, such that you can reproduce an order-related test failure
later. To specify the random seed explicitly, use the `--gtest_random_seed=SEED` later. To specify the random seed explicitly, use the `--gtest_random_seed=SEED`
flag (or set the `GTEST_RANDOM_SEED` environment variable), where `SEED` is an flag (or set the `GTEST_RANDOM_SEED` environment variable), where `SEED` is an
integer in the range [0, 99999]. The seed value 0 is special: it tells integer in the range [0, 99999]. The seed value 0 is special: it tells
googletest to do the default behavior of calculating the seed from the current GoogleTest to do the default behavior of calculating the seed from the current
time. time.
If you combine this with `--gtest_repeat=N`, googletest will pick a different If you combine this with `--gtest_repeat=N`, GoogleTest will pick a different
random seed and re-shuffle the tests in each iteration. random seed and re-shuffle the tests in each iteration.
### Distributing Test Functions to Multiple Machines ### Distributing Test Functions to Multiple Machines
@ -1994,7 +2069,7 @@ shards, but here's one possible scenario:
#### Colored Terminal Output #### Colored Terminal Output
googletest can use colors in its terminal output to make it easier to spot the GoogleTest can use colors in its terminal output to make it easier to spot the
important information: important information:
<pre>... <pre>...
@ -2019,25 +2094,25 @@ important information:
You can set the `GTEST_COLOR` environment variable or the `--gtest_color` You can set the `GTEST_COLOR` environment variable or the `--gtest_color`
command line flag to `yes`, `no`, or `auto` (the default) to enable colors, command line flag to `yes`, `no`, or `auto` (the default) to enable colors,
disable colors, or let googletest decide. When the value is `auto`, googletest disable colors, or let GoogleTest decide. When the value is `auto`, GoogleTest
will use colors if and only if the output goes to a terminal and (on non-Windows will use colors if and only if the output goes to a terminal and (on non-Windows
platforms) the `TERM` environment variable is set to `xterm` or `xterm-color`. platforms) the `TERM` environment variable is set to `xterm` or `xterm-color`.
#### Suppressing test passes #### Suppressing test passes
By default, googletest prints 1 line of output for each test, indicating if it By default, GoogleTest prints 1 line of output for each test, indicating if it
passed or failed. To show only test failures, run the test program with passed or failed. To show only test failures, run the test program with
`--gtest_brief=1`, or set the GTEST_BRIEF environment variable to `1`. `--gtest_brief=1`, or set the GTEST_BRIEF environment variable to `1`.
#### Suppressing the Elapsed Time #### Suppressing the Elapsed Time
By default, googletest prints the time it takes to run each test. To disable By default, GoogleTest prints the time it takes to run each test. To disable
that, run the test program with the `--gtest_print_time=0` command line flag, or that, run the test program with the `--gtest_print_time=0` command line flag, or
set the GTEST_PRINT_TIME environment variable to `0`. set the GTEST_PRINT_TIME environment variable to `0`.
#### Suppressing UTF-8 Text Output #### Suppressing UTF-8 Text Output
In case of assertion failures, googletest prints expected and actual values of In case of assertion failures, GoogleTest prints expected and actual values of
type `string` both as hex-encoded strings as well as in readable UTF-8 text if type `string` both as hex-encoded strings as well as in readable UTF-8 text if
they contain valid non-ASCII UTF-8 characters. If you want to suppress the UTF-8 they contain valid non-ASCII UTF-8 characters. If you want to suppress the UTF-8
text because, for example, you don't have an UTF-8 compatible output medium, run text because, for example, you don't have an UTF-8 compatible output medium, run
@ -2046,7 +2121,7 @@ environment variable to `0`.
#### Generating an XML Report #### Generating an XML Report
googletest can emit a detailed XML report to a file in addition to its normal GoogleTest can emit a detailed XML report to a file in addition to its normal
textual output. The report contains the duration of each test, and thus can help textual output. The report contains the duration of each test, and thus can help
you identify slow tests. you identify slow tests.
@ -2057,15 +2132,15 @@ in which case the output can be found in the `test_detail.xml` file in the
current directory. current directory.
If you specify a directory (for example, `"xml:output/directory/"` on Linux or If you specify a directory (for example, `"xml:output/directory/"` on Linux or
`"xml:output\directory\"` on Windows), googletest will create the XML file in `"xml:output\directory\"` on Windows), GoogleTest will create the XML file in
that directory, named after the test executable (e.g. `foo_test.xml` for test that directory, named after the test executable (e.g. `foo_test.xml` for test
program `foo_test` or `foo_test.exe`). If the file already exists (perhaps left program `foo_test` or `foo_test.exe`). If the file already exists (perhaps left
over from a previous run), googletest will pick a different name (e.g. over from a previous run), GoogleTest will pick a different name (e.g.
`foo_test_1.xml`) to avoid overwriting it. `foo_test_1.xml`) to avoid overwriting it.
The report is based on the `junitreport` Ant task. Since that format was The report is based on the `junitreport` Ant task. Since that format was
originally intended for Java, a little interpretation is required to make it originally intended for Java, a little interpretation is required to make it
apply to googletest tests, as shown here: apply to GoogleTest tests, as shown here:
```xml ```xml
<testsuites name="AllTests" ...> <testsuites name="AllTests" ...>
@ -2080,8 +2155,8 @@ apply to googletest tests, as shown here:
``` ```
* The root `<testsuites>` element corresponds to the entire test program. * The root `<testsuites>` element corresponds to the entire test program.
* `<testsuite>` elements correspond to googletest test suites. * `<testsuite>` elements correspond to GoogleTest test suites.
* `<testcase>` elements correspond to googletest test functions. * `<testcase>` elements correspond to GoogleTest test functions.
For instance, the following program For instance, the following program
@ -2114,7 +2189,7 @@ could generate this report:
Things to note: Things to note:
* The `tests` attribute of a `<testsuites>` or `<testsuite>` element tells how * The `tests` attribute of a `<testsuites>` or `<testsuite>` element tells how
many test functions the googletest program or test suite contains, while the many test functions the GoogleTest program or test suite contains, while the
`failures` attribute tells how many of them failed. `failures` attribute tells how many of them failed.
* The `time` attribute expresses the duration of the test, test suite, or * The `time` attribute expresses the duration of the test, test suite, or
@ -2126,12 +2201,12 @@ Things to note:
* The `file` and `line` attributes record the source file location, where the * The `file` and `line` attributes record the source file location, where the
test was defined. test was defined.
* Each `<failure>` element corresponds to a single failed googletest * Each `<failure>` element corresponds to a single failed GoogleTest
assertion. assertion.
#### Generating a JSON Report #### Generating a JSON Report
googletest can also emit a JSON report as an alternative format to XML. To GoogleTest can also emit a JSON report as an alternative format to XML. To
generate the JSON report, set the `GTEST_OUTPUT` environment variable or the generate the JSON report, set the `GTEST_OUTPUT` environment variable or the
`--gtest_output` flag to the string `"json:path_to_output_file"`, which will `--gtest_output` flag to the string `"json:path_to_output_file"`, which will
create the file at the given location. You can also just use the string create the file at the given location. You can also just use the string
@ -2142,7 +2217,7 @@ The report format conforms to the following JSON Schema:
```json ```json
{ {
"$schema": "http://json-schema.org/schema#", "$schema": "https://json-schema.org/schema#",
"type": "object", "type": "object",
"definitions": { "definitions": {
"TestCase": { "TestCase": {
@ -2343,7 +2418,7 @@ IMPORTANT: The exact format of the JSON document is subject to change.
#### Detecting Test Premature Exit #### Detecting Test Premature Exit
Google Test implements the _premature-exit-file_ protocol for test runners to Google Test implements the *premature-exit-file* protocol for test runners to
catch any kind of unexpected exits of test programs. Upon start, Google Test catch any kind of unexpected exits of test programs. Upon start, Google Test
creates the file which will be automatically deleted after all work has been creates the file which will be automatically deleted after all work has been
finished. Then, the test runner can check if this file exists. In case the file finished. Then, the test runner can check if this file exists. In case the file
@ -2356,7 +2431,7 @@ variable has been set.
When running test programs under a debugger, it's very convenient if the When running test programs under a debugger, it's very convenient if the
debugger can catch an assertion failure and automatically drop into interactive debugger can catch an assertion failure and automatically drop into interactive
mode. googletest's *break-on-failure* mode supports this behavior. mode. GoogleTest's *break-on-failure* mode supports this behavior.
To enable it, set the `GTEST_BREAK_ON_FAILURE` environment variable to a value To enable it, set the `GTEST_BREAK_ON_FAILURE` environment variable to a value
other than `0`. Alternatively, you can use the `--gtest_break_on_failure` other than `0`. Alternatively, you can use the `--gtest_break_on_failure`
@ -2364,9 +2439,9 @@ command line flag.
#### Disabling Catching Test-Thrown Exceptions #### Disabling Catching Test-Thrown Exceptions
googletest can be used either with or without exceptions enabled. If a test GoogleTest can be used either with or without exceptions enabled. If a test
throws a C++ exception or (on Windows) a structured exception (SEH), by default throws a C++ exception or (on Windows) a structured exception (SEH), by default
googletest catches it, reports it as a test failure, and continues with the next GoogleTest catches it, reports it as a test failure, and continues with the next
test method. This maximizes the coverage of a test run. Also, on Windows an test method. This maximizes the coverage of a test run. Also, on Windows an
uncaught exception will cause a pop-up window, so catching the exceptions allows uncaught exception will cause a pop-up window, so catching the exceptions allows
you to run the tests automatically. you to run the tests automatically.
@ -2404,4 +2479,4 @@ void __tsan_on_report() {
``` ```
After compiling your project with one of the sanitizers enabled, if a particular After compiling your project with one of the sanitizers enabled, if a particular
test triggers a sanitizer error, googletest will report that it failed. test triggers a sanitizer error, GoogleTest will report that it failed.

View File

@ -3,7 +3,7 @@
## Why should test suite names and test names not contain underscore? ## Why should test suite names and test names not contain underscore?
{: .callout .note} {: .callout .note}
Note: GoogleTest reserves underscore (`_`) for special purpose keywords, such as Note: GoogleTest reserves underscore (`_`) for special-purpose keywords, such as
[the `DISABLED_` prefix](advanced.md#temporarily-disabling-tests), in addition [the `DISABLED_` prefix](advanced.md#temporarily-disabling-tests), in addition
to the following rationale. to the following rationale.
@ -33,9 +33,9 @@ contains `_`?
`TestSuiteName_Bar__Test`, which is invalid. `TestSuiteName_Bar__Test`, which is invalid.
So clearly `TestSuiteName` and `TestName` cannot start or end with `_` So clearly `TestSuiteName` and `TestName` cannot start or end with `_`
(Actually, `TestSuiteName` can start with `_` -- as long as the `_` isn't (Actually, `TestSuiteName` can start with `_`—as long as the `_` isn't followed
followed by an upper-case letter. But that's getting complicated. So for by an upper-case letter. But that's getting complicated. So for simplicity we
simplicity we just say that it cannot start with `_`.). just say that it cannot start with `_`.).
It may seem fine for `TestSuiteName` and `TestName` to contain `_` in the It may seem fine for `TestSuiteName` and `TestName` to contain `_` in the
middle. However, consider this: middle. However, consider this:
@ -128,30 +128,9 @@ both approaches a try. Practice is a much better way to grasp the subtle
differences between the two tools. Once you have some concrete experience, you differences between the two tools. Once you have some concrete experience, you
can much more easily decide which one to use the next time. can much more easily decide which one to use the next time.
## I got some run-time errors about invalid proto descriptors when using `ProtocolMessageEquals`. Help!
{: .callout .note}
**Note:** `ProtocolMessageEquals` and `ProtocolMessageEquiv` are *deprecated*
now. Please use `EqualsProto`, etc instead.
`ProtocolMessageEquals` and `ProtocolMessageEquiv` were redefined recently and
are now less tolerant of invalid protocol buffer definitions. In particular, if
you have a `foo.proto` that doesn't fully qualify the type of a protocol message
it references (e.g. `message<Bar>` where it should be `message<blah.Bar>`), you
will now get run-time errors like:
```
... descriptor.cc:...] Invalid proto descriptor for file "path/to/foo.proto":
... descriptor.cc:...] blah.MyMessage.my_field: ".Bar" is not defined.
```
If you see this, your `.proto` file is broken and needs to be fixed by making
the types fully qualified. The new definition of `ProtocolMessageEquals` and
`ProtocolMessageEquiv` just happen to reveal your bug.
## My death test modifies some state, but the change seems lost after the death test finishes. Why? ## My death test modifies some state, but the change seems lost after the death test finishes. Why?
Death tests (`EXPECT_DEATH`, etc) are executed in a sub-process s.t. the Death tests (`EXPECT_DEATH`, etc.) are executed in a sub-process s.t. the
expected crash won't kill the test program (i.e. the parent process). As a expected crash won't kill the test program (i.e. the parent process). As a
result, any in-memory side effects they incur are observable in their respective result, any in-memory side effects they incur are observable in their respective
sub-processes, but not in the parent process. You can think of them as running sub-processes, but not in the parent process. You can think of them as running
@ -192,16 +171,16 @@ class Foo {
}; };
``` ```
You also need to define it *outside* of the class body in `foo.cc`: you also need to define it *outside* of the class body in `foo.cc`:
```c++ ```c++
const int Foo::kBar; // No initializer here. const int Foo::kBar; // No initializer here.
``` ```
Otherwise your code is **invalid C++**, and may break in unexpected ways. In Otherwise your code is **invalid C++**, and may break in unexpected ways. In
particular, using it in GoogleTest comparison assertions (`EXPECT_EQ`, etc) will particular, using it in GoogleTest comparison assertions (`EXPECT_EQ`, etc.)
generate an "undefined reference" linker error. The fact that "it used to work" will generate an "undefined reference" linker error. The fact that "it used to
doesn't mean it's valid. It just means that you were lucky. :-) work" doesn't mean it's valid. It just means that you were lucky. :-)
If the declaration of the static data member is `constexpr` then it is If the declaration of the static data member is `constexpr` then it is
implicitly an `inline` definition, and a separate definition in `foo.cc` is not implicitly an `inline` definition, and a separate definition in `foo.cc` is not
@ -311,7 +290,7 @@ a **fresh** test fixture object, immediately call `SetUp()`, run the test body,
call `TearDown()`, and then delete the test fixture object. call `TearDown()`, and then delete the test fixture object.
When you need to write per-test set-up and tear-down logic, you have the choice When you need to write per-test set-up and tear-down logic, you have the choice
between using the test fixture constructor/destructor or `SetUp()/TearDown()`. between using the test fixture constructor/destructor or `SetUp()`/`TearDown()`.
The former is usually preferred, as it has the following benefits: The former is usually preferred, as it has the following benefits:
* By initializing a member variable in the constructor, we have the option to * By initializing a member variable in the constructor, we have the option to
@ -352,7 +331,7 @@ You may still want to use `SetUp()/TearDown()` in the following cases:
GoogleTest assertions in a destructor if your code could run on such a GoogleTest assertions in a destructor if your code could run on such a
platform. platform.
## The compiler complains "no matching function to call" when I use ASSERT_PRED*. How do I fix it? ## The compiler complains "no matching function to call" when I use `ASSERT_PRED*`. How do I fix it?
See details for [`EXPECT_PRED*`](reference/assertions.md#EXPECT_PRED) in the See details for [`EXPECT_PRED*`](reference/assertions.md#EXPECT_PRED) in the
Assertions Reference. Assertions Reference.
@ -410,7 +389,7 @@ C++ is case-sensitive. Did you spell it as `Setup()`?
Similarly, sometimes people spell `SetUpTestSuite()` as `SetupTestSuite()` and Similarly, sometimes people spell `SetUpTestSuite()` as `SetupTestSuite()` and
wonder why it's never called. wonder why it's never called.
## I have several test suites which share the same test fixture logic, do I have to define a new test fixture class for each of them? This seems pretty tedious. ## I have several test suites which share the same test fixture logic; do I have to define a new test fixture class for each of them? This seems pretty tedious.
You don't have to. Instead of You don't have to. Instead of
@ -532,20 +511,7 @@ However, there are cases where you have to define your own:
list of the constructor. (Early versions of `gcc` doesn't force you to list of the constructor. (Early versions of `gcc` doesn't force you to
initialize the const member. It's a bug that has been fixed in `gcc 4`.) initialize the const member. It's a bug that has been fixed in `gcc 4`.)
## Why does ASSERT_DEATH complain about previous threads that were already joined? ## Why does GoogleTest require the entire test suite, instead of individual tests, to be named `*DeathTest` when it uses `ASSERT_DEATH`?
With the Linux pthread library, there is no turning back once you cross the line
from a single thread to multiple threads. The first time you create a thread, a
manager thread is created in addition, so you get 3, not 2, threads. Later when
the thread you create joins the main thread, the thread count decrements by 1,
but the manager thread will never be killed, so you still have 2 threads, which
means you cannot safely run a death test.
The new NPTL thread library doesn't suffer from this problem, as it doesn't
create a manager thread. However, if you don't control which machine your test
runs on, you shouldn't depend on this.
## Why does GoogleTest require the entire test suite, instead of individual tests, to be named *DeathTest when it uses ASSERT_DEATH?
GoogleTest does not interleave tests from different test suites. That is, it GoogleTest does not interleave tests from different test suites. That is, it
runs all tests in one test suite first, and then runs all tests in the next test runs all tests in one test suite first, and then runs all tests in the next test
@ -570,7 +536,7 @@ interleave tests from different test suites, we need to run all tests in the
`FooTest` case before running any test in the `BarTest` case. This contradicts `FooTest` case before running any test in the `BarTest` case. This contradicts
with the requirement to run `BarTest.DefDeathTest` before `FooTest.Uvw`. with the requirement to run `BarTest.DefDeathTest` before `FooTest.Uvw`.
## But I don't like calling my entire test suite \*DeathTest when it contains both death tests and non-death tests. What do I do? ## But I don't like calling my entire test suite `*DeathTest` when it contains both death tests and non-death tests. What do I do?
You don't have to, but if you like, you may split up the test suite into You don't have to, but if you like, you may split up the test suite into
`FooTest` and `FooDeathTest`, where the names make it clear that they are `FooTest` and `FooDeathTest`, where the names make it clear that they are
@ -607,7 +573,7 @@ defined such that we can print a value of `FooType`.
In addition, if `FooType` is declared in a name space, the `<<` operator also In addition, if `FooType` is declared in a name space, the `<<` operator also
needs to be defined in the *same* name space. See needs to be defined in the *same* name space. See
[Tip of the Week #49](http://abseil.io/tips/49) for details. [Tip of the Week #49](https://abseil.io/tips/49) for details.
## How do I suppress the memory leak messages on Windows? ## How do I suppress the memory leak messages on Windows?
@ -628,10 +594,10 @@ mistake in production. Such cleverness also leads to
advise against the practice, and GoogleTest doesn't provide a way to do it. advise against the practice, and GoogleTest doesn't provide a way to do it.
In general, the recommended way to cause the code to behave differently under In general, the recommended way to cause the code to behave differently under
test is [Dependency Injection](http://en.wikipedia.org/wiki/Dependency_injection). You can inject test is [Dependency Injection](https://en.wikipedia.org/wiki/Dependency_injection). You can inject
different functionality from the test and from the production code. Since your different functionality from the test and from the production code. Since your
production code doesn't link in the for-test logic at all (the production code doesn't link in the for-test logic at all (the
[`testonly`](http://docs.bazel.build/versions/master/be/common-definitions.html#common.testonly) attribute for BUILD targets helps to ensure [`testonly`](https://docs.bazel.build/versions/master/be/common-definitions.html#common.testonly) attribute for BUILD targets helps to ensure
that), there is no danger in accidentally running it. that), there is no danger in accidentally running it.
However, if you *really*, *really*, *really* have no choice, and if you follow However, if you *really*, *really*, *really* have no choice, and if you follow
@ -654,7 +620,7 @@ the `--gtest_also_run_disabled_tests` flag.
Yes. Yes.
The rule is **all test methods in the same test suite must use the same fixture The rule is **all test methods in the same test suite must use the same fixture
class.** This means that the following is **allowed** because both tests use the class**. This means that the following is **allowed** because both tests use the
same fixture class (`::testing::Test`). same fixture class (`::testing::Test`).
```c++ ```c++

View File

@ -20,7 +20,7 @@ class Foo {
(note that `~Foo()` **must** be virtual) we can define its mock as (note that `~Foo()` **must** be virtual) we can define its mock as
```cpp ```cpp
#include "gmock/gmock.h" #include <gmock/gmock.h>
class MockFoo : public Foo { class MockFoo : public Foo {
public: public:

View File

@ -177,7 +177,7 @@ class StackInterface {
template <typename Elem> template <typename Elem>
class MockStack : public StackInterface<Elem> { class MockStack : public StackInterface<Elem> {
... ...
MOCK_METHOD(int, GetSize, (), (override)); MOCK_METHOD(int, GetSize, (), (const, override));
MOCK_METHOD(void, Push, (const Elem& x), (override)); MOCK_METHOD(void, Push, (const Elem& x), (override));
}; };
``` ```
@ -285,6 +285,10 @@ If you are concerned about the performance overhead incurred by virtual
functions, and profiling confirms your concern, you can combine this with the functions, and profiling confirms your concern, you can combine this with the
recipe for [mocking non-virtual methods](#MockingNonVirtualMethods). recipe for [mocking non-virtual methods](#MockingNonVirtualMethods).
Alternatively, instead of introducing a new interface, you can rewrite your code
to accept a std::function instead of the free function, and then use
[MockFunction](#MockFunction) to mock the std::function.
### Old-Style `MOCK_METHODn` Macros ### Old-Style `MOCK_METHODn` Macros
Before the generic `MOCK_METHOD` macro Before the generic `MOCK_METHOD` macro
@ -693,9 +697,9 @@ TEST(AbcTest, Xyz) {
EXPECT_CALL(foo, DoThat(_, _)); EXPECT_CALL(foo, DoThat(_, _));
int n = 0; int n = 0;
EXPECT_EQ('+', foo.DoThis(5)); // FakeFoo::DoThis() is invoked. EXPECT_EQ(foo.DoThis(5), '+'); // FakeFoo::DoThis() is invoked.
foo.DoThat("Hi", &n); // FakeFoo::DoThat() is invoked. foo.DoThat("Hi", &n); // FakeFoo::DoThat() is invoked.
EXPECT_EQ(2, n); EXPECT_EQ(n, 2);
} }
``` ```
@ -932,8 +936,8 @@ casts a matcher `m` to type `Matcher<T>`. To ensure safety, gMock checks that
floating-point numbers), the conversion from `T` to `U` is not lossy (in floating-point numbers), the conversion from `T` to `U` is not lossy (in
other words, any value representable by `T` can also be represented by `U`); other words, any value representable by `T` can also be represented by `U`);
and and
3. When `U` is a reference, `T` must also be a reference (as the underlying 3. When `U` is a non-const reference, `T` must also be a reference (as the
matcher may be interested in the address of the `U` value). underlying matcher may be interested in the address of the `U` value).
The code won't compile if any of these conditions isn't met. The code won't compile if any of these conditions isn't met.
@ -1125,11 +1129,11 @@ using STL's `<functional>` header is just painful). For example, here's a
predicate that's satisfied by any number that is >= 0, <= 100, and != 50: predicate that's satisfied by any number that is >= 0, <= 100, and != 50:
```cpp ```cpp
using testing::AllOf; using ::testing::AllOf;
using testing::Ge; using ::testing::Ge;
using testing::Le; using ::testing::Le;
using testing::Matches; using ::testing::Matches;
using testing::Ne; using ::testing::Ne;
... ...
Matches(AllOf(Ge(0), Le(100), Ne(50))) Matches(AllOf(Ge(0), Le(100), Ne(50)))
``` ```
@ -1857,7 +1861,7 @@ error. So, what shall you do?
Though you may be tempted, DO NOT use `std::ref()`: Though you may be tempted, DO NOT use `std::ref()`:
```cpp ```cpp
using testing::Return; using ::testing::Return;
class MockFoo : public Foo { class MockFoo : public Foo {
public: public:
@ -1869,7 +1873,7 @@ class MockFoo : public Foo {
EXPECT_CALL(foo, GetValue()) EXPECT_CALL(foo, GetValue())
.WillRepeatedly(Return(std::ref(x))); // Wrong! .WillRepeatedly(Return(std::ref(x))); // Wrong!
x = 42; x = 42;
EXPECT_EQ(42, foo.GetValue()); EXPECT_EQ(foo.GetValue(), 42);
``` ```
Unfortunately, it doesn't work here. The above code will fail with error: Unfortunately, it doesn't work here. The above code will fail with error:
@ -1891,14 +1895,14 @@ the expectation is set, and `Return(std::ref(x))` will always return 0.
returns the value pointed to by `pointer` at the time the action is *executed*: returns the value pointed to by `pointer` at the time the action is *executed*:
```cpp ```cpp
using testing::ReturnPointee; using ::testing::ReturnPointee;
... ...
int x = 0; int x = 0;
MockFoo foo; MockFoo foo;
EXPECT_CALL(foo, GetValue()) EXPECT_CALL(foo, GetValue())
.WillRepeatedly(ReturnPointee(&x)); // Note the & here. .WillRepeatedly(ReturnPointee(&x)); // Note the & here.
x = 42; x = 42;
EXPECT_EQ(42, foo.GetValue()); // This will succeed now. EXPECT_EQ(foo.GetValue(), 42); // This will succeed now.
``` ```
### Combining Actions ### Combining Actions
@ -1923,6 +1927,12 @@ class MockFoo : public Foo {
action_n)); action_n));
``` ```
The return value of the last action **must** match the return type of the mocked
method. In the example above, `action_n` could be `Return(true)`, or a lambda
that returns a `bool`, but not `SaveArg`, which returns `void`. Otherwise the
signature of `DoAll` would not match the signature expected by `WillOnce`, which
is the signature of the mocked method, and it wouldn't compile.
### Verifying Complex Arguments {#SaveArgVerify} ### Verifying Complex Arguments {#SaveArgVerify}
If you want to verify that a method is called with a particular argument but the If you want to verify that a method is called with a particular argument but the
@ -2260,7 +2270,7 @@ TEST_F(FooTest, Test) {
EXPECT_CALL(foo, DoThis(2)) EXPECT_CALL(foo, DoThis(2))
.WillOnce(Invoke(NewPermanentCallback(SignOfSum, 5))); .WillOnce(Invoke(NewPermanentCallback(SignOfSum, 5)));
EXPECT_EQ('+', foo.DoThis(2)); // Invokes SignOfSum(5, 2). EXPECT_EQ(foo.DoThis(2), '+'); // Invokes SignOfSum(5, 2).
} }
``` ```
@ -2636,8 +2646,8 @@ action will exhibit different behaviors. Example:
.WillRepeatedly(IncrementCounter(0)); .WillRepeatedly(IncrementCounter(0));
foo.DoThis(); // Returns 1. foo.DoThis(); // Returns 1.
foo.DoThis(); // Returns 2. foo.DoThis(); // Returns 2.
foo.DoThat(); // Returns 1 - Blah() uses a different foo.DoThat(); // Returns 1 - DoThat() uses a different
// counter than Bar()'s. // counter than DoThis()'s.
``` ```
versus versus
@ -2767,36 +2777,33 @@ returns a null `unique_ptr`, thats what youll get if you dont specify a
action: action:
```cpp ```cpp
using ::testing::IsNull;
...
// Use the default action. // Use the default action.
EXPECT_CALL(mock_buzzer_, MakeBuzz("hello")); EXPECT_CALL(mock_buzzer_, MakeBuzz("hello"));
// Triggers the previous EXPECT_CALL. // Triggers the previous EXPECT_CALL.
EXPECT_EQ(nullptr, mock_buzzer_.MakeBuzz("hello")); EXPECT_THAT(mock_buzzer_.MakeBuzz("hello"), IsNull());
``` ```
If you are not happy with the default action, you can tweak it as usual; see If you are not happy with the default action, you can tweak it as usual; see
[Setting Default Actions](#OnCall). [Setting Default Actions](#OnCall).
If you just need to return a pre-defined move-only value, you can use the If you just need to return a move-only value, you can use it in combination with
`Return(ByMove(...))` action: `WillOnce`. For example:
```cpp ```cpp
// When this fires, the unique_ptr<> specified by ByMove(...) will EXPECT_CALL(mock_buzzer_, MakeBuzz("hello"))
// be returned. .WillOnce(Return(std::make_unique<Buzz>(AccessLevel::kInternal)));
EXPECT_CALL(mock_buzzer_, MakeBuzz("world")) EXPECT_NE(nullptr, mock_buzzer_.MakeBuzz("hello"));
.WillOnce(Return(ByMove(std::make_unique<Buzz>(AccessLevel::kInternal))));
EXPECT_NE(nullptr, mock_buzzer_.MakeBuzz("world"));
``` ```
Note that `ByMove()` is essential here - if you drop it, the code wont compile. Quiz time! What do you think will happen if a `Return` action is performed more
than once (e.g. you write `... .WillRepeatedly(Return(std::move(...)));`)? Come
Quiz time! What do you think will happen if a `Return(ByMove(...))` action is think of it, after the first time the action runs, the source value will be
performed more than once (e.g. you write `... consumed (since its a move-only value), so the next time around, theres no
.WillRepeatedly(Return(ByMove(...)));`)? Come think of it, after the first time value to move from -- youll get a run-time error that `Return(std::move(...))`
the action runs, the source value will be consumed (since its a move-only can only be run once.
value), so the next time around, theres no value to move from -- youll get a
run-time error that `Return(ByMove(...))` can only be run once.
If you need your mock method to do more than just moving a pre-defined value, If you need your mock method to do more than just moving a pre-defined value,
remember that you can always use a lambda or a callable object, which can do remember that you can always use a lambda or a callable object, which can do
@ -2813,7 +2820,7 @@ pretty much anything you want:
``` ```
Every time this `EXPECT_CALL` fires, a new `unique_ptr<Buzz>` will be created Every time this `EXPECT_CALL` fires, a new `unique_ptr<Buzz>` will be created
and returned. You cannot do this with `Return(ByMove(...))`. and returned. You cannot do this with `Return(std::make_unique<...>(...))`.
That covers returning move-only values; but how do we work with methods That covers returning move-only values; but how do we work with methods
accepting move-only arguments? The answer is that they work normally, although accepting move-only arguments? The answer is that they work normally, although
@ -3193,11 +3200,11 @@ You can unlock this power by running your test with the `--gmock_verbose=info`
flag. For example, given the test program: flag. For example, given the test program:
```cpp ```cpp
#include "gmock/gmock.h" #include <gmock/gmock.h>
using testing::_; using ::testing::_;
using testing::HasSubstr; using ::testing::HasSubstr;
using testing::Return; using ::testing::Return;
class MockFoo { class MockFoo {
public: public:
@ -3305,7 +3312,7 @@ For convenience, we allow the description string to be empty (`""`), in which
case gMock will use the sequence of words in the matcher name as the case gMock will use the sequence of words in the matcher name as the
description. description.
For example: #### Basic Example
```cpp ```cpp
MATCHER(IsDivisibleBy7, "") { return (arg % 7) == 0; } MATCHER(IsDivisibleBy7, "") { return (arg % 7) == 0; }
@ -3343,6 +3350,8 @@ If the above assertions fail, they will print something like:
where the descriptions `"is divisible by 7"` and `"not (is divisible by 7)"` are where the descriptions `"is divisible by 7"` and `"not (is divisible by 7)"` are
automatically calculated from the matcher name `IsDivisibleBy7`. automatically calculated from the matcher name `IsDivisibleBy7`.
#### Adding Custom Failure Messages
As you may have noticed, the auto-generated descriptions (especially those for As you may have noticed, the auto-generated descriptions (especially those for
the negation) may not be so great. You can always override them with a `string` the negation) may not be so great. You can always override them with a `string`
expression of your own: expression of your own:
@ -3376,21 +3385,48 @@ With this definition, the above assertion will give a better message:
Actual: 27 (the remainder is 6) Actual: 27 (the remainder is 6)
``` ```
#### Using EXPECT_ Statements in Matchers
You can also use `EXPECT_...` statements inside custom matcher definitions. In
many cases, this allows you to write your matcher more concisely while still
providing an informative error message. For example:
```cpp
MATCHER(IsDivisibleBy7, "") {
const auto remainder = arg % 7;
EXPECT_EQ(remainder, 0);
return true;
}
```
If you write a test that includes the line `EXPECT_THAT(27, IsDivisibleBy7());`,
you will get an error something like the following:
```shell
Expected equality of these values:
remainder
Which is: 6
0
```
#### `MatchAndExplain`
You should let `MatchAndExplain()` print *any additional information* that can You should let `MatchAndExplain()` print *any additional information* that can
help a user understand the match result. Note that it should explain why the help a user understand the match result. Note that it should explain why the
match succeeds in case of a success (unless it's obvious) - this is useful when match succeeds in case of a success (unless it's obvious) - this is useful when
the matcher is used inside `Not()`. There is no need to print the argument value the matcher is used inside `Not()`. There is no need to print the argument value
itself, as gMock already prints it for you. itself, as gMock already prints it for you.
{: .callout .note} #### Argument Types
NOTE: The type of the value being matched (`arg_type`) is determined by the
context in which you use the matcher and is supplied to you by the compiler, so The type of the value being matched (`arg_type`) is determined by the context in
you don't need to worry about declaring it (nor can you). This allows the which you use the matcher and is supplied to you by the compiler, so you don't
matcher to be polymorphic. For example, `IsDivisibleBy7()` can be used to match need to worry about declaring it (nor can you). This allows the matcher to be
any type where the value of `(arg % 7) == 0` can be implicitly converted to a polymorphic. For example, `IsDivisibleBy7()` can be used to match any type where
`bool`. In the `Bar(IsDivisibleBy7())` example above, if method `Bar()` takes an the value of `(arg % 7) == 0` can be implicitly converted to a `bool`. In the
`int`, `arg_type` will be `int`; if it takes an `unsigned long`, `arg_type` will `Bar(IsDivisibleBy7())` example above, if method `Bar()` takes an `int`,
be `unsigned long`; and so on. `arg_type` will be `int`; if it takes an `unsigned long`, `arg_type` will be
`unsigned long`; and so on.
### Writing New Parameterized Matchers Quickly ### Writing New Parameterized Matchers Quickly
@ -3531,10 +3567,15 @@ just based on the number of parameters).
### Writing New Monomorphic Matchers ### Writing New Monomorphic Matchers
A matcher of argument type `T` implements the matcher interface for `T` and does A matcher of type `testing::Matcher<T>` implements the matcher interface for `T`
two things: it tests whether a value of type `T` matches the matcher, and can and does two things: it tests whether a value of type `T` matches the matcher,
describe what kind of values it matches. The latter ability is used for and can describe what kind of values it matches. The latter ability is used for
generating readable error messages when expectations are violated. generating readable error messages when expectations are violated. Some matchers
can even explain why it matches or doesn't match a certain value, which can be
helpful when the reason isn't obvious.
Because a matcher of type `testing::Matcher<T>` for a particular type `T` can
only be used to match a value of type `T`, we call it "monomorphic."
A matcher of `T` must declare a typedef like: A matcher of `T` must declare a typedef like:
@ -3626,8 +3667,16 @@ instead of `std::ostream*`.
### Writing New Polymorphic Matchers ### Writing New Polymorphic Matchers
Expanding what we learned above to *polymorphic* matchers is now just as simple Unlike a monomorphic matcher, which can only be used to match a value of a
as adding templates in the right place. particular type, a *polymorphic* matcher is one that can be used to match values
of multiple types. For example, `Eq(5)` is a polymorhpic matcher as it can be
used to match an `int`, a `double`, a `float`, and so on. You should think of a
polymorphic matcher as a *matcher factory* as opposed to a
`testing::Matcher<SomeType>` - itself is not an actual matcher, but can be
implicitly converted to a `testing::Matcher<SomeType>` depending on the context.
Expanding what we learned above to polymorphic matchers is now as simple as
adding templates in the right place.
```cpp ```cpp
@ -3753,6 +3802,26 @@ virtual.
Like in a monomorphic matcher, you may explain the match result by streaming Like in a monomorphic matcher, you may explain the match result by streaming
additional information to the `listener` argument in `MatchAndExplain()`. additional information to the `listener` argument in `MatchAndExplain()`.
### Implementing Composite Matchers {#CompositeMatchers}
Sometimes we want to define a matcher that takes other matchers as parameters.
For example, `DistanceFrom(target, m)` is a polymorphic matcher that takes a
matcher `m` as a parameter. It tests that the distance from `target` to the
value being matched satisfies sub-matcher `m`.
If you are implementing such a composite matcher, you'll need to generate the
description of the matcher based on the description(s) of its sub-matcher(s).
You can see the implementation of `DistanceFrom()` in
`googlemock/include/gmock/gmock-matchers.h` for an example. In particular, pay
attention to `DistanceFromMatcherImpl`. Notice that it stores the sub-matcher as
a `const Matcher<const Distance&> distance_matcher_` instead of a polymorphic
matcher - this allows it to call `distance_matcher_.DescribeTo(os)` to describe
the sub-matcher. If the sub-matcher is stored as a polymorphic matcher instead,
it would not be possible to get its description as in general polymorphic
matchers don't know how to describe themselves - they are matcher factories
instead of actual matchers; only after being converted to `Matcher<SomeType>`
can they be described.
### Writing New Cardinalities ### Writing New Cardinalities
A cardinality is used in `Times()` to tell gMock how many times you expect a A cardinality is used in `Times()` to tell gMock how many times you expect a
@ -3818,15 +3887,15 @@ If the built-in actions don't work for you, you can easily define your own one.
All you need is a call operator with a signature compatible with the mocked All you need is a call operator with a signature compatible with the mocked
function. So you can use a lambda: function. So you can use a lambda:
``` ```cpp
MockFunction<int(int)> mock; MockFunction<int(int)> mock;
EXPECT_CALL(mock, Call).WillOnce([](const int input) { return input * 7; }); EXPECT_CALL(mock, Call).WillOnce([](const int input) { return input * 7; });
EXPECT_EQ(14, mock.AsStdFunction()(2)); EXPECT_EQ(mock.AsStdFunction()(2), 14);
``` ```
Or a struct with a call operator (even a templated one): Or a struct with a call operator (even a templated one):
``` ```cpp
struct MultiplyBy { struct MultiplyBy {
template <typename T> template <typename T>
T operator()(T arg) { return arg * multiplier; } T operator()(T arg) { return arg * multiplier; }
@ -3841,16 +3910,16 @@ struct MultiplyBy {
It's also fine for the callable to take no arguments, ignoring the arguments It's also fine for the callable to take no arguments, ignoring the arguments
supplied to the mock function: supplied to the mock function:
``` ```cpp
MockFunction<int(int)> mock; MockFunction<int(int)> mock;
EXPECT_CALL(mock, Call).WillOnce([] { return 17; }); EXPECT_CALL(mock, Call).WillOnce([] { return 17; });
EXPECT_EQ(17, mock.AsStdFunction()(0)); EXPECT_EQ(mock.AsStdFunction()(0), 17);
``` ```
When used with `WillOnce`, the callable can assume it will be called at most When used with `WillOnce`, the callable can assume it will be called at most
once and is allowed to be a move-only type: once and is allowed to be a move-only type:
``` ```cpp
// An action that contains move-only types and has an &&-qualified operator, // An action that contains move-only types and has an &&-qualified operator,
// demanding in the type system that it be called at most once. This can be // demanding in the type system that it be called at most once. This can be
// used with WillOnce, but the compiler will reject it if handed to // used with WillOnce, but the compiler will reject it if handed to
@ -4294,7 +4363,7 @@ particular type than to dump the bytes.
### Mock std::function {#MockFunction} ### Mock std::function {#MockFunction}
`std::function` is a general function type introduced in C++11. It is a `std::function` is a general function type introduced in C++11. It is a
preferred way of passing callbacks to new interfaces. Functions are copiable, preferred way of passing callbacks to new interfaces. Functions are copyable,
and are not usually passed around by pointer, which makes them tricky to mock. and are not usually passed around by pointer, which makes them tricky to mock.
But fear not - `MockFunction` can help you with that. But fear not - `MockFunction` can help you with that.

View File

@ -90,14 +90,14 @@ gMock is bundled with googletest.
## A Case for Mock Turtles ## A Case for Mock Turtles
Let's look at an example. Suppose you are developing a graphics program that Let's look at an example. Suppose you are developing a graphics program that
relies on a [LOGO](http://en.wikipedia.org/wiki/Logo_programming_language)-like relies on a [LOGO](https://en.wikipedia.org/wiki/Logo_programming_language)-like
API for drawing. How would you test that it does the right thing? Well, you can API for drawing. How would you test that it does the right thing? Well, you can
run it and compare the screen with a golden screen snapshot, but let's admit it: run it and compare the screen with a golden screen snapshot, but let's admit it:
tests like this are expensive to run and fragile (What if you just upgraded to a tests like this are expensive to run and fragile (What if you just upgraded to a
shiny new graphics card that has better anti-aliasing? Suddenly you have to shiny new graphics card that has better anti-aliasing? Suddenly you have to
update all your golden images.). It would be too painful if all your tests are update all your golden images.). It would be too painful if all your tests are
like this. Fortunately, you learned about like this. Fortunately, you learned about
[Dependency Injection](http://en.wikipedia.org/wiki/Dependency_injection) and know the right thing [Dependency Injection](https://en.wikipedia.org/wiki/Dependency_injection) and know the right thing
to do: instead of having your application talk to the system API directly, wrap to do: instead of having your application talk to the system API directly, wrap
the API in an interface (say, `Turtle`) and code to that interface: the API in an interface (say, `Turtle`) and code to that interface:
@ -164,7 +164,7 @@ follow:
After the process, you should have something like: After the process, you should have something like:
```cpp ```cpp
#include "gmock/gmock.h" // Brings in gMock. #include <gmock/gmock.h> // Brings in gMock.
class MockTurtle : public Turtle { class MockTurtle : public Turtle {
public: public:
@ -224,8 +224,8 @@ Here's an example:
```cpp ```cpp
#include "path/to/mock-turtle.h" #include "path/to/mock-turtle.h"
#include "gmock/gmock.h" #include <gmock/gmock.h>
#include "gtest/gtest.h" #include <gtest/gtest.h>
using ::testing::AtLeast; // #1 using ::testing::AtLeast; // #1
@ -261,6 +261,8 @@ happen. Therefore it's a good idea to turn on the heap checker in your tests
when you allocate mocks on the heap. You get that automatically if you use the when you allocate mocks on the heap. You get that automatically if you use the
`gtest_main` library already. `gtest_main` library already.
###### Expectation Ordering
**Important note:** gMock requires expectations to be set **before** the mock **Important note:** gMock requires expectations to be set **before** the mock
functions are called, otherwise the behavior is **undefined**. Do not alternate functions are called, otherwise the behavior is **undefined**. Do not alternate
between calls to `EXPECT_CALL()` and calls to the mock functions, and do not set between calls to `EXPECT_CALL()` and calls to the mock functions, and do not set

View File

@ -19,19 +19,15 @@ examples here we assume you want to compile the sample
Using `pkg-config` in CMake is fairly easy: Using `pkg-config` in CMake is fairly easy:
```cmake ```cmake
cmake_minimum_required(VERSION 3.0)
cmake_policy(SET CMP0048 NEW)
project(my_gtest_pkgconfig VERSION 0.0.1 LANGUAGES CXX)
find_package(PkgConfig) find_package(PkgConfig)
pkg_search_module(GTEST REQUIRED gtest_main) pkg_search_module(GTEST REQUIRED gtest_main)
add_executable(testapp samples/sample3_unittest.cc) add_executable(testapp)
target_link_libraries(testapp ${GTEST_LDFLAGS}) target_sources(testapp PRIVATE samples/sample3_unittest.cc)
target_compile_options(testapp PUBLIC ${GTEST_CFLAGS}) target_link_libraries(testapp PRIVATE ${GTEST_LDFLAGS})
target_compile_options(testapp PRIVATE ${GTEST_CFLAGS})
include(CTest) enable_testing()
add_test(first_and_only_test testapp) add_test(first_and_only_test testapp)
``` ```

View File

@ -1,35 +1,8 @@
# Supported Platforms # Supported Platforms
GoogleTest requires a codebase and compiler compliant with the C++11 standard or GoogleTest follows Google's
newer. [Foundational C++ Support Policy](https://opensource.google/documentation/policies/cplusplus-support).
See
The GoogleTest code is officially supported on the following platforms. [this table](https://github.com/google/oss-policies-info/blob/main/foundational-cxx-support-matrix.md)
Operating systems or tools not listed below are community-supported. For for a list of currently supported versions compilers, platforms, and build
community-supported platforms, patches that do not complicate the code may be tools.
considered.
If you notice any problems on your platform, please file an issue on the
[GoogleTest GitHub Issue Tracker](https://github.com/google/googletest/issues).
Pull requests containing fixes are welcome!
### Operating systems
* Linux
* macOS
* Windows
### Compilers
* gcc 5.0+
* clang 5.0+
* MSVC 2015+
**macOS users:** Xcode 9.3+ provides clang 5.0+.
### Build systems
* [Bazel](https://bazel.build/)
* [CMake](https://cmake.org/)
Bazel is the build system used by the team internally and in tests. CMake is
supported on a best-effort basis and by the community.

View File

@ -1,84 +1,84 @@
# Googletest Primer # GoogleTest Primer
## Introduction: Why googletest? ## Introduction: Why GoogleTest?
*googletest* helps you write better C++ tests. *GoogleTest* helps you write better C++ tests.
googletest is a testing framework developed by the Testing Technology team with GoogleTest is a testing framework developed by the Testing Technology team with
Google's specific requirements and constraints in mind. Whether you work on Google's specific requirements and constraints in mind. Whether you work on
Linux, Windows, or a Mac, if you write C++ code, googletest can help you. And it Linux, Windows, or a Mac, if you write C++ code, GoogleTest can help you. And it
supports *any* kind of tests, not just unit tests. supports *any* kind of tests, not just unit tests.
So what makes a good test, and how does googletest fit in? We believe: So what makes a good test, and how does GoogleTest fit in? We believe:
1. Tests should be *independent* and *repeatable*. It's a pain to debug a test 1. Tests should be *independent* and *repeatable*. It's a pain to debug a test
that succeeds or fails as a result of other tests. googletest isolates the that succeeds or fails as a result of other tests. GoogleTest isolates the
tests by running each of them on a different object. When a test fails, tests by running each of them on a different object. When a test fails,
googletest allows you to run it in isolation for quick debugging. GoogleTest allows you to run it in isolation for quick debugging.
2. Tests should be well *organized* and reflect the structure of the tested 2. Tests should be well *organized* and reflect the structure of the tested
code. googletest groups related tests into test suites that can share data code. GoogleTest groups related tests into test suites that can share data
and subroutines. This common pattern is easy to recognize and makes tests and subroutines. This common pattern is easy to recognize and makes tests
easy to maintain. Such consistency is especially helpful when people switch easy to maintain. Such consistency is especially helpful when people switch
projects and start to work on a new code base. projects and start to work on a new code base.
3. Tests should be *portable* and *reusable*. Google has a lot of code that is 3. Tests should be *portable* and *reusable*. Google has a lot of code that is
platform-neutral; its tests should also be platform-neutral. googletest platform-neutral; its tests should also be platform-neutral. GoogleTest
works on different OSes, with different compilers, with or without works on different OSes, with different compilers, with or without
exceptions, so googletest tests can work with a variety of configurations. exceptions, so GoogleTest tests can work with a variety of configurations.
4. When tests fail, they should provide as much *information* about the problem 4. When tests fail, they should provide as much *information* about the problem
as possible. googletest doesn't stop at the first test failure. Instead, it as possible. GoogleTest doesn't stop at the first test failure. Instead, it
only stops the current test and continues with the next. You can also set up only stops the current test and continues with the next. You can also set up
tests that report non-fatal failures after which the current test continues. tests that report non-fatal failures after which the current test continues.
Thus, you can detect and fix multiple bugs in a single run-edit-compile Thus, you can detect and fix multiple bugs in a single run-edit-compile
cycle. cycle.
5. The testing framework should liberate test writers from housekeeping chores 5. The testing framework should liberate test writers from housekeeping chores
and let them focus on the test *content*. googletest automatically keeps and let them focus on the test *content*. GoogleTest automatically keeps
track of all tests defined, and doesn't require the user to enumerate them track of all tests defined, and doesn't require the user to enumerate them
in order to run them. in order to run them.
6. Tests should be *fast*. With googletest, you can reuse shared resources 6. Tests should be *fast*. With GoogleTest, you can reuse shared resources
across tests and pay for the set-up/tear-down only once, without making across tests and pay for the set-up/tear-down only once, without making
tests depend on each other. tests depend on each other.
Since googletest is based on the popular xUnit architecture, you'll feel right Since GoogleTest is based on the popular xUnit architecture, you'll feel right
at home if you've used JUnit or PyUnit before. If not, it will take you about 10 at home if you've used JUnit or PyUnit before. If not, it will take you about 10
minutes to learn the basics and get started. So let's go! minutes to learn the basics and get started. So let's go!
## Beware of the nomenclature ## Beware of the Nomenclature
{: .callout .note} {: .callout .note}
_Note:_ There might be some confusion arising from different definitions of the *Note:* There might be some confusion arising from different definitions of the
terms _Test_, _Test Case_ and _Test Suite_, so beware of misunderstanding these. terms *Test*, *Test Case* and *Test Suite*, so beware of misunderstanding these.
Historically, googletest started to use the term _Test Case_ for grouping Historically, GoogleTest started to use the term *Test Case* for grouping
related tests, whereas current publications, including International Software related tests, whereas current publications, including International Software
Testing Qualifications Board ([ISTQB](http://www.istqb.org/)) materials and Testing Qualifications Board ([ISTQB](https://www.istqb.org/)) materials and
various textbooks on software quality, use the term various textbooks on software quality, use the term
_[Test Suite][istqb test suite]_ for this. *[Test Suite][istqb test suite]* for this.
The related term _Test_, as it is used in googletest, corresponds to the term The related term *Test*, as it is used in GoogleTest, corresponds to the term
_[Test Case][istqb test case]_ of ISTQB and others. *[Test Case][istqb test case]* of ISTQB and others.
The term _Test_ is commonly of broad enough sense, including ISTQB's definition The term *Test* is commonly of broad enough sense, including ISTQB's definition
of _Test Case_, so it's not much of a problem here. But the term _Test Case_ as of *Test Case*, so it's not much of a problem here. But the term *Test Case* as
was used in Google Test is of contradictory sense and thus confusing. was used in Google Test is of contradictory sense and thus confusing.
googletest recently started replacing the term _Test Case_ with _Test Suite_. GoogleTest recently started replacing the term *Test Case* with *Test Suite*.
The preferred API is *TestSuite*. The older TestCase API is being slowly The preferred API is *TestSuite*. The older TestCase API is being slowly
deprecated and refactored away. deprecated and refactored away.
So please be aware of the different definitions of the terms: So please be aware of the different definitions of the terms:
Meaning | googletest Term | [ISTQB](http://www.istqb.org/) Term Meaning | GoogleTest Term | [ISTQB](https://www.istqb.org/) Term
:----------------------------------------------------------------------------------- | :---------------------- | :---------------------------------- :----------------------------------------------------------------------------------- | :---------------------- | :----------------------------------
Exercise a particular program path with specific input values and verify the results | [TEST()](#simple-tests) | [Test Case][istqb test case] Exercise a particular program path with specific input values and verify the results | [TEST()](#simple-tests) | [Test Case][istqb test case]
[istqb test case]: http://glossary.istqb.org/en/search/test%20case [istqb test case]: https://glossary.istqb.org/en_US/term/test-case
[istqb test suite]: http://glossary.istqb.org/en/search/test%20suite [istqb test suite]: https://glossary.istqb.org/en_US/term/test-suite
## Basic Concepts ## Basic Concepts
When using googletest, you start by writing *assertions*, which are statements When using GoogleTest, you start by writing *assertions*, which are statements
that check whether a condition is true. An assertion's result can be *success*, that check whether a condition is true. An assertion's result can be *success*,
*nonfatal failure*, or *fatal failure*. If a fatal failure occurs, it aborts the *nonfatal failure*, or *fatal failure*. If a fatal failure occurs, it aborts the
current function; otherwise the program continues normally. current function; otherwise the program continues normally.
@ -98,11 +98,11 @@ assertion level and building up to tests and test suites.
## Assertions ## Assertions
googletest assertions are macros that resemble function calls. You test a class GoogleTest assertions are macros that resemble function calls. You test a class
or function by making assertions about its behavior. When an assertion fails, or function by making assertions about its behavior. When an assertion fails,
googletest prints the assertion's source file and line number location, along GoogleTest prints the assertion's source file and line number location, along
with a failure message. You may also supply a custom failure message which will with a failure message. You may also supply a custom failure message which will
be appended to googletest's message. be appended to GoogleTest's message.
The assertions come in pairs that test the same thing but have different effects The assertions come in pairs that test the same thing but have different effects
on the current function. `ASSERT_*` versions generate fatal failures when they on the current function. `ASSERT_*` versions generate fatal failures when they
@ -149,7 +149,7 @@ To create a test:
1. Use the `TEST()` macro to define and name a test function. These are 1. Use the `TEST()` macro to define and name a test function. These are
ordinary C++ functions that don't return a value. ordinary C++ functions that don't return a value.
2. In this function, along with any valid C++ statements you want to include, 2. In this function, along with any valid C++ statements you want to include,
use the various googletest assertions to check values. use the various GoogleTest assertions to check values.
3. The test's result is determined by the assertions; if any assertion in the 3. The test's result is determined by the assertions; if any assertion in the
test fails (either fatally or non-fatally), or if the test crashes, the test fails (either fatally or non-fatally), or if the test crashes, the
entire test fails. Otherwise, it succeeds. entire test fails. Otherwise, it succeeds.
@ -190,7 +190,7 @@ TEST(FactorialTest, HandlesPositiveInput) {
} }
``` ```
googletest groups the test results by test suites, so logically related tests GoogleTest groups the test results by test suites, so logically related tests
should be in the same test suite; in other words, the first argument to their should be in the same test suite; in other words, the first argument to their
`TEST()` should be the same. In the above example, we have two tests, `TEST()` should be the same. In the above example, we have two tests,
`HandlesZeroInput` and `HandlesPositiveInput`, that belong to the same test `HandlesZeroInput` and `HandlesPositiveInput`, that belong to the same test
@ -210,7 +210,7 @@ objects for several different tests.
To create a fixture: To create a fixture:
1. Derive a class from `::testing::Test` . Start its body with `protected:`, as 1. Derive a class from `testing::Test` . Start its body with `protected:`, as
we'll want to access fixture members from sub-classes. we'll want to access fixture members from sub-classes.
2. Inside the class, declare any objects you plan to use. 2. Inside the class, declare any objects you plan to use.
3. If necessary, write a default constructor or `SetUp()` function to prepare 3. If necessary, write a default constructor or `SetUp()` function to prepare
@ -227,14 +227,14 @@ When using a fixture, use `TEST_F()` instead of `TEST()` as it allows you to
access objects and subroutines in the test fixture: access objects and subroutines in the test fixture:
```c++ ```c++
TEST_F(TestFixtureName, TestName) { TEST_F(TestFixtureClassName, TestName) {
... test body ... ... test body ...
} }
``` ```
Like `TEST()`, the first argument is the test suite name, but for `TEST_F()` Unlike `TEST()`, in `TEST_F()` the first argument must be the name of the test
this must be the name of the test fixture class. You've probably guessed: `_F` fixture class. (`_F` stands for "Fixture"). No test suite name is specified for
is for fixture. this macro.
Unfortunately, the C++ macro system does not allow us to create a single macro Unfortunately, the C++ macro system does not allow us to create a single macro
that can handle both types of tests. Using the wrong macro causes a compiler that can handle both types of tests. Using the wrong macro causes a compiler
@ -244,12 +244,12 @@ Also, you must first define a test fixture class before using it in a
`TEST_F()`, or you'll get the compiler error "`virtual outside class `TEST_F()`, or you'll get the compiler error "`virtual outside class
declaration`". declaration`".
For each test defined with `TEST_F()`, googletest will create a *fresh* test For each test defined with `TEST_F()`, GoogleTest will create a *fresh* test
fixture at runtime, immediately initialize it via `SetUp()`, run the test, clean fixture at runtime, immediately initialize it via `SetUp()`, run the test, clean
up by calling `TearDown()`, and then delete the test fixture. Note that up by calling `TearDown()`, and then delete the test fixture. Note that
different tests in the same test suite have different test fixture objects, and different tests in the same test suite have different test fixture objects, and
googletest always deletes a test fixture before it creates the next one. GoogleTest always deletes a test fixture before it creates the next one.
googletest does **not** reuse the same test fixture for multiple tests. Any GoogleTest does **not** reuse the same test fixture for multiple tests. Any
changes one test makes to the fixture do not affect other tests. changes one test makes to the fixture do not affect other tests.
As an example, let's write tests for a FIFO queue class named `Queue`, which has As an example, let's write tests for a FIFO queue class named `Queue`, which has
@ -271,16 +271,16 @@ First, define a fixture class. By convention, you should give it the name
`FooTest` where `Foo` is the class being tested. `FooTest` where `Foo` is the class being tested.
```c++ ```c++
class QueueTest : public ::testing::Test { class QueueTest : public testing::Test {
protected: protected:
void SetUp() override { QueueTest() {
// q0_ remains empty // q0_ remains empty
q1_.Enqueue(1); q1_.Enqueue(1);
q2_.Enqueue(2); q2_.Enqueue(2);
q2_.Enqueue(3); q2_.Enqueue(3);
} }
// void TearDown() override {} // ~QueueTest() override = default;
Queue<int> q0_; Queue<int> q0_;
Queue<int> q1_; Queue<int> q1_;
@ -288,8 +288,9 @@ class QueueTest : public ::testing::Test {
}; };
``` ```
In this case, `TearDown()` is not needed since we don't have to clean up after In this case, we don't need to define a destructor or a `TearDown()` method,
each test, other than what's already done by the destructor. because the implicit destructor generated by the compiler will perform all of
the necessary cleanup.
Now we'll write tests using `TEST_F()` and this fixture. Now we'll write tests using `TEST_F()` and this fixture.
@ -325,19 +326,17 @@ would lead to a segfault when `n` is `NULL`.
When these tests run, the following happens: When these tests run, the following happens:
1. googletest constructs a `QueueTest` object (let's call it `t1`). 1. GoogleTest constructs a `QueueTest` object (let's call it `t1`).
2. `t1.SetUp()` initializes `t1`. 2. The first test (`IsEmptyInitially`) runs on `t1`.
3. The first test (`IsEmptyInitially`) runs on `t1`. 3. `t1` is destructed.
4. `t1.TearDown()` cleans up after the test finishes. 4. The above steps are repeated on another `QueueTest` object, this time
5. `t1` is destructed.
6. The above steps are repeated on another `QueueTest` object, this time
running the `DequeueWorks` test. running the `DequeueWorks` test.
**Availability**: Linux, Windows, Mac. **Availability**: Linux, Windows, Mac.
## Invoking the Tests ## Invoking the Tests
`TEST()` and `TEST_F()` implicitly register their tests with googletest. So, `TEST()` and `TEST_F()` implicitly register their tests with GoogleTest. So,
unlike with many other C++ testing frameworks, you don't have to re-list all unlike with many other C++ testing frameworks, you don't have to re-list all
your defined tests in order to run them. your defined tests in order to run them.
@ -348,7 +347,7 @@ test suites, or even different source files.
When invoked, the `RUN_ALL_TESTS()` macro: When invoked, the `RUN_ALL_TESTS()` macro:
* Saves the state of all googletest flags. * Saves the state of all GoogleTest flags.
* Creates a test fixture object for the first test. * Creates a test fixture object for the first test.
@ -360,7 +359,7 @@ When invoked, the `RUN_ALL_TESTS()` macro:
* Deletes the fixture. * Deletes the fixture.
* Restores the state of all googletest flags. * Restores the state of all GoogleTest flags.
* Repeats the above steps for the next test, until all tests have run. * Repeats the above steps for the next test, until all tests have run.
@ -374,14 +373,14 @@ If a fatal failure happens the subsequent steps will be skipped.
> return the value of `RUN_ALL_TESTS()`. > return the value of `RUN_ALL_TESTS()`.
> >
> Also, you should call `RUN_ALL_TESTS()` only **once**. Calling it more than > Also, you should call `RUN_ALL_TESTS()` only **once**. Calling it more than
> once conflicts with some advanced googletest features (e.g., thread-safe > once conflicts with some advanced GoogleTest features (e.g., thread-safe
> [death tests](advanced.md#death-tests)) and thus is not supported. > [death tests](advanced.md#death-tests)) and thus is not supported.
**Availability**: Linux, Windows, Mac. **Availability**: Linux, Windows, Mac.
## Writing the main() Function ## Writing the main() Function
Most users should _not_ need to write their own `main` function and instead link Most users should *not* need to write their own `main` function and instead link
with `gtest_main` (as opposed to with `gtest`), which defines a suitable entry with `gtest_main` (as opposed to with `gtest`), which defines a suitable entry
point. See the end of this section for details. The remainder of this section point. See the end of this section for details. The remainder of this section
should only apply when you need to do something custom before the tests run that should only apply when you need to do something custom before the tests run that
@ -395,14 +394,14 @@ You can start from this boilerplate:
```c++ ```c++
#include "this/package/foo.h" #include "this/package/foo.h"
#include "gtest/gtest.h" #include <gtest/gtest.h>
namespace my { namespace my {
namespace project { namespace project {
namespace { namespace {
// The fixture for testing class Foo. // The fixture for testing class Foo.
class FooTest : public ::testing::Test { class FooTest : public testing::Test {
protected: protected:
// You can remove any or all of the following functions if their bodies would // You can remove any or all of the following functions if their bodies would
// be empty. // be empty.
@ -450,14 +449,14 @@ TEST_F(FooTest, DoesXyz) {
} // namespace my } // namespace my
int main(int argc, char **argv) { int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv); testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS(); return RUN_ALL_TESTS();
} }
``` ```
The `::testing::InitGoogleTest()` function parses the command line for The `testing::InitGoogleTest()` function parses the command line for GoogleTest
googletest flags, and removes all recognized flags. This allows the user to flags, and removes all recognized flags. This allows the user to control a test
control a test program's behavior via various flags, which we'll cover in the program's behavior via various flags, which we'll cover in the
[AdvancedGuide](advanced.md). You **must** call this function before calling [AdvancedGuide](advanced.md). You **must** call this function before calling
`RUN_ALL_TESTS()`, or the flags won't be properly initialized. `RUN_ALL_TESTS()`, or the flags won't be properly initialized.
@ -476,7 +475,7 @@ NOTE: `ParseGUnitFlags()` is deprecated in favor of `InitGoogleTest()`.
* Google Test is designed to be thread-safe. The implementation is thread-safe * Google Test is designed to be thread-safe. The implementation is thread-safe
on systems where the `pthreads` library is available. It is currently on systems where the `pthreads` library is available. It is currently
_unsafe_ to use Google Test assertions from two threads concurrently on *unsafe* to use Google Test assertions from two threads concurrently on
other systems (e.g. Windows). In most tests this is not an issue as usually other systems (e.g. Windows). In most tests this is not an issue as usually
the assertions are done in the main thread. If you want to help, you can the assertions are done in the main thread. If you want to help, you can
volunteer to implement the necessary synchronization primitives in volunteer to implement the necessary synchronization primitives in

View File

@ -9,9 +9,9 @@ we recommend this tutorial as a starting point.
To complete this tutorial, you'll need: To complete this tutorial, you'll need:
* A compatible operating system (e.g. Linux, macOS, Windows). * A compatible operating system (e.g. Linux, macOS, Windows).
* A compatible C++ compiler that supports at least C++14. * A compatible C++ compiler that supports at least C++17.
* [Bazel](https://bazel.build/), the preferred build system used by the * [Bazel](https://bazel.build/) 7.0 or higher, the preferred build system used
GoogleTest team. by the GoogleTest team.
See [Supported Platforms](platforms.md) for more information about platforms See [Supported Platforms](platforms.md) for more information about platforms
compatible with GoogleTest. compatible with GoogleTest.
@ -28,7 +28,7 @@ A
[Bazel workspace](https://docs.bazel.build/versions/main/build-ref.html#workspace) [Bazel workspace](https://docs.bazel.build/versions/main/build-ref.html#workspace)
is a directory on your filesystem that you use to manage source files for the is a directory on your filesystem that you use to manage source files for the
software you want to build. Each workspace directory has a text file named software you want to build. Each workspace directory has a text file named
`WORKSPACE` which may be empty, or may contain references to external `MODULE.bazel` which may be empty, or may contain references to external
dependencies required to build the outputs. dependencies required to build the outputs.
First, create a directory for your workspace: First, create a directory for your workspace:
@ -37,30 +37,20 @@ First, create a directory for your workspace:
$ mkdir my_workspace && cd my_workspace $ mkdir my_workspace && cd my_workspace
``` ```
Next, youll create the `WORKSPACE` file to specify dependencies. A common and Next, youll create the `MODULE.bazel` file to specify dependencies. As of Bazel
recommended way to depend on GoogleTest is to use a 7.0, the recommended way to consume GoogleTest is through the
[Bazel external dependency](https://docs.bazel.build/versions/main/external.html) [Bazel Central Registry](https://registry.bazel.build/modules/googletest). To do
via the this, create a `MODULE.bazel` file in the root directory of your Bazel workspace
[`http_archive` rule](https://docs.bazel.build/versions/main/repo/http.html#http_archive). with the following content:
To do this, in the root directory of your workspace (`my_workspace/`), create a
file named `WORKSPACE` with the following contents:
``` ```
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") # MODULE.bazel
http_archive( # Choose the most recent version available at
name = "com_google_googletest", # https://registry.bazel.build/modules/googletest
urls = ["https://github.com/google/googletest/archive/5ab508a01f9eb089207ee87fd547d290da39d015.zip"], bazel_dep(name = "googletest", version = "1.17.0")
strip_prefix = "googletest-5ab508a01f9eb089207ee87fd547d290da39d015",
)
``` ```
The above configuration declares a dependency on GoogleTest which is downloaded
as a ZIP archive from GitHub. In the above example,
`5ab508a01f9eb089207ee87fd547d290da39d015` is the Git commit hash of the
GoogleTest version to use; we recommend updating the hash often to point to the
latest version. Use a recent hash on the `main` branch.
Now you're ready to build C++ code that uses GoogleTest. Now you're ready to build C++ code that uses GoogleTest.
## Create and run a binary ## Create and run a binary
@ -95,20 +85,30 @@ cc_test(
name = "hello_test", name = "hello_test",
size = "small", size = "small",
srcs = ["hello_test.cc"], srcs = ["hello_test.cc"],
deps = ["@com_google_googletest//:gtest_main"], deps = [
"@googletest//:gtest",
"@googletest//:gtest_main",
],
) )
``` ```
This `cc_test` rule declares the C++ test binary you want to build, and links to This `cc_test` rule declares the C++ test binary you want to build, and links to
GoogleTest (`//:gtest_main`) using the prefix you specified in the `WORKSPACE` the GoogleTest library (`@googletest//:gtest"`) and the GoogleTest `main()`
file (`@com_google_googletest`). For more information about Bazel `BUILD` files, function (`@googletest//:gtest_main`). For more information about Bazel `BUILD`
see the files, see the
[Bazel C++ Tutorial](https://docs.bazel.build/versions/main/tutorial/cpp.html). [Bazel C++ Tutorial](https://docs.bazel.build/versions/main/tutorial/cpp.html).
{: .callout .note}
NOTE: In the example below, we assume Clang or GCC and set `--cxxopt=-std=c++17`
to ensure that GoogleTest is compiled as C++17 instead of the compiler's default
setting. For MSVC, the equivalent would be `--cxxopt=/std:c++17`. See
[Supported Platforms](platforms.md) for more details on supported language
versions.
Now you can build and run your test: Now you can build and run your test:
<pre> <pre>
<strong>my_workspace$ bazel test --test_output=all //:hello_test</strong> <strong>$ bazel test --cxxopt=-std=c++17 --test_output=all //:hello_test</strong>
INFO: Analyzed target //:hello_test (26 packages loaded, 362 targets configured). INFO: Analyzed target //:hello_test (26 packages loaded, 362 targets configured).
INFO: Found 1 test target... INFO: Found 1 test target...
INFO: From Testing //:hello_test: INFO: From Testing //:hello_test:

View File

@ -10,7 +10,7 @@ this tutorial as a starting point. If your project uses Bazel, see the
To complete this tutorial, you'll need: To complete this tutorial, you'll need:
* A compatible operating system (e.g. Linux, macOS, Windows). * A compatible operating system (e.g. Linux, macOS, Windows).
* A compatible C++ compiler that supports at least C++14. * A compatible C++ compiler that supports at least C++17.
* [CMake](https://cmake.org/) and a compatible build tool for building the * [CMake](https://cmake.org/) and a compatible build tool for building the
project. project.
* Compatible build tools include * Compatible build tools include
@ -52,8 +52,9 @@ To do this, in your project directory (`my_project`), create a file named
cmake_minimum_required(VERSION 3.14) cmake_minimum_required(VERSION 3.14)
project(my_project) project(my_project)
# GoogleTest requires at least C++14 # GoogleTest requires at least C++17
set(CMAKE_CXX_STANDARD 14) set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
include(FetchContent) include(FetchContent)
FetchContent_Declare( FetchContent_Declare(

View File

@ -24,7 +24,8 @@ provided by GoogleTest. All actions are defined in the `::testing` namespace.
| :--------------------------------- | :-------------------------------------- | | :--------------------------------- | :-------------------------------------- |
| `Assign(&variable, value)` | Assign `value` to variable. | | `Assign(&variable, value)` | Assign `value` to variable. |
| `DeleteArg<N>()` | Delete the `N`-th (0-based) argument, which must be a pointer. | | `DeleteArg<N>()` | Delete the `N`-th (0-based) argument, which must be a pointer. |
| `SaveArg<N>(pointer)` | Save the `N`-th (0-based) argument to `*pointer`. | | `SaveArg<N>(pointer)` | Save the `N`-th (0-based) argument to `*pointer` by copy-assignment. |
| `SaveArgByMove<N>(pointer)` | Save the `N`-th (0-based) argument to `*pointer` by move-assignment. |
| `SaveArgPointee<N>(pointer)` | Save the value pointed to by the `N`-th (0-based) argument to `*pointer`. | | `SaveArgPointee<N>(pointer)` | Save the value pointed to by the `N`-th (0-based) argument to `*pointer`. |
| `SetArgReferee<N>(value)` | Assign `value` to the variable referenced by the `N`-th (0-based) argument. | | `SetArgReferee<N>(value)` | Assign `value` to the variable referenced by the `N`-th (0-based) argument. |
| `SetArgPointee<N>(value)` | Assign `value` to the variable pointed by the `N`-th (0-based) argument. | | `SetArgPointee<N>(value)` | Assign `value` to the variable pointed by the `N`-th (0-based) argument. |
@ -47,8 +48,8 @@ functor, or lambda.
| `InvokeWithoutArgs(object_pointer, &class::method)` | Invoke the method on the object, which takes no arguments. | | `InvokeWithoutArgs(object_pointer, &class::method)` | Invoke the method on the object, which takes no arguments. |
| `InvokeArgument<N>(arg1, arg2, ..., argk)` | Invoke the mock function's `N`-th (0-based) argument, which must be a function or a functor, with the `k` arguments. | | `InvokeArgument<N>(arg1, arg2, ..., argk)` | Invoke the mock function's `N`-th (0-based) argument, which must be a function or a functor, with the `k` arguments. |
The return value of the invoked function is used as the return value of the The return value of the invoked function (except `InvokeArgument`) is used as
action. the return value of the action.
When defining a callable to be used with `Invoke*()`, you can declare any unused When defining a callable to be used with `Invoke*()`, you can declare any unused
parameters as `Unused`: parameters as `Unused`:

View File

@ -1,7 +1,7 @@
# Assertions Reference # Assertions Reference
This page lists the assertion macros provided by GoogleTest for verifying code This page lists the assertion macros provided by GoogleTest for verifying code
behavior. To use them, include the header `gtest/gtest.h`. behavior. To use them, add `#include <gtest/gtest.h>`.
The majority of the macros listed below come as a pair with an `EXPECT_` variant The majority of the macros listed below come as a pair with an `EXPECT_` variant
and an `ASSERT_` variant. Upon failure, `EXPECT_` macros generate nonfatal and an `ASSERT_` variant. Upon failure, `EXPECT_` macros generate nonfatal
@ -88,7 +88,7 @@ For example, the following code verifies that the string `value1` starts with
10: 10:
```cpp ```cpp
#include "gmock/gmock.h" #include <gmock/gmock.h>
using ::testing::AllOf; using ::testing::AllOf;
using ::testing::Gt; using ::testing::Gt;
@ -276,7 +276,8 @@ Units in the Last Place (ULPs). To learn more about ULPs, see the article
`ASSERT_FLOAT_EQ(`*`val1`*`,`*`val2`*`)` `ASSERT_FLOAT_EQ(`*`val1`*`,`*`val2`*`)`
Verifies that the two `float` values *`val1`* and *`val2`* are approximately Verifies that the two `float` values *`val1`* and *`val2`* are approximately
equal, to within 4 ULPs from each other. equal, to within 4 ULPs from each other. Infinity and the largest finite float
value are considered to be one ULP apart.
### EXPECT_DOUBLE_EQ {#EXPECT_DOUBLE_EQ} ### EXPECT_DOUBLE_EQ {#EXPECT_DOUBLE_EQ}
@ -284,7 +285,8 @@ equal, to within 4 ULPs from each other.
`ASSERT_DOUBLE_EQ(`*`val1`*`,`*`val2`*`)` `ASSERT_DOUBLE_EQ(`*`val1`*`,`*`val2`*`)`
Verifies that the two `double` values *`val1`* and *`val2`* are approximately Verifies that the two `double` values *`val1`* and *`val2`* are approximately
equal, to within 4 ULPs from each other. equal, to within 4 ULPs from each other. Infinity and the largest finite double
value are considered to be one ULP apart.
### EXPECT_NEAR {#EXPECT_NEAR} ### EXPECT_NEAR {#EXPECT_NEAR}
@ -294,6 +296,11 @@ equal, to within 4 ULPs from each other.
Verifies that the difference between *`val1`* and *`val2`* does not exceed the Verifies that the difference between *`val1`* and *`val2`* does not exceed the
absolute error bound *`abs_error`*. absolute error bound *`abs_error`*.
If *`val`* and *`val2`* are both infinity of the same sign, the difference is
considered to be 0. Otherwise, if either value is infinity, the difference is
considered to be infinity. All non-NaN values (including infinity) are
considered to not exceed an *`abs_error`* of infinity.
## Exception Assertions {#exceptions} ## Exception Assertions {#exceptions}
The following assertions verify that a piece of code throws, or does not throw, The following assertions verify that a piece of code throws, or does not throw,
@ -515,7 +522,7 @@ Verifies that *`expression`* is a success `HRESULT`.
### EXPECT_HRESULT_FAILED {#EXPECT_HRESULT_FAILED} ### EXPECT_HRESULT_FAILED {#EXPECT_HRESULT_FAILED}
`EXPECT_HRESULT_FAILED(`*`expression`*`)` \ `EXPECT_HRESULT_FAILED(`*`expression`*`)` \
`EXPECT_HRESULT_FAILED(`*`expression`*`)` `ASSERT_HRESULT_FAILED(`*`expression`*`)`
Verifies that *`expression`* is a failure `HRESULT`. Verifies that *`expression`* is a failure `HRESULT`.

View File

@ -42,6 +42,8 @@ Matcher | Description
| `Lt(value)` | `argument < value` | | `Lt(value)` | `argument < value` |
| `Ne(value)` | `argument != value` | | `Ne(value)` | `argument != value` |
| `IsFalse()` | `argument` evaluates to `false` in a Boolean context. | | `IsFalse()` | `argument` evaluates to `false` in a Boolean context. |
| `DistanceFrom(target, m)` | The distance between `argument` and `target` (computed by `abs(argument - target)`) matches `m`. |
| `DistanceFrom(target, get_distance, m)` | The distance between `argument` and `target` (computed by `get_distance(argument, target)`) matches `m`. |
| `IsTrue()` | `argument` evaluates to `true` in a Boolean context. | | `IsTrue()` | `argument` evaluates to `true` in a Boolean context. |
| `IsNull()` | `argument` is a `NULL` pointer (raw or smart). | | `IsNull()` | `argument` is a `NULL` pointer (raw or smart). |
| `NotNull()` | `argument` is a non-null pointer (raw or smart). | | `NotNull()` | `argument` is a non-null pointer (raw or smart). |
@ -102,13 +104,40 @@ The `argument` can be either a C string or a C++ string object:
| `StrCaseNe(string)` | `argument` is not equal to `string`, ignoring case. | | `StrCaseNe(string)` | `argument` is not equal to `string`, ignoring case. |
| `StrEq(string)` | `argument` is equal to `string`. | | `StrEq(string)` | `argument` is equal to `string`. |
| `StrNe(string)` | `argument` is not equal to `string`. | | `StrNe(string)` | `argument` is not equal to `string`. |
| `WhenBase64Unescaped(m)` | `argument` is a base-64 escaped string whose unescaped string matches `m`. | | `WhenBase64Unescaped(m)` | `argument` is a base-64 escaped string whose unescaped string matches `m`. The web-safe format from [RFC 4648](https://www.rfc-editor.org/rfc/rfc4648#section-5) is supported. |
`ContainsRegex()` and `MatchesRegex()` take ownership of the `RE` object. They `ContainsRegex()` and `MatchesRegex()` take ownership of the `RE` object. They
use the regular expression syntax defined use the regular expression syntax defined
[here](../advanced.md#regular-expression-syntax). All of these matchers, except [here](../advanced.md#regular-expression-syntax). All of these matchers, except
`ContainsRegex()` and `MatchesRegex()` work for wide strings as well. `ContainsRegex()` and `MatchesRegex()` work for wide strings as well.
## Exception Matchers
| Matcher | Description |
| :---------------------------------------- | :------------------------------- |
| `Throws<E>()` | The `argument` is a callable object that, when called, throws an exception of the expected type `E`. |
| `Throws<E>(m)` | The `argument` is a callable object that, when called, throws an exception of type `E` that satisfies the matcher `m`. |
| `ThrowsMessage<E>(m)` | The `argument` is a callable object that, when called, throws an exception of type `E` with a message that satisfies the matcher `m`. |
Examples:
```cpp
auto argument = [] { throw std::runtime_error("error msg"); };
// Checks if the lambda throws a `std::runtime_error`.
EXPECT_THAT(argument, Throws<std::runtime_error>());
// Checks if the lambda throws a `std::runtime_error` with a specific message
// that matches "error msg".
EXPECT_THAT(argument,
Throws<std::runtime_error>(Property(&std::runtime_error::what,
Eq("error msg"))));
// Checks if the lambda throws a `std::runtime_error` with a message that
// contains "msg".
EXPECT_THAT(argument, ThrowsMessage<std::runtime_error>(HasSubstr("msg")));
```
## Container Matchers ## Container Matchers
Most STL-style containers support `==`, so you can use `Eq(expected_container)` Most STL-style containers support `==`, so you can use `Eq(expected_container)`
@ -171,6 +200,11 @@ messages, you can use:
| `Property(&class::property, m)` | `argument.property()` (or `argument->property()` when `argument` is a plain pointer) matches matcher `m`, where `argument` is an object of type _class_. The method `property()` must take no argument and be declared as `const`. | | `Property(&class::property, m)` | `argument.property()` (or `argument->property()` when `argument` is a plain pointer) matches matcher `m`, where `argument` is an object of type _class_. The method `property()` must take no argument and be declared as `const`. |
| `Property(property_name, &class::property, m)` | The same as the two-parameter version, but provides a better error message. | `Property(property_name, &class::property, m)` | The same as the two-parameter version, but provides a better error message.
{: .callout .warning}
Warning: Don't use `Property()` against member functions that you do not own,
because taking addresses of functions is fragile and generally not part of the
contract of the function.
**Notes:** **Notes:**
* You can use `FieldsAre()` to match any type that supports structured * You can use `FieldsAre()` to match any type that supports structured
@ -189,10 +223,6 @@ messages, you can use:
EXPECT_THAT(s, FieldsAre(42, "aloha")); EXPECT_THAT(s, FieldsAre(42, "aloha"));
``` ```
* Don't use `Property()` against member functions that you do not own, because
taking addresses of functions is fragile and generally not part of the
contract of the function.
## Matching the Result of a Function, Functor, or Callback ## Matching the Result of a Function, Functor, or Callback
| Matcher | Description | | Matcher | Description |
@ -288,3 +318,15 @@ which must be a permanent callback.
return ExplainMatchResult(matcher, arg.nested().property(), result_listener); return ExplainMatchResult(matcher, arg.nested().property(), result_listener);
} }
``` ```
5. You can use `DescribeMatcher<>` to describe another matcher. For example:
```cpp
MATCHER_P(XAndYThat, matcher,
"X that " + DescribeMatcher<int>(matcher, negation) +
(negation ? " or" : " and") + " Y that " +
DescribeMatcher<double>(matcher, negation)) {
return ExplainMatchResult(matcher, arg.x(), result_listener) &&
ExplainMatchResult(matcher, arg.y(), result_listener);
}
```

View File

@ -1,8 +1,7 @@
# Mocking Reference # Mocking Reference
This page lists the facilities provided by GoogleTest for creating and working This page lists the facilities provided by GoogleTest for creating and working
with mock objects. To use them, include the header with mock objects. To use them, add `#include <gmock/gmock.h>`.
`gmock/gmock.h`.
## Macros {#macros} ## Macros {#macros}

View File

@ -3,7 +3,7 @@
<!--* toc_depth: 3 *--> <!--* toc_depth: 3 *-->
This page lists the facilities provided by GoogleTest for writing test programs. This page lists the facilities provided by GoogleTest for writing test programs.
To use them, include the header `gtest/gtest.h`. To use them, add `#include <gtest/gtest.h>`.
## Macros ## Macros
@ -94,7 +94,8 @@ Instantiates the value-parameterized test suite *`TestSuiteName`* (defined with
The argument *`InstantiationName`* is a unique name for the instantiation of the The argument *`InstantiationName`* is a unique name for the instantiation of the
test suite, to distinguish between multiple instantiations. In test output, the test suite, to distinguish between multiple instantiations. In test output, the
instantiation name is added as a prefix to the test suite name instantiation name is added as a prefix to the test suite name
*`TestSuiteName`*. *`TestSuiteName`*. If *`InstantiationName`* is empty
(`INSTANTIATE_TEST_SUITE_P(, ...)`), no prefix is added.
The argument *`param_generator`* is one of the following GoogleTest-provided The argument *`param_generator`* is one of the following GoogleTest-provided
functions that generate the test parameters, all defined in the `::testing` functions that generate the test parameters, all defined in the `::testing`
@ -109,7 +110,8 @@ namespace:
| `ValuesIn(container)` or `ValuesIn(begin,end)` | Yields values from a C-style array, an STL-style container, or an iterator range `[begin, end)`. | | `ValuesIn(container)` or `ValuesIn(begin,end)` | Yields values from a C-style array, an STL-style container, or an iterator range `[begin, end)`. |
| `Bool()` | Yields sequence `{false, true}`. | | `Bool()` | Yields sequence `{false, true}`. |
| `Combine(g1, g2, ..., gN)` | Yields as `std::tuple` *n*-tuples all combinations (Cartesian product) of the values generated by the given *n* generators `g1`, `g2`, ..., `gN`. | | `Combine(g1, g2, ..., gN)` | Yields as `std::tuple` *n*-tuples all combinations (Cartesian product) of the values generated by the given *n* generators `g1`, `g2`, ..., `gN`. |
| `ConvertGenerator<T>(g)` | Yields values generated by generator `g`, `static_cast` to `T`. | | `ConvertGenerator<T>(g)` or `ConvertGenerator(g, func)` | Yields values generated by generator `g`, `static_cast` from `T`. (Note: `T` might not be what you expect. See [*Using ConvertGenerator*](#using-convertgenerator) below.) The second overload uses `func` to perform the conversion. |
The optional last argument *`name_generator`* is a function or functor that The optional last argument *`name_generator`* is a function or functor that
generates custom test name suffixes based on the test parameters. The function generates custom test name suffixes based on the test parameters. The function
must accept an argument of type must accept an argument of type
@ -121,8 +123,8 @@ custom function can be used for more control:
```cpp ```cpp
INSTANTIATE_TEST_SUITE_P( INSTANTIATE_TEST_SUITE_P(
MyInstantiation, MyTestSuite, MyInstantiation, MyTestSuite,
::testing::Values(...), testing::Values(...),
[](const ::testing::TestParamInfo<MyTestSuite::ParamType>& info) { [](const testing::TestParamInfo<MyTestSuite::ParamType>& info) {
// Can use info.param here to generate the test suffix // Can use info.param here to generate the test suffix
std::string name = ... std::string name = ...
return name; return name;
@ -135,9 +137,107 @@ For more information, see
See also See also
[`GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST`](#GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST). [`GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST`](#GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST).
###### Using `ConvertGenerator`
The functions listed in the table above appear to return generators that create
values of the desired types, but this is not generally the case. Rather, they
typically return factory objects that convert to the the desired generators.
This affords some flexibility in allowing you to specify values of types that
are different from, yet implicitly convertible to, the actual parameter type
required by your fixture class.
For example, you can do the following with a fixture that requires an `int`
parameter:
```cpp
INSTANTIATE_TEST_SUITE_P(MyInstantiation, MyTestSuite,
testing::Values(1, 1.2)); // Yes, Values() supports heterogeneous argument types.
```
It might seem obvious that `1.2` &mdash; a `double` &mdash; will be converted to
an `int` but in actuality it requires some template gymnastics involving the
indirection described in the previous paragraph.
What if your parameter type is not implicitly convertible from the generated
type but is *explicitly* convertible? There will be no automatic conversion, but
you can force it by applying `ConvertGenerator<T>`. The compiler can
automatically deduce the target type (your fixture's parameter type), but
because of the aforementioned indirection it cannot decide what the generated
type should be. You need to tell it, by providing the type `T` explicitly. Thus
`T` should not be your fixture's parameter type, but rather an intermediate type
that is supported by the factory object, and which can be `static_cast` to the
fixture's parameter type:
```cpp
// The fixture's parameter type.
class MyParam {
public:
// Explicit converting ctor.
explicit MyParam(const std::tuple<int, bool>& t);
...
};
INSTANTIATE_TEST_SUITE_P(MyInstantiation, MyTestSuite,
ConvertGenerator<std::tuple<int, bool>>(Combine(Values(0.1, 1.2), Bool())));
```
In this example `Combine` supports the generation of `std::tuple<int, bool>>`
objects (even though the provided values for the first tuple element are
`double`s) and those `tuple`s get converted into `MyParam` objects by virtue of
the call to `ConvertGenerator`.
For parameter types that are not convertible from the generated types you can
provide a callable that does the conversion. The callable accepts an object of
the generated type and returns an object of the fixture's parameter type. The
generated type can often be deduced by the compiler from the callable's call
signature so you do not usually need specify it explicitly (but see a caveat
below).
```cpp
// The fixture's parameter type.
class MyParam {
public:
MyParam(int, bool);
...
};
INSTANTIATE_TEST_SUITE_P(MyInstantiation, MyTestSuite,
ConvertGenerator(Combine(Values(1, 1.2), Bool()),
[](const std::tuple<int, bool>& t){
const auto [i, b] = t;
return MyParam(i, b);
}));
```
The callable may be anything that can be used to initialize a `std::function`
with the appropriate call signature. Note the callable's return object gets
`static_cast` to the fixture's parameter type, so it does not have to be of that
exact type, only convertible to it.
**Caveat:** Consider the following example.
```cpp
INSTANTIATE_TEST_SUITE_P(MyInstantiation, MyTestSuite,
ConvertGenerator(Values(std::string("s")), [](std::string_view s) { ... }));
```
The `string` argument gets copied into the factory object returned by `Values`.
Then, because the generated type deduced from the lambda is `string_view`, the
factory object spawns a generator that holds a `string_view` referencing that
`string`. Unfortunately, by the time this generator gets invoked, the factory
object is gone and the `string_view` is dangling.
To overcome this problem you can specify the generated type explicitly:
`ConvertGenerator<std::string>(Values(std::string("s")), [](std::string_view s)
{ ... })`. Alternatively, you can change the lambda's signature to take a
`std::string` or a `const std::string&` (the latter will not leave you with a
dangling reference because the type deduction strips off the reference and the
`const`).
### TYPED_TEST_SUITE {#TYPED_TEST_SUITE} ### TYPED_TEST_SUITE {#TYPED_TEST_SUITE}
`TYPED_TEST_SUITE(`*`TestFixtureName`*`,`*`Types`*`)` `TYPED_TEST_SUITE(`*`TestFixtureName`*`,`*`Types`*`)`
`TYPED_TEST_SUITE(`*`TestFixtureName`*`,`*`Types`*`,`*`NameGenerator`*`)`
Defines a typed test suite based on the test fixture *`TestFixtureName`*. The Defines a typed test suite based on the test fixture *`TestFixtureName`*. The
test suite name is *`TestFixtureName`*. test suite name is *`TestFixtureName`*.
@ -147,7 +247,7 @@ type, for example:
```cpp ```cpp
template <typename T> template <typename T>
class MyFixture : public ::testing::Test { class MyFixture : public testing::Test {
public: public:
... ...
using List = std::list<T>; using List = std::list<T>;
@ -167,6 +267,22 @@ TYPED_TEST_SUITE(MyFixture, MyTypes);
The type alias (`using` or `typedef`) is necessary for the `TYPED_TEST_SUITE` The type alias (`using` or `typedef`) is necessary for the `TYPED_TEST_SUITE`
macro to parse correctly. macro to parse correctly.
The optional third argument *`NameGenerator`* allows specifying a class that
exposes a templated static function `GetName(int)`. For example:
```cpp
class NameGenerator {
public:
template <typename T>
static std::string GetName(int) {
if constexpr (std::is_same_v<T, char>) return "char";
if constexpr (std::is_same_v<T, int>) return "int";
if constexpr (std::is_same_v<T, unsigned int>) return "unsignedInt";
}
};
TYPED_TEST_SUITE(MyFixture, MyTypes, NameGenerator);
```
See also [`TYPED_TEST`](#TYPED_TEST) and See also [`TYPED_TEST`](#TYPED_TEST) and
[Typed Tests](../advanced.md#typed-tests) for more information. [Typed Tests](../advanced.md#typed-tests) for more information.
@ -276,7 +392,8 @@ must be registered with
The argument *`InstantiationName`* is a unique name for the instantiation of the The argument *`InstantiationName`* is a unique name for the instantiation of the
test suite, to distinguish between multiple instantiations. In test output, the test suite, to distinguish between multiple instantiations. In test output, the
instantiation name is added as a prefix to the test suite name instantiation name is added as a prefix to the test suite name
*`TestSuiteName`*. *`TestSuiteName`*. If *`InstantiationName`* is empty
(`INSTANTIATE_TYPED_TEST_SUITE_P(, ...)`), no prefix is added.
The argument *`Types`* is a [`Types`](#Types) object representing the list of The argument *`Types`* is a [`Types`](#Types) object representing the list of
types to run the tests on, for example: types to run the tests on, for example:
@ -323,7 +440,7 @@ Then the test code should look like:
```cpp ```cpp
namespace my_namespace { namespace my_namespace {
class MyClassTest : public ::testing::Test { class MyClassTest : public testing::Test {
... ...
}; };
@ -386,7 +503,7 @@ GoogleTest defines the following classes and types to help with writing tests.
### AssertionResult {#AssertionResult} ### AssertionResult {#AssertionResult}
`::testing::AssertionResult` `testing::AssertionResult`
A class for indicating whether an assertion was successful. A class for indicating whether an assertion was successful.
@ -400,14 +517,14 @@ To create an instance of this class, use one of the factory functions
### AssertionException {#AssertionException} ### AssertionException {#AssertionException}
`::testing::AssertionException` `testing::AssertionException`
Exception which can be thrown from Exception which can be thrown from
[`TestEventListener::OnTestPartResult`](#TestEventListener::OnTestPartResult). [`TestEventListener::OnTestPartResult`](#TestEventListener::OnTestPartResult).
### EmptyTestEventListener {#EmptyTestEventListener} ### EmptyTestEventListener {#EmptyTestEventListener}
`::testing::EmptyTestEventListener` `testing::EmptyTestEventListener`
Provides an empty implementation of all methods in the Provides an empty implementation of all methods in the
[`TestEventListener`](#TestEventListener) interface, such that a subclass only [`TestEventListener`](#TestEventListener) interface, such that a subclass only
@ -415,7 +532,7 @@ needs to override the methods it cares about.
### Environment {#Environment} ### Environment {#Environment}
`::testing::Environment` `testing::Environment`
Represents a global test environment. See Represents a global test environment. See
[Global Set-Up and Tear-Down](../advanced.md#global-set-up-and-tear-down). [Global Set-Up and Tear-Down](../advanced.md#global-set-up-and-tear-down).
@ -436,7 +553,7 @@ Override this to define how to tear down the environment.
### ScopedTrace {#ScopedTrace} ### ScopedTrace {#ScopedTrace}
`::testing::ScopedTrace` `testing::ScopedTrace`
An instance of this class causes a trace to be included in every test failure An instance of this class causes a trace to be included in every test failure
message generated by code in the scope of the lifetime of the `ScopedTrace` message generated by code in the scope of the lifetime of the `ScopedTrace`
@ -452,7 +569,7 @@ ScopedTrace(const char* file, int line, const T& message)
Example usage: Example usage:
```cpp ```cpp
::testing::ScopedTrace trace("file.cc", 123, "message"); testing::ScopedTrace trace("file.cc", 123, "message");
``` ```
The resulting trace includes the given source file path and line number, and the The resulting trace includes the given source file path and line number, and the
@ -463,7 +580,7 @@ See also [`SCOPED_TRACE`](#SCOPED_TRACE).
### Test {#Test} ### Test {#Test}
`::testing::Test` `testing::Test`
The abstract class that all tests inherit from. `Test` is not copyable. The abstract class that all tests inherit from. `Test` is not copyable.
@ -551,7 +668,7 @@ after running each individual test.
### TestWithParam {#TestWithParam} ### TestWithParam {#TestWithParam}
`::testing::TestWithParam<T>` `testing::TestWithParam<T>`
A convenience class which inherits from both [`Test`](#Test) and A convenience class which inherits from both [`Test`](#Test) and
[`WithParamInterface<T>`](#WithParamInterface). [`WithParamInterface<T>`](#WithParamInterface).
@ -671,7 +788,7 @@ during execution of `SetUpTestSuite` and `TearDownTestSuite`.
### TestInfo {#TestInfo} ### TestInfo {#TestInfo}
`::testing::TestInfo` `testing::TestInfo`
Stores information about a test. Stores information about a test.
@ -750,7 +867,7 @@ Returns the result of the test. See [`TestResult`](#TestResult).
### TestParamInfo {#TestParamInfo} ### TestParamInfo {#TestParamInfo}
`::testing::TestParamInfo<T>` `testing::TestParamInfo<T>`
Describes a parameter to a value-parameterized test. The type `T` is the type of Describes a parameter to a value-parameterized test. The type `T` is the type of
the parameter. the parameter.
@ -760,7 +877,7 @@ and its integer index respectively.
### UnitTest {#UnitTest} ### UnitTest {#UnitTest}
`::testing::UnitTest` `testing::UnitTest`
This class contains information about the test program. This class contains information about the test program.
@ -928,7 +1045,7 @@ GoogleTest. See [`TestEventListeners`](#TestEventListeners).
### TestEventListener {#TestEventListener} ### TestEventListener {#TestEventListener}
`::testing::TestEventListener` `testing::TestEventListener`
The interface for tracing execution of tests. The methods below are listed in The interface for tracing execution of tests. The methods below are listed in
the order the corresponding events are fired. the order the corresponding events are fired.
@ -1026,7 +1143,7 @@ Fired after all test activities have ended.
### TestEventListeners {#TestEventListeners} ### TestEventListeners {#TestEventListeners}
`::testing::TestEventListeners` `testing::TestEventListeners`
Lets users add listeners to track events in GoogleTest. Lets users add listeners to track events in GoogleTest.
@ -1071,7 +1188,7 @@ the caller and makes this function return `NULL` the next time.
### TestPartResult {#TestPartResult} ### TestPartResult {#TestPartResult}
`::testing::TestPartResult` `testing::TestPartResult`
A copyable object representing the result of a test part (i.e. an assertion or A copyable object representing the result of a test part (i.e. an assertion or
an explicit `FAIL()`, `ADD_FAILURE()`, or `SUCCESS()`). an explicit `FAIL()`, `ADD_FAILURE()`, or `SUCCESS()`).
@ -1153,7 +1270,7 @@ Returns true if and only if the test part failed.
### TestProperty {#TestProperty} ### TestProperty {#TestProperty}
`::testing::TestProperty` `testing::TestProperty`
A copyable object representing a user-specified test property which can be A copyable object representing a user-specified test property which can be
output as a key/value string pair. output as a key/value string pair.
@ -1180,7 +1297,7 @@ Sets a new value, overriding the previous one.
### TestResult {#TestResult} ### TestResult {#TestResult}
`::testing::TestResult` `testing::TestResult`
Contains information about the result of a single test. Contains information about the result of a single test.
@ -1261,20 +1378,20 @@ range, aborts the program.
### TimeInMillis {#TimeInMillis} ### TimeInMillis {#TimeInMillis}
`::testing::TimeInMillis` `testing::TimeInMillis`
An integer type representing time in milliseconds. An integer type representing time in milliseconds.
### Types {#Types} ### Types {#Types}
`::testing::Types<T...>` `testing::Types<T...>`
Represents a list of types for use in typed tests and type-parameterized tests. Represents a list of types for use in typed tests and type-parameterized tests.
The template argument `T...` can be any number of types, for example: The template argument `T...` can be any number of types, for example:
``` ```
::testing::Types<char, int, unsigned int> testing::Types<char, int, unsigned int>
``` ```
See [Typed Tests](../advanced.md#typed-tests) and See [Typed Tests](../advanced.md#typed-tests) and
@ -1283,7 +1400,7 @@ information.
### WithParamInterface {#WithParamInterface} ### WithParamInterface {#WithParamInterface}
`::testing::WithParamInterface<T>` `testing::WithParamInterface<T>`
The pure interface class that all value-parameterized tests inherit from. The pure interface class that all value-parameterized tests inherit from.
@ -1309,14 +1426,16 @@ tests.
### InitGoogleTest {#InitGoogleTest} ### InitGoogleTest {#InitGoogleTest}
`void ::testing::InitGoogleTest(int* argc, char** argv)` \ `void testing::InitGoogleTest(int* argc, char** argv)` \
`void ::testing::InitGoogleTest(int* argc, wchar_t** argv)` \ `void testing::InitGoogleTest(int* argc, wchar_t** argv)` \
`void ::testing::InitGoogleTest()` `void testing::InitGoogleTest()`
Initializes GoogleTest. This must be called before calling Initializes GoogleTest. This must be called before calling
[`RUN_ALL_TESTS()`](#RUN_ALL_TESTS). In particular, it parses the command line [`RUN_ALL_TESTS()`](#RUN_ALL_TESTS). In particular, it parses the command line
for the flags that GoogleTest recognizes. Whenever a GoogleTest flag is seen, it for the flags that GoogleTest recognizes. Whenever a GoogleTest flag is seen, it
is removed from `argv`, and `*argc` is decremented. is removed from `argv`, and `*argc` is decremented. Keep in mind that `argv`
must terminate with a `NULL` pointer (i.e. `argv[argc]` is `NULL`), which is
already the case with the default `argv` passed to `main`.
No value is returned. Instead, the GoogleTest flag variables are updated. No value is returned. Instead, the GoogleTest flag variables are updated.
@ -1328,7 +1447,7 @@ platforms where there is no `argc`/`argv`.
### AddGlobalTestEnvironment {#AddGlobalTestEnvironment} ### AddGlobalTestEnvironment {#AddGlobalTestEnvironment}
`Environment* ::testing::AddGlobalTestEnvironment(Environment* env)` `Environment* testing::AddGlobalTestEnvironment(Environment* env)`
Adds a test environment to the test program. Must be called before Adds a test environment to the test program. Must be called before
[`RUN_ALL_TESTS()`](#RUN_ALL_TESTS) is called. See [`RUN_ALL_TESTS()`](#RUN_ALL_TESTS) is called. See
@ -1341,7 +1460,7 @@ See also [`Environment`](#Environment).
```cpp ```cpp
template <typename Factory> template <typename Factory>
TestInfo* ::testing::RegisterTest(const char* test_suite_name, const char* test_name, TestInfo* testing::RegisterTest(const char* test_suite_name, const char* test_name,
const char* type_param, const char* value_param, const char* type_param, const char* value_param,
const char* file, int line, Factory factory) const char* file, int line, Factory factory)
``` ```
@ -1380,27 +1499,27 @@ an all-caps name.
### AssertionSuccess {#AssertionSuccess} ### AssertionSuccess {#AssertionSuccess}
`AssertionResult ::testing::AssertionSuccess()` `AssertionResult testing::AssertionSuccess()`
Creates a successful assertion result. See Creates a successful assertion result. See
[`AssertionResult`](#AssertionResult). [`AssertionResult`](#AssertionResult).
### AssertionFailure {#AssertionFailure} ### AssertionFailure {#AssertionFailure}
`AssertionResult ::testing::AssertionFailure()` `AssertionResult testing::AssertionFailure()`
Creates a failed assertion result. Use the `<<` operator to store a failure Creates a failed assertion result. Use the `<<` operator to store a failure
message: message:
```cpp ```cpp
::testing::AssertionFailure() << "My failure message"; testing::AssertionFailure() << "My failure message";
``` ```
See [`AssertionResult`](#AssertionResult). See [`AssertionResult`](#AssertionResult).
### StaticAssertTypeEq {#StaticAssertTypeEq} ### StaticAssertTypeEq {#StaticAssertTypeEq}
`::testing::StaticAssertTypeEq<T1, T2>()` `testing::StaticAssertTypeEq<T1, T2>()`
Compile-time assertion for type equality. Compiles if and only if `T1` and `T2` Compile-time assertion for type equality. Compiles if and only if `T1` and `T2`
are the same type. The value it returns is irrelevant. are the same type. The value it returns is irrelevant.
@ -1409,7 +1528,7 @@ See [Type Assertions](../advanced.md#type-assertions) for more information.
### PrintToString {#PrintToString} ### PrintToString {#PrintToString}
`std::string ::testing::PrintToString(x)` `std::string testing::PrintToString(x)`
Prints any value `x` using GoogleTest's value printer. Prints any value `x` using GoogleTest's value printer.
@ -1419,7 +1538,7 @@ for more information.
### PrintToStringParamName {#PrintToStringParamName} ### PrintToStringParamName {#PrintToStringParamName}
`std::string ::testing::PrintToStringParamName(TestParamInfo<T>& info)` `std::string testing::PrintToStringParamName(TestParamInfo<T>& info)`
A built-in parameterized test name generator which returns the result of A built-in parameterized test name generator which returns the result of
[`PrintToString`](#PrintToString) called on `info.param`. Does not work when the [`PrintToString`](#PrintToString) called on `info.param`. Does not work when the

61
fake_fuchsia_sdk.bzl Normal file
View File

@ -0,0 +1,61 @@
"""Provides a fake @fuchsia_sdk implementation that's used when the real one isn't available.
GoogleTest can be used with the [Fuchsia](https://fuchsia.dev/) SDK. However,
because the Fuchsia SDK does not yet support bzlmod, GoogleTest's `MODULE.bazel`
file by default provides a "fake" Fuchsia SDK.
To override this and use the real Fuchsia SDK, you can add the following to your
project's `MODULE.bazel` file:
fake_fuchsia_sdk_extension =
use_extension("@com_google_googletest//:fake_fuchsia_sdk.bzl", "fuchsia_sdk")
override_repo(fake_fuchsia_sdk_extension, "fuchsia_sdk")
NOTE: The `override_repo` built-in is only available in Bazel 8.0 and higher.
See https://github.com/google/googletest/issues/4472 for more details of why the
fake Fuchsia SDK is needed.
"""
def _fake_fuchsia_sdk_impl(repo_ctx):
for stub_target in repo_ctx.attr._stub_build_targets:
stub_package = stub_target
stub_target_name = stub_target.split("/")[-1]
repo_ctx.file("%s/BUILD.bazel" % stub_package, """
filegroup(
name = "%s",
)
""" % stub_target_name)
fake_fuchsia_sdk = repository_rule(
doc = "Used to create a fake @fuchsia_sdk repository with stub build targets.",
implementation = _fake_fuchsia_sdk_impl,
attrs = {
"_stub_build_targets": attr.string_list(
doc = "The stub build targets to initialize.",
default = [
"pkg/fdio",
"pkg/syslog",
"pkg/zx",
],
),
},
)
_create_fake = tag_class()
def _fuchsia_sdk_impl(module_ctx):
create_fake_sdk = False
for mod in module_ctx.modules:
for _ in mod.tags.create_fake:
create_fake_sdk = True
if create_fake_sdk:
fake_fuchsia_sdk(name = "fuchsia_sdk")
return module_ctx.extension_metadata(reproducible = True)
fuchsia_sdk = module_extension(
implementation = _fuchsia_sdk_impl,
tag_classes = {"create_fake": _create_fake},
)

View File

@ -36,8 +36,7 @@ endif()
# as ${gmock_SOURCE_DIR} and to the root binary directory as # as ${gmock_SOURCE_DIR} and to the root binary directory as
# ${gmock_BINARY_DIR}. # ${gmock_BINARY_DIR}.
# Language "C" is required for find_package(Threads). # Language "C" is required for find_package(Threads).
cmake_minimum_required(VERSION 3.5) cmake_minimum_required(VERSION 3.13)
cmake_policy(SET CMP0048 NEW)
project(gmock VERSION ${GOOGLETEST_VERSION} LANGUAGES CXX C) project(gmock VERSION ${GOOGLETEST_VERSION} LANGUAGES CXX C)
if (COMMAND set_up_hermetic_build) if (COMMAND set_up_hermetic_build)
@ -66,12 +65,13 @@ endif()
config_compiler_and_linker() # from ${gtest_dir}/cmake/internal_utils.cmake config_compiler_and_linker() # from ${gtest_dir}/cmake/internal_utils.cmake
# Adds Google Mock's and Google Test's header directories to the search path. # Adds Google Mock's and Google Test's header directories to the search path.
# Get Google Test's include dirs from the target, gtest_SOURCE_DIR is broken
# when using fetch-content with the name "GTest".
get_target_property(gtest_include_dirs gtest INCLUDE_DIRECTORIES)
set(gmock_build_include_dirs set(gmock_build_include_dirs
"${gmock_SOURCE_DIR}/include" "${gmock_SOURCE_DIR}/include"
"${gmock_SOURCE_DIR}" "${gmock_SOURCE_DIR}"
"${gtest_SOURCE_DIR}/include" "${gtest_include_dirs}")
# This directory is needed to build directly from Google Test sources.
"${gtest_SOURCE_DIR}")
include_directories(${gmock_build_include_dirs}) include_directories(${gmock_build_include_dirs})
######################################################################## ########################################################################
@ -101,22 +101,18 @@ else()
target_link_libraries(gmock_main PUBLIC gmock) target_link_libraries(gmock_main PUBLIC gmock)
set_target_properties(gmock_main PROPERTIES VERSION ${GOOGLETEST_VERSION}) set_target_properties(gmock_main PROPERTIES VERSION ${GOOGLETEST_VERSION})
endif() endif()
# If the CMake version supports it, attach header directory information
# to the targets for when we are part of a parent build (ie being pulled string(REPLACE ";" "$<SEMICOLON>" dirs "${gmock_build_include_dirs}")
# in via add_subdirectory() rather than being a standalone build). target_include_directories(gmock SYSTEM INTERFACE
if (DEFINED CMAKE_VERSION AND NOT "${CMAKE_VERSION}" VERSION_LESS "2.8.11")
string(REPLACE ";" "$<SEMICOLON>" dirs "${gmock_build_include_dirs}")
target_include_directories(gmock SYSTEM INTERFACE
"$<BUILD_INTERFACE:${dirs}>" "$<BUILD_INTERFACE:${dirs}>"
"$<INSTALL_INTERFACE:$<INSTALL_PREFIX>/${CMAKE_INSTALL_INCLUDEDIR}>") "$<INSTALL_INTERFACE:$<INSTALL_PREFIX>/${CMAKE_INSTALL_INCLUDEDIR}>")
target_include_directories(gmock_main SYSTEM INTERFACE target_include_directories(gmock_main SYSTEM INTERFACE
"$<BUILD_INTERFACE:${dirs}>" "$<BUILD_INTERFACE:${dirs}>"
"$<INSTALL_INTERFACE:$<INSTALL_PREFIX>/${CMAKE_INSTALL_INCLUDEDIR}>") "$<INSTALL_INTERFACE:$<INSTALL_PREFIX>/${CMAKE_INSTALL_INCLUDEDIR}>")
endif()
######################################################################## ########################################################################
# #
# Install rules # Install rules.
install_project(gmock gmock_main) install_project(gmock gmock_main)
######################################################################## ########################################################################
@ -136,11 +132,7 @@ if (gmock_build_tests)
enable_testing() enable_testing()
if (MINGW OR CYGWIN) if (MINGW OR CYGWIN)
if (CMAKE_VERSION VERSION_LESS "2.8.12")
add_compile_options("-Wa,-mbig-obj") add_compile_options("-Wa,-mbig-obj")
else()
add_definitions("-Wa,-mbig-obj")
endif()
endif() endif()
############################################################ ############################################################

View File

@ -8,8 +8,8 @@ derive better designs of your system and write better tests.
It is inspired by: It is inspired by:
* [jMock](http://www.jmock.org/) * [jMock](http://www.jmock.org/)
* [EasyMock](http://www.easymock.org/) * [EasyMock](https://easymock.org/)
* [Hamcrest](http://code.google.com/p/hamcrest/) * [Hamcrest](https://code.google.com/p/hamcrest/)
It is designed with C++'s specifics in mind. It is designed with C++'s specifics in mind.
@ -36,5 +36,5 @@ Details and examples can be found here:
* [gMock Cheat Sheet](https://google.github.io/googletest/gmock_cheat_sheet.html) * [gMock Cheat Sheet](https://google.github.io/googletest/gmock_cheat_sheet.html)
GoogleMock is a part of GoogleMock is a part of
[GoogleTest C++ testing framework](http://github.com/google/googletest/) and a [GoogleTest C++ testing framework](https://github.com/google/googletest/) and a
subject to the same requirements. subject to the same requirements.

View File

@ -135,6 +135,7 @@
#endif #endif
#include <algorithm> #include <algorithm>
#include <exception>
#include <functional> #include <functional>
#include <memory> #include <memory>
#include <string> #include <string>
@ -146,10 +147,7 @@
#include "gmock/internal/gmock-port.h" #include "gmock/internal/gmock-port.h"
#include "gmock/internal/gmock-pp.h" #include "gmock/internal/gmock-pp.h"
#ifdef _MSC_VER GTEST_DISABLE_MSC_WARNINGS_PUSH_(4100)
#pragma warning(push)
#pragma warning(disable : 4100)
#endif
namespace testing { namespace testing {
@ -178,9 +176,15 @@ struct BuiltInDefaultValueGetter<T, false> {
static T Get() { static T Get() {
Assert(false, __FILE__, __LINE__, Assert(false, __FILE__, __LINE__,
"Default action undefined for the function return type."); "Default action undefined for the function return type.");
return internal::Invalid<T>(); #if defined(__GNUC__) || defined(__clang__)
__builtin_unreachable();
#elif defined(_MSC_VER)
__assume(0);
#else
return Invalid<T>();
// The above statement will never be reached, but is required in // The above statement will never be reached, but is required in
// order for this function to compile. // order for this function to compile.
#endif
} }
}; };
@ -614,7 +618,7 @@ class DefaultValue {
private: private:
class ValueProducer { class ValueProducer {
public: public:
virtual ~ValueProducer() {} virtual ~ValueProducer() = default;
virtual T Produce() = 0; virtual T Produce() = 0;
}; };
@ -702,8 +706,8 @@ class ActionInterface {
typedef typename internal::Function<F>::Result Result; typedef typename internal::Function<F>::Result Result;
typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple; typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple;
ActionInterface() {} ActionInterface() = default;
virtual ~ActionInterface() {} virtual ~ActionInterface() = default;
// Performs the action. This method is not const, as in general an // Performs the action. This method is not const, as in general an
// action can have side effects and be stateful. For example, a // action can have side effects and be stateful. For example, a
@ -752,7 +756,7 @@ class Action<R(Args...)> {
// Constructs a null Action. Needed for storing Action objects in // Constructs a null Action. Needed for storing Action objects in
// STL containers. // STL containers.
Action() {} Action() = default;
// Construct an Action from a specified callable. // Construct an Action from a specified callable.
// This cannot take std::function directly, because then Action would not be // This cannot take std::function directly, because then Action would not be
@ -831,6 +835,10 @@ class Action<R(Args...)> {
Result operator()(const InArgs&...) const { Result operator()(const InArgs&...) const {
return function_impl(); return function_impl();
} }
template <typename... InArgs>
Result operator()(const InArgs&...) {
return function_impl();
}
FunctionImpl function_impl; FunctionImpl function_impl;
}; };
@ -1276,7 +1284,7 @@ class AssignAction {
const T2 value_; const T2 value_;
}; };
#if !GTEST_OS_WINDOWS_MOBILE #ifndef GTEST_OS_WINDOWS_MOBILE
// Implements the SetErrnoAndReturn action to simulate return from // Implements the SetErrnoAndReturn action to simulate return from
// various system calls and libc functions. // various system calls and libc functions.
@ -1420,14 +1428,14 @@ struct WithArgsAction {
// providing a call operator because even with a particular set of arguments // providing a call operator because even with a particular set of arguments
// they don't have a fixed return type. // they don't have a fixed return type.
template <typename R, typename... Args, template <
typename R, typename... Args,
typename std::enable_if< typename std::enable_if<
std::is_convertible< std::is_convertible<InnerAction,
InnerAction, // Unfortunately we can't use the InnerSignature
// Unfortunately we can't use the InnerSignature alias here; // alias here; MSVC complains about the I
// MSVC complains about the I parameter pack not being // parameter pack not being expanded (error C3520)
// expanded (error C3520) despite it being expanded in the // despite it being expanded in the type alias.
// type alias.
// TupleElement is also an MSVC workaround. // TupleElement is also an MSVC workaround.
// See its definition for details. // See its definition for details.
OnceAction<R(internal::TupleElement< OnceAction<R(internal::TupleElement<
@ -1447,14 +1455,38 @@ struct WithArgsAction {
return OA{std::move(inner_action)}; return OA{std::move(inner_action)};
} }
template <typename R, typename... Args, // As above, but in the case where we want to create a OnceAction from a const
// WithArgsAction. This is fine as long as the inner action doesn't need to
// move any of its state to create a OnceAction.
template <
typename R, typename... Args,
typename std::enable_if< typename std::enable_if<
std::is_convertible< std::is_convertible<const InnerAction&,
const InnerAction&, OnceAction<R(internal::TupleElement<
// Unfortunately we can't use the InnerSignature alias here; I, std::tuple<Args...>>...)>>::value,
// MSVC complains about the I parameter pack not being int>::type = 0>
// expanded (error C3520) despite it being expanded in the operator OnceAction<R(Args...)>() const& { // NOLINT
// type alias. struct OA {
OnceAction<InnerSignature<R, Args...>> inner_action;
R operator()(Args&&... args) && {
return std::move(inner_action)
.Call(std::get<I>(
std::forward_as_tuple(std::forward<Args>(args)...))...);
}
};
return OA{inner_action};
}
template <
typename R, typename... Args,
typename std::enable_if<
std::is_convertible<const InnerAction&,
// Unfortunately we can't use the InnerSignature
// alias here; MSVC complains about the I
// parameter pack not being expanded (error C3520)
// despite it being expanded in the type alias.
// TupleElement is also an MSVC workaround. // TupleElement is also an MSVC workaround.
// See its definition for details. // See its definition for details.
Action<R(internal::TupleElement< Action<R(internal::TupleElement<
@ -1489,6 +1521,7 @@ class DoAllAction<FinalAction> {
// providing a call operator because even with a particular set of arguments // providing a call operator because even with a particular set of arguments
// they don't have a fixed return type. // they don't have a fixed return type.
// We support conversion to OnceAction whenever the sub-action does.
template <typename R, typename... Args, template <typename R, typename... Args,
typename std::enable_if< typename std::enable_if<
std::is_convertible<FinalAction, OnceAction<R(Args...)>>::value, std::is_convertible<FinalAction, OnceAction<R(Args...)>>::value,
@ -1497,6 +1530,21 @@ class DoAllAction<FinalAction> {
return std::move(final_action_); return std::move(final_action_);
} }
// We also support conversion to OnceAction whenever the sub-action supports
// conversion to Action (since any Action can also be a OnceAction).
template <
typename R, typename... Args,
typename std::enable_if<
conjunction<
negation<
std::is_convertible<FinalAction, OnceAction<R(Args...)>>>,
std::is_convertible<FinalAction, Action<R(Args...)>>>::value,
int>::type = 0>
operator OnceAction<R(Args...)>() && { // NOLINT
return Action<R(Args...)>(std::move(final_action_));
}
// We support conversion to Action whenever the sub-action does.
template < template <
typename R, typename... Args, typename R, typename... Args,
typename std::enable_if< typename std::enable_if<
@ -1576,12 +1624,12 @@ class DoAllAction<InitialAction, OtherActions...>
: Base({}, std::forward<U>(other_actions)...), : Base({}, std::forward<U>(other_actions)...),
initial_action_(std::forward<T>(initial_action)) {} initial_action_(std::forward<T>(initial_action)) {}
template <typename R, typename... Args, // We support conversion to OnceAction whenever both the initial action and
// the rest support conversion to OnceAction.
template <
typename R, typename... Args,
typename std::enable_if< typename std::enable_if<
conjunction< conjunction<std::is_convertible<
// Both the initial action and the rest must support
// conversion to OnceAction.
std::is_convertible<
InitialAction, InitialAction,
OnceAction<void(InitialActionArgType<Args>...)>>, OnceAction<void(InitialActionArgType<Args>...)>>,
std::is_convertible<Base, OnceAction<R(Args...)>>>::value, std::is_convertible<Base, OnceAction<R(Args...)>>>::value,
@ -1608,12 +1656,34 @@ class DoAllAction<InitialAction, OtherActions...>
}; };
} }
// We also support conversion to OnceAction whenever the initial action
// supports conversion to Action (since any Action can also be a OnceAction).
//
// The remaining sub-actions must also be compatible, but we don't need to
// special case them because the base class deals with them.
template <
typename R, typename... Args,
typename std::enable_if<
conjunction<
negation<std::is_convertible<
InitialAction,
OnceAction<void(InitialActionArgType<Args>...)>>>,
std::is_convertible<InitialAction,
Action<void(InitialActionArgType<Args>...)>>,
std::is_convertible<Base, OnceAction<R(Args...)>>>::value,
int>::type = 0>
operator OnceAction<R(Args...)>() && { // NOLINT
return DoAll(
Action<void(InitialActionArgType<Args>...)>(std::move(initial_action_)),
std::move(static_cast<Base&>(*this)));
}
// We support conversion to Action whenever both the initial action and the
// rest support conversion to Action.
template < template <
typename R, typename... Args, typename R, typename... Args,
typename std::enable_if< typename std::enable_if<
conjunction< conjunction<
// Both the initial action and the rest must support conversion to
// Action.
std::is_convertible<const InitialAction&, std::is_convertible<const InitialAction&,
Action<void(InitialActionArgType<Args>...)>>, Action<void(InitialActionArgType<Args>...)>>,
std::is_convertible<const Base&, Action<R(Args...)>>>::value, std::is_convertible<const Base&, Action<R(Args...)>>>::value,
@ -1677,6 +1747,16 @@ struct SaveArgAction {
} }
}; };
template <size_t k, typename Ptr>
struct SaveArgByMoveAction {
Ptr pointer;
template <typename... Args>
void operator()(Args&&... args) const {
*pointer = std::move(std::get<k>(std::tie(args...)));
}
};
template <size_t k, typename Ptr> template <size_t k, typename Ptr>
struct SaveArgPointeeAction { struct SaveArgPointeeAction {
Ptr pointer; Ptr pointer;
@ -1743,6 +1823,13 @@ struct ThrowAction {
return [copy](Args...) -> R { throw copy; }; return [copy](Args...) -> R { throw copy; };
} }
}; };
struct RethrowAction {
std::exception_ptr exception;
template <typename R, typename... Args>
operator Action<R(Args...)>() const { // NOLINT
return [ex = exception](Args...) -> R { std::rethrow_exception(ex); };
}
};
#endif // GTEST_HAS_EXCEPTIONS #endif // GTEST_HAS_EXCEPTIONS
} // namespace internal } // namespace internal
@ -1929,7 +2016,7 @@ PolymorphicAction<internal::AssignAction<T1, T2>> Assign(T1* ptr, T2 val) {
return MakePolymorphicAction(internal::AssignAction<T1, T2>(ptr, val)); return MakePolymorphicAction(internal::AssignAction<T1, T2>(ptr, val));
} }
#if !GTEST_OS_WINDOWS_MOBILE #ifndef GTEST_OS_WINDOWS_MOBILE
// Creates an action that sets errno and returns the appropriate error. // Creates an action that sets errno and returns the appropriate error.
template <typename T> template <typename T>
@ -2020,6 +2107,13 @@ internal::SaveArgAction<k, Ptr> SaveArg(Ptr pointer) {
return {pointer}; return {pointer};
} }
// Action SaveArgByMove<k>(pointer) moves the k-th (0-based) argument of the
// mock function into *pointer.
template <size_t k, typename Ptr>
internal::SaveArgByMoveAction<k, Ptr> SaveArgByMove(Ptr pointer) {
return {pointer};
}
// Action SaveArgPointee<k>(pointer) saves the value pointed to // Action SaveArgPointee<k>(pointer) saves the value pointed to
// by the k-th (0-based) argument of the mock function to *pointer. // by the k-th (0-based) argument of the mock function to *pointer.
template <size_t k, typename Ptr> template <size_t k, typename Ptr>
@ -2059,13 +2153,23 @@ internal::ReturnPointeeAction<Ptr> ReturnPointee(Ptr pointer) {
return {pointer}; return {pointer};
} }
// Action Throw(exception) can be used in a mock function of any type
// to throw the given exception. Any copyable value can be thrown.
#if GTEST_HAS_EXCEPTIONS #if GTEST_HAS_EXCEPTIONS
// Action Throw(exception) can be used in a mock function of any type
// to throw the given exception. Any copyable value can be thrown,
// except for std::exception_ptr, which is likely a mistake if
// thrown directly.
template <typename T> template <typename T>
internal::ThrowAction<typename std::decay<T>::type> Throw(T&& exception) { typename std::enable_if<
!std::is_base_of<std::exception_ptr, typename std::decay<T>::type>::value,
internal::ThrowAction<typename std::decay<T>::type>>::type
Throw(T&& exception) {
return {std::forward<T>(exception)}; return {std::forward<T>(exception)};
} }
// Action Rethrow(exception_ptr) can be used in a mock function of any type
// to rethrow any exception_ptr. Note that the same object is thrown each time.
inline internal::RethrowAction Rethrow(std::exception_ptr exception) {
return {std::move(exception)};
}
#endif // GTEST_HAS_EXCEPTIONS #endif // GTEST_HAS_EXCEPTIONS
namespace internal { namespace internal {
@ -2114,13 +2218,13 @@ struct ActionImpl<R(Args...), Impl> : ImplBase<Impl>::type {
R operator()(Args&&... arg) const { R operator()(Args&&... arg) const {
static constexpr size_t kMaxArgs = static constexpr size_t kMaxArgs =
sizeof...(Args) <= 10 ? sizeof...(Args) : 10; sizeof...(Args) <= 10 ? sizeof...(Args) : 10;
return Apply(MakeIndexSequence<kMaxArgs>{}, return Apply(std::make_index_sequence<kMaxArgs>{},
MakeIndexSequence<10 - kMaxArgs>{}, std::make_index_sequence<10 - kMaxArgs>{},
args_type{std::forward<Args>(arg)...}); args_type{std::forward<Args>(arg)...});
} }
template <std::size_t... arg_id, std::size_t... excess_id> template <std::size_t... arg_id, std::size_t... excess_id>
R Apply(IndexSequence<arg_id...>, IndexSequence<excess_id...>, R Apply(std::index_sequence<arg_id...>, std::index_sequence<excess_id...>,
const args_type& args) const { const args_type& args) const {
// Impl need not be specific to the signature of action being implemented; // Impl need not be specific to the signature of action being implemented;
// only the implementing function body needs to have all of the specific // only the implementing function body needs to have all of the specific
@ -2153,9 +2257,9 @@ template <typename F, typename Impl>
} }
#define GMOCK_INTERNAL_ARG_UNUSED(i, data, el) \ #define GMOCK_INTERNAL_ARG_UNUSED(i, data, el) \
, const arg##i##_type& arg##i GTEST_ATTRIBUTE_UNUSED_ , [[maybe_unused]] const arg##i##_type& arg##i
#define GMOCK_ACTION_ARG_TYPES_AND_NAMES_UNUSED_ \ #define GMOCK_ACTION_ARG_TYPES_AND_NAMES_UNUSED_ \
const args_type& args GTEST_ATTRIBUTE_UNUSED_ GMOCK_PP_REPEAT( \ [[maybe_unused]] const args_type& args GMOCK_PP_REPEAT( \
GMOCK_INTERNAL_ARG_UNUSED, , 10) GMOCK_INTERNAL_ARG_UNUSED, , 10)
#define GMOCK_INTERNAL_ARG(i, data, el) , const arg##i##_type& arg##i #define GMOCK_INTERNAL_ARG(i, data, el) , const arg##i##_type& arg##i
@ -2220,8 +2324,8 @@ template <typename F, typename Impl>
std::shared_ptr<const gmock_Impl> impl_; \ std::shared_ptr<const gmock_Impl> impl_; \
}; \ }; \
template <GMOCK_ACTION_TYPENAME_PARAMS_(params)> \ template <GMOCK_ACTION_TYPENAME_PARAMS_(params)> \
inline full_name<GMOCK_ACTION_TYPE_PARAMS_(params)> name( \ [[nodiscard]] inline full_name<GMOCK_ACTION_TYPE_PARAMS_(params)> name( \
GMOCK_ACTION_TYPE_GVALUE_PARAMS_(params)) GTEST_MUST_USE_RESULT_; \ GMOCK_ACTION_TYPE_GVALUE_PARAMS_(params)); \
template <GMOCK_ACTION_TYPENAME_PARAMS_(params)> \ template <GMOCK_ACTION_TYPENAME_PARAMS_(params)> \
inline full_name<GMOCK_ACTION_TYPE_PARAMS_(params)> name( \ inline full_name<GMOCK_ACTION_TYPE_PARAMS_(params)> name( \
GMOCK_ACTION_TYPE_GVALUE_PARAMS_(params)) { \ GMOCK_ACTION_TYPE_GVALUE_PARAMS_(params)) { \
@ -2256,7 +2360,7 @@ template <typename F, typename Impl>
return_type gmock_PerformImpl(GMOCK_ACTION_ARG_TYPES_AND_NAMES_) const; \ return_type gmock_PerformImpl(GMOCK_ACTION_ARG_TYPES_AND_NAMES_) const; \
}; \ }; \
}; \ }; \
inline name##Action name() GTEST_MUST_USE_RESULT_; \ [[nodiscard]] inline name##Action name(); \
inline name##Action name() { return name##Action(); } \ inline name##Action name() { return name##Action(); } \
template <typename function_type, typename return_type, typename args_type, \ template <typename function_type, typename return_type, typename args_type, \
GMOCK_ACTION_TEMPLATE_ARGS_NAMES_> \ GMOCK_ACTION_TEMPLATE_ARGS_NAMES_> \
@ -2295,8 +2399,6 @@ template <typename F, typename Impl>
} // namespace testing } // namespace testing
#ifdef _MSC_VER GTEST_DISABLE_MSC_WARNINGS_POP_() // 4100
#pragma warning(pop)
#endif
#endif // GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_ACTIONS_H_ #endif // GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_ACTIONS_H_

View File

@ -65,7 +65,7 @@ namespace testing {
// The implementation of a cardinality. // The implementation of a cardinality.
class CardinalityInterface { class CardinalityInterface {
public: public:
virtual ~CardinalityInterface() {} virtual ~CardinalityInterface() = default;
// Conservative estimate on the lower/upper bound of the number of // Conservative estimate on the lower/upper bound of the number of
// calls allowed. // calls allowed.
@ -92,7 +92,7 @@ class GTEST_API_ Cardinality {
public: public:
// Constructs a null cardinality. Needed for storing Cardinality // Constructs a null cardinality. Needed for storing Cardinality
// objects in STL containers. // objects in STL containers.
Cardinality() {} Cardinality() = default;
// Constructs a Cardinality from its implementation. // Constructs a Cardinality from its implementation.
explicit Cardinality(const CardinalityInterface* impl) : impl_(impl) {} explicit Cardinality(const CardinalityInterface* impl) : impl_(impl) {}

View File

@ -37,6 +37,7 @@
#ifndef GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_FUNCTION_MOCKER_H_ #ifndef GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_FUNCTION_MOCKER_H_
#define GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_FUNCTION_MOCKER_H_ #define GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_FUNCTION_MOCKER_H_
#include <cstddef>
#include <type_traits> // IWYU pragma: keep #include <type_traits> // IWYU pragma: keep
#include <utility> // IWYU pragma: keep #include <utility> // IWYU pragma: keep
@ -69,22 +70,22 @@ constexpr bool PrefixOf(const char* a, const char* b) {
return *a == 0 || (*a == *b && internal::PrefixOf(a + 1, b + 1)); return *a == 0 || (*a == *b && internal::PrefixOf(a + 1, b + 1));
} }
template <int N, int M> template <size_t N, size_t M>
constexpr bool StartsWith(const char (&prefix)[N], const char (&str)[M]) { constexpr bool StartsWith(const char (&prefix)[N], const char (&str)[M]) {
return N <= M && internal::PrefixOf(prefix, str); return N <= M && internal::PrefixOf(prefix, str);
} }
template <int N, int M> template <size_t N, size_t M>
constexpr bool EndsWith(const char (&suffix)[N], const char (&str)[M]) { constexpr bool EndsWith(const char (&suffix)[N], const char (&str)[M]) {
return N <= M && internal::PrefixOf(suffix, str + M - N); return N <= M && internal::PrefixOf(suffix, str + M - N);
} }
template <int N, int M> template <size_t N, size_t M>
constexpr bool Equals(const char (&a)[N], const char (&b)[M]) { constexpr bool Equals(const char (&a)[N], const char (&b)[M]) {
return N == M && internal::PrefixOf(a, b); return N == M && internal::PrefixOf(a, b);
} }
template <int N> template <size_t N>
constexpr bool ValidateSpec(const char (&spec)[N]) { constexpr bool ValidateSpec(const char (&spec)[N]) {
return internal::Equals("const", spec) || return internal::Equals("const", spec) ||
internal::Equals("override", spec) || internal::Equals("override", spec) ||
@ -180,8 +181,9 @@ using internal::FunctionMocker;
_Signature)>::Result \ _Signature)>::Result \
GMOCK_INTERNAL_EXPAND(_CallType) \ GMOCK_INTERNAL_EXPAND(_CallType) \
_MethodName(GMOCK_PP_REPEAT(GMOCK_INTERNAL_PARAMETER, _Signature, _N)) \ _MethodName(GMOCK_PP_REPEAT(GMOCK_INTERNAL_PARAMETER, _Signature, _N)) \
GMOCK_PP_IF(_Constness, const, ) _RefSpec _NoexceptSpec \ GMOCK_PP_IF(_Constness, const, ) \
GMOCK_PP_IF(_Override, override, ) GMOCK_PP_IF(_Final, final, ) { \ _RefSpec _NoexceptSpec GMOCK_PP_IF(_Override, override, ) \
GMOCK_PP_IF(_Final, final, ) { \
GMOCK_MOCKER_(_N, _Constness, _MethodName) \ GMOCK_MOCKER_(_N, _Constness, _MethodName) \
.SetOwnerAndName(this, #_MethodName); \ .SetOwnerAndName(this, #_MethodName); \
return GMOCK_MOCKER_(_N, _Constness, _MethodName) \ return GMOCK_MOCKER_(_N, _Constness, _MethodName) \

File diff suppressed because it is too large Load Diff

View File

@ -521,14 +521,14 @@
GMOCK_INTERNAL_DECL_##value_params) \ GMOCK_INTERNAL_DECL_##value_params) \
GMOCK_PP_IF(GMOCK_PP_IS_EMPTY(GMOCK_INTERNAL_COUNT_##value_params), \ GMOCK_PP_IF(GMOCK_PP_IS_EMPTY(GMOCK_INTERNAL_COUNT_##value_params), \
= default; \ = default; \
, \ , : impl_(std::make_shared<gmock_Impl>( \
: impl_(std::make_shared<gmock_Impl>( \
GMOCK_INTERNAL_LIST_##value_params)){}) \ GMOCK_INTERNAL_LIST_##value_params)){}) \
GMOCK_ACTION_CLASS_(name, value_params)(const GMOCK_ACTION_CLASS_( \ GMOCK_ACTION_CLASS_(name, value_params)(const GMOCK_ACTION_CLASS_( \
name, value_params) &) noexcept GMOCK_INTERNAL_DEFN_COPY_ \ name, value_params) &) noexcept GMOCK_INTERNAL_DEFN_COPY_ \
##value_params GMOCK_ACTION_CLASS_(name, value_params)( \ ##value_params \
GMOCK_ACTION_CLASS_(name, value_params) &&) noexcept \ GMOCK_ACTION_CLASS_(name, value_params)(GMOCK_ACTION_CLASS_( \
GMOCK_INTERNAL_DEFN_COPY_##value_params template <typename F> \ name, value_params) &&) noexcept GMOCK_INTERNAL_DEFN_COPY_ \
##value_params template <typename F> \
operator ::testing::Action<F>() const { \ operator ::testing::Action<F>() const { \
return GMOCK_PP_IF( \ return GMOCK_PP_IF( \
GMOCK_PP_IS_EMPTY(GMOCK_INTERNAL_COUNT_##value_params), \ GMOCK_PP_IS_EMPTY(GMOCK_INTERNAL_COUNT_##value_params), \
@ -550,10 +550,10 @@
}; \ }; \
template <GMOCK_INTERNAL_DECL_##template_params \ template <GMOCK_INTERNAL_DECL_##template_params \
GMOCK_INTERNAL_DECL_TYPE_##value_params> \ GMOCK_INTERNAL_DECL_TYPE_##value_params> \
GMOCK_ACTION_CLASS_( \ [[nodiscard]] GMOCK_ACTION_CLASS_( \
name, value_params)<GMOCK_INTERNAL_LIST_##template_params \ name, value_params)<GMOCK_INTERNAL_LIST_##template_params \
GMOCK_INTERNAL_LIST_TYPE_##value_params> \ GMOCK_INTERNAL_LIST_TYPE_##value_params> \
name(GMOCK_INTERNAL_DECL_##value_params) GTEST_MUST_USE_RESULT_; \ name(GMOCK_INTERNAL_DECL_##value_params); \
template <GMOCK_INTERNAL_DECL_##template_params \ template <GMOCK_INTERNAL_DECL_##template_params \
GMOCK_INTERNAL_DECL_TYPE_##value_params> \ GMOCK_INTERNAL_DECL_TYPE_##value_params> \
inline GMOCK_ACTION_CLASS_( \ inline GMOCK_ACTION_CLASS_( \
@ -582,10 +582,7 @@ namespace testing {
// the macro definition, as the warnings are generated when the macro // the macro definition, as the warnings are generated when the macro
// is expanded and macro expansion cannot contain #pragma. Therefore // is expanded and macro expansion cannot contain #pragma. Therefore
// we suppress them here. // we suppress them here.
#ifdef _MSC_VER GTEST_DISABLE_MSC_WARNINGS_PUSH_(4100)
#pragma warning(push)
#pragma warning(disable : 4100)
#endif
namespace internal { namespace internal {
@ -594,21 +591,23 @@ namespace internal {
// Overloads for other custom-callables are provided in the // Overloads for other custom-callables are provided in the
// internal/custom/gmock-generated-actions.h header. // internal/custom/gmock-generated-actions.h header.
template <typename F, typename... Args> template <typename F, typename... Args>
auto InvokeArgument(F f, Args... args) -> decltype(f(args...)) { auto InvokeArgument(F &&f,
return f(args...); Args... args) -> decltype(std::forward<F>(f)(args...)) {
return std::forward<F>(f)(args...);
} }
template <std::size_t index, typename... Params> template <std::size_t index, typename... Params>
struct InvokeArgumentAction { struct InvokeArgumentAction {
template <typename... Args, template <typename... Args,
typename = typename std::enable_if<(index < sizeof...(Args))>::type> typename = typename std::enable_if<(index < sizeof...(Args))>::type>
auto operator()(Args&&... args) const -> decltype(internal::InvokeArgument( auto operator()(Args &&...args) const
-> decltype(internal::InvokeArgument(
std::get<index>(std::forward_as_tuple(std::forward<Args>(args)...)), std::get<index>(std::forward_as_tuple(std::forward<Args>(args)...)),
std::declval<const Params&>()...)) { std::declval<const Params &>()...)) {
internal::FlatTuple<Args&&...> args_tuple(FlatTupleConstructTag{}, internal::FlatTuple<Args &&...> args_tuple(FlatTupleConstructTag{},
std::forward<Args>(args)...); std::forward<Args>(args)...);
return params.Apply([&](const Params&... unpacked_params) { return params.Apply([&](const Params &...unpacked_params) {
auto&& callable = args_tuple.template Get<index>(); auto &&callable = std::move(args_tuple.template Get<index>());
return internal::InvokeArgument( return internal::InvokeArgument(
std::forward<decltype(callable)>(callable), unpacked_params...); std::forward<decltype(callable)>(callable), unpacked_params...);
}); });
@ -648,14 +647,12 @@ struct InvokeArgumentAction {
// later. // later.
template <std::size_t index, typename... Params> template <std::size_t index, typename... Params>
internal::InvokeArgumentAction<index, typename std::decay<Params>::type...> internal::InvokeArgumentAction<index, typename std::decay<Params>::type...>
InvokeArgument(Params&&... params) { InvokeArgument(Params &&...params) {
return {internal::FlatTuple<typename std::decay<Params>::type...>( return {internal::FlatTuple<typename std::decay<Params>::type...>(
internal::FlatTupleConstructTag{}, std::forward<Params>(params)...)}; internal::FlatTupleConstructTag{}, std::forward<Params>(params)...)};
} }
#ifdef _MSC_VER GTEST_DISABLE_MSC_WARNINGS_POP_() // 4100
#pragma warning(pop)
#endif
} // namespace testing } // namespace testing

View File

@ -49,14 +49,11 @@ namespace testing {
// Silence C4100 (unreferenced formal // Silence C4100 (unreferenced formal
// parameter) for MSVC // parameter) for MSVC
#ifdef _MSC_VER GTEST_DISABLE_MSC_WARNINGS_PUSH_(4100)
#pragma warning(push) #if defined(_MSC_VER) && (_MSC_VER == 1900)
#pragma warning(disable : 4100)
#if (_MSC_VER == 1900)
// and silence C4800 (C4800: 'int *const ': forcing value // and silence C4800 (C4800: 'int *const ': forcing value
// to bool 'true' or 'false') for MSVC 14 // to bool 'true' or 'false') for MSVC 14
#pragma warning(disable : 4800) GTEST_DISABLE_MSC_WARNINGS_PUSH_(4800)
#endif
#endif #endif
namespace internal { namespace internal {
@ -113,9 +110,10 @@ MATCHER(IsFalse, negation ? "is true" : "is false") {
return !static_cast<bool>(arg); return !static_cast<bool>(arg);
} }
#ifdef _MSC_VER #if defined(_MSC_VER) && (_MSC_VER == 1900)
#pragma warning(pop) GTEST_DISABLE_MSC_WARNINGS_POP_() // 4800
#endif #endif
GTEST_DISABLE_MSC_WARNINGS_POP_() // 4100
} // namespace testing } // namespace testing

View File

@ -98,7 +98,7 @@ constexpr bool HasStrictnessModifier() {
// deregistration. This guarantees that MockClass's constructor and destructor // deregistration. This guarantees that MockClass's constructor and destructor
// run with the same level of strictness as its instance methods. // run with the same level of strictness as its instance methods.
#if GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MINGW && \ #if defined(GTEST_OS_WINDOWS) && !defined(GTEST_OS_WINDOWS_MINGW) && \
(defined(_MSC_VER) || defined(__clang__)) (defined(_MSC_VER) || defined(__clang__))
// We need to mark these classes with this declspec to ensure that // We need to mark these classes with this declspec to ensure that
// the empty base class optimization is performed. // the empty base class optimization is performed.

View File

@ -204,6 +204,9 @@ class GTEST_API_ UntypedFunctionMockerBase {
using UntypedExpectations = std::vector<std::shared_ptr<ExpectationBase>>; using UntypedExpectations = std::vector<std::shared_ptr<ExpectationBase>>;
struct UninterestingCallCleanupHandler;
struct FailureCleanupHandler;
// Returns an Expectation object that references and co-owns exp, // Returns an Expectation object that references and co-owns exp,
// which must be an expectation on this mock function. // which must be an expectation on this mock function.
Expectation GetHandleOf(ExpectationBase* exp); Expectation GetHandleOf(ExpectationBase* exp);
@ -563,7 +566,7 @@ class ExpectationSet {
typedef Expectation::Set::value_type value_type; typedef Expectation::Set::value_type value_type;
// Constructs an empty set. // Constructs an empty set.
ExpectationSet() {} ExpectationSet() = default;
// This single-argument ctor must not be explicit, in order to support the // This single-argument ctor must not be explicit, in order to support the
// ExpectationSet es = EXPECT_CALL(...); // ExpectationSet es = EXPECT_CALL(...);
@ -1396,6 +1399,41 @@ class Cleanup final {
std::function<void()> f_; std::function<void()> f_;
}; };
struct UntypedFunctionMockerBase::UninterestingCallCleanupHandler {
CallReaction reaction;
std::stringstream& ss;
~UninterestingCallCleanupHandler() {
ReportUninterestingCall(reaction, ss.str());
}
};
struct UntypedFunctionMockerBase::FailureCleanupHandler {
std::stringstream& ss;
std::stringstream& why;
std::stringstream& loc;
const ExpectationBase* untyped_expectation;
bool found;
bool is_excessive;
~FailureCleanupHandler() {
ss << "\n" << why.str();
if (!found) {
// No expectation matches this call - reports a failure.
Expect(false, nullptr, -1, ss.str());
} else if (is_excessive) {
// We had an upper-bound violation and the failure message is in ss.
Expect(false, untyped_expectation->file(), untyped_expectation->line(),
ss.str());
} else {
// We had an expected call and the matching expectation is
// described in ss.
Log(kInfo, loc.str() + ss.str(), 2);
}
}
};
template <typename F> template <typename F>
class FunctionMocker; class FunctionMocker;
@ -1408,7 +1446,7 @@ class FunctionMocker<R(Args...)> final : public UntypedFunctionMockerBase {
using ArgumentTuple = std::tuple<Args...>; using ArgumentTuple = std::tuple<Args...>;
using ArgumentMatcherTuple = std::tuple<Matcher<Args>...>; using ArgumentMatcherTuple = std::tuple<Matcher<Args>...>;
FunctionMocker() {} FunctionMocker() = default;
// There is no generally useful and implementable semantics of // There is no generally useful and implementable semantics of
// copying a mock object, so copying a mock is usually a user error. // copying a mock object, so copying a mock is usually a user error.
@ -1794,8 +1832,14 @@ R FunctionMocker<R(Args...)>::InvokeWith(ArgumentTuple&& args)
// //
// We use RAII to do the latter in case R is void or a non-moveable type. In // We use RAII to do the latter in case R is void or a non-moveable type. In
// either case we can't assign it to a local variable. // either case we can't assign it to a local variable.
const Cleanup report_uninteresting_call( //
[&] { ReportUninterestingCall(reaction, ss.str()); }); // Note that std::bind() is essential here.
// We *don't* use any local callback types (like lambdas).
// Doing so slows down compilation dramatically because the *constructor* of
// std::function<T> is re-instantiated with different template
// parameters each time.
const UninterestingCallCleanupHandler report_uninteresting_call = {reaction,
ss};
return PerformActionAndPrintResult(nullptr, std::move(args), ss.str(), ss); return PerformActionAndPrintResult(nullptr, std::move(args), ss.str(), ss);
} }
@ -1839,22 +1883,13 @@ R FunctionMocker<R(Args...)>::InvokeWith(ArgumentTuple&& args)
// //
// We use RAII to do the latter in case R is void or a non-moveable type. In // We use RAII to do the latter in case R is void or a non-moveable type. In
// either case we can't assign it to a local variable. // either case we can't assign it to a local variable.
const Cleanup handle_failures([&] { //
ss << "\n" << why.str(); // Note that we *don't* use any local callback types (like lambdas) here.
// Doing so slows down compilation dramatically because the *constructor* of
if (!found) { // std::function<T> is re-instantiated with different template
// No expectation matches this call - reports a failure. // parameters each time.
Expect(false, nullptr, -1, ss.str()); const FailureCleanupHandler handle_failures = {
} else if (is_excessive) { ss, why, loc, untyped_expectation, found, is_excessive};
// We had an upper-bound violation and the failure message is in ss.
Expect(false, untyped_expectation->file(), untyped_expectation->line(),
ss.str());
} else {
// We had an expected call and the matching expectation is
// described in ss.
Log(kInfo, loc.str() + ss.str(), 2);
}
});
return PerformActionAndPrintResult(untyped_action, std::move(args), ss.str(), return PerformActionAndPrintResult(untyped_action, std::move(args), ss.str(),
ss); ss);

View File

@ -53,13 +53,14 @@
// //
// where all clauses are optional and WillOnce() can be repeated. // where all clauses are optional and WillOnce() can be repeated.
#include "gmock/gmock-actions.h" #include "gmock/gmock-actions.h" // IWYU pragma: export
#include "gmock/gmock-cardinalities.h" #include "gmock/gmock-cardinalities.h" // IWYU pragma: export
#include "gmock/gmock-function-mocker.h" #include "gmock/gmock-function-mocker.h" // IWYU pragma: export
#include "gmock/gmock-matchers.h" #include "gmock/gmock-matchers.h" // IWYU pragma: export
#include "gmock/gmock-more-actions.h" #include "gmock/gmock-more-actions.h" // IWYU pragma: export
#include "gmock/gmock-more-matchers.h" #include "gmock/gmock-more-matchers.h" // IWYU pragma: export
#include "gmock/gmock-nice-strict.h" #include "gmock/gmock-nice-strict.h" // IWYU pragma: export
#include "gmock/gmock-spec-builders.h" // IWYU pragma: export
#include "gmock/internal/gmock-internal-utils.h" #include "gmock/internal/gmock-internal-utils.h"
#include "gmock/internal/gmock-port.h" #include "gmock/internal/gmock-port.h"

View File

@ -44,6 +44,7 @@
#include <ostream> // NOLINT #include <ostream> // NOLINT
#include <string> #include <string>
#include <type_traits> #include <type_traits>
#include <utility>
#include <vector> #include <vector>
#include "gmock/internal/gmock-port.h" #include "gmock/internal/gmock-port.h"
@ -58,11 +59,7 @@ namespace internal {
// Silence MSVC C4100 (unreferenced formal parameter) and // Silence MSVC C4100 (unreferenced formal parameter) and
// C4805('==': unsafe mix of type 'const int' and type 'const bool') // C4805('==': unsafe mix of type 'const int' and type 'const bool')
#ifdef _MSC_VER GTEST_DISABLE_MSC_WARNINGS_PUSH_(4100 4805)
#pragma warning(push)
#pragma warning(disable : 4100)
#pragma warning(disable : 4805)
#endif
// Joins a vector of strings as if they are fields of a tuple; returns // Joins a vector of strings as if they are fields of a tuple; returns
// the joined string. // the joined string.
@ -228,7 +225,7 @@ class FailureReporterInterface {
// The type of a failure (either non-fatal or fatal). // The type of a failure (either non-fatal or fatal).
enum FailureType { kNonfatal, kFatal }; enum FailureType { kNonfatal, kFatal };
virtual ~FailureReporterInterface() {} virtual ~FailureReporterInterface() = default;
// Reports a failure that occurred at the given source file location. // Reports a failure that occurred at the given source file location.
virtual void ReportFailure(FailureType type, const char* file, int line, virtual void ReportFailure(FailureType type, const char* file, int line,
@ -315,7 +312,8 @@ GTEST_API_ WithoutMatchers GetWithoutMatchers();
// crashes). // crashes).
template <typename T> template <typename T>
inline T Invalid() { inline T Invalid() {
Assert(false, "", -1, "Internal error: attempt to return invalid value"); Assert(/*condition=*/false, /*file=*/"", /*line=*/-1,
"Internal error: attempt to return invalid value");
#if defined(__GNUC__) || defined(__clang__) #if defined(__GNUC__) || defined(__clang__)
__builtin_unreachable(); __builtin_unreachable();
#elif defined(_MSC_VER) #elif defined(_MSC_VER)
@ -423,7 +421,7 @@ struct RemoveConstFromKey<std::pair<const K, V> > {
GTEST_API_ void IllegalDoDefault(const char* file, int line); GTEST_API_ void IllegalDoDefault(const char* file, int line);
template <typename F, typename Tuple, size_t... Idx> template <typename F, typename Tuple, size_t... Idx>
auto ApplyImpl(F&& f, Tuple&& args, IndexSequence<Idx...>) auto ApplyImpl(F&& f, Tuple&& args, std::index_sequence<Idx...>)
-> decltype(std::forward<F>(f)( -> decltype(std::forward<F>(f)(
std::get<Idx>(std::forward<Tuple>(args))...)) { std::get<Idx>(std::forward<Tuple>(args))...)) {
return std::forward<F>(f)(std::get<Idx>(std::forward<Tuple>(args))...); return std::forward<F>(f)(std::get<Idx>(std::forward<Tuple>(args))...);
@ -431,12 +429,13 @@ auto ApplyImpl(F&& f, Tuple&& args, IndexSequence<Idx...>)
// Apply the function to a tuple of arguments. // Apply the function to a tuple of arguments.
template <typename F, typename Tuple> template <typename F, typename Tuple>
auto Apply(F&& f, Tuple&& args) -> decltype(ApplyImpl( auto Apply(F&& f, Tuple&& args)
-> decltype(ApplyImpl(
std::forward<F>(f), std::forward<Tuple>(args), std::forward<F>(f), std::forward<Tuple>(args),
MakeIndexSequence<std::tuple_size< std::make_index_sequence<std::tuple_size<
typename std::remove_reference<Tuple>::type>::value>())) { typename std::remove_reference<Tuple>::type>::value>())) {
return ApplyImpl(std::forward<F>(f), std::forward<Tuple>(args), return ApplyImpl(std::forward<F>(f), std::forward<Tuple>(args),
MakeIndexSequence<std::tuple_size< std::make_index_sequence<std::tuple_size<
typename std::remove_reference<Tuple>::type>::value>()); typename std::remove_reference<Tuple>::type>::value>());
} }
@ -468,9 +467,6 @@ struct Function<R(Args...)> {
using MakeResultIgnoredValue = IgnoredValue(Args...); using MakeResultIgnoredValue = IgnoredValue(Args...);
}; };
template <typename R, typename... Args>
constexpr size_t Function<R(Args...)>::ArgumentCount;
// Workaround for MSVC error C2039: 'type': is not a member of 'std' // Workaround for MSVC error C2039: 'type': is not a member of 'std'
// when std::tuple_element is used. // when std::tuple_element is used.
// See: https://github.com/google/googletest/issues/3931 // See: https://github.com/google/googletest/issues/3931
@ -480,9 +476,7 @@ using TupleElement = typename std::tuple_element<I, T>::type;
bool Base64Unescape(const std::string& encoded, std::string* decoded); bool Base64Unescape(const std::string& encoded, std::string* decoded);
#ifdef _MSC_VER GTEST_DISABLE_MSC_WARNINGS_POP_() // 4100 4805
#pragma warning(pop)
#endif
} // namespace internal } // namespace internal
} // namespace testing } // namespace testing

View File

@ -42,6 +42,7 @@
#include <assert.h> #include <assert.h>
#include <stdlib.h> #include <stdlib.h>
#include <cstdint> #include <cstdint>
#include <iostream> #include <iostream>
@ -56,7 +57,7 @@
#include "gmock/internal/custom/gmock-port.h" #include "gmock/internal/custom/gmock-port.h"
#include "gtest/internal/gtest-port.h" #include "gtest/internal/gtest-port.h"
#if GTEST_HAS_ABSL #if defined(GTEST_HAS_ABSL) && !defined(GTEST_NO_ABSL_FLAGS)
#include "absl/flags/declare.h" #include "absl/flags/declare.h"
#include "absl/flags/flag.h" #include "absl/flags/flag.h"
#endif #endif
@ -73,7 +74,7 @@
#define GMOCK_FLAG(name) FLAGS_gmock_##name #define GMOCK_FLAG(name) FLAGS_gmock_##name
// Pick a command line flags implementation. // Pick a command line flags implementation.
#if GTEST_HAS_ABSL #if defined(GTEST_HAS_ABSL) && !defined(GTEST_NO_ABSL_FLAGS)
// Macros for defining flags. // Macros for defining flags.
#define GMOCK_DEFINE_bool_(name, default_val, doc) \ #define GMOCK_DEFINE_bool_(name, default_val, doc) \
@ -95,7 +96,7 @@
#define GMOCK_FLAG_SET(name, value) \ #define GMOCK_FLAG_SET(name, value) \
(void)(::absl::SetFlag(&GMOCK_FLAG(name), value)) (void)(::absl::SetFlag(&GMOCK_FLAG(name), value))
#else // GTEST_HAS_ABSL #else // defined(GTEST_HAS_ABSL) && !defined(GTEST_NO_ABSL_FLAGS)
// Macros for defining flags. // Macros for defining flags.
#define GMOCK_DEFINE_bool_(name, default_val, doc) \ #define GMOCK_DEFINE_bool_(name, default_val, doc) \
@ -134,6 +135,6 @@
#define GMOCK_FLAG_GET(name) ::testing::GMOCK_FLAG(name) #define GMOCK_FLAG_GET(name) ::testing::GMOCK_FLAG(name)
#define GMOCK_FLAG_SET(name, value) (void)(::testing::GMOCK_FLAG(name) = value) #define GMOCK_FLAG_SET(name, value) (void)(::testing::GMOCK_FLAG(name) = value)
#endif // GTEST_HAS_ABSL #endif // defined(GTEST_HAS_ABSL) && !defined(GTEST_NO_ABSL_FLAGS)
#endif // GOOGLEMOCK_INCLUDE_GMOCK_INTERNAL_GMOCK_PORT_H_ #endif // GOOGLEMOCK_INCLUDE_GMOCK_INTERNAL_GMOCK_PORT_H_

View File

@ -53,12 +53,12 @@ class BetweenCardinalityImpl : public CardinalityInterface {
: min_(min >= 0 ? min : 0), max_(max >= min_ ? max : min_) { : min_(min >= 0 ? min : 0), max_(max >= min_ ? max : min_) {
std::stringstream ss; std::stringstream ss;
if (min < 0) { if (min < 0) {
ss << "The invocation lower bound must be >= 0, " ss << "The invocation lower bound must be >= 0, " << "but is actually "
<< "but is actually " << min << "."; << min << ".";
internal::Expect(false, __FILE__, __LINE__, ss.str()); internal::Expect(false, __FILE__, __LINE__, ss.str());
} else if (max < 0) { } else if (max < 0) {
ss << "The invocation upper bound must be >= 0, " ss << "The invocation upper bound must be >= 0, " << "but is actually "
<< "but is actually " << max << "."; << max << ".";
internal::Expect(false, __FILE__, __LINE__, ss.str()); internal::Expect(false, __FILE__, __LINE__, ss.str());
} else if (min > max) { } else if (min > max) {
ss << "The invocation upper bound (" << max ss << "The invocation upper bound (" << max

View File

@ -41,8 +41,10 @@
#include <cctype> #include <cctype>
#include <cstdint> #include <cstdint>
#include <cstring> #include <cstring>
#include <iostream>
#include <ostream> // NOLINT #include <ostream> // NOLINT
#include <string> #include <string>
#include <utility>
#include <vector> #include <vector>
#include "gmock/gmock.h" #include "gmock/gmock.h"
@ -87,7 +89,7 @@ GTEST_API_ std::string ConvertIdentifierNameToWords(const char* id_name) {
(!IsDigit(prev_char) && IsDigit(*p)); (!IsDigit(prev_char) && IsDigit(*p));
if (IsAlNum(*p)) { if (IsAlNum(*p)) {
if (starts_new_word && result != "") result += ' '; if (starts_new_word && !result.empty()) result += ' ';
result += ToLower(*p); result += ToLower(*p);
} }
} }
@ -198,6 +200,10 @@ GTEST_API_ void IllegalDoDefault(const char* file, int line) {
"the variable in various places."); "the variable in various places.");
} }
constexpr char UndoWebSafeEncoding(char c) {
return c == '-' ? '+' : c == '_' ? '/' : c;
}
constexpr char UnBase64Impl(char c, const char* const base64, char carry) { constexpr char UnBase64Impl(char c, const char* const base64, char carry) {
return *base64 == 0 ? static_cast<char>(65) return *base64 == 0 ? static_cast<char>(65)
: *base64 == c : *base64 == c
@ -206,13 +212,14 @@ constexpr char UnBase64Impl(char c, const char* const base64, char carry) {
} }
template <size_t... I> template <size_t... I>
constexpr std::array<char, 256> UnBase64Impl(IndexSequence<I...>, constexpr std::array<char, 256> UnBase64Impl(std::index_sequence<I...>,
const char* const base64) { const char* const base64) {
return {{UnBase64Impl(static_cast<char>(I), base64, 0)...}}; return {
{UnBase64Impl(UndoWebSafeEncoding(static_cast<char>(I)), base64, 0)...}};
} }
constexpr std::array<char, 256> UnBase64(const char* const base64) { constexpr std::array<char, 256> UnBase64(const char* const base64) {
return UnBase64Impl(MakeIndexSequence<256>{}, base64); return UnBase64Impl(std::make_index_sequence<256>{}, base64);
} }
static constexpr char kBase64[] = static constexpr char kBase64[] =

View File

@ -53,7 +53,7 @@ GTEST_API_ std::string FormatMatcherDescription(
bool negation, const char* matcher_name, bool negation, const char* matcher_name,
const std::vector<const char*>& param_names, const Strings& param_values) { const std::vector<const char*>& param_names, const Strings& param_values) {
std::string result = ConvertIdentifierNameToWords(matcher_name); std::string result = ConvertIdentifierNameToWords(matcher_name);
if (param_values.size() >= 1) { if (!param_values.empty()) {
result += " " + JoinAsKeyValueTuple(param_names, param_values); result += " " + JoinAsKeyValueTuple(param_names, param_values);
} }
return negation ? "not (" + result + ")" : result; return negation ? "not (" + result + ")" : result;
@ -120,7 +120,7 @@ GTEST_API_ std::string FormatMatcherDescription(
// [1] Cormen, et al (2001). "Section 26.2: The Ford-Fulkerson method". // [1] Cormen, et al (2001). "Section 26.2: The Ford-Fulkerson method".
// "Introduction to Algorithms (Second ed.)", pp. 651-664. // "Introduction to Algorithms (Second ed.)", pp. 651-664.
// [2] "Ford-Fulkerson algorithm", Wikipedia, // [2] "Ford-Fulkerson algorithm", Wikipedia,
// 'http://en.wikipedia.org/wiki/Ford%E2%80%93Fulkerson_algorithm' // 'https://en.wikipedia.org/wiki/Ford%E2%80%93Fulkerson_algorithm'
class MaxBipartiteMatchState { class MaxBipartiteMatchState {
public: public:
explicit MaxBipartiteMatchState(const MatchMatrix& graph) explicit MaxBipartiteMatchState(const MatchMatrix& graph)
@ -236,9 +236,8 @@ static void LogElementMatcherPairVec(const ElementMatcherPairs& pairs,
os << "{"; os << "{";
const char* sep = ""; const char* sep = "";
for (Iter it = pairs.begin(); it != pairs.end(); ++it) { for (Iter it = pairs.begin(); it != pairs.end(); ++it) {
os << sep << "\n (" os << sep << "\n (" << "element #" << it->first << ", " << "matcher #"
<< "element #" << it->first << ", " << it->second << ")";
<< "matcher #" << it->second << ")";
sep = ","; sep = ",";
} }
os << "\n}"; os << "\n}";
@ -374,20 +373,20 @@ bool UnorderedElementsAreMatcherImplBase::VerifyMatchMatrix(
return true; return true;
} }
if (match_flags() == UnorderedMatcherRequire::ExactMatch) { const bool is_exact_match_with_size_discrepency =
if (matrix.LhsSize() != matrix.RhsSize()) { match_flags() == UnorderedMatcherRequire::ExactMatch &&
matrix.LhsSize() != matrix.RhsSize();
if (is_exact_match_with_size_discrepency) {
// The element count doesn't match. If the container is empty, // The element count doesn't match. If the container is empty,
// there's no need to explain anything as Google Mock already // there's no need to explain anything as Google Mock already
// prints the empty container. Otherwise we just need to show // prints the empty container. Otherwise we just need to show
// how many elements there actually are. // how many elements there actually are.
if (matrix.LhsSize() != 0 && listener->IsInterested()) { if (matrix.LhsSize() != 0 && listener->IsInterested()) {
*listener << "which has " << Elements(matrix.LhsSize()); *listener << "which has " << Elements(matrix.LhsSize()) << "\n";
}
return false;
} }
} }
bool result = true; bool result = !is_exact_match_with_size_discrepency;
::std::vector<char> element_matched(matrix.LhsSize(), 0); ::std::vector<char> element_matched(matrix.LhsSize(), 0);
::std::vector<char> matcher_matched(matrix.RhsSize(), 0); ::std::vector<char> matcher_matched(matrix.RhsSize(), 0);

View File

@ -40,6 +40,7 @@
#include <map> #include <map>
#include <memory> #include <memory>
#include <set> #include <set>
#include <sstream>
#include <string> #include <string>
#include <unordered_map> #include <unordered_map>
#include <vector> #include <vector>
@ -48,20 +49,17 @@
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include "gtest/internal/gtest-port.h" #include "gtest/internal/gtest-port.h"
#if GTEST_OS_CYGWIN || GTEST_OS_LINUX || GTEST_OS_MAC #if defined(GTEST_OS_CYGWIN) || defined(GTEST_OS_LINUX) || defined(GTEST_OS_MAC)
#include <unistd.h> // NOLINT #include <unistd.h> // NOLINT
#endif #endif
#if GTEST_OS_QURT #ifdef GTEST_OS_QURT
#include <qurt_event.h> #include <qurt_event.h>
#endif #endif
// Silence C4800 (C4800: 'int *const ': forcing value // Silence C4800 (C4800: 'int *const ': forcing value
// to bool 'true' or 'false') for MSVC 15 // to bool 'true' or 'false') for MSVC 15
#ifdef _MSC_VER #if defined(_MSC_VER) && (_MSC_VER == 1900)
#if _MSC_VER == 1900 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4800)
#pragma warning(push)
#pragma warning(disable : 4800)
#endif
#endif #endif
namespace testing { namespace testing {
@ -98,7 +96,7 @@ ExpectationBase::ExpectationBase(const char* a_file, int a_line,
action_count_checked_(false) {} action_count_checked_(false) {}
// Destructs an ExpectationBase object. // Destructs an ExpectationBase object.
ExpectationBase::~ExpectationBase() {} ExpectationBase::~ExpectationBase() = default;
// Explicitly specifies the cardinality of this expectation. Used by // Explicitly specifies the cardinality of this expectation. Used by
// the subclasses to implement the .Times() clause. // the subclasses to implement the .Times() clause.
@ -295,12 +293,12 @@ void ReportUninterestingCall(CallReaction reaction, const std::string& msg) {
Log(kWarning, Log(kWarning,
msg + msg +
"\nNOTE: You can safely ignore the above warning unless this " "\nNOTE: You can safely ignore the above warning unless this "
"call should not happen. Do not suppress it by blindly adding " "call should not happen. Do not suppress it by adding "
"an EXPECT_CALL() if you don't mean to enforce the call. " "an EXPECT_CALL() if you don't mean to enforce the call. "
"See " "See "
"https://github.com/google/googletest/blob/main/docs/" "https://github.com/google/googletest/blob/main/docs/"
"gmock_cook_book.md#" "gmock_cook_book.md#"
"knowing-when-to-expect for details.\n", "knowing-when-to-expect-useoncall for details.\n",
stack_frames_to_skip); stack_frames_to_skip);
break; break;
default: // FAIL default: // FAIL
@ -311,7 +309,7 @@ void ReportUninterestingCall(CallReaction reaction, const std::string& msg) {
UntypedFunctionMockerBase::UntypedFunctionMockerBase() UntypedFunctionMockerBase::UntypedFunctionMockerBase()
: mock_obj_(nullptr), name_("") {} : mock_obj_(nullptr), name_("") {}
UntypedFunctionMockerBase::~UntypedFunctionMockerBase() {} UntypedFunctionMockerBase::~UntypedFunctionMockerBase() = default;
// Sets the mock object this mock method belongs to, and registers // Sets the mock object this mock method belongs to, and registers
// this information in the global mock registry. Will be called // this information in the global mock registry. Will be called
@ -492,6 +490,7 @@ class MockObjectRegistry {
// failure, unless the user explicitly asked us to ignore it. // failure, unless the user explicitly asked us to ignore it.
~MockObjectRegistry() { ~MockObjectRegistry() {
if (!GMOCK_FLAG_GET(catch_leaked_mocks)) return; if (!GMOCK_FLAG_GET(catch_leaked_mocks)) return;
internal::MutexLock l(&internal::g_gmock_mutex);
int leaked_count = 0; int leaked_count = 0;
for (StateMap::const_iterator it = states_.begin(); it != states_.end(); for (StateMap::const_iterator it = states_.begin(); it != states_.end();
@ -506,7 +505,7 @@ class MockObjectRegistry {
std::cout << internal::FormatFileLocation(state.first_used_file, std::cout << internal::FormatFileLocation(state.first_used_file,
state.first_used_line); state.first_used_line);
std::cout << " ERROR: this mock object"; std::cout << " ERROR: this mock object";
if (state.first_used_test != "") { if (!state.first_used_test.empty()) {
std::cout << " (used in test " << state.first_used_test_suite << "." std::cout << " (used in test " << state.first_used_test_suite << "."
<< state.first_used_test << ")"; << state.first_used_test << ")";
} }
@ -529,10 +528,10 @@ class MockObjectRegistry {
// RUN_ALL_TESTS() has already returned when this destructor is // RUN_ALL_TESTS() has already returned when this destructor is
// called. Therefore we cannot use the normal Google Test // called. Therefore we cannot use the normal Google Test
// failure reporting mechanism. // failure reporting mechanism.
#if GTEST_OS_QURT #ifdef GTEST_OS_QURT
qurt_exception_raise_fatal(); qurt_exception_raise_fatal();
#else #else
_exit(1); // We cannot call exit() as it is not reentrant and _Exit(1); // We cannot call exit() as it is not reentrant and
// may already have been called. // may already have been called.
#endif #endif
} }
@ -748,13 +747,13 @@ void Mock::ClearDefaultActionsLocked(void* mock_obj)
// needed by VerifyAndClearExpectationsLocked(). // needed by VerifyAndClearExpectationsLocked().
} }
Expectation::Expectation() {} Expectation::Expectation() = default;
Expectation::Expectation( Expectation::Expectation(
const std::shared_ptr<internal::ExpectationBase>& an_expectation_base) const std::shared_ptr<internal::ExpectationBase>& an_expectation_base)
: expectation_base_(an_expectation_base) {} : expectation_base_(an_expectation_base) {}
Expectation::~Expectation() {} Expectation::~Expectation() = default;
// Adds an expectation to a sequence. // Adds an expectation to a sequence.
void Sequence::AddExpectation(const Expectation& expectation) const { void Sequence::AddExpectation(const Expectation& expectation) const {
@ -788,8 +787,6 @@ InSequence::~InSequence() {
} // namespace testing } // namespace testing
#ifdef _MSC_VER #if defined(_MSC_VER) && (_MSC_VER == 1900)
#if _MSC_VER == 1900 GTEST_DISABLE_MSC_WARNINGS_POP_() // 4800
#pragma warning(pop)
#endif
#endif #endif

View File

@ -29,6 +29,8 @@
#include "gmock/gmock.h" #include "gmock/gmock.h"
#include <string>
#include "gmock/internal/gmock-port.h" #include "gmock/internal/gmock-port.h"
GMOCK_DEFINE_bool_(catch_leaked_mocks, true, GMOCK_DEFINE_bool_(catch_leaked_mocks, true,

View File

@ -32,8 +32,9 @@
#include "gmock/gmock.h" #include "gmock/gmock.h"
#include "gtest/gtest.h" #include "gtest/gtest.h"
#if GTEST_OS_ESP8266 || GTEST_OS_ESP32 #if defined(GTEST_OS_ESP8266) || defined(GTEST_OS_ESP32) || \
#if GTEST_OS_ESP8266 (defined(GTEST_OS_NRF52) && defined(ARDUINO))
#ifdef GTEST_OS_ESP8266
extern "C" { extern "C" {
#endif #endif
void setup() { void setup() {
@ -43,7 +44,7 @@ void setup() {
testing::InitGoogleMock(); testing::InitGoogleMock();
} }
void loop() { RUN_ALL_TESTS(); } void loop() { RUN_ALL_TESTS(); }
#if GTEST_OS_ESP8266 #ifdef GTEST_OS_ESP8266
} }
#endif #endif
@ -55,7 +56,7 @@ void loop() { RUN_ALL_TESTS(); }
// Windows. See the following link to track the current status of this bug: // Windows. See the following link to track the current status of this bug:
// https://web.archive.org/web/20170912203238/connect.microsoft.com/VisualStudio/feedback/details/394464/wmain-link-error-in-the-static-library // https://web.archive.org/web/20170912203238/connect.microsoft.com/VisualStudio/feedback/details/394464/wmain-link-error-in-the-static-library
// // NOLINT // // NOLINT
#if GTEST_OS_WINDOWS_MOBILE #ifdef GTEST_OS_WINDOWS_MOBILE
#include <tchar.h> // NOLINT #include <tchar.h> // NOLINT
GTEST_API_ int _tmain(int argc, TCHAR** argv) { GTEST_API_ int _tmain(int argc, TCHAR** argv) {

View File

@ -31,33 +31,33 @@
// //
// This file tests the built-in actions. // This file tests the built-in actions.
// Silence C4100 (unreferenced formal parameter) and C4503 (decorated name
// length exceeded) for MSVC.
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable : 4100)
#pragma warning(disable : 4503)
#if _MSC_VER == 1900
// and silence C4800 (C4800: 'int *const ': forcing value
// to bool 'true' or 'false') for MSVC 15
#pragma warning(disable : 4800)
#endif
#endif
#include "gmock/gmock-actions.h" #include "gmock/gmock-actions.h"
#include <algorithm> #include <algorithm>
#include <functional> #include <functional>
#include <iterator> #include <iterator>
#include <memory> #include <memory>
#include <sstream>
#include <string> #include <string>
#include <tuple>
#include <type_traits> #include <type_traits>
#include <utility>
#include <vector> #include <vector>
#include "gmock/gmock.h" #include "gmock/gmock.h"
#include "gmock/internal/gmock-port.h" #include "gmock/internal/gmock-port.h"
#include "gtest/gtest-spi.h" #include "gtest/gtest-spi.h"
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include "gtest/internal/gtest-port.h"
// Silence C4100 (unreferenced formal parameter) and C4503 (decorated name
// length exceeded) for MSVC.
GTEST_DISABLE_MSC_WARNINGS_PUSH_(4100 4503)
#if defined(_MSC_VER) && (_MSC_VER == 1900)
// and silence C4800 (C4800: 'int *const ': forcing value
// to bool 'true' or 'false') for MSVC 15
GTEST_DISABLE_MSC_WARNINGS_PUSH_(4800)
#endif
namespace testing { namespace testing {
namespace { namespace {
@ -188,7 +188,7 @@ TEST(TypeTraits, IsInvocableRV) {
struct C { struct C {
int operator()() const { return 0; } int operator()() const { return 0; }
void operator()(int) & {} void operator()(int) & {}
std::string operator()(int) && { return ""; }; std::string operator()(int) && { return ""; }
}; };
// The first overload is callable for const and non-const rvalues and lvalues. // The first overload is callable for const and non-const rvalues and lvalues.
@ -222,7 +222,8 @@ TEST(TypeTraits, IsInvocableRV) {
// In C++17 and above, where it's guaranteed that functions can return // In C++17 and above, where it's guaranteed that functions can return
// non-moveable objects, everything should work fine for non-moveable rsult // non-moveable objects, everything should work fine for non-moveable rsult
// types too. // types too.
#if defined(__cplusplus) && __cplusplus >= 201703L // TODO(b/396121064) - Fix this test under MSVC
#ifndef _MSC_VER
{ {
struct NonMoveable { struct NonMoveable {
NonMoveable() = default; NonMoveable() = default;
@ -243,7 +244,7 @@ TEST(TypeTraits, IsInvocableRV) {
static_assert(!internal::is_callable_r<int, Callable>::value); static_assert(!internal::is_callable_r<int, Callable>::value);
static_assert(!internal::is_callable_r<NonMoveable, Callable, int>::value); static_assert(!internal::is_callable_r<NonMoveable, Callable, int>::value);
} }
#endif // C++17 and above #endif // _MSC_VER
// Nothing should choke when we try to call other arguments besides directly // Nothing should choke when we try to call other arguments besides directly
// callable objects, but they should not show up as callable. // callable objects, but they should not show up as callable.
@ -440,15 +441,15 @@ TEST(DefaultValueDeathTest, GetReturnsBuiltInDefaultValueWhenUnset) {
EXPECT_EQ(0, DefaultValue<int>::Get()); EXPECT_EQ(0, DefaultValue<int>::Get());
EXPECT_DEATH_IF_SUPPORTED({ DefaultValue<MyNonDefaultConstructible>::Get(); }, EXPECT_DEATH_IF_SUPPORTED(
""); { DefaultValue<MyNonDefaultConstructible>::Get(); }, "");
} }
TEST(DefaultValueTest, GetWorksForMoveOnlyIfSet) { TEST(DefaultValueTest, GetWorksForMoveOnlyIfSet) {
EXPECT_TRUE(DefaultValue<std::unique_ptr<int>>::Exists()); EXPECT_TRUE(DefaultValue<std::unique_ptr<int>>::Exists());
EXPECT_TRUE(DefaultValue<std::unique_ptr<int>>::Get() == nullptr); EXPECT_TRUE(DefaultValue<std::unique_ptr<int>>::Get() == nullptr);
DefaultValue<std::unique_ptr<int>>::SetFactory( DefaultValue<std::unique_ptr<int>>::SetFactory(
[] { return std::unique_ptr<int>(new int(42)); }); [] { return std::make_unique<int>(42); });
EXPECT_TRUE(DefaultValue<std::unique_ptr<int>>::Exists()); EXPECT_TRUE(DefaultValue<std::unique_ptr<int>>::Exists());
std::unique_ptr<int> i = DefaultValue<std::unique_ptr<int>>::Get(); std::unique_ptr<int> i = DefaultValue<std::unique_ptr<int>>::Get();
EXPECT_EQ(42, *i); EXPECT_EQ(42, *i);
@ -504,8 +505,8 @@ TEST(DefaultValueOfReferenceDeathTest, GetReturnsBuiltInDefaultValueWhenUnset) {
EXPECT_FALSE(DefaultValue<MyNonDefaultConstructible&>::IsSet()); EXPECT_FALSE(DefaultValue<MyNonDefaultConstructible&>::IsSet());
EXPECT_DEATH_IF_SUPPORTED({ DefaultValue<int&>::Get(); }, ""); EXPECT_DEATH_IF_SUPPORTED({ DefaultValue<int&>::Get(); }, "");
EXPECT_DEATH_IF_SUPPORTED({ DefaultValue<MyNonDefaultConstructible>::Get(); }, EXPECT_DEATH_IF_SUPPORTED(
""); { DefaultValue<MyNonDefaultConstructible>::Get(); }, "");
} }
// Tests that ActionInterface can be implemented by defining the // Tests that ActionInterface can be implemented by defining the
@ -986,7 +987,7 @@ TEST(ReturnRoundRobinTest, WorksForVector) {
class MockClass { class MockClass {
public: public:
MockClass() {} MockClass() = default;
MOCK_METHOD1(IntFunc, int(bool flag)); // NOLINT MOCK_METHOD1(IntFunc, int(bool flag)); // NOLINT
MOCK_METHOD0(Foo, MyNonDefaultConstructible()); MOCK_METHOD0(Foo, MyNonDefaultConstructible());
@ -1410,7 +1411,7 @@ TEST(DoAll, ProvidesLvalueReferencesToInitialActions) {
void operator()(Obj&&) const { FAIL() << "Unexpected call"; } void operator()(Obj&&) const { FAIL() << "Unexpected call"; }
}; };
MockFunction<void(Obj &&)> mock; MockFunction<void(Obj&&)> mock;
EXPECT_CALL(mock, Call) EXPECT_CALL(mock, Call)
.WillOnce(DoAll(InitialAction{}, InitialAction{}, [](Obj&&) {})) .WillOnce(DoAll(InitialAction{}, InitialAction{}, [](Obj&&) {}))
.WillRepeatedly(DoAll(InitialAction{}, InitialAction{}, [](Obj&&) {})); .WillRepeatedly(DoAll(InitialAction{}, InitialAction{}, [](Obj&&) {}));
@ -1438,7 +1439,7 @@ TEST(DoAll, ProvidesLvalueReferencesToInitialActions) {
void operator()(Obj&) && {} void operator()(Obj&) && {}
}; };
MockFunction<void(Obj &&)> mock; MockFunction<void(Obj&&)> mock;
EXPECT_CALL(mock, Call) EXPECT_CALL(mock, Call)
.WillOnce(DoAll(InitialAction{}, InitialAction{}, [](Obj&&) {})); .WillOnce(DoAll(InitialAction{}, InitialAction{}, [](Obj&&) {}));
@ -1476,6 +1477,54 @@ TEST(DoAll, SupportsTypeErasedActions) {
} }
} }
// A DoAll action should be convertible to a OnceAction, even when its component
// sub-actions are user-provided types that define only an Action conversion
// operator. If they supposed being called more than once then they also support
// being called at most once.
TEST(DoAll, ConvertibleToOnceActionWithUserProvidedActionConversion) {
// Simplest case: only one sub-action.
struct CustomFinal final {
operator Action<int()>() { // NOLINT
return Return(17);
}
operator Action<int(int, char)>() { // NOLINT
return Return(19);
}
};
{
OnceAction<int()> action = DoAll(CustomFinal{});
EXPECT_EQ(17, std::move(action).Call());
}
{
OnceAction<int(int, char)> action = DoAll(CustomFinal{});
EXPECT_EQ(19, std::move(action).Call(0, 0));
}
// It should also work with multiple sub-actions.
struct CustomInitial final {
operator Action<void()>() { // NOLINT
return [] {};
}
operator Action<void(int, char)>() { // NOLINT
return [] {};
}
};
{
OnceAction<int()> action = DoAll(CustomInitial{}, CustomFinal{});
EXPECT_EQ(17, std::move(action).Call());
}
{
OnceAction<int(int, char)> action = DoAll(CustomInitial{}, CustomFinal{});
EXPECT_EQ(19, std::move(action).Call(0, 0));
}
}
// Tests using WithArgs and with an action that takes 1 argument. // Tests using WithArgs and with an action that takes 1 argument.
TEST(WithArgsTest, OneArg) { TEST(WithArgsTest, OneArg) {
Action<bool(double x, int n)> a = WithArgs<1>(Invoke(Unary)); // NOLINT Action<bool(double x, int n)> a = WithArgs<1>(Invoke(Unary)); // NOLINT
@ -1596,7 +1645,23 @@ TEST(WithArgsTest, RefQualifiedInnerAction) {
EXPECT_EQ(19, mock.AsStdFunction()(0, 17)); EXPECT_EQ(19, mock.AsStdFunction()(0, 17));
} }
#if !GTEST_OS_WINDOWS_MOBILE // It should be fine to provide an lvalue WithArgsAction to WillOnce, even when
// the inner action only wants to convert to OnceAction.
TEST(WithArgsTest, ProvideAsLvalueToWillOnce) {
struct SomeAction {
operator OnceAction<int(int)>() const { // NOLINT
return [](const int arg) { return arg + 2; };
}
};
const auto wa = WithArg<1>(SomeAction{});
MockFunction<int(int, int)> mock;
EXPECT_CALL(mock, Call).WillOnce(wa);
EXPECT_EQ(19, mock.AsStdFunction()(0, 17));
}
#ifndef GTEST_OS_WINDOWS_MOBILE
class SetErrnoAndReturnTest : public testing::Test { class SetErrnoAndReturnTest : public testing::Test {
protected: protected:
@ -1755,9 +1820,7 @@ TEST(ReturnNewTest, ConstructorThatTakes10Arguments) {
delete c; delete c;
} }
std::unique_ptr<int> UniquePtrSource() { std::unique_ptr<int> UniquePtrSource() { return std::make_unique<int>(19); }
return std::unique_ptr<int>(new int(19));
}
std::vector<std::unique_ptr<int>> VectorUniquePtrSource() { std::vector<std::unique_ptr<int>> VectorUniquePtrSource() {
std::vector<std::unique_ptr<int>> out; std::vector<std::unique_ptr<int>> out;
@ -1806,7 +1869,7 @@ TEST(MockMethodTest, CanReturnMoveOnlyValue_Invoke) {
// Check default value // Check default value
DefaultValue<std::unique_ptr<int>>::SetFactory( DefaultValue<std::unique_ptr<int>>::SetFactory(
[] { return std::unique_ptr<int>(new int(42)); }); [] { return std::make_unique<int>(42); });
EXPECT_EQ(42, *mock.MakeUnique()); EXPECT_EQ(42, *mock.MakeUnique());
EXPECT_CALL(mock, MakeUnique()).WillRepeatedly(Invoke(UniquePtrSource)); EXPECT_CALL(mock, MakeUnique()).WillRepeatedly(Invoke(UniquePtrSource));
@ -1826,7 +1889,7 @@ TEST(MockMethodTest, CanReturnMoveOnlyValue_Invoke) {
TEST(MockMethodTest, CanTakeMoveOnlyValue) { TEST(MockMethodTest, CanTakeMoveOnlyValue) {
MockClass mock; MockClass mock;
auto make = [](int i) { return std::unique_ptr<int>(new int(i)); }; auto make = [](int i) { return std::make_unique<int>(i); };
EXPECT_CALL(mock, TakeUnique(_)).WillRepeatedly([](std::unique_ptr<int> i) { EXPECT_CALL(mock, TakeUnique(_)).WillRepeatedly([](std::unique_ptr<int> i) {
return *i; return *i;
@ -2057,9 +2120,7 @@ struct Double {
} }
}; };
std::unique_ptr<int> UniqueInt(int i) { std::unique_ptr<int> UniqueInt(int i) { return std::make_unique<int>(i); }
return std::unique_ptr<int>(new int(i));
}
TEST(FunctorActionTest, ActionFromFunction) { TEST(FunctorActionTest, ActionFromFunction) {
Action<int(int, int&, int*)> a = &Add; Action<int(int, int&, int*)> a = &Add;
@ -2165,3 +2226,8 @@ TEST(ActionMacro, LargeArity) {
} // namespace } // namespace
} // namespace testing } // namespace testing
#if defined(_MSC_VER) && (_MSC_VER == 1900)
GTEST_DISABLE_MSC_WARNINGS_POP_() // 4800
#endif
GTEST_DISABLE_MSC_WARNINGS_POP_() // 4100 4503

View File

@ -31,6 +31,8 @@
// //
// This file tests the built-in cardinalities. // This file tests the built-in cardinalities.
#include <ostream>
#include "gmock/gmock.h" #include "gmock/gmock.h"
#include "gtest/gtest-spi.h" #include "gtest/gtest-spi.h"
#include "gtest/gtest.h" #include "gtest/gtest.h"
@ -50,7 +52,7 @@ using testing::MakeCardinality;
class MockFoo { class MockFoo {
public: public:
MockFoo() {} MockFoo() = default;
MOCK_METHOD0(Bar, int()); // NOLINT MOCK_METHOD0(Bar, int()); // NOLINT
private: private:

View File

@ -27,18 +27,15 @@
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// Silence C4503 (decorated name length exceeded) for MSVC.
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable : 4503)
#endif
// Google Mock - a framework for writing C++ mock classes. // Google Mock - a framework for writing C++ mock classes.
// //
// This file tests the function mocker classes. // This file tests the function mocker classes.
#include "gmock/gmock-function-mocker.h" #include "gmock/gmock-function-mocker.h"
#if GTEST_OS_WINDOWS // Silence C4503 (decorated name length exceeded) for MSVC.
GTEST_DISABLE_MSC_WARNINGS_PUSH_(4503)
#ifdef GTEST_OS_WINDOWS
// MSDN says the header file to be included for STDMETHOD is BaseTyps.h but // MSDN says the header file to be included for STDMETHOD is BaseTyps.h but
// we are getting compiler errors if we use basetyps.h, hence including // we are getting compiler errors if we use basetyps.h, hence including
// objbase.h for definition of STDMETHOD. // objbase.h for definition of STDMETHOD.
@ -73,7 +70,7 @@ using testing::TypedEq;
template <typename T> template <typename T>
class TemplatedCopyable { class TemplatedCopyable {
public: public:
TemplatedCopyable() {} TemplatedCopyable() = default;
template <typename U> template <typename U>
TemplatedCopyable(const U& other) {} // NOLINT TemplatedCopyable(const U& other) {} // NOLINT
@ -81,7 +78,7 @@ class TemplatedCopyable {
class FooInterface { class FooInterface {
public: public:
virtual ~FooInterface() {} virtual ~FooInterface() = default;
virtual void VoidReturning(int x) = 0; virtual void VoidReturning(int x) = 0;
@ -94,7 +91,7 @@ class FooInterface {
virtual bool TakesNonConstReference(int& n) = 0; // NOLINT virtual bool TakesNonConstReference(int& n) = 0; // NOLINT
virtual std::string TakesConstReference(const int& n) = 0; virtual std::string TakesConstReference(const int& n) = 0;
virtual bool TakesConst(const int x) = 0; virtual bool TakesConst(int x) = 0;
virtual int OverloadedOnArgumentNumber() = 0; virtual int OverloadedOnArgumentNumber() = 0;
virtual int OverloadedOnArgumentNumber(int n) = 0; virtual int OverloadedOnArgumentNumber(int n) = 0;
@ -123,7 +120,7 @@ class FooInterface {
virtual int RefQualifiedOverloaded() & = 0; virtual int RefQualifiedOverloaded() & = 0;
virtual int RefQualifiedOverloaded() && = 0; virtual int RefQualifiedOverloaded() && = 0;
#if GTEST_OS_WINDOWS #ifdef GTEST_OS_WINDOWS
STDMETHOD_(int, CTNullary)() = 0; STDMETHOD_(int, CTNullary)() = 0;
STDMETHOD_(bool, CTUnary)(int x) = 0; STDMETHOD_(bool, CTUnary)(int x) = 0;
STDMETHOD_(int, CTDecimal) STDMETHOD_(int, CTDecimal)
@ -137,13 +134,10 @@ class FooInterface {
// significant in determining whether two virtual functions had the same // significant in determining whether two virtual functions had the same
// signature. This was fixed in Visual Studio 2008. However, the compiler // signature. This was fixed in Visual Studio 2008. However, the compiler
// still emits a warning that alerts about this change in behavior. // still emits a warning that alerts about this change in behavior.
#ifdef _MSC_VER GTEST_DISABLE_MSC_WARNINGS_PUSH_(4373)
#pragma warning(push)
#pragma warning(disable : 4373)
#endif
class MockFoo : public FooInterface { class MockFoo : public FooInterface {
public: public:
MockFoo() {} MockFoo() = default;
// Makes sure that a mock function parameter can be named. // Makes sure that a mock function parameter can be named.
MOCK_METHOD(void, VoidReturning, (int n)); // NOLINT MOCK_METHOD(void, VoidReturning, (int n)); // NOLINT
@ -184,7 +178,7 @@ class MockFoo : public FooInterface {
MOCK_METHOD(int (*)(bool), ReturnsFunctionPointer1, (int), ()); MOCK_METHOD(int (*)(bool), ReturnsFunctionPointer1, (int), ());
MOCK_METHOD(fn_ptr, ReturnsFunctionPointer2, (int), ()); MOCK_METHOD(fn_ptr, ReturnsFunctionPointer2, (int), ());
#if GTEST_OS_WINDOWS #ifdef GTEST_OS_WINDOWS
MOCK_METHOD(int, CTNullary, (), (Calltype(STDMETHODCALLTYPE))); MOCK_METHOD(int, CTNullary, (), (Calltype(STDMETHODCALLTYPE)));
MOCK_METHOD(bool, CTUnary, (int), (Calltype(STDMETHODCALLTYPE))); MOCK_METHOD(bool, CTUnary, (int), (Calltype(STDMETHODCALLTYPE)));
MOCK_METHOD(int, CTDecimal, MOCK_METHOD(int, CTDecimal,
@ -214,7 +208,7 @@ class MockFoo : public FooInterface {
class LegacyMockFoo : public FooInterface { class LegacyMockFoo : public FooInterface {
public: public:
LegacyMockFoo() {} LegacyMockFoo() = default;
// Makes sure that a mock function parameter can be named. // Makes sure that a mock function parameter can be named.
MOCK_METHOD1(VoidReturning, void(int n)); // NOLINT MOCK_METHOD1(VoidReturning, void(int n)); // NOLINT
@ -254,7 +248,7 @@ class LegacyMockFoo : public FooInterface {
MOCK_METHOD1(ReturnsFunctionPointer1, int (*(int))(bool)); MOCK_METHOD1(ReturnsFunctionPointer1, int (*(int))(bool));
MOCK_METHOD1(ReturnsFunctionPointer2, fn_ptr(int)); MOCK_METHOD1(ReturnsFunctionPointer2, fn_ptr(int));
#if GTEST_OS_WINDOWS #ifdef GTEST_OS_WINDOWS
MOCK_METHOD0_WITH_CALLTYPE(STDMETHODCALLTYPE, CTNullary, int()); MOCK_METHOD0_WITH_CALLTYPE(STDMETHODCALLTYPE, CTNullary, int());
MOCK_METHOD1_WITH_CALLTYPE(STDMETHODCALLTYPE, CTUnary, bool(int)); // NOLINT MOCK_METHOD1_WITH_CALLTYPE(STDMETHODCALLTYPE, CTUnary, bool(int)); // NOLINT
MOCK_METHOD10_WITH_CALLTYPE(STDMETHODCALLTYPE, CTDecimal, MOCK_METHOD10_WITH_CALLTYPE(STDMETHODCALLTYPE, CTDecimal,
@ -285,9 +279,7 @@ class LegacyMockFoo : public FooInterface {
LegacyMockFoo& operator=(const LegacyMockFoo&) = delete; LegacyMockFoo& operator=(const LegacyMockFoo&) = delete;
}; };
#ifdef _MSC_VER GTEST_DISABLE_MSC_WARNINGS_POP_() // 4373
#pragma warning(pop)
#endif
template <class T> template <class T>
class FunctionMockerTest : public testing::Test { class FunctionMockerTest : public testing::Test {
@ -333,8 +325,8 @@ TYPED_TEST(FunctionMockerTest, MocksBinaryFunction) {
// Tests mocking a decimal function. // Tests mocking a decimal function.
TYPED_TEST(FunctionMockerTest, MocksDecimalFunction) { TYPED_TEST(FunctionMockerTest, MocksDecimalFunction) {
EXPECT_CALL(this->mock_foo_, EXPECT_CALL(this->mock_foo_, Decimal(true, 'a', 0, 0, 1L, A<float>(), Lt(100),
Decimal(true, 'a', 0, 0, 1L, A<float>(), Lt(100), 5U, NULL, "hi")) 5U, nullptr, "hi"))
.WillOnce(Return(5)); .WillOnce(Return(5));
EXPECT_EQ(5, this->foo_->Decimal(true, 'a', 0, 0, 1, 0, 0, 5, nullptr, "hi")); EXPECT_EQ(5, this->foo_->Decimal(true, 'a', 0, 0, 1, 0, 0, 5, nullptr, "hi"));
@ -412,7 +404,7 @@ TYPED_TEST(FunctionMockerTest, MocksTypeWithTemplatedCopyCtor) {
EXPECT_TRUE(this->foo_->TypeWithTemplatedCopyCtor(TemplatedCopyable<int>())); EXPECT_TRUE(this->foo_->TypeWithTemplatedCopyCtor(TemplatedCopyable<int>()));
} }
#if GTEST_OS_WINDOWS #ifdef GTEST_OS_WINDOWS
// Tests mocking a nullary function with calltype. // Tests mocking a nullary function with calltype.
TYPED_TEST(FunctionMockerTest, MocksNullaryFunctionWithCallType) { TYPED_TEST(FunctionMockerTest, MocksNullaryFunctionWithCallType) {
EXPECT_CALL(this->mock_foo_, CTNullary()) EXPECT_CALL(this->mock_foo_, CTNullary())
@ -495,7 +487,7 @@ TEST(FunctionMockerTest, RefQualified) {
class MockB { class MockB {
public: public:
MockB() {} MockB() = default;
MOCK_METHOD(void, DoB, ()); MOCK_METHOD(void, DoB, ());
@ -506,7 +498,7 @@ class MockB {
class LegacyMockB { class LegacyMockB {
public: public:
LegacyMockB() {} LegacyMockB() = default;
MOCK_METHOD0(DoB, void()); MOCK_METHOD0(DoB, void());
@ -542,7 +534,7 @@ TYPED_TEST(ExpectCallTest, UnmentionedFunctionCanBeCalledAnyNumberOfTimes) {
template <typename T> template <typename T>
class StackInterface { class StackInterface {
public: public:
virtual ~StackInterface() {} virtual ~StackInterface() = default;
// Template parameter appears in function parameter. // Template parameter appears in function parameter.
virtual void Push(const T& value) = 0; virtual void Push(const T& value) = 0;
@ -555,7 +547,7 @@ class StackInterface {
template <typename T> template <typename T>
class MockStack : public StackInterface<T> { class MockStack : public StackInterface<T> {
public: public:
MockStack() {} MockStack() = default;
MOCK_METHOD(void, Push, (const T& elem), ()); MOCK_METHOD(void, Push, (const T& elem), ());
MOCK_METHOD(void, Pop, (), (final)); MOCK_METHOD(void, Pop, (), (final));
@ -574,7 +566,7 @@ class MockStack : public StackInterface<T> {
template <typename T> template <typename T>
class LegacyMockStack : public StackInterface<T> { class LegacyMockStack : public StackInterface<T> {
public: public:
LegacyMockStack() {} LegacyMockStack() = default;
MOCK_METHOD1_T(Push, void(const T& elem)); MOCK_METHOD1_T(Push, void(const T& elem));
MOCK_METHOD0_T(Pop, void()); MOCK_METHOD0_T(Pop, void());
@ -628,7 +620,7 @@ TYPED_TEST(TemplateMockTest, MethodWithCommaInReturnTypeWorks) {
EXPECT_EQ(a_map, mock.ReturnTypeWithComma(1)); EXPECT_EQ(a_map, mock.ReturnTypeWithComma(1));
} }
#if GTEST_OS_WINDOWS #ifdef GTEST_OS_WINDOWS
// Tests mocking template interfaces with calltype. // Tests mocking template interfaces with calltype.
template <typename T> template <typename T>
@ -719,7 +711,7 @@ TYPED_TEST(TemplateMockTestWithCallType, Works) {
class MockOverloadedOnArgNumber { class MockOverloadedOnArgNumber {
public: public:
MockOverloadedOnArgNumber() {} MockOverloadedOnArgNumber() = default;
MY_MOCK_METHODS1_; MY_MOCK_METHODS1_;
@ -731,7 +723,7 @@ class MockOverloadedOnArgNumber {
class LegacyMockOverloadedOnArgNumber { class LegacyMockOverloadedOnArgNumber {
public: public:
LegacyMockOverloadedOnArgNumber() {} LegacyMockOverloadedOnArgNumber() = default;
LEGACY_MY_MOCK_METHODS1_; LEGACY_MY_MOCK_METHODS1_;
@ -766,7 +758,7 @@ TYPED_TEST(OverloadedMockMethodTest, CanOverloadOnArgNumberInMacroBody) {
class MockOverloadedOnConstness { class MockOverloadedOnConstness {
public: public:
MockOverloadedOnConstness() {} MockOverloadedOnConstness() = default;
MY_MOCK_METHODS2_; MY_MOCK_METHODS2_;
@ -1002,3 +994,5 @@ TEST(MockMethodMockFunctionTest, NoexceptSpecifierPreserved) {
} // namespace gmock_function_mocker_test } // namespace gmock_function_mocker_test
} // namespace testing } // namespace testing
GTEST_DISABLE_MSC_WARNINGS_POP_() // 4503

View File

@ -40,6 +40,7 @@
#include <memory> #include <memory>
#include <sstream> #include <sstream>
#include <string> #include <string>
#include <tuple>
#include <vector> #include <vector>
#include "gmock/gmock.h" #include "gmock/gmock.h"
@ -56,7 +57,7 @@
#include "src/gtest-internal-inl.h" #include "src/gtest-internal-inl.h"
#undef GTEST_IMPLEMENTATION_ #undef GTEST_IMPLEMENTATION_
#if GTEST_OS_CYGWIN #ifdef GTEST_OS_CYGWIN
#include <sys/types.h> // For ssize_t. NOLINT #include <sys/types.h> // For ssize_t. NOLINT
#endif #endif
@ -167,7 +168,7 @@ TEST(KindOfTest, Integer) {
EXPECT_EQ(kInteger, GMOCK_KIND_OF_(unsigned long long)); // NOLINT EXPECT_EQ(kInteger, GMOCK_KIND_OF_(unsigned long long)); // NOLINT
EXPECT_EQ(kInteger, GMOCK_KIND_OF_(wchar_t)); // NOLINT EXPECT_EQ(kInteger, GMOCK_KIND_OF_(wchar_t)); // NOLINT
EXPECT_EQ(kInteger, GMOCK_KIND_OF_(size_t)); // NOLINT EXPECT_EQ(kInteger, GMOCK_KIND_OF_(size_t)); // NOLINT
#if GTEST_OS_LINUX || GTEST_OS_MAC || GTEST_OS_CYGWIN #if defined(GTEST_OS_LINUX) || defined(GTEST_OS_MAC) || defined(GTEST_OS_CYGWIN)
// ssize_t is not defined on Windows and possibly some other OSes. // ssize_t is not defined on Windows and possibly some other OSes.
EXPECT_EQ(kInteger, GMOCK_KIND_OF_(ssize_t)); // NOLINT EXPECT_EQ(kInteger, GMOCK_KIND_OF_(ssize_t)); // NOLINT
#endif #endif

View File

@ -31,15 +31,19 @@
// //
// This file tests some commonly used argument matchers. // This file tests some commonly used argument matchers.
#include <cmath>
#include <limits>
#include <memory>
#include <ostream>
#include <string>
#include "gmock/gmock.h"
#include "test/gmock-matchers_test.h"
#include "gtest/gtest.h"
// Silence warning C4244: 'initializing': conversion from 'int' to 'short', // Silence warning C4244: 'initializing': conversion from 'int' to 'short',
// possible loss of data and C4100, unreferenced local parameter // possible loss of data and C4100, unreferenced local parameter
#ifdef _MSC_VER GTEST_DISABLE_MSC_WARNINGS_PUSH_(4244 4100)
#pragma warning(push)
#pragma warning(disable : 4244)
#pragma warning(disable : 4100)
#endif
#include "test/gmock-matchers_test.h"
namespace testing { namespace testing {
namespace gmock_matchers_test { namespace gmock_matchers_test {
@ -395,6 +399,188 @@ TEST(NanSensitiveDoubleNearTest, CanDescribeSelfWithNaNs) {
EXPECT_EQ("are an almost-equal pair", Describe(m)); EXPECT_EQ("are an almost-equal pair", Describe(m));
} }
// Tests that DistanceFrom() can describe itself properly.
TEST(DistanceFrom, CanDescribeSelf) {
Matcher<double> m = DistanceFrom(1.5, Lt(0.1));
EXPECT_EQ(Describe(m), "is < 0.1 away from 1.5");
m = DistanceFrom(2.5, Gt(0.2));
EXPECT_EQ(Describe(m), "is > 0.2 away from 2.5");
}
// Tests that DistanceFrom() can explain match failure.
TEST(DistanceFrom, CanExplainMatchFailure) {
Matcher<double> m = DistanceFrom(1.5, Lt(0.1));
EXPECT_EQ(Explain(m, 2.0), "which is 0.5 away from 1.5");
}
// Tests that DistanceFrom() matches a double that is within the given range of
// the given value.
TEST(DistanceFrom, MatchesDoubleWithinRange) {
const Matcher<double> m = DistanceFrom(0.5, Le(0.1));
EXPECT_TRUE(m.Matches(0.45));
EXPECT_TRUE(m.Matches(0.5));
EXPECT_TRUE(m.Matches(0.55));
EXPECT_FALSE(m.Matches(0.39));
EXPECT_FALSE(m.Matches(0.61));
}
// Tests that DistanceFrom() matches a double reference that is within the given
// range of the given value.
TEST(DistanceFrom, MatchesDoubleRefWithinRange) {
const Matcher<const double&> m = DistanceFrom(0.5, Le(0.1));
EXPECT_TRUE(m.Matches(0.45));
EXPECT_TRUE(m.Matches(0.5));
EXPECT_TRUE(m.Matches(0.55));
EXPECT_FALSE(m.Matches(0.39));
EXPECT_FALSE(m.Matches(0.61));
}
// Tests that DistanceFrom() can be implicitly converted to a matcher depending
// on the type of the argument.
TEST(DistanceFrom, CanBeImplicitlyConvertedToMatcher) {
EXPECT_THAT(0.58, DistanceFrom(0.5, Le(0.1)));
EXPECT_THAT(0.2, Not(DistanceFrom(0.5, Le(0.1))));
EXPECT_THAT(0.58f, DistanceFrom(0.5f, Le(0.1f)));
EXPECT_THAT(0.7f, Not(DistanceFrom(0.5f, Le(0.1f))));
}
// Tests that DistanceFrom() can be used on compatible types (i.e. not
// everything has to be of the same type).
TEST(DistanceFrom, CanBeUsedOnCompatibleTypes) {
EXPECT_THAT(0.58, DistanceFrom(0.5, Le(0.1f)));
EXPECT_THAT(0.2, Not(DistanceFrom(0.5, Le(0.1f))));
EXPECT_THAT(0.58, DistanceFrom(0.5f, Le(0.1)));
EXPECT_THAT(0.2, Not(DistanceFrom(0.5f, Le(0.1))));
EXPECT_THAT(0.58, DistanceFrom(0.5f, Le(0.1f)));
EXPECT_THAT(0.2, Not(DistanceFrom(0.5f, Le(0.1f))));
EXPECT_THAT(0.58f, DistanceFrom(0.5, Le(0.1)));
EXPECT_THAT(0.2f, Not(DistanceFrom(0.5, Le(0.1))));
EXPECT_THAT(0.58f, DistanceFrom(0.5, Le(0.1f)));
EXPECT_THAT(0.2f, Not(DistanceFrom(0.5, Le(0.1f))));
EXPECT_THAT(0.58f, DistanceFrom(0.5f, Le(0.1)));
EXPECT_THAT(0.2f, Not(DistanceFrom(0.5f, Le(0.1))));
}
// A 2-dimensional point. For testing using DistanceFrom() with a custom type
// that doesn't have a built-in distance function.
class Point {
public:
Point(double x, double y) : x_(x), y_(y) {}
double x() const { return x_; }
double y() const { return y_; }
private:
double x_;
double y_;
};
// Returns the distance between two points.
double PointDistance(const Point& lhs, const Point& rhs) {
return std::sqrt(std::pow(lhs.x() - rhs.x(), 2) +
std::pow(lhs.y() - rhs.y(), 2));
}
// Tests that DistanceFrom() can be used on a type with a custom distance
// function.
TEST(DistanceFrom, CanBeUsedOnTypeWithCustomDistanceFunction) {
const Matcher<Point> m =
DistanceFrom(Point(0.5, 0.5), PointDistance, Le(0.1));
EXPECT_THAT(Point(0.45, 0.45), m);
EXPECT_THAT(Point(0.2, 0.45), Not(m));
}
// A wrapper around a double value. For testing using DistanceFrom() with a
// custom type that has neither a built-in distance function nor a built-in
// distance comparator.
class Double {
public:
explicit Double(double value) : value_(value) {}
Double(const Double& other) = default;
double value() const { return value_; }
// Defines how to print a Double value. We don't use the AbslStringify API
// because googletest doesn't require absl yet.
friend void PrintTo(const Double& value, std::ostream* os) {
*os << "Double(" << value.value() << ")";
}
private:
double value_;
};
// Returns the distance between two Double values.
Double DoubleDistance(Double lhs, Double rhs) {
return Double(std::abs(lhs.value() - rhs.value()));
}
MATCHER_P(DoubleLe, rhs, (negation ? "is > " : "is <= ") + PrintToString(rhs)) {
return arg.value() <= rhs.value();
}
// Tests that DistanceFrom() can describe itself properly for a type with a
// custom printer.
TEST(DistanceFrom, CanDescribeWithCustomPrinter) {
const Matcher<Double> m =
DistanceFrom(Double(0.5), DoubleDistance, DoubleLe(Double(0.1)));
EXPECT_EQ(Describe(m), "is <= Double(0.1) away from Double(0.5)");
EXPECT_EQ(DescribeNegation(m), "is > Double(0.1) away from Double(0.5)");
}
// Tests that DistanceFrom() can be used with a custom distance function and
// comparator.
TEST(DistanceFrom, CanCustomizeDistanceAndComparator) {
const Matcher<Double> m =
DistanceFrom(Double(0.5), DoubleDistance, DoubleLe(Double(0.1)));
EXPECT_TRUE(m.Matches(Double(0.45)));
EXPECT_TRUE(m.Matches(Double(0.5)));
EXPECT_FALSE(m.Matches(Double(0.39)));
EXPECT_FALSE(m.Matches(Double(0.61)));
}
// For testing using DistanceFrom() with a type that supports both - and abs.
class Float {
public:
explicit Float(float value) : value_(value) {}
Float(const Float& other) = default;
float value() const { return value_; }
private:
float value_ = 0.0f;
};
// Returns the difference between two Float values. This must be defined in the
// same namespace as Float.
Float operator-(const Float& lhs, const Float& rhs) {
return Float(lhs.value() - rhs.value());
}
// Returns the absolute value of a Float value. This must be defined in the
// same namespace as Float.
Float abs(Float value) { return Float(std::abs(value.value())); }
// Returns true if and only if the first Float value is less than the second
// Float value. This must be defined in the same namespace as Float.
bool operator<(const Float& lhs, const Float& rhs) {
return lhs.value() < rhs.value();
}
// Tests that DistanceFrom() can be used with a type that supports both - and
// abs.
TEST(DistanceFrom, CanBeUsedWithTypeThatSupportsBothMinusAndAbs) {
const Matcher<Float> m = DistanceFrom(Float(0.5f), Lt(Float(0.1f)));
EXPECT_TRUE(m.Matches(Float(0.45f)));
EXPECT_TRUE(m.Matches(Float(0.55f)));
EXPECT_FALSE(m.Matches(Float(0.39f)));
EXPECT_FALSE(m.Matches(Float(0.61f)));
}
// Tests that Not(m) matches any value that doesn't match m. // Tests that Not(m) matches any value that doesn't match m.
TEST(NotTest, NegatesMatcher) { TEST(NotTest, NegatesMatcher) {
Matcher<int> m; Matcher<int> m;
@ -558,10 +744,9 @@ TEST_P(AllOfTestP, ExplainsResult) {
Matcher<int> m; Matcher<int> m;
// Successful match. Both matchers need to explain. The second // Successful match. Both matchers need to explain. The second
// matcher doesn't give an explanation, so only the first matcher's // matcher doesn't give an explanation, so the matcher description is used.
// explanation is printed.
m = AllOf(GreaterThan(10), Lt(30)); m = AllOf(GreaterThan(10), Lt(30));
EXPECT_EQ("which is 15 more than 10", Explain(m, 25)); EXPECT_EQ("which is 15 more than 10, and is < 30", Explain(m, 25));
// Successful match. Both matchers need to explain. // Successful match. Both matchers need to explain.
m = AllOf(GreaterThan(10), GreaterThan(20)); m = AllOf(GreaterThan(10), GreaterThan(20));
@ -571,7 +756,8 @@ TEST_P(AllOfTestP, ExplainsResult) {
// Successful match. All matchers need to explain. The second // Successful match. All matchers need to explain. The second
// matcher doesn't given an explanation. // matcher doesn't given an explanation.
m = AllOf(GreaterThan(10), Lt(30), GreaterThan(20)); m = AllOf(GreaterThan(10), Lt(30), GreaterThan(20));
EXPECT_EQ("which is 15 more than 10, and which is 5 more than 20", EXPECT_EQ(
"which is 15 more than 10, and is < 30, and which is 5 more than 20",
Explain(m, 25)); Explain(m, 25));
// Successful match. All matchers need to explain. // Successful match. All matchers need to explain.
@ -587,10 +773,10 @@ TEST_P(AllOfTestP, ExplainsResult) {
EXPECT_EQ("which is 5 less than 10", Explain(m, 5)); EXPECT_EQ("which is 5 less than 10", Explain(m, 5));
// Failed match. The second matcher, which failed, needs to // Failed match. The second matcher, which failed, needs to
// explain. Since it doesn't given an explanation, nothing is // explain. Since it doesn't given an explanation, the matcher text is
// printed. // printed.
m = AllOf(GreaterThan(10), Lt(30)); m = AllOf(GreaterThan(10), Lt(30));
EXPECT_EQ("", Explain(m, 40)); EXPECT_EQ("which doesn't match (is < 30)", Explain(m, 40));
// Failed match. The second matcher, which failed, needs to // Failed match. The second matcher, which failed, needs to
// explain. // explain.
@ -773,45 +959,43 @@ TEST(AnyOfTest, AnyOfMatcherSafelyCastsMonomorphicMatchers) {
TEST_P(AnyOfTestP, ExplainsResult) { TEST_P(AnyOfTestP, ExplainsResult) {
Matcher<int> m; Matcher<int> m;
// Failed match. Both matchers need to explain. The second // Failed match. The second matcher have no explanation (description is used).
// matcher doesn't give an explanation, so only the first matcher's
// explanation is printed.
m = AnyOf(GreaterThan(10), Lt(0)); m = AnyOf(GreaterThan(10), Lt(0));
EXPECT_EQ("which is 5 less than 10", Explain(m, 5)); EXPECT_EQ("which is 5 less than 10, and isn't < 0", Explain(m, 5));
// Failed match. Both matchers need to explain. // Failed match. Both matchers have explanations.
m = AnyOf(GreaterThan(10), GreaterThan(20)); m = AnyOf(GreaterThan(10), GreaterThan(20));
EXPECT_EQ("which is 5 less than 10, and which is 15 less than 20", EXPECT_EQ("which is 5 less than 10, and which is 15 less than 20",
Explain(m, 5)); Explain(m, 5));
// Failed match. All matchers need to explain. The second // Failed match. The middle matcher have no explanation.
// matcher doesn't given an explanation.
m = AnyOf(GreaterThan(10), Gt(20), GreaterThan(30)); m = AnyOf(GreaterThan(10), Gt(20), GreaterThan(30));
EXPECT_EQ("which is 5 less than 10, and which is 25 less than 30", EXPECT_EQ(
"which is 5 less than 10, and isn't > 20, and which is 25 less than 30",
Explain(m, 5)); Explain(m, 5));
// Failed match. All matchers need to explain. // Failed match. All three matchers have explanations.
m = AnyOf(GreaterThan(10), GreaterThan(20), GreaterThan(30)); m = AnyOf(GreaterThan(10), GreaterThan(20), GreaterThan(30));
EXPECT_EQ( EXPECT_EQ(
"which is 5 less than 10, and which is 15 less than 20, " "which is 5 less than 10, and which is 15 less than 20, "
"and which is 25 less than 30", "and which is 25 less than 30",
Explain(m, 5)); Explain(m, 5));
// Successful match. The first matcher, which succeeded, needs to // Successful match. The first macher succeeded and has explanation.
// explain.
m = AnyOf(GreaterThan(10), GreaterThan(20)); m = AnyOf(GreaterThan(10), GreaterThan(20));
EXPECT_EQ("which is 5 more than 10", Explain(m, 15)); EXPECT_EQ("which is 5 more than 10", Explain(m, 15));
// Successful match. The second matcher, which succeeded, needs to // Successful match. The second matcher succeeded and has explanation.
// explain. Since it doesn't given an explanation, nothing is
// printed.
m = AnyOf(GreaterThan(10), Lt(30));
EXPECT_EQ("", Explain(m, 0));
// Successful match. The second matcher, which succeeded, needs to
// explain.
m = AnyOf(GreaterThan(30), GreaterThan(20)); m = AnyOf(GreaterThan(30), GreaterThan(20));
EXPECT_EQ("which is 5 more than 20", Explain(m, 25)); EXPECT_EQ("which is 5 more than 20", Explain(m, 25));
// Successful match. The first matcher succeeded and has no explanation.
m = AnyOf(Gt(10), Lt(20));
EXPECT_EQ("which matches (is > 10)", Explain(m, 15));
// Successful match. The second matcher succeeded and has no explanation.
m = AnyOf(Gt(30), Gt(20));
EXPECT_EQ("which matches (is > 20)", Explain(m, 25));
} }
// The following predicate function and predicate functor are for // The following predicate function and predicate functor are for
@ -954,7 +1138,7 @@ TEST(AllArgsTest, WorksForNonTuple) {
class AllArgsHelper { class AllArgsHelper {
public: public:
AllArgsHelper() {} AllArgsHelper() = default;
MOCK_METHOD2(Helper, int(char x, int y)); MOCK_METHOD2(Helper, int(char x, int y));
@ -975,7 +1159,7 @@ TEST(AllArgsTest, WorksInWithClause) {
class OptionalMatchersHelper { class OptionalMatchersHelper {
public: public:
OptionalMatchersHelper() {} OptionalMatchersHelper() = default;
MOCK_METHOD0(NoArgs, int()); MOCK_METHOD0(NoArgs, int());
@ -1037,7 +1221,7 @@ class FloatingPointTest : public testing::Test {
Floating::ReinterpretBits(infinity_bits_ - max_ulps_)), Floating::ReinterpretBits(infinity_bits_ - max_ulps_)),
further_from_infinity_( further_from_infinity_(
Floating::ReinterpretBits(infinity_bits_ - max_ulps_ - 1)), Floating::ReinterpretBits(infinity_bits_ - max_ulps_ - 1)),
max_(Floating::Max()), max_(std::numeric_limits<RawType>::max()),
nan1_(Floating::ReinterpretBits(Floating::kExponentBitMask | 1)), nan1_(Floating::ReinterpretBits(Floating::kExponentBitMask | 1)),
nan2_(Floating::ReinterpretBits(Floating::kExponentBitMask | 200)) {} nan2_(Floating::ReinterpretBits(Floating::kExponentBitMask | 200)) {}
@ -1512,6 +1696,4 @@ TEST(AnyOfTest, WorksOnMoveOnlyType) {
} // namespace gmock_matchers_test } // namespace gmock_matchers_test
} // namespace testing } // namespace testing
#ifdef _MSC_VER GTEST_DISABLE_MSC_WARNINGS_POP_() // 4244 4100
#pragma warning(pop)
#endif

View File

@ -31,17 +31,20 @@
// //
// This file tests some commonly used argument matchers. // This file tests some commonly used argument matchers.
// Silence warning C4244: 'initializing': conversion from 'int' to 'short', #include <functional>
// possible loss of data and C4100, unreferenced local parameter #include <memory>
#ifdef _MSC_VER #include <optional>
#pragma warning(push) #include <string>
#pragma warning(disable : 4244) #include <tuple>
#pragma warning(disable : 4100)
#endif
#include <vector> #include <vector>
#include "gmock/gmock.h"
#include "test/gmock-matchers_test.h" #include "test/gmock-matchers_test.h"
#include "gtest/gtest.h"
// Silence warning C4244: 'initializing': conversion from 'int' to 'short',
// possible loss of data and C4100, unreferenced local parameter
GTEST_DISABLE_MSC_WARNINGS_PUSH_(4244 4100)
namespace testing { namespace testing {
namespace gmock_matchers_test { namespace gmock_matchers_test {
@ -409,9 +412,27 @@ class IntValue {
int value_; int value_;
}; };
// For testing casting matchers between compatible types. This is similar to
// IntValue, but takes a non-const reference to the value, showing MatcherCast
// works with such types (and doesn't, for example, use a const ref internally).
class MutableIntView {
public:
// An int& can be statically (although not implicitly) cast to a
// MutableIntView.
explicit MutableIntView(int& a_value) : value_(a_value) {}
int& value() const { return value_; }
private:
int& value_;
};
// For testing casting matchers between compatible types. // For testing casting matchers between compatible types.
bool IsPositiveIntValue(const IntValue& foo) { return foo.value() > 0; } bool IsPositiveIntValue(const IntValue& foo) { return foo.value() > 0; }
// For testing casting matchers between compatible types.
bool IsPositiveMutableIntView(MutableIntView foo) { return foo.value() > 0; }
// Tests that MatcherCast<T>(m) works when m is a Matcher<U> where T // Tests that MatcherCast<T>(m) works when m is a Matcher<U> where T
// can be statically converted to U. // can be statically converted to U.
TEST(MatcherCastTest, FromCompatibleType) { TEST(MatcherCastTest, FromCompatibleType) {
@ -427,14 +448,34 @@ TEST(MatcherCastTest, FromCompatibleType) {
// predicate. // predicate.
EXPECT_TRUE(m4.Matches(1)); EXPECT_TRUE(m4.Matches(1));
EXPECT_FALSE(m4.Matches(0)); EXPECT_FALSE(m4.Matches(0));
Matcher<MutableIntView> m5 = Truly(IsPositiveMutableIntView);
Matcher<int> m6 = MatcherCast<int>(m5);
// In the following, the arguments 1 and 0 are statically converted to
// MutableIntView objects, and then tested by the IsPositiveMutableIntView()
// predicate.
EXPECT_TRUE(m6.Matches(1));
EXPECT_FALSE(m6.Matches(0));
} }
// Tests that MatcherCast<T>(m) works when m is a Matcher<const T&>. // Tests that MatcherCast<T>(m) works when m is a Matcher<const T&>.
TEST(MatcherCastTest, FromConstReferenceToNonReference) { TEST(MatcherCastTest, FromConstReferenceToNonReference) {
Matcher<const int&> m1 = Eq(0); int n = 0;
Matcher<const int&> m1 = Ref(n);
Matcher<int> m2 = MatcherCast<int>(m1); Matcher<int> m2 = MatcherCast<int>(m1);
EXPECT_TRUE(m2.Matches(0)); int n1 = 0;
EXPECT_FALSE(m2.Matches(1)); EXPECT_TRUE(m2.Matches(n));
EXPECT_FALSE(m2.Matches(n1));
}
// Tests that MatcherCast<T&>(m) works when m is a Matcher<const T&>.
TEST(MatcherCastTest, FromConstReferenceToReference) {
int n = 0;
Matcher<const int&> m1 = Ref(n);
Matcher<int&> m2 = MatcherCast<int&>(m1);
int n1 = 0;
EXPECT_TRUE(m2.Matches(n));
EXPECT_FALSE(m2.Matches(n1));
} }
// Tests that MatcherCast<T>(m) works when m is a Matcher<T&>. // Tests that MatcherCast<T>(m) works when m is a Matcher<T&>.
@ -443,6 +484,12 @@ TEST(MatcherCastTest, FromReferenceToNonReference) {
Matcher<int> m2 = MatcherCast<int>(m1); Matcher<int> m2 = MatcherCast<int>(m1);
EXPECT_TRUE(m2.Matches(0)); EXPECT_TRUE(m2.Matches(0));
EXPECT_FALSE(m2.Matches(1)); EXPECT_FALSE(m2.Matches(1));
// Of course, reference identity isn't preserved since a copy is required.
int n = 0;
Matcher<int&> m3 = Ref(n);
Matcher<int> m4 = MatcherCast<int>(m3);
EXPECT_FALSE(m4.Matches(n));
} }
// Tests that MatcherCast<const T&>(m) works when m is a Matcher<T>. // Tests that MatcherCast<const T&>(m) works when m is a Matcher<T>.
@ -575,21 +622,48 @@ struct IntReferenceWrapper {
const int* value; const int* value;
}; };
// Compared the contained values
bool operator==(const IntReferenceWrapper& a, const IntReferenceWrapper& b) { bool operator==(const IntReferenceWrapper& a, const IntReferenceWrapper& b) {
return a.value == b.value; return *a.value == *b.value;
} }
TEST(MatcherCastTest, ValueIsNotCopied) { TEST(MatcherCastTest, ValueIsCopied) {
{
// When an IntReferenceWrapper is passed.
int n = 42;
Matcher<IntReferenceWrapper> m =
MatcherCast<IntReferenceWrapper>(IntReferenceWrapper(n));
{
int value = 42;
EXPECT_TRUE(m.Matches(value));
value = 10;
EXPECT_FALSE(m.Matches(value));
// This changes the stored reference.
n = 10;
EXPECT_TRUE(m.Matches(value));
}
}
{
// When an int is passed.
int n = 42; int n = 42;
Matcher<IntReferenceWrapper> m = MatcherCast<IntReferenceWrapper>(n); Matcher<IntReferenceWrapper> m = MatcherCast<IntReferenceWrapper>(n);
// Verify that the matcher holds a reference to n, not to its temporary copy. {
EXPECT_TRUE(m.Matches(n)); int value = 42;
EXPECT_TRUE(m.Matches(value));
value = 10;
EXPECT_FALSE(m.Matches(value));
// This does not change the stored int.
n = 10;
EXPECT_FALSE(m.Matches(value));
}
}
} }
class Base { class Base {
public: public:
virtual ~Base() {} virtual ~Base() = default;
Base() {} Base() = default;
private: private:
Base(const Base&) = delete; Base(const Base&) = delete;
@ -647,6 +721,16 @@ TEST(SafeMatcherCastTest, FromBaseClass) {
EXPECT_FALSE(m4.Matches(d2)); EXPECT_FALSE(m4.Matches(d2));
} }
// Tests that SafeMatcherCast<T>(m) works when m is a Matcher<const T&>.
TEST(SafeMatcherCastTest, FromConstReferenceToNonReference) {
int n = 0;
Matcher<const int&> m1 = Ref(n);
Matcher<int> m2 = SafeMatcherCast<int>(m1);
int n1 = 0;
EXPECT_TRUE(m2.Matches(n));
EXPECT_FALSE(m2.Matches(n1));
}
// Tests that SafeMatcherCast<T&>(m) works when m is a Matcher<const T&>. // Tests that SafeMatcherCast<T&>(m) works when m is a Matcher<const T&>.
TEST(SafeMatcherCastTest, FromConstReferenceToReference) { TEST(SafeMatcherCastTest, FromConstReferenceToReference) {
int n = 0; int n = 0;
@ -864,7 +948,7 @@ struct Type {
}; };
TEST(TypedEqTest, HasSpecifiedType) { TEST(TypedEqTest, HasSpecifiedType) {
// Verfies that the type of TypedEq<T>(v) is Matcher<T>. // Verifies that the type of TypedEq<T>(v) is Matcher<T>.
Type<Matcher<int>>::IsTypeOf(TypedEq<int>(5)); Type<Matcher<int>>::IsTypeOf(TypedEq<int>(5));
Type<Matcher<double>>::IsTypeOf(TypedEq<double>(5)); Type<Matcher<double>>::IsTypeOf(TypedEq<double>(5));
} }
@ -1530,7 +1614,7 @@ TEST(PairTest, MatchesCorrectly) {
EXPECT_THAT(p, Pair(25, "foo")); EXPECT_THAT(p, Pair(25, "foo"));
EXPECT_THAT(p, Pair(Ge(20), HasSubstr("o"))); EXPECT_THAT(p, Pair(Ge(20), HasSubstr("o")));
// 'first' doesnt' match, but 'second' matches. // 'first' doesn't match, but 'second' matches.
EXPECT_THAT(p, Not(Pair(42, "foo"))); EXPECT_THAT(p, Not(Pair(42, "foo")));
EXPECT_THAT(p, Not(Pair(Lt(25), "foo"))); EXPECT_THAT(p, Not(Pair(Lt(25), "foo")));
@ -1545,7 +1629,7 @@ TEST(PairTest, MatchesCorrectly) {
TEST(PairTest, WorksWithMoveOnly) { TEST(PairTest, WorksWithMoveOnly) {
pair<std::unique_ptr<int>, std::unique_ptr<int>> p; pair<std::unique_ptr<int>, std::unique_ptr<int>> p;
p.second.reset(new int(7)); p.second = std::make_unique<int>(7);
EXPECT_THAT(p, Pair(Eq(nullptr), Ne(nullptr))); EXPECT_THAT(p, Pair(Eq(nullptr), Ne(nullptr)));
} }
@ -1768,6 +1852,15 @@ TEST(StartsWithTest, CanDescribeSelf) {
EXPECT_EQ("starts with \"Hi\"", Describe(m)); EXPECT_EQ("starts with \"Hi\"", Describe(m));
} }
TEST(StartsWithTest, WorksWithStringMatcherOnStringViewMatchee) {
#if GTEST_INTERNAL_HAS_STRING_VIEW
EXPECT_THAT(internal::StringView("talk to me goose"),
StartsWith(std::string("talk")));
#else
GTEST_SKIP() << "Not applicable without internal::StringView.";
#endif // GTEST_INTERNAL_HAS_STRING_VIEW
}
// Tests EndsWith(s). // Tests EndsWith(s).
TEST(EndsWithTest, MatchesStringWithGivenSuffix) { TEST(EndsWithTest, MatchesStringWithGivenSuffix) {
@ -1805,11 +1898,13 @@ TEST(WhenBase64UnescapedTest, MatchesUnescapedBase64Strings) {
EXPECT_FALSE(m1.Matches("invalid base64")); EXPECT_FALSE(m1.Matches("invalid base64"));
EXPECT_FALSE(m1.Matches("aGVsbG8gd29ybGQ=")); // hello world EXPECT_FALSE(m1.Matches("aGVsbG8gd29ybGQ=")); // hello world
EXPECT_TRUE(m1.Matches("aGVsbG8gd29ybGQh")); // hello world! EXPECT_TRUE(m1.Matches("aGVsbG8gd29ybGQh")); // hello world!
EXPECT_TRUE(m1.Matches("+/-_IQ")); // \xfb\xff\xbf!
const Matcher<const std::string&> m2 = WhenBase64Unescaped(EndsWith("!")); const Matcher<const std::string&> m2 = WhenBase64Unescaped(EndsWith("!"));
EXPECT_FALSE(m2.Matches("invalid base64")); EXPECT_FALSE(m2.Matches("invalid base64"));
EXPECT_FALSE(m2.Matches("aGVsbG8gd29ybGQ=")); // hello world EXPECT_FALSE(m2.Matches("aGVsbG8gd29ybGQ=")); // hello world
EXPECT_TRUE(m2.Matches("aGVsbG8gd29ybGQh")); // hello world! EXPECT_TRUE(m2.Matches("aGVsbG8gd29ybGQh")); // hello world!
EXPECT_TRUE(m2.Matches("+/-_IQ")); // \xfb\xff\xbf!
#if GTEST_INTERNAL_HAS_STRING_VIEW #if GTEST_INTERNAL_HAS_STRING_VIEW
const Matcher<const internal::StringView&> m3 = const Matcher<const internal::StringView&> m3 =
@ -1817,6 +1912,7 @@ TEST(WhenBase64UnescapedTest, MatchesUnescapedBase64Strings) {
EXPECT_FALSE(m3.Matches("invalid base64")); EXPECT_FALSE(m3.Matches("invalid base64"));
EXPECT_FALSE(m3.Matches("aGVsbG8gd29ybGQ=")); // hello world EXPECT_FALSE(m3.Matches("aGVsbG8gd29ybGQ=")); // hello world
EXPECT_TRUE(m3.Matches("aGVsbG8gd29ybGQh")); // hello world! EXPECT_TRUE(m3.Matches("aGVsbG8gd29ybGQh")); // hello world!
EXPECT_TRUE(m3.Matches("+/-_IQ")); // \xfb\xff\xbf!
#endif // GTEST_INTERNAL_HAS_STRING_VIEW #endif // GTEST_INTERNAL_HAS_STRING_VIEW
} }
@ -2321,9 +2417,79 @@ TEST(ExplainMatchResultTest, AllOf_True_True) {
EXPECT_EQ("which is 0 modulo 2, and which is 0 modulo 3", Explain(m, 6)); EXPECT_EQ("which is 0 modulo 2, and which is 0 modulo 3", Explain(m, 6));
} }
// Tests that when AllOf() succeeds, but matchers have no explanation,
// the matcher description is used.
TEST(ExplainMatchResultTest, AllOf_True_True_2) { TEST(ExplainMatchResultTest, AllOf_True_True_2) {
const Matcher<int> m = AllOf(Ge(2), Le(3)); const Matcher<int> m = AllOf(Ge(2), Le(3));
EXPECT_EQ("", Explain(m, 2)); EXPECT_EQ("is >= 2, and is <= 3", Explain(m, 2));
}
// A matcher that records whether the listener was interested.
template <typename T>
class CountingMatcher : public MatcherInterface<T> {
public:
explicit CountingMatcher(const Matcher<T>& base_matcher,
std::vector<bool>* listener_interested)
: base_matcher_(base_matcher),
listener_interested_(listener_interested) {}
bool MatchAndExplain(T x, MatchResultListener* listener) const override {
listener_interested_->push_back(listener->IsInterested());
return base_matcher_.MatchAndExplain(x, listener);
}
void DescribeTo(ostream* os) const override { base_matcher_.DescribeTo(os); }
private:
Matcher<T> base_matcher_;
std::vector<bool>* listener_interested_;
};
TEST(AllOfTest, DoesNotFormatChildMatchersWhenNotInterested) {
std::vector<bool> listener_interested;
Matcher<int> matcher =
MakeMatcher(new CountingMatcher<int>(Eq(1), &listener_interested));
EXPECT_TRUE(matcher.Matches(1));
EXPECT_THAT(listener_interested, ElementsAre(false));
listener_interested.clear();
Matcher<int> all_of_matcher = AllOf(matcher, matcher);
EXPECT_TRUE(all_of_matcher.Matches(1));
EXPECT_THAT(listener_interested, ElementsAre(false, false));
listener_interested.clear();
EXPECT_FALSE(all_of_matcher.Matches(0));
EXPECT_THAT(listener_interested, ElementsAre(false));
}
TEST(AnyOfTest, DoesNotFormatChildMatchersWhenNotInterested) {
std::vector<bool> listener_interested;
Matcher<int> matcher =
MakeMatcher(new CountingMatcher<int>(Eq(1), &listener_interested));
EXPECT_TRUE(matcher.Matches(1));
EXPECT_THAT(listener_interested, ElementsAre(false));
listener_interested.clear();
Matcher<int> any_of_matcher = AnyOf(matcher, matcher);
EXPECT_TRUE(any_of_matcher.Matches(1));
EXPECT_THAT(listener_interested, ElementsAre(false));
listener_interested.clear();
EXPECT_FALSE(any_of_matcher.Matches(0));
EXPECT_THAT(listener_interested, ElementsAre(false, false));
}
TEST(OptionalTest, DoesNotFormatChildMatcherWhenNotInterested) {
std::vector<bool> listener_interested;
Matcher<int> matcher =
MakeMatcher(new CountingMatcher<int>(Eq(1), &listener_interested));
EXPECT_TRUE(matcher.Matches(1));
EXPECT_THAT(listener_interested, ElementsAre(false));
listener_interested.clear();
Matcher<std::optional<int>> optional_matcher = Optional(matcher);
EXPECT_FALSE(optional_matcher.Matches(std::nullopt));
EXPECT_THAT(listener_interested, ElementsAre());
EXPECT_TRUE(optional_matcher.Matches(1));
EXPECT_THAT(listener_interested, ElementsAre(false));
listener_interested.clear();
EXPECT_FALSE(matcher.Matches(0));
EXPECT_THAT(listener_interested, ElementsAre(false));
} }
INSTANTIATE_GTEST_MATCHER_TEST_P(ExplainmatcherResultTest); INSTANTIATE_GTEST_MATCHER_TEST_P(ExplainmatcherResultTest);
@ -2354,6 +2520,4 @@ TEST(PolymorphicMatcherTest, CanAccessImpl) {
} // namespace gmock_matchers_test } // namespace gmock_matchers_test
} // namespace testing } // namespace testing
#ifdef _MSC_VER GTEST_DISABLE_MSC_WARNINGS_POP_() // 4244 4100
#pragma warning(pop)
#endif

View File

@ -31,15 +31,26 @@
// //
// This file tests some commonly used argument matchers. // This file tests some commonly used argument matchers.
#include <algorithm>
#include <array>
#include <cstddef>
#include <deque>
#include <forward_list>
#include <iterator>
#include <list>
#include <memory>
#include <ostream>
#include <string>
#include <tuple>
#include <vector>
#include "gmock/gmock.h"
#include "test/gmock-matchers_test.h"
#include "gtest/gtest.h"
// Silence warning C4244: 'initializing': conversion from 'int' to 'short', // Silence warning C4244: 'initializing': conversion from 'int' to 'short',
// possible loss of data and C4100, unreferenced local parameter // possible loss of data and C4100, unreferenced local parameter
#ifdef _MSC_VER GTEST_DISABLE_MSC_WARNINGS_PUSH_(4244 4100)
#pragma warning(push)
#pragma warning(disable : 4244)
#pragma warning(disable : 4100)
#endif
#include "test/gmock-matchers_test.h"
namespace testing { namespace testing {
namespace gmock_matchers_test { namespace gmock_matchers_test {
@ -1194,13 +1205,16 @@ TEST(SizeIsTest, ExplainsResult) {
vector<int> container; vector<int> container;
EXPECT_EQ("whose size 0 doesn't match", Explain(m1, container)); EXPECT_EQ("whose size 0 doesn't match", Explain(m1, container));
EXPECT_EQ("whose size 0 matches", Explain(m2, container)); EXPECT_EQ("whose size 0 matches", Explain(m2, container));
EXPECT_EQ("whose size 0 matches", Explain(m3, container)); EXPECT_EQ("whose size 0 matches, which matches (is equal to 0)",
Explain(m3, container));
EXPECT_EQ("whose size 0 doesn't match", Explain(m4, container)); EXPECT_EQ("whose size 0 doesn't match", Explain(m4, container));
container.push_back(0); container.push_back(0);
container.push_back(0); container.push_back(0);
EXPECT_EQ("whose size 2 matches", Explain(m1, container)); EXPECT_EQ("whose size 2 matches", Explain(m1, container));
EXPECT_EQ("whose size 2 doesn't match", Explain(m2, container)); EXPECT_EQ("whose size 2 doesn't match", Explain(m2, container));
EXPECT_EQ("whose size 2 doesn't match", Explain(m3, container)); EXPECT_EQ(
"whose size 2 doesn't match, isn't equal to 0, and isn't equal to 3",
Explain(m3, container));
EXPECT_EQ("whose size 2 matches", Explain(m4, container)); EXPECT_EQ("whose size 2 matches", Explain(m4, container));
} }
@ -1258,10 +1272,11 @@ TEST(WhenSortedByTest, CanDescribeSelf) {
TEST(WhenSortedByTest, ExplainsMatchResult) { TEST(WhenSortedByTest, ExplainsMatchResult) {
const int a[] = {2, 1}; const int a[] = {2, 1};
EXPECT_EQ("which is { 1, 2 } when sorted, whose element #0 doesn't match", EXPECT_EQ(
Explain(WhenSortedBy(less<int>(), ElementsAre(2, 3)), a)); Explain(WhenSortedBy(less<int>(), ElementsAre(2, 3)), a),
EXPECT_EQ("which is { 1, 2 } when sorted", "which is { 1, 2 } when sorted, whose element #0 (1) isn't equal to 2");
Explain(WhenSortedBy(less<int>(), ElementsAre(1, 2)), a)); EXPECT_EQ(Explain(WhenSortedBy(less<int>(), ElementsAre(1, 2)), a),
"which is { 1, 2 } when sorted");
} }
// WhenSorted() is a simple wrapper on WhenSortedBy(). Hence we don't // WhenSorted() is a simple wrapper on WhenSortedBy(). Hence we don't
@ -1465,7 +1480,9 @@ TEST_P(BeginEndDistanceIsTestP, ExplainsResult) {
Explain(m1, container)); Explain(m1, container));
EXPECT_EQ("whose distance between begin() and end() 0 matches", EXPECT_EQ("whose distance between begin() and end() 0 matches",
Explain(m2, container)); Explain(m2, container));
EXPECT_EQ("whose distance between begin() and end() 0 matches", EXPECT_EQ(
"whose distance between begin() and end() 0 matches, which matches (is "
"equal to 0)",
Explain(m3, container)); Explain(m3, container));
EXPECT_EQ( EXPECT_EQ(
"whose distance between begin() and end() 0 doesn't match, which is 1 " "whose distance between begin() and end() 0 doesn't match, which is 1 "
@ -1477,7 +1494,9 @@ TEST_P(BeginEndDistanceIsTestP, ExplainsResult) {
Explain(m1, container)); Explain(m1, container));
EXPECT_EQ("whose distance between begin() and end() 2 doesn't match", EXPECT_EQ("whose distance between begin() and end() 2 doesn't match",
Explain(m2, container)); Explain(m2, container));
EXPECT_EQ("whose distance between begin() and end() 2 doesn't match", EXPECT_EQ(
"whose distance between begin() and end() 2 doesn't match, isn't equal "
"to 0, and isn't equal to 3",
Explain(m3, container)); Explain(m3, container));
EXPECT_EQ( EXPECT_EQ(
"whose distance between begin() and end() 2 matches, which is 1 more " "whose distance between begin() and end() 2 matches, which is 1 more "
@ -1758,6 +1777,295 @@ TEST(IsSubsetOfTest, WorksWithMoveOnly) {
helper.Call(MakeUniquePtrs({2})); helper.Call(MakeUniquePtrs({2}));
} }
// A container whose iterator returns a temporary. This can iterate over the
// characters in a string.
class CharString {
public:
using value_type = char;
class const_iterator {
public:
using iterator_category = std::input_iterator_tag;
using value_type = char;
using difference_type = std::ptrdiff_t;
using pointer = const char*;
using reference = const char&;
// Create an iterator that points to the given character.
explicit const_iterator(const char* ptr) : ptr_(ptr) {}
// Returns the current character. IMPORTANT: this must return a temporary,
// not a reference, to test that ElementsAre() works with containers whose
// iterators return temporaries.
char operator*() const { return *ptr_; }
// Advances to the next character.
const_iterator& operator++() {
++ptr_;
return *this;
}
// Compares two iterators.
bool operator==(const const_iterator& other) const {
return ptr_ == other.ptr_;
}
bool operator!=(const const_iterator& other) const {
return ptr_ != other.ptr_;
}
private:
const char* ptr_ = nullptr;
};
// Creates a CharString that contains the given string.
explicit CharString(const std::string& s) : s_(s) {}
// Returns an iterator pointing to the first character in the string.
const_iterator begin() const { return const_iterator(s_.c_str()); }
// Returns an iterator pointing past the last character in the string.
const_iterator end() const { return const_iterator(s_.c_str() + s_.size()); }
private:
std::string s_;
};
// Tests using ElementsAre() with a container whose iterator returns a
// temporary.
TEST(ElementsAreTest, WorksWithContainerThatReturnsTempInIterator) {
CharString s("abc");
EXPECT_THAT(s, ElementsAre('a', 'b', 'c'));
EXPECT_THAT(s, Not(ElementsAre('a', 'b', 'd')));
}
// Tests using ElementsAreArray() with a container whose iterator returns a
// temporary.
TEST(ElementsAreArrayTest, WorksWithContainerThatReturnsTempInIterator) {
CharString s("abc");
EXPECT_THAT(s, ElementsAreArray({'a', 'b', 'c'}));
EXPECT_THAT(s, Not(ElementsAreArray({'a', 'b', 'd'})));
}
// A container whose iterator returns a temporary and is not copy-assignable.
// This simulates the behavior of the proxy object returned by absl::StrSplit().
class CharString2 {
public:
using value_type = char;
class const_iterator {
public:
using iterator_category = std::input_iterator_tag;
using value_type = char;
using difference_type = std::ptrdiff_t;
using pointer = const char*;
using reference = const char&;
// Make const_iterator copy-constructible but not copy-assignable,
// simulating the behavior of the proxy object returned by absl::StrSplit().
const_iterator(const const_iterator&) = default;
const_iterator& operator=(const const_iterator&) = delete;
// Create an iterator that points to the given character.
explicit const_iterator(const char* ptr) : ptr_(ptr) {}
// Returns the current character. IMPORTANT: this must return a temporary,
// not a reference, to test that ElementsAre() works with containers whose
// iterators return temporaries.
char operator*() const { return *ptr_; }
// Advances to the next character.
const_iterator& operator++() {
++ptr_;
return *this;
}
// Compares two iterators.
bool operator==(const const_iterator& other) const {
return ptr_ == other.ptr_;
}
bool operator!=(const const_iterator& other) const {
return ptr_ != other.ptr_;
}
private:
const char* ptr_ = nullptr;
};
// Creates a CharString that contains the given string.
explicit CharString2(const std::string& s) : s_(s) {}
// Returns an iterator pointing to the first character in the string.
const_iterator begin() const { return const_iterator(s_.c_str()); }
// Returns an iterator pointing past the last character in the string.
const_iterator end() const { return const_iterator(s_.c_str() + s_.size()); }
private:
std::string s_;
};
// Tests using ElementsAre() with a container whose iterator returns a
// temporary and is not copy-assignable.
TEST(ElementsAreTest, WorksWithContainerThatReturnsTempInUnassignableIterator) {
CharString2 s("abc");
EXPECT_THAT(s, ElementsAre('a', 'b', 'c'));
EXPECT_THAT(s, Not(ElementsAre('a', 'b', 'd')));
}
// Tests using ElementsAreArray() with a container whose iterator returns a
// temporary and is not copy-assignable.
TEST(ElementsAreArrayTest,
WorksWithContainerThatReturnsTempInUnassignableIterator) {
CharString2 s("abc");
EXPECT_THAT(s, ElementsAreArray({'a', 'b', 'c'}));
EXPECT_THAT(s, Not(ElementsAreArray({'a', 'b', 'd'})));
}
// A container whose iterator returns a temporary and is neither
// copy-constructible nor copy-assignable.
class CharString3 {
public:
using value_type = char;
class const_iterator {
public:
using iterator_category = std::input_iterator_tag;
using value_type = char;
using difference_type = std::ptrdiff_t;
using pointer = const char*;
using reference = const char&;
// Make const_iterator neither copy-constructible nor copy-assignable.
const_iterator(const const_iterator&) = delete;
const_iterator& operator=(const const_iterator&) = delete;
// Create an iterator that points to the given character.
explicit const_iterator(const char* ptr) : ptr_(ptr) {}
// Returns the current character. IMPORTANT: this must return a temporary,
// not a reference, to test that ElementsAre() works with containers whose
// iterators return temporaries.
char operator*() const { return *ptr_; }
// Advances to the next character.
const_iterator& operator++() {
++ptr_;
return *this;
}
// Compares two iterators.
bool operator==(const const_iterator& other) const {
return ptr_ == other.ptr_;
}
bool operator!=(const const_iterator& other) const {
return ptr_ != other.ptr_;
}
private:
const char* ptr_ = nullptr;
};
// Creates a CharString that contains the given string.
explicit CharString3(const std::string& s) : s_(s) {}
// Returns an iterator pointing to the first character in the string.
const_iterator begin() const { return const_iterator(s_.c_str()); }
// Returns an iterator pointing past the last character in the string.
const_iterator end() const { return const_iterator(s_.c_str() + s_.size()); }
private:
std::string s_;
};
// Tests using ElementsAre() with a container whose iterator returns a
// temporary and is neither copy-constructible nor copy-assignable.
TEST(ElementsAreTest, WorksWithContainerThatReturnsTempInUncopyableIterator) {
CharString3 s("abc");
EXPECT_THAT(s, ElementsAre('a', 'b', 'c'));
EXPECT_THAT(s, Not(ElementsAre('a', 'b', 'd')));
}
// Tests using ElementsAreArray() with a container whose iterator returns a
// temporary and is neither copy-constructible nor copy-assignable.
TEST(ElementsAreArrayTest,
WorksWithContainerThatReturnsTempInUncopyableIterator) {
CharString3 s("abc");
EXPECT_THAT(s, ElementsAreArray({'a', 'b', 'c'}));
EXPECT_THAT(s, Not(ElementsAreArray({'a', 'b', 'd'})));
}
// A container whose iterator returns a temporary, is neither
// copy-constructible nor copy-assignable, and has no member types.
class CharString4 {
public:
using value_type = char;
class const_iterator {
public:
// Do not define difference_type, etc.
// Make const_iterator neither copy-constructible nor copy-assignable.
const_iterator(const const_iterator&) = delete;
const_iterator& operator=(const const_iterator&) = delete;
// Create an iterator that points to the given character.
explicit const_iterator(const char* ptr) : ptr_(ptr) {}
// Returns the current character. IMPORTANT: this must return a temporary,
// not a reference, to test that ElementsAre() works with containers whose
// iterators return temporaries.
char operator*() const { return *ptr_; }
// Advances to the next character.
const_iterator& operator++() {
++ptr_;
return *this;
}
// Compares two iterators.
bool operator==(const const_iterator& other) const {
return ptr_ == other.ptr_;
}
bool operator!=(const const_iterator& other) const {
return ptr_ != other.ptr_;
}
private:
const char* ptr_ = nullptr;
};
// Creates a CharString that contains the given string.
explicit CharString4(const std::string& s) : s_(s) {}
// Returns an iterator pointing to the first character in the string.
const_iterator begin() const { return const_iterator(s_.c_str()); }
// Returns an iterator pointing past the last character in the string.
const_iterator end() const { return const_iterator(s_.c_str() + s_.size()); }
private:
std::string s_;
};
// Tests using ElementsAre() with a container whose iterator returns a
// temporary, is neither copy-constructible nor copy-assignable, and has no
// member types.
TEST(ElementsAreTest, WorksWithContainerWithIteratorWithNoMemberTypes) {
CharString4 s("abc");
EXPECT_THAT(s, ElementsAre('a', 'b', 'c'));
EXPECT_THAT(s, Not(ElementsAre('a', 'b', 'd')));
}
// Tests using ElementsAreArray() with a container whose iterator returns a
// temporary, is neither copy-constructible nor copy-assignable, and has no
// member types.
TEST(ElementsAreArrayTest, WorksWithContainerWithIteratorWithNoMemberTypes) {
CharString4 s("abc");
EXPECT_THAT(s, ElementsAreArray({'a', 'b', 'c'}));
EXPECT_THAT(s, Not(ElementsAreArray({'a', 'b', 'd'})));
}
// Tests using ElementsAre() and ElementsAreArray() with stream-like // Tests using ElementsAre() and ElementsAreArray() with stream-like
// "containers". // "containers".
@ -1826,8 +2134,8 @@ TEST(UnorderedElementsAreArrayTest, SucceedsWhenExpected) {
} }
TEST(UnorderedElementsAreArrayTest, VectorBool) { TEST(UnorderedElementsAreArrayTest, VectorBool) {
const bool a[] = {0, 1, 0, 1, 1}; const bool a[] = {false, true, false, true, true};
const bool b[] = {1, 0, 1, 1, 0}; const bool b[] = {true, false, true, true, false};
std::vector<bool> expected(std::begin(a), std::end(a)); std::vector<bool> expected(std::begin(a), std::end(a));
std::vector<bool> actual(std::begin(b), std::end(b)); std::vector<bool> actual(std::begin(b), std::end(b));
StringMatchResultListener listener; StringMatchResultListener listener;
@ -2004,7 +2312,14 @@ TEST_F(UnorderedElementsAreTest, FailMessageCountWrong) {
StringMatchResultListener listener; StringMatchResultListener listener;
EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAre(1, 2, 3), v, &listener)) EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAre(1, 2, 3), v, &listener))
<< listener.str(); << listener.str();
EXPECT_THAT(listener.str(), Eq("which has 1 element")); EXPECT_THAT(listener.str(),
Eq("which has 1 element\n"
"where the following matchers don't match any elements:\n"
"matcher #0: is equal to 1,\n"
"matcher #1: is equal to 2,\n"
"matcher #2: is equal to 3\n"
"and where the following elements don't match any matchers:\n"
"element #0: 4"));
} }
TEST_F(UnorderedElementsAreTest, FailMessageCountWrongZero) { TEST_F(UnorderedElementsAreTest, FailMessageCountWrongZero) {
@ -2012,7 +2327,11 @@ TEST_F(UnorderedElementsAreTest, FailMessageCountWrongZero) {
StringMatchResultListener listener; StringMatchResultListener listener;
EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAre(1, 2, 3), v, &listener)) EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAre(1, 2, 3), v, &listener))
<< listener.str(); << listener.str();
EXPECT_THAT(listener.str(), Eq("")); EXPECT_THAT(listener.str(),
Eq("where the following matchers don't match any elements:\n"
"matcher #0: is equal to 1,\n"
"matcher #1: is equal to 2,\n"
"matcher #2: is equal to 3"));
} }
TEST_F(UnorderedElementsAreTest, FailMessageUnmatchedMatchers) { TEST_F(UnorderedElementsAreTest, FailMessageUnmatchedMatchers) {
@ -2127,7 +2446,7 @@ TEST_P(EachTestP, ExplainsMatchResultCorrectly) {
Matcher<set<int>> m = Each(2); Matcher<set<int>> m = Each(2);
EXPECT_EQ("", Explain(m, a)); EXPECT_EQ("", Explain(m, a));
Matcher<const int(&)[1]> n = Each(1); // NOLINT Matcher<const int (&)[1]> n = Each(1); // NOLINT
const int b[1] = {1}; const int b[1] = {1};
EXPECT_EQ("", Explain(n, b)); EXPECT_EQ("", Explain(n, b));
@ -2262,7 +2581,7 @@ TEST(PointwiseTest, MakesCopyOfRhs) {
rhs.push_back(4); rhs.push_back(4);
int lhs[] = {1, 2}; int lhs[] = {1, 2};
const Matcher<const int(&)[2]> m = Pointwise(IsHalfOf(), rhs); const Matcher<const int (&)[2]> m = Pointwise(IsHalfOf(), rhs);
EXPECT_THAT(lhs, m); EXPECT_THAT(lhs, m);
// Changing rhs now shouldn't affect m, which made a copy of rhs. // Changing rhs now shouldn't affect m, which made a copy of rhs.
@ -2390,7 +2709,7 @@ TEST(UnorderedPointwiseTest, MakesCopyOfRhs) {
rhs.push_back(4); rhs.push_back(4);
int lhs[] = {2, 1}; int lhs[] = {2, 1};
const Matcher<const int(&)[2]> m = UnorderedPointwise(IsHalfOf(), rhs); const Matcher<const int (&)[2]> m = UnorderedPointwise(IsHalfOf(), rhs);
EXPECT_THAT(lhs, m); EXPECT_THAT(lhs, m);
// Changing rhs now shouldn't affect m, which made a copy of rhs. // Changing rhs now shouldn't affect m, which made a copy of rhs.
@ -2428,7 +2747,7 @@ TEST(UnorderedPointwiseTest, RejectsWrongSize) {
const double lhs[2] = {1, 2}; const double lhs[2] = {1, 2};
const int rhs[1] = {0}; const int rhs[1] = {0};
EXPECT_THAT(lhs, Not(UnorderedPointwise(Gt(), rhs))); EXPECT_THAT(lhs, Not(UnorderedPointwise(Gt(), rhs)));
EXPECT_EQ("which has 2 elements", EXPECT_EQ("which has 2 elements\n",
Explain(UnorderedPointwise(Gt(), rhs), lhs)); Explain(UnorderedPointwise(Gt(), rhs), lhs));
const int rhs2[3] = {0, 1, 2}; const int rhs2[3] = {0, 1, 2};
@ -2641,11 +2960,11 @@ TEST_P(ElementsAreTestP, CanExplainMismatchRightSize) {
vector<int> v; vector<int> v;
v.push_back(2); v.push_back(2);
v.push_back(1); v.push_back(1);
EXPECT_EQ("whose element #0 doesn't match", Explain(m, v)); EXPECT_EQ(Explain(m, v), "whose element #0 (2) isn't equal to 1");
v[0] = 1; v[0] = 1;
EXPECT_EQ("whose element #1 doesn't match, which is 4 less than 5", EXPECT_EQ(Explain(m, v),
Explain(m, v)); "whose element #1 (1) is <= 5, which is 4 less than 5");
} }
TEST(ElementsAreTest, MatchesOneElementVector) { TEST(ElementsAreTest, MatchesOneElementVector) {
@ -2778,7 +3097,7 @@ TEST(ElementsAreTest, WorksWithNativeArrayPassedByReference) {
class NativeArrayPassedAsPointerAndSize { class NativeArrayPassedAsPointerAndSize {
public: public:
NativeArrayPassedAsPointerAndSize() {} NativeArrayPassedAsPointerAndSize() = default;
MOCK_METHOD(void, Helper, (int* array, int size)); MOCK_METHOD(void, Helper, (int* array, int size));
@ -3045,7 +3364,7 @@ TEST(ContainsTest, SetDoesNotMatchWhenElementIsNotInContainer) {
TEST_P(ContainsTestP, ExplainsMatchResultCorrectly) { TEST_P(ContainsTestP, ExplainsMatchResultCorrectly) {
const int a[2] = {1, 2}; const int a[2] = {1, 2};
Matcher<const int(&)[2]> m = Contains(2); Matcher<const int (&)[2]> m = Contains(2);
EXPECT_EQ("whose element #1 matches", Explain(m, a)); EXPECT_EQ("whose element #1 matches", Explain(m, a));
m = Contains(3); m = Contains(3);
@ -3124,6 +3443,4 @@ TEST(ContainsTest, WorksForTwoDimensionalNativeArray) {
} // namespace gmock_matchers_test } // namespace gmock_matchers_test
} // namespace testing } // namespace testing
#ifdef _MSC_VER GTEST_DISABLE_MSC_WARNINGS_POP_() // 4244 4100
#pragma warning(pop)
#endif

View File

@ -31,15 +31,22 @@
// //
// This file tests some commonly used argument matchers. // This file tests some commonly used argument matchers.
#include <array>
#include <cstdint>
#include <memory>
#include <ostream>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#include "gmock/gmock.h"
#include "test/gmock-matchers_test.h"
#include "gtest/gtest.h"
// Silence warning C4244: 'initializing': conversion from 'int' to 'short', // Silence warning C4244: 'initializing': conversion from 'int' to 'short',
// possible loss of data and C4100, unreferenced local parameter // possible loss of data and C4100, unreferenced local parameter
#ifdef _MSC_VER GTEST_DISABLE_MSC_WARNINGS_PUSH_(4244 4100)
#pragma warning(push)
#pragma warning(disable : 4244)
#pragma warning(disable : 4100)
#endif
#include "test/gmock-matchers_test.h"
namespace testing { namespace testing {
namespace gmock_matchers_test { namespace gmock_matchers_test {
@ -202,7 +209,7 @@ TEST(IsTrueTest, IsTrueIsFalse) {
EXPECT_THAT(nonnull_unique, Not(IsFalse())); EXPECT_THAT(nonnull_unique, Not(IsFalse()));
} }
#if GTEST_HAS_TYPED_TEST #ifdef GTEST_HAS_TYPED_TEST
// Tests ContainerEq with different container types, and // Tests ContainerEq with different container types, and
// different element types. // different element types.
@ -668,6 +675,8 @@ TEST_P(MatcherTupleTestP, ExplainsMatchFailure) {
// explanation. // explanation.
} }
#if GTEST_HAS_TYPED_TEST
// Sample optional type implementation with minimal requirements for use with // Sample optional type implementation with minimal requirements for use with
// Optional matcher. // Optional matcher.
template <typename T> template <typename T>
@ -685,38 +694,94 @@ class SampleOptional {
bool has_value_; bool has_value_;
}; };
TEST(OptionalTest, DescribesSelf) { // Sample optional type implementation with alternative minimal requirements for
const Matcher<SampleOptional<int>> m = Optional(Eq(1)); // use with Optional matcher. In particular, while it doesn't have a bool
// conversion operator, it does have a has_value() method.
template <typename T>
class SampleOptionalWithoutBoolConversion {
public:
using value_type = T;
explicit SampleOptionalWithoutBoolConversion(T value)
: value_(std::move(value)), has_value_(true) {}
SampleOptionalWithoutBoolConversion() : value_(), has_value_(false) {}
bool has_value() const { return has_value_; }
const T& operator*() const { return value_; }
private:
T value_;
bool has_value_;
};
template <typename T>
class OptionalTest : public testing::Test {};
using OptionalTestTypes =
testing::Types<SampleOptional<int>,
SampleOptionalWithoutBoolConversion<int>>;
TYPED_TEST_SUITE(OptionalTest, OptionalTestTypes);
TYPED_TEST(OptionalTest, DescribesSelf) {
const Matcher<TypeParam> m = Optional(Eq(1));
EXPECT_EQ("value is equal to 1", Describe(m)); EXPECT_EQ("value is equal to 1", Describe(m));
} }
TEST(OptionalTest, ExplainsSelf) { TYPED_TEST(OptionalTest, ExplainsSelf) {
const Matcher<SampleOptional<int>> m = Optional(Eq(1)); const Matcher<TypeParam> m = Optional(Eq(1));
EXPECT_EQ("whose value 1 matches", Explain(m, SampleOptional<int>(1))); EXPECT_EQ("whose value 1 matches", Explain(m, TypeParam(1)));
EXPECT_EQ("whose value 2 doesn't match", Explain(m, SampleOptional<int>(2))); EXPECT_EQ("whose value 2 doesn't match", Explain(m, TypeParam(2)));
} }
TEST(OptionalTest, MatchesNonEmptyOptional) { TYPED_TEST(OptionalTest, MatchesNonEmptyOptional) {
const Matcher<SampleOptional<int>> m1 = Optional(1); const Matcher<TypeParam> m1 = Optional(1);
const Matcher<SampleOptional<int>> m2 = Optional(Eq(2)); const Matcher<TypeParam> m2 = Optional(Eq(2));
const Matcher<SampleOptional<int>> m3 = Optional(Lt(3)); const Matcher<TypeParam> m3 = Optional(Lt(3));
SampleOptional<int> opt(1); TypeParam opt(1);
EXPECT_TRUE(m1.Matches(opt)); EXPECT_TRUE(m1.Matches(opt));
EXPECT_FALSE(m2.Matches(opt)); EXPECT_FALSE(m2.Matches(opt));
EXPECT_TRUE(m3.Matches(opt)); EXPECT_TRUE(m3.Matches(opt));
} }
TEST(OptionalTest, DoesNotMatchNullopt) { TYPED_TEST(OptionalTest, DoesNotMatchNullopt) {
const Matcher<SampleOptional<int>> m = Optional(1); const Matcher<TypeParam> m = Optional(1);
SampleOptional<int> empty; TypeParam empty;
EXPECT_FALSE(m.Matches(empty)); EXPECT_FALSE(m.Matches(empty));
} }
TEST(OptionalTest, WorksWithMoveOnly) { TYPED_TEST(OptionalTest, ComposesWithMonomorphicMatchersTakingReferences) {
Matcher<SampleOptional<std::unique_ptr<int>>> m = Optional(Eq(nullptr)); const Matcher<const int&> eq1 = Eq(1);
EXPECT_TRUE(m.Matches(SampleOptional<std::unique_ptr<int>>(nullptr))); const Matcher<const int&> eq2 = Eq(2);
TypeParam opt(1);
EXPECT_THAT(opt, Optional(eq1));
EXPECT_THAT(opt, Optional(Not(eq2)));
EXPECT_THAT(opt, Optional(AllOf(eq1, Not(eq2))));
} }
TYPED_TEST(OptionalTest, ComposesWithMonomorphicMatchersRequiringConversion) {
const Matcher<int64_t> eq1 = Eq(1);
const Matcher<int64_t> eq2 = Eq(2);
TypeParam opt(1);
EXPECT_THAT(opt, Optional(eq1));
EXPECT_THAT(opt, Optional(Not(eq2)));
EXPECT_THAT(opt, Optional(AllOf(eq1, Not(eq2))));
}
template <typename T>
class MoveOnlyOptionalTest : public testing::Test {};
using MoveOnlyOptionalTestTypes =
testing::Types<SampleOptional<std::unique_ptr<int>>,
SampleOptionalWithoutBoolConversion<std::unique_ptr<int>>>;
TYPED_TEST_SUITE(MoveOnlyOptionalTest, MoveOnlyOptionalTestTypes);
TYPED_TEST(MoveOnlyOptionalTest, WorksWithMoveOnly) {
Matcher<TypeParam> m = Optional(Eq(nullptr));
EXPECT_TRUE(m.Matches(TypeParam(nullptr)));
}
#endif // GTEST_HAS_TYPED_TEST
class SampleVariantIntString { class SampleVariantIntString {
public: public:
SampleVariantIntString(int i) : i_(i), has_int_(true) {} SampleVariantIntString(int i) : i_(i), has_int_(true) {}
@ -863,7 +928,7 @@ TEST(ArgsTest, AcceptsOneTemplateArg) {
} }
TEST(ArgsTest, AcceptsTwoTemplateArgs) { TEST(ArgsTest, AcceptsTwoTemplateArgs) {
const std::tuple<short, int, long> t(4, 5, 6L); // NOLINT const std::tuple<short, int, long> t(short{4}, 5, 6L); // NOLINT
EXPECT_THAT(t, (Args<0, 1>(Lt()))); EXPECT_THAT(t, (Args<0, 1>(Lt())));
EXPECT_THAT(t, (Args<1, 2>(Lt()))); EXPECT_THAT(t, (Args<1, 2>(Lt())));
@ -871,13 +936,13 @@ TEST(ArgsTest, AcceptsTwoTemplateArgs) {
} }
TEST(ArgsTest, AcceptsRepeatedTemplateArgs) { TEST(ArgsTest, AcceptsRepeatedTemplateArgs) {
const std::tuple<short, int, long> t(4, 5, 6L); // NOLINT const std::tuple<short, int, long> t(short{4}, 5, 6L); // NOLINT
EXPECT_THAT(t, (Args<0, 0>(Eq()))); EXPECT_THAT(t, (Args<0, 0>(Eq())));
EXPECT_THAT(t, Not(Args<1, 1>(Ne()))); EXPECT_THAT(t, Not(Args<1, 1>(Ne())));
} }
TEST(ArgsTest, AcceptsDecreasingTemplateArgs) { TEST(ArgsTest, AcceptsDecreasingTemplateArgs) {
const std::tuple<short, int, long> t(4, 5, 6L); // NOLINT const std::tuple<short, int, long> t(short{4}, 5, 6L); // NOLINT
EXPECT_THAT(t, (Args<2, 0>(Gt()))); EXPECT_THAT(t, (Args<2, 0>(Gt())));
EXPECT_THAT(t, Not(Args<2, 1>(Lt()))); EXPECT_THAT(t, Not(Args<2, 1>(Lt())));
} }
@ -892,7 +957,7 @@ TEST(ArgsTest, AcceptsMoreTemplateArgsThanArityOfOriginalTuple) {
} }
TEST(ArgsTest, CanBeNested) { TEST(ArgsTest, CanBeNested) {
const std::tuple<short, int, long, int> t(4, 5, 6L, 6); // NOLINT const std::tuple<short, int, long, int> t(short{4}, 5, 6L, 6); // NOLINT
EXPECT_THAT(t, (Args<1, 2, 3>(Args<1, 2>(Eq())))); EXPECT_THAT(t, (Args<1, 2, 3>(Args<1, 2>(Eq()))));
EXPECT_THAT(t, (Args<0, 1, 3>(Args<0, 2>(Lt())))); EXPECT_THAT(t, (Args<0, 1, 3>(Args<0, 2>(Lt()))));
} }
@ -1570,10 +1635,10 @@ TEST_P(AnyOfArrayTestP, ExplainsMatchResultCorrectly) {
const Matcher<int> m1 = AnyOfArray(v1); const Matcher<int> m1 = AnyOfArray(v1);
const Matcher<int> m2 = AnyOfArray(v2); const Matcher<int> m2 = AnyOfArray(v2);
EXPECT_EQ("", Explain(m0, 0)); EXPECT_EQ("", Explain(m0, 0));
EXPECT_EQ("", Explain(m1, 1)); EXPECT_EQ("which matches (is equal to 1)", Explain(m1, 1));
EXPECT_EQ("", Explain(m1, 2)); EXPECT_EQ("isn't equal to 1", Explain(m1, 2));
EXPECT_EQ("", Explain(m2, 3)); EXPECT_EQ("which matches (is equal to 3)", Explain(m2, 3));
EXPECT_EQ("", Explain(m2, 4)); EXPECT_EQ("isn't equal to 2, and isn't equal to 3", Explain(m2, 4));
EXPECT_EQ("()", Describe(m0)); EXPECT_EQ("()", Describe(m0));
EXPECT_EQ("(is equal to 1)", Describe(m1)); EXPECT_EQ("(is equal to 1)", Describe(m1));
EXPECT_EQ("(is equal to 2) or (is equal to 3)", Describe(m2)); EXPECT_EQ("(is equal to 2) or (is equal to 3)", Describe(m2));
@ -1792,8 +1857,8 @@ TEST(ThrowsPredicateCompilesTest, ExceptionMatcherAcceptsBroadType) {
{ {
Matcher<uint64_t> inner = Eq(10); Matcher<uint64_t> inner = Eq(10);
Matcher<std::function<void()>> matcher = Throws<uint32_t>(inner); Matcher<std::function<void()>> matcher = Throws<uint32_t>(inner);
EXPECT_TRUE(matcher.Matches([]() { throw(uint32_t) 10; })); EXPECT_TRUE(matcher.Matches([]() { throw (uint32_t)10; }));
EXPECT_FALSE(matcher.Matches([]() { throw(uint32_t) 11; })); EXPECT_FALSE(matcher.Matches([]() { throw (uint32_t)11; }));
} }
} }
@ -1814,6 +1879,4 @@ TEST(ThrowsPredicateCompilesTest, MessageMatcherAcceptsNonMatcher) {
} // namespace gmock_matchers_test } // namespace gmock_matchers_test
} // namespace testing } // namespace testing
#ifdef _MSC_VER GTEST_DISABLE_MSC_WARNINGS_POP_() // 4244 4100
#pragma warning(pop)
#endif

View File

@ -31,22 +31,23 @@
// //
// This file tests the built-in actions in gmock-actions.h. // This file tests the built-in actions in gmock-actions.h.
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable : 4577)
#endif
#include "gmock/gmock-more-actions.h" #include "gmock/gmock-more-actions.h"
#include <algorithm>
#include <functional> #include <functional>
#include <iterator>
#include <memory> #include <memory>
#include <sstream> #include <sstream>
#include <string> #include <string>
#include <tuple>
#include <vector>
#include "gmock/gmock.h" #include "gmock/gmock.h"
#include "gtest/gtest-spi.h" #include "gtest/gtest-spi.h"
#include "gtest/gtest.h" #include "gtest/gtest.h"
GTEST_DISABLE_MSC_WARNINGS_PUSH_(4577)
namespace testing { namespace testing {
namespace gmock_more_actions_test { namespace gmock_more_actions_test {
@ -58,6 +59,7 @@ using testing::Invoke;
using testing::ReturnArg; using testing::ReturnArg;
using testing::ReturnPointee; using testing::ReturnPointee;
using testing::SaveArg; using testing::SaveArg;
using testing::SaveArgByMove;
using testing::SaveArgPointee; using testing::SaveArgPointee;
using testing::SetArgReferee; using testing::SetArgReferee;
using testing::Unused; using testing::Unused;
@ -84,6 +86,16 @@ struct UnaryFunctor {
int operator()(bool x) { return x ? 1 : -1; } int operator()(bool x) { return x ? 1 : -1; }
}; };
struct UnaryMoveOnlyFunctor : UnaryFunctor {
UnaryMoveOnlyFunctor() = default;
UnaryMoveOnlyFunctor(const UnaryMoveOnlyFunctor&) = delete;
UnaryMoveOnlyFunctor(UnaryMoveOnlyFunctor&&) = default;
};
struct OneShotUnaryFunctor {
int operator()(bool x) && { return x ? 1 : -1; }
};
const char* Binary(const char* input, short n) { return input + n; } // NOLINT const char* Binary(const char* input, short n) { return input + n; } // NOLINT
int Ternary(int x, char y, short z) { return x + y + z; } // NOLINT int Ternary(int x, char y, short z) { return x + y + z; } // NOLINT
@ -481,6 +493,34 @@ TEST(SaveArgActionTest, WorksForCompatibleType) {
EXPECT_EQ('a', result); EXPECT_EQ('a', result);
} }
struct MoveOnly {
explicit MoveOnly(int v) : i(v) {}
MoveOnly(MoveOnly&& o) {
i = o.i;
o.i = -1;
}
MoveOnly& operator=(MoveOnly&& o) {
i = o.i;
o.i = -1;
return *this;
}
int i;
};
TEST(SaveArgByMoveActionTest, WorksForSameType) {
MoveOnly result{0};
const Action<void(MoveOnly v)> a1 = SaveArgByMove<0>(&result);
a1.Perform(std::make_tuple(MoveOnly{5}));
EXPECT_EQ(5, result.i);
}
TEST(SaveArgByMoveActionTest, WorksForCompatibleType) {
MoveOnly result{0};
const Action<void(bool, MoveOnly)> a1 = SaveArgByMove<1>(&result);
a1.Perform(std::make_tuple(true, MoveOnly{7}));
EXPECT_EQ(7, result.i);
}
TEST(SaveArgPointeeActionTest, WorksForSameType) { TEST(SaveArgPointeeActionTest, WorksForSameType) {
int result = 0; int result = 0;
const int value = 5; const int value = 5;
@ -676,7 +716,7 @@ TEST(SetArrayArgumentTest, SetsTheNthArrayWithIteratorArgument) {
Action<MyFunction> a = SetArrayArgument<1>(letters.begin(), letters.end()); Action<MyFunction> a = SetArrayArgument<1>(letters.begin(), letters.end());
std::string s; std::string s;
a.Perform(std::make_tuple(true, back_inserter(s))); a.Perform(std::make_tuple(true, std::back_inserter(s)));
EXPECT_EQ(letters, s); EXPECT_EQ(letters, s);
} }
@ -697,12 +737,24 @@ TEST(InvokeArgumentTest, Function0) {
EXPECT_EQ(1, a.Perform(std::make_tuple(2, &Nullary))); EXPECT_EQ(1, a.Perform(std::make_tuple(2, &Nullary)));
} }
// Tests using InvokeArgument with a unary function. // Tests using InvokeArgument with a unary functor.
TEST(InvokeArgumentTest, Functor1) { TEST(InvokeArgumentTest, Functor1) {
Action<int(UnaryFunctor)> a = InvokeArgument<0>(true); // NOLINT Action<int(UnaryFunctor)> a = InvokeArgument<0>(true); // NOLINT
EXPECT_EQ(1, a.Perform(std::make_tuple(UnaryFunctor()))); EXPECT_EQ(1, a.Perform(std::make_tuple(UnaryFunctor())));
} }
// Tests using InvokeArgument with a unary move-only functor.
TEST(InvokeArgumentTest, Functor1MoveOnly) {
Action<int(UnaryMoveOnlyFunctor)> a = InvokeArgument<0>(true); // NOLINT
EXPECT_EQ(1, a.Perform(std::make_tuple(UnaryMoveOnlyFunctor())));
}
// Tests using InvokeArgument with a one-shot unary functor.
TEST(InvokeArgumentTest, OneShotFunctor1) {
Action<int(OneShotUnaryFunctor)> a = InvokeArgument<0>(true); // NOLINT
EXPECT_EQ(1, a.Perform(std::make_tuple(OneShotUnaryFunctor())));
}
// Tests using InvokeArgument with a 5-ary function. // Tests using InvokeArgument with a 5-ary function.
TEST(InvokeArgumentTest, Function5) { TEST(InvokeArgumentTest, Function5) {
Action<int(int (*)(int, int, int, int, int))> a = // NOLINT Action<int(int (*)(int, int, int, int, int))> a = // NOLINT
@ -733,7 +785,7 @@ TEST(InvokeArgumentTest, Functor6) {
// Tests using InvokeArgument with a 7-ary function. // Tests using InvokeArgument with a 7-ary function.
TEST(InvokeArgumentTest, Function7) { TEST(InvokeArgumentTest, Function7) {
Action<std::string(std::string(*)(const char*, const char*, const char*, Action<std::string(std::string (*)(const char*, const char*, const char*,
const char*, const char*, const char*, const char*, const char*, const char*,
const char*))> const char*))>
a = InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7"); a = InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7");
@ -742,7 +794,7 @@ TEST(InvokeArgumentTest, Function7) {
// Tests using InvokeArgument with a 8-ary function. // Tests using InvokeArgument with a 8-ary function.
TEST(InvokeArgumentTest, Function8) { TEST(InvokeArgumentTest, Function8) {
Action<std::string(std::string(*)(const char*, const char*, const char*, Action<std::string(std::string (*)(const char*, const char*, const char*,
const char*, const char*, const char*, const char*, const char*, const char*,
const char*, const char*))> const char*, const char*))>
a = InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7", "8"); a = InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7", "8");
@ -751,7 +803,7 @@ TEST(InvokeArgumentTest, Function8) {
// Tests using InvokeArgument with a 9-ary function. // Tests using InvokeArgument with a 9-ary function.
TEST(InvokeArgumentTest, Function9) { TEST(InvokeArgumentTest, Function9) {
Action<std::string(std::string(*)(const char*, const char*, const char*, Action<std::string(std::string (*)(const char*, const char*, const char*,
const char*, const char*, const char*, const char*, const char*, const char*,
const char*, const char*, const char*))> const char*, const char*, const char*))>
a = InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7", "8", "9"); a = InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7", "8", "9");
@ -760,7 +812,7 @@ TEST(InvokeArgumentTest, Function9) {
// Tests using InvokeArgument with a 10-ary function. // Tests using InvokeArgument with a 10-ary function.
TEST(InvokeArgumentTest, Function10) { TEST(InvokeArgumentTest, Function10) {
Action<std::string(std::string(*)( Action<std::string(std::string (*)(
const char*, const char*, const char*, const char*, const char*, const char*, const char*, const char*, const char*, const char*,
const char*, const char*, const char*, const char*, const char*))> const char*, const char*, const char*, const char*, const char*))>
a = InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7", "8", "9", "0"); a = InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7", "8", "9", "0");
@ -805,6 +857,22 @@ TEST(InvokeArgumentTest, ByExplicitConstReferenceFunction) {
EXPECT_FALSE(a.Perform(std::make_tuple(&ReferencesGlobalDouble))); EXPECT_FALSE(a.Perform(std::make_tuple(&ReferencesGlobalDouble)));
} }
TEST(InvokeArgumentTest, MoveOnlyType) {
struct Marker {};
struct {
// Method takes a unique_ptr (to a type we don't care about), and an
// invocable type.
MOCK_METHOD(bool, MockMethod,
(std::unique_ptr<Marker>, std::function<int()>), ());
} mock;
ON_CALL(mock, MockMethod(_, _)).WillByDefault(InvokeArgument<1>());
// This compiles, but is a little opaque as a workaround:
ON_CALL(mock, MockMethod(_, _))
.WillByDefault(WithArg<1>(InvokeArgument<0>()));
}
// Tests DoAll(a1, a2). // Tests DoAll(a1, a2).
TEST(DoAllTest, TwoActions) { TEST(DoAllTest, TwoActions) {
int n = 0; int n = 0;
@ -982,11 +1050,7 @@ TEST(DoAllTest, ImplicitlyConvertsActionArguments) {
// is expanded and macro expansion cannot contain #pragma. Therefore // is expanded and macro expansion cannot contain #pragma. Therefore
// we suppress them here. // we suppress them here.
// Also suppress C4503 decorated name length exceeded, name was truncated // Also suppress C4503 decorated name length exceeded, name was truncated
#ifdef _MSC_VER GTEST_DISABLE_MSC_WARNINGS_PUSH_(4100 4503)
#pragma warning(push)
#pragma warning(disable : 4100)
#pragma warning(disable : 4503)
#endif
// Tests the ACTION*() macro family. // Tests the ACTION*() macro family.
// Tests that ACTION() can define an action that doesn't reference the // Tests that ACTION() can define an action that doesn't reference the
@ -1548,3 +1612,6 @@ TEST(ActionTemplateTest, CanBeOverloadedOnNumberOfValueParameters) {
} // namespace gmock_more_actions_test } // namespace gmock_more_actions_test
} // namespace testing } // namespace testing
GTEST_DISABLE_MSC_WARNINGS_POP_() // 4100 4503
GTEST_DISABLE_MSC_WARNINGS_POP_() // 4577

View File

@ -40,7 +40,7 @@
// clash with ::testing::Mock. // clash with ::testing::Mock.
class Mock { class Mock {
public: public:
Mock() {} Mock() = default;
MOCK_METHOD0(DoThis, void()); MOCK_METHOD0(DoThis, void());
@ -78,7 +78,7 @@ class CallsMockMethodInDestructor {
class Foo { class Foo {
public: public:
virtual ~Foo() {} virtual ~Foo() = default;
virtual void DoThis() = 0; virtual void DoThis() = 0;
virtual int DoThat(bool flag) = 0; virtual int DoThat(bool flag) = 0;
@ -86,7 +86,7 @@ class Foo {
class MockFoo : public Foo { class MockFoo : public Foo {
public: public:
MockFoo() {} MockFoo() = default;
void Delete() { delete this; } void Delete() { delete this; }
MOCK_METHOD0(DoThis, void()); MOCK_METHOD0(DoThis, void());
@ -109,7 +109,7 @@ class MockBar {
(a10 ? 'T' : 'F'); (a10 ? 'T' : 'F');
} }
virtual ~MockBar() {} virtual ~MockBar() = default;
const std::string& str() const { return str_; } const std::string& str() const { return str_; }

View File

@ -70,7 +70,7 @@ static_assert(GMOCK_PP_INTERNAL_VAR_TEST(x, y) == 2, "");
static_assert(GMOCK_PP_INTERNAL_VAR_TEST(silly) == 1, ""); static_assert(GMOCK_PP_INTERNAL_VAR_TEST(silly) == 1, "");
static_assert(GMOCK_PP_INTERNAL_VAR_TEST(x, y, z) == 3, ""); static_assert(GMOCK_PP_INTERNAL_VAR_TEST(x, y, z) == 3, "");
// TODO(iserna): The following asserts fail in --config=lexan. // TODO(iserna): The following asserts fail in --config=windows.
#define GMOCK_PP_INTERNAL_IS_EMPTY_TEST_1 #define GMOCK_PP_INTERNAL_IS_EMPTY_TEST_1
static_assert(GMOCK_PP_IS_EMPTY(GMOCK_PP_INTERNAL_IS_EMPTY_TEST_1), ""); static_assert(GMOCK_PP_IS_EMPTY(GMOCK_PP_INTERNAL_IS_EMPTY_TEST_1), "");
static_assert(GMOCK_PP_IS_EMPTY(), ""); static_assert(GMOCK_PP_IS_EMPTY(), "");

View File

@ -98,7 +98,7 @@ class NonDefaultConstructible {
class MockA { class MockA {
public: public:
MockA() {} MockA() = default;
MOCK_METHOD1(DoA, void(int n)); MOCK_METHOD1(DoA, void(int n));
MOCK_METHOD1(ReturnResult, Result(int n)); MOCK_METHOD1(ReturnResult, Result(int n));
@ -113,7 +113,7 @@ class MockA {
class MockB { class MockB {
public: public:
MockB() {} MockB() = default;
MOCK_CONST_METHOD0(DoB, int()); // NOLINT MOCK_CONST_METHOD0(DoB, int()); // NOLINT
MOCK_METHOD1(DoB, int(int n)); // NOLINT MOCK_METHOD1(DoB, int(int n)); // NOLINT
@ -125,7 +125,7 @@ class MockB {
class ReferenceHoldingMock { class ReferenceHoldingMock {
public: public:
ReferenceHoldingMock() {} ReferenceHoldingMock() = default;
MOCK_METHOD1(AcceptReference, void(std::shared_ptr<MockA>*)); MOCK_METHOD1(AcceptReference, void(std::shared_ptr<MockA>*));
@ -143,12 +143,12 @@ class ReferenceHoldingMock {
class CC { class CC {
public: public:
virtual ~CC() {} virtual ~CC() = default;
virtual int Method() = 0; virtual int Method() = 0;
}; };
class MockCC : public CC { class MockCC : public CC {
public: public:
MockCC() {} MockCC() = default;
MOCK_METHOD0(Method, int()); MOCK_METHOD0(Method, int());
@ -804,39 +804,39 @@ TEST(ExpectCallTest, InfersCardinality1WhenThereIsWillRepeatedly) {
"to be called at least once"); "to be called at least once");
} }
#if defined(__cplusplus) && __cplusplus >= 201703L // TODO(b/396121064) - Fix this test under MSVC
#ifndef _MSC_VER
// It should be possible to return a non-moveable type from a mock action in // It should be possible to return a non-moveable type from a mock action in
// C++17 and above, where it's guaranteed that such a type can be initialized // C++17 and above, where it's guaranteed that such a type can be initialized
// from a prvalue returned from a function. // from a prvalue returned from a function.
TEST(ExpectCallTest, NonMoveableType) { TEST(ExpectCallTest, NonMoveableType) {
// Define a non-moveable result type. // Define a non-moveable result type.
struct Result { struct NonMoveableStruct {
explicit Result(int x_in) : x(x_in) {} explicit NonMoveableStruct(int x_in) : x(x_in) {}
Result(Result&&) = delete; NonMoveableStruct(NonMoveableStruct&&) = delete;
int x; int x;
}; };
static_assert(!std::is_move_constructible_v<Result>); static_assert(!std::is_move_constructible_v<NonMoveableStruct>);
static_assert(!std::is_copy_constructible_v<Result>); static_assert(!std::is_copy_constructible_v<NonMoveableStruct>);
static_assert(!std::is_move_assignable_v<Result>); static_assert(!std::is_move_assignable_v<NonMoveableStruct>);
static_assert(!std::is_copy_assignable_v<Result>); static_assert(!std::is_copy_assignable_v<NonMoveableStruct>);
// We should be able to use a callable that returns that result as both a // We should be able to use a callable that returns that result as both a
// OnceAction and an Action, whether the callable ignores arguments or not. // OnceAction and an Action, whether the callable ignores arguments or not.
const auto return_17 = [] { return Result(17); }; const auto return_17 = [] { return NonMoveableStruct(17); };
static_cast<void>(OnceAction<Result()>{return_17}); static_cast<void>(OnceAction<NonMoveableStruct()>{return_17});
static_cast<void>(Action<Result()>{return_17}); static_cast<void>(Action<NonMoveableStruct()>{return_17});
static_cast<void>(OnceAction<Result(int)>{return_17}); static_cast<void>(OnceAction<NonMoveableStruct(int)>{return_17});
static_cast<void>(Action<Result(int)>{return_17}); static_cast<void>(Action<NonMoveableStruct(int)>{return_17});
// It should be possible to return the result end to end through an // It should be possible to return the result end to end through an
// EXPECT_CALL statement, with both WillOnce and WillRepeatedly. // EXPECT_CALL statement, with both WillOnce and WillRepeatedly.
MockFunction<Result()> mock; MockFunction<NonMoveableStruct()> mock;
EXPECT_CALL(mock, Call) // EXPECT_CALL(mock, Call) //
.WillOnce(return_17) // .WillOnce(return_17) //
.WillRepeatedly(return_17); .WillRepeatedly(return_17);
@ -846,7 +846,7 @@ TEST(ExpectCallTest, NonMoveableType) {
EXPECT_EQ(17, mock.AsStdFunction()().x); EXPECT_EQ(17, mock.AsStdFunction()().x);
} }
#endif // C++17 and above #endif // _MSC_VER
// Tests that the n-th action is taken for the n-th matching // Tests that the n-th action is taken for the n-th matching
// invocation. // invocation.
@ -905,7 +905,7 @@ TEST(ExpectCallTest, TakesDefaultActionWhenWillListIsExhausted) {
" - returning default value.")); " - returning default value."));
} }
TEST(FunctionMockerMessageTest, ReportsExpectCallLocationForExhausedActions) { TEST(FunctionMockerMessageTest, ReportsExpectCallLocationForExhaustedActions) {
MockB b; MockB b;
std::string expect_call_location = FormatFileLocation(__FILE__, __LINE__ + 1); std::string expect_call_location = FormatFileLocation(__FILE__, __LINE__ + 1);
EXPECT_CALL(b, DoB()).Times(AnyNumber()).WillOnce(Return(1)); EXPECT_CALL(b, DoB()).Times(AnyNumber()).WillOnce(Return(1));
@ -1088,16 +1088,7 @@ TEST(UnexpectedCallTest, UnsatisfiedPrerequisites) {
// Verifies that the failure message contains the two unsatisfied // Verifies that the failure message contains the two unsatisfied
// pre-requisites but not the satisfied one. // pre-requisites but not the satisfied one.
#if GTEST_USES_PCRE #ifdef GTEST_USES_POSIX_RE
EXPECT_THAT(
r.message(),
ContainsRegex(
// PCRE has trouble using (.|\n) to match any character, but
// supports the (?s) prefix for using . to match any character.
"(?s)the following immediate pre-requisites are not satisfied:\n"
".*: pre-requisite #0\n"
".*: pre-requisite #1"));
#elif GTEST_USES_POSIX_RE
EXPECT_THAT(r.message(), EXPECT_THAT(r.message(),
ContainsRegex( ContainsRegex(
// POSIX RE doesn't understand the (?s) prefix, but has no // POSIX RE doesn't understand the (?s) prefix, but has no
@ -1112,7 +1103,7 @@ TEST(UnexpectedCallTest, UnsatisfiedPrerequisites) {
"the following immediate pre-requisites are not satisfied:")); "the following immediate pre-requisites are not satisfied:"));
EXPECT_THAT(r.message(), ContainsRegex(": pre-requisite #0")); EXPECT_THAT(r.message(), ContainsRegex(": pre-requisite #0"));
EXPECT_THAT(r.message(), ContainsRegex(": pre-requisite #1")); EXPECT_THAT(r.message(), ContainsRegex(": pre-requisite #1"));
#endif // GTEST_USES_PCRE #endif // GTEST_USES_POSIX_RE
b.DoB(1); b.DoB(1);
b.DoB(3); b.DoB(3);
@ -1779,16 +1770,11 @@ TEST(DeletingMockEarlyTest, Success2) {
// Suppresses warning on unreferenced formal parameter in MSVC with // Suppresses warning on unreferenced formal parameter in MSVC with
// -W4. // -W4.
#ifdef _MSC_VER GTEST_DISABLE_MSC_WARNINGS_PUSH_(4100)
#pragma warning(push)
#pragma warning(disable : 4100)
#endif
ACTION_P(Delete, ptr) { delete ptr; } ACTION_P(Delete, ptr) { delete ptr; }
#ifdef _MSC_VER GTEST_DISABLE_MSC_WARNINGS_POP_() // 4100
#pragma warning(pop)
#endif
TEST(DeletingMockEarlyTest, CanDeleteSelfInActionReturningVoid) { TEST(DeletingMockEarlyTest, CanDeleteSelfInActionReturningVoid) {
MockA* const a = new MockA; MockA* const a = new MockA;
@ -1895,7 +1881,7 @@ struct Unprintable {
class MockC { class MockC {
public: public:
MockC() {} MockC() = default;
MOCK_METHOD6(VoidMethod, void(bool cond, int n, std::string s, void* p, MOCK_METHOD6(VoidMethod, void(bool cond, int n, std::string s, void* p,
const Printable& x, Unprintable y)); const Printable& x, Unprintable y));
@ -2059,12 +2045,12 @@ class GMockVerboseFlagTest : public VerboseFlagPreservingFixture {
NaggyMock<MockA> a; NaggyMock<MockA> a;
const std::string note = const std::string note =
"NOTE: You can safely ignore the above warning unless this " "NOTE: You can safely ignore the above warning unless this "
"call should not happen. Do not suppress it by blindly adding " "call should not happen. Do not suppress it by adding "
"an EXPECT_CALL() if you don't mean to enforce the call. " "an EXPECT_CALL() if you don't mean to enforce the call. "
"See " "See "
"https://github.com/google/googletest/blob/main/docs/" "https://github.com/google/googletest/blob/main/docs/"
"gmock_cook_book.md#" "gmock_cook_book.md#"
"knowing-when-to-expect for details."; "knowing-when-to-expect-useoncall for details.";
// A void-returning function. // A void-returning function.
CaptureStdout(); CaptureStdout();
@ -2135,7 +2121,7 @@ void PrintTo(PrintMeNot /* dummy */, ::std::ostream* /* os */) {
class LogTestHelper { class LogTestHelper {
public: public:
LogTestHelper() {} LogTestHelper() = default;
MOCK_METHOD1(Foo, PrintMeNot(PrintMeNot)); MOCK_METHOD1(Foo, PrintMeNot(PrintMeNot));
@ -2602,14 +2588,7 @@ TEST(ParameterlessExpectationsTest,
} // namespace } // namespace
} // namespace testing } // namespace testing
// Allows the user to define their own main and then invoke gmock_main
// from it. This might be necessary on some platforms which require
// specific setup and teardown.
#if GMOCK_RENAME_MAIN
int gmock_main(int argc, char** argv) {
#else
int main(int argc, char** argv) { int main(int argc, char** argv) {
#endif // GMOCK_RENAME_MAIN
testing::InitGoogleMock(&argc, argv); testing::InitGoogleMock(&argc, argv);
// Ensures that the tests pass no matter what value of // Ensures that the tests pass no matter what value of
// --gmock_catch_leaked_mocks and --gmock_verbose the user specifies. // --gmock_catch_leaked_mocks and --gmock_verbose the user specifies.

View File

@ -29,6 +29,8 @@
// Tests Google Mock's functionality that depends on exceptions. // Tests Google Mock's functionality that depends on exceptions.
#include <exception>
#include "gmock/gmock.h" #include "gmock/gmock.h"
#include "gtest/gtest.h" #include "gtest/gtest.h"

View File

@ -54,50 +54,59 @@ class GMockLeakTest(gmock_test_utils.TestCase):
def testCatchesLeakedMockByDefault(self): def testCatchesLeakedMockByDefault(self):
self.assertNotEqual( self.assertNotEqual(
0, 0,
gmock_test_utils.Subprocess(TEST_WITH_EXPECT_CALL, gmock_test_utils.Subprocess(
env=environ).exit_code) TEST_WITH_EXPECT_CALL, env=environ
).exit_code,
)
self.assertNotEqual( self.assertNotEqual(
0, 0, gmock_test_utils.Subprocess(TEST_WITH_ON_CALL, env=environ).exit_code
gmock_test_utils.Subprocess(TEST_WITH_ON_CALL, )
env=environ).exit_code)
def testDoesNotCatchLeakedMockWhenDisabled(self): def testDoesNotCatchLeakedMockWhenDisabled(self):
self.assertEquals( self.assertEqual(
0, 0,
gmock_test_utils.Subprocess(TEST_WITH_EXPECT_CALL + gmock_test_utils.Subprocess(
['--gmock_catch_leaked_mocks=0'], TEST_WITH_EXPECT_CALL + ['--gmock_catch_leaked_mocks=0'],
env=environ).exit_code) env=environ,
self.assertEquals( ).exit_code,
)
self.assertEqual(
0, 0,
gmock_test_utils.Subprocess(TEST_WITH_ON_CALL + gmock_test_utils.Subprocess(
['--gmock_catch_leaked_mocks=0'], TEST_WITH_ON_CALL + ['--gmock_catch_leaked_mocks=0'], env=environ
env=environ).exit_code) ).exit_code,
)
def testCatchesLeakedMockWhenEnabled(self): def testCatchesLeakedMockWhenEnabled(self):
self.assertNotEqual( self.assertNotEqual(
0, 0,
gmock_test_utils.Subprocess(TEST_WITH_EXPECT_CALL + gmock_test_utils.Subprocess(
['--gmock_catch_leaked_mocks'], TEST_WITH_EXPECT_CALL + ['--gmock_catch_leaked_mocks'], env=environ
env=environ).exit_code) ).exit_code,
)
self.assertNotEqual( self.assertNotEqual(
0, 0,
gmock_test_utils.Subprocess(TEST_WITH_ON_CALL + gmock_test_utils.Subprocess(
['--gmock_catch_leaked_mocks'], TEST_WITH_ON_CALL + ['--gmock_catch_leaked_mocks'], env=environ
env=environ).exit_code) ).exit_code,
)
def testCatchesLeakedMockWhenEnabledWithExplictFlagValue(self): def testCatchesLeakedMockWhenEnabledWithExplictFlagValue(self):
self.assertNotEqual( self.assertNotEqual(
0, 0,
gmock_test_utils.Subprocess(TEST_WITH_EXPECT_CALL + gmock_test_utils.Subprocess(
['--gmock_catch_leaked_mocks=1'], TEST_WITH_EXPECT_CALL + ['--gmock_catch_leaked_mocks=1'],
env=environ).exit_code) env=environ,
).exit_code,
)
def testCatchesMultipleLeakedMocks(self): def testCatchesMultipleLeakedMocks(self):
self.assertNotEqual( self.assertNotEqual(
0, 0,
gmock_test_utils.Subprocess(TEST_MULTIPLE_LEAKS + gmock_test_utils.Subprocess(
['--gmock_catch_leaked_mocks'], TEST_MULTIPLE_LEAKS + ['--gmock_catch_leaked_mocks'], env=environ
env=environ).exit_code) ).exit_code,
)
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -40,13 +40,13 @@ using ::testing::Return;
class FooInterface { class FooInterface {
public: public:
virtual ~FooInterface() {} virtual ~FooInterface() = default;
virtual void DoThis() = 0; virtual void DoThis() = 0;
}; };
class MockFoo : public FooInterface { class MockFoo : public FooInterface {
public: public:
MockFoo() {} MockFoo() = default;
MOCK_METHOD0(DoThis, void()); MOCK_METHOD0(DoThis, void());

View File

@ -116,7 +116,7 @@
#include "gmock/gmock.h" #include "gmock/gmock.h"
#if !GTEST_OS_WINDOWS_MOBILE #ifndef GTEST_OS_WINDOWS_MOBILE
#include <errno.h> #include <errno.h>
#endif #endif
@ -181,11 +181,12 @@ using testing::WithArg;
using testing::WithArgs; using testing::WithArgs;
using testing::WithoutArgs; using testing::WithoutArgs;
#if !GTEST_OS_WINDOWS_MOBILE #ifndef GTEST_OS_WINDOWS_MOBILE
using testing::SetErrnoAndReturn; using testing::SetErrnoAndReturn;
#endif #endif
#if GTEST_HAS_EXCEPTIONS #if GTEST_HAS_EXCEPTIONS
using testing::Rethrow;
using testing::Throw; using testing::Throw;
#endif #endif
@ -194,7 +195,7 @@ using testing::MatchesRegex;
class Interface { class Interface {
public: public:
virtual ~Interface() {} virtual ~Interface() = default;
virtual void VoidFromString(char* str) = 0; virtual void VoidFromString(char* str) = 0;
virtual char* StringFromString(char* str) = 0; virtual char* StringFromString(char* str) = 0;
virtual int IntFromString(char* str) = 0; virtual int IntFromString(char* str) = 0;
@ -208,7 +209,7 @@ class Interface {
class Mock : public Interface { class Mock : public Interface {
public: public:
Mock() {} Mock() = default;
MOCK_METHOD1(VoidFromString, void(char* str)); MOCK_METHOD1(VoidFromString, void(char* str));
MOCK_METHOD1(StringFromString, char*(char* str)); MOCK_METHOD1(StringFromString, char*(char* str));
@ -306,7 +307,7 @@ TEST(LinkTest, TestSetArrayArgument) {
mock.VoidFromString(&ch); mock.VoidFromString(&ch);
} }
#if !GTEST_OS_WINDOWS_MOBILE #ifndef GTEST_OS_WINDOWS_MOBILE
// Tests the linkage of the SetErrnoAndReturn action. // Tests the linkage of the SetErrnoAndReturn action.
TEST(LinkTest, TestSetErrnoAndReturn) { TEST(LinkTest, TestSetErrnoAndReturn) {
@ -416,6 +417,14 @@ TEST(LinkTest, TestThrow) {
EXPECT_CALL(mock, VoidFromString(_)).WillOnce(Throw(42)); EXPECT_CALL(mock, VoidFromString(_)).WillOnce(Throw(42));
EXPECT_THROW(mock.VoidFromString(nullptr), int); EXPECT_THROW(mock.VoidFromString(nullptr), int);
} }
// Tests the linkage of the Rethrow action.
TEST(LinkTest, TestRethrow) {
Mock mock;
EXPECT_CALL(mock, VoidFromString(_))
.WillOnce(Rethrow(std::make_exception_ptr(42)));
EXPECT_THROW(mock.VoidFromString(nullptr), int);
}
#endif // GTEST_HAS_EXCEPTIONS #endif // GTEST_HAS_EXCEPTIONS
// The ACTION*() macros trigger warning C4100 (unreferenced formal // The ACTION*() macros trigger warning C4100 (unreferenced formal
@ -423,10 +432,7 @@ TEST(LinkTest, TestThrow) {
// the macro definition, as the warnings are generated when the macro // the macro definition, as the warnings are generated when the macro
// is expanded and macro expansion cannot contain #pragma. Therefore // is expanded and macro expansion cannot contain #pragma. Therefore
// we suppress them here. // we suppress them here.
#ifdef _MSC_VER GTEST_DISABLE_MSC_WARNINGS_PUSH_(4100)
#pragma warning(push)
#pragma warning(disable : 4100)
#endif
// Tests the linkage of actions created using ACTION macro. // Tests the linkage of actions created using ACTION macro.
namespace { namespace {
@ -459,9 +465,7 @@ ACTION_P2(ReturnEqualsEitherOf, first, second) {
} }
} // namespace } // namespace
#ifdef _MSC_VER GTEST_DISABLE_MSC_WARNINGS_POP_() // 4100
#pragma warning(pop)
#endif
TEST(LinkTest, TestActionP2Macro) { TEST(LinkTest, TestActionP2Macro) {
Mock mock; Mock mock;

View File

@ -168,9 +168,13 @@ class GMockOutputTest(gmock_test_utils.TestCase):
# The raw output should contain 2 leaked mock object errors for # The raw output should contain 2 leaked mock object errors for
# test GMockOutputTest.CatchesLeakedMocks. # test GMockOutputTest.CatchesLeakedMocks.
self.assertEqual(['GMockOutputTest.CatchesLeakedMocks', self.assertEqual(
'GMockOutputTest.CatchesLeakedMocks'], [
leaky_tests) 'GMockOutputTest.CatchesLeakedMocks',
'GMockOutputTest.CatchesLeakedMocks',
],
leaky_tests,
)
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -38,10 +38,7 @@
#include "gtest/gtest.h" #include "gtest/gtest.h"
// Silence C4100 (unreferenced formal parameter) // Silence C4100 (unreferenced formal parameter)
#ifdef _MSC_VER GTEST_DISABLE_MSC_WARNINGS_PUSH_(4100)
#pragma warning(push)
#pragma warning(disable : 4100)
#endif
using testing::_; using testing::_;
using testing::AnyNumber; using testing::AnyNumber;
@ -55,7 +52,7 @@ using testing::Value;
class MockFoo { class MockFoo {
public: public:
MockFoo() {} MockFoo() = default;
MOCK_METHOD3(Bar, char(const std::string& s, int i, double x)); MOCK_METHOD3(Bar, char(const std::string& s, int i, double x));
MOCK_METHOD2(Bar2, bool(int x, int y)); MOCK_METHOD2(Bar2, bool(int x, int y));
@ -286,6 +283,4 @@ int main(int argc, char** argv) {
return RUN_ALL_TESTS(); return RUN_ALL_TESTS();
} }
#ifdef _MSC_VER GTEST_DISABLE_MSC_WARNINGS_POP_() // 4100
#pragma warning(pop)
#endif

View File

@ -40,6 +40,7 @@ FILE:#: EXPECT_CALL(foo_, Bar2(0, _))...
Actual: 1 Actual: 1
Expected: to be called once Expected: to be called once
Actual: never called - unsatisfied and active Actual: never called - unsatisfied and active
[ FAILED ] GMockOutputTest.UnexpectedCall [ FAILED ] GMockOutputTest.UnexpectedCall
[ RUN ] GMockOutputTest.UnexpectedCallToVoidFunction [ RUN ] GMockOutputTest.UnexpectedCallToVoidFunction
unknown file: Failure unknown file: Failure
@ -53,6 +54,7 @@ FILE:#: EXPECT_CALL(foo_, Bar3(0, _))...
Actual: 1 Actual: 1
Expected: to be called once Expected: to be called once
Actual: never called - unsatisfied and active Actual: never called - unsatisfied and active
[ FAILED ] GMockOutputTest.UnexpectedCallToVoidFunction [ FAILED ] GMockOutputTest.UnexpectedCallToVoidFunction
[ RUN ] GMockOutputTest.ExcessiveCall [ RUN ] GMockOutputTest.ExcessiveCall
FILE:#: Failure FILE:#: Failure
@ -61,6 +63,7 @@ Mock function called more times than expected - returning default value.
Returns: false Returns: false
Expected: to be called once Expected: to be called once
Actual: called twice - over-saturated and active Actual: called twice - over-saturated and active
[ FAILED ] GMockOutputTest.ExcessiveCall [ FAILED ] GMockOutputTest.ExcessiveCall
[ RUN ] GMockOutputTest.ExcessiveCallToVoidFunction [ RUN ] GMockOutputTest.ExcessiveCallToVoidFunction
FILE:#: Failure FILE:#: Failure
@ -68,6 +71,7 @@ Mock function called more times than expected - returning directly.
Function call: Bar3(0, 1) Function call: Bar3(0, 1)
Expected: to be called once Expected: to be called once
Actual: called twice - over-saturated and active Actual: called twice - over-saturated and active
[ FAILED ] GMockOutputTest.ExcessiveCallToVoidFunction [ FAILED ] GMockOutputTest.ExcessiveCallToVoidFunction
[ RUN ] GMockOutputTest.UninterestingCall [ RUN ] GMockOutputTest.UninterestingCall
@ -75,14 +79,14 @@ GMOCK WARNING:
Uninteresting mock function call - returning default value. Uninteresting mock function call - returning default value.
Function call: Bar2(0, 1) Function call: Bar2(0, 1)
Returns: false Returns: false
NOTE: You can safely ignore the above warning unless this call should not happen. Do not suppress it by blindly adding an EXPECT_CALL() if you don't mean to enforce the call. See https://github.com/google/googletest/blob/main/docs/gmock_cook_book.md#knowing-when-to-expect for details. NOTE: You can safely ignore the above warning unless this call should not happen. Do not suppress it by adding an EXPECT_CALL() if you don't mean to enforce the call. See https://github.com/google/googletest/blob/main/docs/gmock_cook_book.md#knowing-when-to-expect-useoncall for details.
[ OK ] GMockOutputTest.UninterestingCall [ OK ] GMockOutputTest.UninterestingCall
[ RUN ] GMockOutputTest.UninterestingCallToVoidFunction [ RUN ] GMockOutputTest.UninterestingCallToVoidFunction
GMOCK WARNING: GMOCK WARNING:
Uninteresting mock function call - returning directly. Uninteresting mock function call - returning directly.
Function call: Bar3(0, 1) Function call: Bar3(0, 1)
NOTE: You can safely ignore the above warning unless this call should not happen. Do not suppress it by blindly adding an EXPECT_CALL() if you don't mean to enforce the call. See https://github.com/google/googletest/blob/main/docs/gmock_cook_book.md#knowing-when-to-expect for details. NOTE: You can safely ignore the above warning unless this call should not happen. Do not suppress it by adding an EXPECT_CALL() if you don't mean to enforce the call. See https://github.com/google/googletest/blob/main/docs/gmock_cook_book.md#knowing-when-to-expect-useoncall for details.
[ OK ] GMockOutputTest.UninterestingCallToVoidFunction [ OK ] GMockOutputTest.UninterestingCallToVoidFunction
[ RUN ] GMockOutputTest.RetiredExpectation [ RUN ] GMockOutputTest.RetiredExpectation
unknown file: Failure unknown file: Failure
@ -104,6 +108,7 @@ FILE:#: tried expectation #1: EXPECT_CALL(foo_, Bar2(0, 0))...
Actual: 1 Actual: 1
Expected: to be called once Expected: to be called once
Actual: never called - unsatisfied and active Actual: never called - unsatisfied and active
[ FAILED ] GMockOutputTest.RetiredExpectation [ FAILED ] GMockOutputTest.RetiredExpectation
[ RUN ] GMockOutputTest.UnsatisfiedPrerequisite [ RUN ] GMockOutputTest.UnsatisfiedPrerequisite
unknown file: Failure unknown file: Failure
@ -125,6 +130,7 @@ FILE:#: pre-requisite #0
(end of pre-requisites) (end of pre-requisites)
Expected: to be called once Expected: to be called once
Actual: never called - unsatisfied and active Actual: never called - unsatisfied and active
[ FAILED ] GMockOutputTest.UnsatisfiedPrerequisite [ FAILED ] GMockOutputTest.UnsatisfiedPrerequisite
[ RUN ] GMockOutputTest.UnsatisfiedPrerequisites [ RUN ] GMockOutputTest.UnsatisfiedPrerequisites
unknown file: Failure unknown file: Failure
@ -147,6 +153,7 @@ FILE:#: pre-requisite #1
(end of pre-requisites) (end of pre-requisites)
Expected: to be called once Expected: to be called once
Actual: never called - unsatisfied and active Actual: never called - unsatisfied and active
[ FAILED ] GMockOutputTest.UnsatisfiedPrerequisites [ FAILED ] GMockOutputTest.UnsatisfiedPrerequisites
[ RUN ] GMockOutputTest.UnsatisfiedWith [ RUN ] GMockOutputTest.UnsatisfiedWith
FILE:#: Failure FILE:#: Failure
@ -154,16 +161,19 @@ Actual function call count doesn't match EXPECT_CALL(foo_, Bar2(_, _))...
Expected args: are a pair where the first >= the second Expected args: are a pair where the first >= the second
Expected: to be called once Expected: to be called once
Actual: never called - unsatisfied and active Actual: never called - unsatisfied and active
[ FAILED ] GMockOutputTest.UnsatisfiedWith [ FAILED ] GMockOutputTest.UnsatisfiedWith
[ RUN ] GMockOutputTest.UnsatisfiedExpectation [ RUN ] GMockOutputTest.UnsatisfiedExpectation
FILE:#: Failure FILE:#: Failure
Actual function call count doesn't match EXPECT_CALL(foo_, Bar2(0, _))... Actual function call count doesn't match EXPECT_CALL(foo_, Bar2(0, _))...
Expected: to be called twice Expected: to be called twice
Actual: called once - unsatisfied and active Actual: called once - unsatisfied and active
FILE:#: Failure FILE:#: Failure
Actual function call count doesn't match EXPECT_CALL(foo_, Bar(_, _, _))... Actual function call count doesn't match EXPECT_CALL(foo_, Bar(_, _, _))...
Expected: to be called once Expected: to be called once
Actual: never called - unsatisfied and active Actual: never called - unsatisfied and active
[ FAILED ] GMockOutputTest.UnsatisfiedExpectation [ FAILED ] GMockOutputTest.UnsatisfiedExpectation
[ RUN ] GMockOutputTest.MismatchArguments [ RUN ] GMockOutputTest.MismatchArguments
unknown file: Failure unknown file: Failure
@ -180,6 +190,7 @@ FILE:#: EXPECT_CALL(foo_, Bar(Ref(s), _, Ge(0)))...
Actual: -0.1 Actual: -0.1
Expected: to be called once Expected: to be called once
Actual: never called - unsatisfied and active Actual: never called - unsatisfied and active
[ FAILED ] GMockOutputTest.MismatchArguments [ FAILED ] GMockOutputTest.MismatchArguments
[ RUN ] GMockOutputTest.MismatchWith [ RUN ] GMockOutputTest.MismatchWith
unknown file: Failure unknown file: Failure
@ -194,6 +205,7 @@ FILE:#: EXPECT_CALL(foo_, Bar2(Ge(2), Ge(1)))...
Actual: don't match Actual: don't match
Expected: to be called once Expected: to be called once
Actual: never called - unsatisfied and active Actual: never called - unsatisfied and active
[ FAILED ] GMockOutputTest.MismatchWith [ FAILED ] GMockOutputTest.MismatchWith
[ RUN ] GMockOutputTest.MismatchArgumentsAndWith [ RUN ] GMockOutputTest.MismatchArgumentsAndWith
unknown file: Failure unknown file: Failure
@ -210,6 +222,7 @@ FILE:#: EXPECT_CALL(foo_, Bar2(Ge(2), Ge(1)))...
Actual: don't match Actual: don't match
Expected: to be called once Expected: to be called once
Actual: never called - unsatisfied and active Actual: never called - unsatisfied and active
[ FAILED ] GMockOutputTest.MismatchArgumentsAndWith [ FAILED ] GMockOutputTest.MismatchArgumentsAndWith
[ RUN ] GMockOutputTest.UnexpectedCallWithDefaultAction [ RUN ] GMockOutputTest.UnexpectedCallWithDefaultAction
unknown file: Failure unknown file: Failure
@ -227,6 +240,7 @@ FILE:#: EXPECT_CALL(foo_, Bar2(2, 2))...
Actual: 0 Actual: 0
Expected: to be called once Expected: to be called once
Actual: never called - unsatisfied and active Actual: never called - unsatisfied and active
unknown file: Failure unknown file: Failure
Unexpected mock function call - taking default action specified at: Unexpected mock function call - taking default action specified at:
@ -242,6 +256,7 @@ FILE:#: EXPECT_CALL(foo_, Bar2(2, 2))...
Actual: 0 Actual: 0
Expected: to be called once Expected: to be called once
Actual: never called - unsatisfied and active Actual: never called - unsatisfied and active
[ FAILED ] GMockOutputTest.UnexpectedCallWithDefaultAction [ FAILED ] GMockOutputTest.UnexpectedCallWithDefaultAction
[ RUN ] GMockOutputTest.ExcessiveCallWithDefaultAction [ RUN ] GMockOutputTest.ExcessiveCallWithDefaultAction
FILE:#: Failure FILE:#: Failure
@ -251,6 +266,7 @@ FILE:#:
Returns: true Returns: true
Expected: to be called once Expected: to be called once
Actual: called twice - over-saturated and active Actual: called twice - over-saturated and active
FILE:#: Failure FILE:#: Failure
Mock function called more times than expected - taking default action specified at: Mock function called more times than expected - taking default action specified at:
FILE:#: FILE:#:
@ -258,6 +274,7 @@ FILE:#:
Returns: false Returns: false
Expected: to be called once Expected: to be called once
Actual: called twice - over-saturated and active Actual: called twice - over-saturated and active
[ FAILED ] GMockOutputTest.ExcessiveCallWithDefaultAction [ FAILED ] GMockOutputTest.ExcessiveCallWithDefaultAction
[ RUN ] GMockOutputTest.UninterestingCallWithDefaultAction [ RUN ] GMockOutputTest.UninterestingCallWithDefaultAction
@ -266,14 +283,14 @@ Uninteresting mock function call - taking default action specified at:
FILE:#: FILE:#:
Function call: Bar2(2, 2) Function call: Bar2(2, 2)
Returns: true Returns: true
NOTE: You can safely ignore the above warning unless this call should not happen. Do not suppress it by blindly adding an EXPECT_CALL() if you don't mean to enforce the call. See https://github.com/google/googletest/blob/main/docs/gmock_cook_book.md#knowing-when-to-expect for details. NOTE: You can safely ignore the above warning unless this call should not happen. Do not suppress it by adding an EXPECT_CALL() if you don't mean to enforce the call. See https://github.com/google/googletest/blob/main/docs/gmock_cook_book.md#knowing-when-to-expect-useoncall for details.
GMOCK WARNING: GMOCK WARNING:
Uninteresting mock function call - taking default action specified at: Uninteresting mock function call - taking default action specified at:
FILE:#: FILE:#:
Function call: Bar2(1, 1) Function call: Bar2(1, 1)
Returns: false Returns: false
NOTE: You can safely ignore the above warning unless this call should not happen. Do not suppress it by blindly adding an EXPECT_CALL() if you don't mean to enforce the call. See https://github.com/google/googletest/blob/main/docs/gmock_cook_book.md#knowing-when-to-expect for details. NOTE: You can safely ignore the above warning unless this call should not happen. Do not suppress it by adding an EXPECT_CALL() if you don't mean to enforce the call. See https://github.com/google/googletest/blob/main/docs/gmock_cook_book.md#knowing-when-to-expect-useoncall for details.
[ OK ] GMockOutputTest.UninterestingCallWithDefaultAction [ OK ] GMockOutputTest.UninterestingCallWithDefaultAction
[ RUN ] GMockOutputTest.ExplicitActionsRunOutWithDefaultAction [ RUN ] GMockOutputTest.ExplicitActionsRunOutWithDefaultAction
@ -292,7 +309,8 @@ Stack trace:
FILE:#: Failure FILE:#: Failure
Value of: (std::pair<int, bool>(42, true)) Value of: (std::pair<int, bool>(42, true))
Expected: is pair (first: is >= 48, second: true) Expected: is pair (first: is >= 48, second: true)
Actual: (42, true) (of type std::pair<int, bool>) Actual: (42, true) (of type std::pair<int,bool>)
[ FAILED ] GMockOutputTest.PrintsMatcher [ FAILED ] GMockOutputTest.PrintsMatcher
[ FAILED ] GMockOutputTest.UnexpectedCall [ FAILED ] GMockOutputTest.UnexpectedCall
[ FAILED ] GMockOutputTest.UnexpectedCallToVoidFunction [ FAILED ] GMockOutputTest.UnexpectedCallToVoidFunction

View File

@ -174,6 +174,6 @@ TEST(WideInitGoogleMockTest, ParsesGoogleMockFlagAndUnrecognizedFlag) {
// Makes sure Google Mock flags can be accessed in code. // Makes sure Google Mock flags can be accessed in code.
TEST(FlagTest, IsAccessibleInCode) { TEST(FlagTest, IsAccessibleInCode) {
bool dummy = bool dummy =
GMOCK_FLAG_GET(catch_leaked_mocks) && GMOCK_FLAG_GET(verbose) == ""; GMOCK_FLAG_GET(catch_leaked_mocks) && GMOCK_FLAG_GET(verbose).empty();
(void)dummy; // Avoids the "unused local variable" warning. (void)dummy; // Avoids the "unused local variable" warning.
} }

View File

@ -77,9 +77,6 @@ def GetExitStatus(exit_code):
return -1 return -1
# Suppresses the "Invalid const name" lint complaint
# pylint: disable-msg=C6409
# Exposes utilities from gtest_test_utils. # Exposes utilities from gtest_test_utils.
Subprocess = gtest_test_utils.Subprocess Subprocess = gtest_test_utils.Subprocess
TestCase = gtest_test_utils.TestCase TestCase = gtest_test_utils.TestCase
@ -87,8 +84,6 @@ environ = gtest_test_utils.environ
SetEnvVar = gtest_test_utils.SetEnvVar SetEnvVar = gtest_test_utils.SetEnvVar
PREMATURE_EXIT_FILE_ENV_VAR = gtest_test_utils.PREMATURE_EXIT_FILE_ENV_VAR PREMATURE_EXIT_FILE_ENV_VAR = gtest_test_utils.PREMATURE_EXIT_FILE_ENV_VAR
# pylint: enable-msg=C6409
def Main(): def Main():
"""Runs the unit test.""" """Runs the unit test."""

View File

@ -35,7 +35,7 @@ endif()
######################################################################## ########################################################################
# #
# Project-wide settings # Project-wide settings.
# Name of the project. # Name of the project.
# #
@ -44,21 +44,16 @@ endif()
# ${gtest_BINARY_DIR}. # ${gtest_BINARY_DIR}.
# Language "C" is required for find_package(Threads). # Language "C" is required for find_package(Threads).
# Project version: # Project version.
cmake_minimum_required(VERSION 3.5) cmake_minimum_required(VERSION 3.13)
cmake_policy(SET CMP0048 NEW)
project(gtest VERSION ${GOOGLETEST_VERSION} LANGUAGES CXX C) project(gtest VERSION ${GOOGLETEST_VERSION} LANGUAGES CXX C)
if (POLICY CMP0063) # Visibility
cmake_policy(SET CMP0063 NEW)
endif (POLICY CMP0063)
if (COMMAND set_up_hermetic_build) if (COMMAND set_up_hermetic_build)
set_up_hermetic_build() set_up_hermetic_build()
endif() endif()
# These commands only run if this is the main project # These commands only run if this is the main project.
if(CMAKE_PROJECT_NAME STREQUAL "gtest" OR CMAKE_PROJECT_NAME STREQUAL "googletest-distribution") if(CMAKE_PROJECT_NAME STREQUAL "gtest" OR CMAKE_PROJECT_NAME STREQUAL "googletest-distribution")
# BUILD_SHARED_LIBS is a standard CMake variable, but we declare it here to # BUILD_SHARED_LIBS is a standard CMake variable, but we declare it here to
@ -88,7 +83,7 @@ include(cmake/internal_utils.cmake)
config_compiler_and_linker() # Defined in internal_utils.cmake. config_compiler_and_linker() # Defined in internal_utils.cmake.
# Needed to set the namespace for both the export targets and the # Needed to set the namespace for both the export targets and the
# alias libraries # alias libraries.
set(cmake_package_name GTest CACHE INTERNAL "") set(cmake_package_name GTest CACHE INTERNAL "")
# Create the CMake package file descriptors. # Create the CMake package file descriptors.
@ -100,12 +95,14 @@ if (INSTALL_GTEST)
set(version_file "${generated_dir}/${cmake_package_name}ConfigVersion.cmake") set(version_file "${generated_dir}/${cmake_package_name}ConfigVersion.cmake")
write_basic_package_version_file(${version_file} VERSION ${GOOGLETEST_VERSION} COMPATIBILITY AnyNewerVersion) write_basic_package_version_file(${version_file} VERSION ${GOOGLETEST_VERSION} COMPATIBILITY AnyNewerVersion)
install(EXPORT ${targets_export_name} install(EXPORT ${targets_export_name}
COMPONENT "${PROJECT_NAME}"
NAMESPACE ${cmake_package_name}:: NAMESPACE ${cmake_package_name}::
DESTINATION ${cmake_files_install_dir}) DESTINATION ${cmake_files_install_dir})
set(config_file "${generated_dir}/${cmake_package_name}Config.cmake") set(config_file "${generated_dir}/${cmake_package_name}Config.cmake")
configure_package_config_file("${gtest_SOURCE_DIR}/cmake/Config.cmake.in" configure_package_config_file("${gtest_SOURCE_DIR}/cmake/Config.cmake.in"
"${config_file}" INSTALL_DESTINATION ${cmake_files_install_dir}) "${config_file}" INSTALL_DESTINATION ${cmake_files_install_dir})
install(FILES ${version_file} ${config_file} install(FILES ${version_file} ${config_file}
COMPONENT "${PROJECT_NAME}"
DESTINATION ${cmake_files_install_dir}) DESTINATION ${cmake_files_install_dir})
endif() endif()
@ -135,34 +132,26 @@ if(GTEST_HAS_ABSL)
absl::flags_reflection absl::flags_reflection
absl::flags_usage absl::flags_usage
absl::strings absl::strings
absl::any
absl::optional
absl::variant
re2::re2 re2::re2
) )
endif() endif()
cxx_library(gtest_main "${cxx_strict}" src/gtest_main.cc) cxx_library(gtest_main "${cxx_strict}" src/gtest_main.cc)
set_target_properties(gtest_main PROPERTIES VERSION ${GOOGLETEST_VERSION}) set_target_properties(gtest_main PROPERTIES VERSION ${GOOGLETEST_VERSION})
# If the CMake version supports it, attach header directory information string(REPLACE ";" "$<SEMICOLON>" dirs "${gtest_build_include_dirs}")
# to the targets for when we are part of a parent build (ie being pulled target_include_directories(gtest SYSTEM INTERFACE
# in via add_subdirectory() rather than being a standalone build).
if (DEFINED CMAKE_VERSION AND NOT "${CMAKE_VERSION}" VERSION_LESS "2.8.11")
string(REPLACE ";" "$<SEMICOLON>" dirs "${gtest_build_include_dirs}")
target_include_directories(gtest SYSTEM INTERFACE
"$<BUILD_INTERFACE:${dirs}>" "$<BUILD_INTERFACE:${dirs}>"
"$<INSTALL_INTERFACE:$<INSTALL_PREFIX>/${CMAKE_INSTALL_INCLUDEDIR}>") "$<INSTALL_INTERFACE:$<INSTALL_PREFIX>/${CMAKE_INSTALL_INCLUDEDIR}>")
target_include_directories(gtest_main SYSTEM INTERFACE target_include_directories(gtest_main SYSTEM INTERFACE
"$<BUILD_INTERFACE:${dirs}>" "$<BUILD_INTERFACE:${dirs}>"
"$<INSTALL_INTERFACE:$<INSTALL_PREFIX>/${CMAKE_INSTALL_INCLUDEDIR}>") "$<INSTALL_INTERFACE:$<INSTALL_PREFIX>/${CMAKE_INSTALL_INCLUDEDIR}>")
endif() if(CMAKE_SYSTEM_NAME MATCHES "QNX" AND CMAKE_SYSTEM_VERSION VERSION_GREATER_EQUAL 7.1)
if(CMAKE_SYSTEM_NAME MATCHES "QNX")
target_link_libraries(gtest PUBLIC regex) target_link_libraries(gtest PUBLIC regex)
endif() endif()
target_link_libraries(gtest_main PUBLIC gtest) target_link_libraries(gtest_main PUBLIC gtest)
######################################################################## ########################################################################
# #
# Install rules # Install rules.
install_project(gtest gtest_main) install_project(gtest gtest_main)
######################################################################## ########################################################################

View File

@ -12,7 +12,7 @@ GoogleTest comes with a CMake build script
([CMakeLists.txt](https://github.com/google/googletest/blob/main/CMakeLists.txt)) ([CMakeLists.txt](https://github.com/google/googletest/blob/main/CMakeLists.txt))
that can be used on a wide range of platforms ("C" stands for cross-platform.). that can be used on a wide range of platforms ("C" stands for cross-platform.).
If you don't have CMake installed already, you can download it for free from If you don't have CMake installed already, you can download it for free from
<http://www.cmake.org/>. <https://cmake.org/>.
CMake works by generating native makefiles or build projects that can be used in CMake works by generating native makefiles or build projects that can be used in
the compiler environment of your choice. You can either build GoogleTest as a the compiler environment of your choice. You can either build GoogleTest as a
@ -25,7 +25,7 @@ When building GoogleTest as a standalone project, the typical workflow starts
with with
``` ```
git clone https://github.com/google/googletest.git -b release-1.12.1 git clone https://github.com/google/googletest.git -b v1.17.0
cd googletest # Main directory of the cloned repository. cd googletest # Main directory of the cloned repository.
mkdir build # Create a directory to hold the build output. mkdir build # Create a directory to hold the build output.
cd build cd build
@ -124,12 +124,12 @@ match the project in which it is included.
#### C++ Standard Version #### C++ Standard Version
An environment that supports C++11 is required in order to successfully build An environment that supports C++17 is required in order to successfully build
GoogleTest. One way to ensure this is to specify the standard in the top-level GoogleTest. One way to ensure this is to specify the standard in the top-level
project, for example by using the `set(CMAKE_CXX_STANDARD 11)` command. If this project, for example by using the `set(CMAKE_CXX_STANDARD 17)` command along
is not feasible, for example in a C project using GoogleTest for validation, with `set(CMAKE_CXX_STANDARD_REQUIRED ON)`. If this is not feasible, for example
then it can be specified by adding it to the options for cmake via the in a C project using GoogleTest for validation, then it can be specified by
`DCMAKE_CXX_FLAGS` option. adding it to the options for cmake via the`-DCMAKE_CXX_FLAGS` option.
### Tweaking GoogleTest ### Tweaking GoogleTest
@ -145,18 +145,22 @@ We list the most frequently used macros below. For a complete list, see file
### Multi-threaded Tests ### Multi-threaded Tests
GoogleTest is thread-safe where the pthread library is available. After GoogleTest is thread-safe where the pthread library is available. After
`#include "gtest/gtest.h"`, you can check the `#include <gtest/gtest.h>`, you can check the `GTEST_IS_THREADSAFE` macro to see
`GTEST_IS_THREADSAFE` macro to see whether this is the case (yes if the macro is whether this is the case (yes if the macro is `#defined` to 1, no if it's
`#defined` to 1, no if it's undefined.). undefined.).
If GoogleTest doesn't correctly detect whether pthread is available in your If GoogleTest doesn't correctly detect whether pthread is available in your
environment, you can force it with environment, you can force it with
-DGTEST_HAS_PTHREAD=1 ```
-DGTEST_HAS_PTHREAD=1
```
or or
-DGTEST_HAS_PTHREAD=0 ```
-DGTEST_HAS_PTHREAD=0
```
When GoogleTest uses pthread, you may need to add flags to your compiler and/or When GoogleTest uses pthread, you may need to add flags to your compiler and/or
linker to select the pthread library, or you'll get link errors. If you use the linker to select the pthread library, or you'll get link errors. If you use the
@ -172,14 +176,18 @@ as a DLL on Windows) if you prefer.
To compile *gtest* as a shared library, add To compile *gtest* as a shared library, add
-DGTEST_CREATE_SHARED_LIBRARY=1 ```
-DGTEST_CREATE_SHARED_LIBRARY=1
```
to the compiler flags. You'll also need to tell the linker to produce a shared to the compiler flags. You'll also need to tell the linker to produce a shared
library instead - consult your linker's manual for how to do it. library instead - consult your linker's manual for how to do it.
To compile your *tests* that use the gtest shared library, add To compile your *tests* that use the gtest shared library, add
-DGTEST_LINKED_AS_SHARED_LIBRARY=1 ```
-DGTEST_LINKED_AS_SHARED_LIBRARY=1
```
to the compiler flags. to the compiler flags.
@ -200,7 +208,9 @@ rename its macro to avoid the conflict.
Specifically, if both GoogleTest and some other code define macro FOO, you can Specifically, if both GoogleTest and some other code define macro FOO, you can
add add
-DGTEST_DONT_DEFINE_FOO=1 ```
-DGTEST_DONT_DEFINE_FOO=1
```
to the compiler flags to tell GoogleTest to change the macro's name from `FOO` to the compiler flags to tell GoogleTest to change the macro's name from `FOO`
to `GTEST_FOO`. Currently `FOO` can be `ASSERT_EQ`, `ASSERT_FALSE`, `ASSERT_GE`, to `GTEST_FOO`. Currently `FOO` can be `ASSERT_EQ`, `ASSERT_FALSE`, `ASSERT_GE`,
@ -208,10 +218,14 @@ to `GTEST_FOO`. Currently `FOO` can be `ASSERT_EQ`, `ASSERT_FALSE`, `ASSERT_GE`,
`EXPECT_FALSE`, `EXPECT_TRUE`, `FAIL`, `SUCCEED`, `TEST`, or `TEST_F`. For `EXPECT_FALSE`, `EXPECT_TRUE`, `FAIL`, `SUCCEED`, `TEST`, or `TEST_F`. For
example, with `-DGTEST_DONT_DEFINE_TEST=1`, you'll need to write example, with `-DGTEST_DONT_DEFINE_TEST=1`, you'll need to write
GTEST_TEST(SomeTest, DoesThis) { ... } ```
GTEST_TEST(SomeTest, DoesThis) { ... }
```
instead of instead of
TEST(SomeTest, DoesThis) { ... } ```
TEST(SomeTest, DoesThis) { ... }
```
in order to define a test. in order to define a test.

View File

@ -4,6 +4,10 @@ if (@GTEST_HAS_PTHREAD@)
set(THREADS_PREFER_PTHREAD_FLAG @THREADS_PREFER_PTHREAD_FLAG@) set(THREADS_PREFER_PTHREAD_FLAG @THREADS_PREFER_PTHREAD_FLAG@)
find_dependency(Threads) find_dependency(Threads)
endif() endif()
if (@GTEST_HAS_ABSL@)
find_dependency(absl)
find_dependency(re2)
endif()
include("${CMAKE_CURRENT_LIST_DIR}/@targets_export_name@.cmake") include("${CMAKE_CURRENT_LIST_DIR}/@targets_export_name@.cmake")
check_required_components("@project_name@") check_required_components("@project_name@")

View File

@ -12,10 +12,6 @@
# Test and Google Mock's option() definitions, and thus must be # Test and Google Mock's option() definitions, and thus must be
# called *after* the options have been defined. # called *after* the options have been defined.
if (POLICY CMP0054)
cmake_policy(SET CMP0054 NEW)
endif (POLICY CMP0054)
# Tweaks CMake's default compiler/linker settings to suit Google Test's needs. # Tweaks CMake's default compiler/linker settings to suit Google Test's needs.
# #
# This must be a macro(), as inside a function string() can only # This must be a macro(), as inside a function string() can only
@ -63,7 +59,7 @@ endmacro()
# variable's value is empty before it's explicitly assigned to. # variable's value is empty before it's explicitly assigned to.
macro(config_compiler_and_linker) macro(config_compiler_and_linker)
# Note: pthreads on MinGW is not supported, even if available # Note: pthreads on MinGW is not supported, even if available
# instead, we use windows threading primitives # instead, we use windows threading primitives.
unset(GTEST_HAS_PTHREAD) unset(GTEST_HAS_PTHREAD)
if (NOT gtest_disable_pthreads AND NOT MINGW) if (NOT gtest_disable_pthreads AND NOT MINGW)
# Defines CMAKE_USE_PTHREADS_INIT and CMAKE_THREAD_LIBS_INIT. # Defines CMAKE_USE_PTHREADS_INIT and CMAKE_THREAD_LIBS_INIT.
@ -83,21 +79,31 @@ macro(config_compiler_and_linker)
set(cxx_exception_flags "-EHsc -D_HAS_EXCEPTIONS=1") set(cxx_exception_flags "-EHsc -D_HAS_EXCEPTIONS=1")
set(cxx_no_exception_flags "-EHs-c- -D_HAS_EXCEPTIONS=0") set(cxx_no_exception_flags "-EHs-c- -D_HAS_EXCEPTIONS=0")
set(cxx_no_rtti_flags "-GR-") set(cxx_no_rtti_flags "-GR-")
# Suppress "unreachable code" warning # Suppress "unreachable code" warning,
# http://stackoverflow.com/questions/3232669 explains the issue. # https://stackoverflow.com/questions/3232669 explains the issue.
set(cxx_base_flags "${cxx_base_flags} -wd4702") set(cxx_base_flags "${cxx_base_flags} -wd4702")
# Ensure MSVC treats source files as UTF-8 encoded. # Ensure MSVC treats source files as UTF-8 encoded.
if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
set(cxx_base_flags "${cxx_base_flags} -utf-8") set(cxx_base_flags "${cxx_base_flags} -utf-8")
endif() endif()
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "Clang") if (CMAKE_CXX_COMPILER_ID STREQUAL "IntelLLVM")
set(cxx_base_flags "-Wall -Wshadow -Wconversion") set(cxx_base_flags "${cxx_base_flags} /fp:precise -Wno-inconsistent-missing-override -Wno-microsoft-exception-spec -Wno-unused-function -Wno-unused-but-set-variable")
endif()
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "Clang" OR
CMAKE_CXX_COMPILER_ID STREQUAL "IntelLLVM")
set(cxx_base_flags "-Wall -Wshadow -Wconversion -Wundef")
set(cxx_exception_flags "-fexceptions") set(cxx_exception_flags "-fexceptions")
set(cxx_no_exception_flags "-fno-exceptions") set(cxx_no_exception_flags "-fno-exceptions")
set(cxx_strict_flags "-W -Wpointer-arith -Wreturn-type -Wcast-qual -Wwrite-strings -Wswitch -Wunused-parameter -Wcast-align -Wchar-subscripts -Winline -Wredundant-decls") set(cxx_strict_flags "-W -Wpointer-arith -Wreturn-type -Wcast-qual -Wwrite-strings -Wswitch -Wunused-parameter -Wcast-align -Winline -Wredundant-decls")
set(cxx_no_rtti_flags "-fno-rtti") set(cxx_no_rtti_flags "-fno-rtti")
if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
set(cxx_strict_flags "${cxx_strict_flags} -Wchar-subscripts")
endif()
if (CMAKE_CXX_COMPILER_ID STREQUAL "IntelLLVM")
set(cxx_base_flags "${cxx_base_flags} -Wno-implicit-float-size-conversion -ffp-model=precise")
endif()
elseif (CMAKE_COMPILER_IS_GNUCXX) elseif (CMAKE_COMPILER_IS_GNUCXX)
set(cxx_base_flags "-Wall -Wshadow") set(cxx_base_flags "-Wall -Wshadow -Wundef")
if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 7.0.0) if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 7.0.0)
set(cxx_base_flags "${cxx_base_flags} -Wno-error=dangling-else") set(cxx_base_flags "${cxx_base_flags} -Wno-error=dangling-else")
endif() endif()
@ -161,7 +167,7 @@ function(cxx_library_with_type name type cxx_flags)
set_target_properties(${name} set_target_properties(${name}
PROPERTIES PROPERTIES
COMPILE_FLAGS "${cxx_flags}") COMPILE_FLAGS "${cxx_flags}")
# Set the output directory for build artifacts # Set the output directory for build artifacts.
set_target_properties(${name} set_target_properties(${name}
PROPERTIES PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin" RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin"
@ -169,7 +175,7 @@ function(cxx_library_with_type name type cxx_flags)
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib" ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib"
PDB_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin" PDB_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin"
COMPILE_PDB_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib") COMPILE_PDB_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")
# make PDBs match library name # Make PDBs match library name.
get_target_property(pdb_debug_postfix ${name} DEBUG_POSTFIX) get_target_property(pdb_debug_postfix ${name} DEBUG_POSTFIX)
set_target_properties(${name} set_target_properties(${name}
PROPERTIES PROPERTIES
@ -182,23 +188,14 @@ function(cxx_library_with_type name type cxx_flags)
set_target_properties(${name} set_target_properties(${name}
PROPERTIES PROPERTIES
COMPILE_DEFINITIONS "GTEST_CREATE_SHARED_LIBRARY=1") COMPILE_DEFINITIONS "GTEST_CREATE_SHARED_LIBRARY=1")
if (NOT "${CMAKE_VERSION}" VERSION_LESS "2.8.11")
target_compile_definitions(${name} INTERFACE target_compile_definitions(${name} INTERFACE
$<INSTALL_INTERFACE:GTEST_LINKED_AS_SHARED_LIBRARY=1>) $<INSTALL_INTERFACE:GTEST_LINKED_AS_SHARED_LIBRARY=1>)
endif() endif()
endif()
if (DEFINED GTEST_HAS_PTHREAD) if (DEFINED GTEST_HAS_PTHREAD)
if ("${CMAKE_VERSION}" VERSION_LESS "3.1.0") target_link_libraries(${name} PUBLIC Threads::Threads)
set(threads_spec ${CMAKE_THREAD_LIBS_INIT})
else()
set(threads_spec Threads::Threads)
endif()
target_link_libraries(${name} PUBLIC ${threads_spec})
endif() endif()
if (NOT "${CMAKE_VERSION}" VERSION_LESS "3.8") target_compile_features(${name} PUBLIC cxx_std_17)
target_compile_features(${name} PUBLIC cxx_std_14)
endif()
endfunction() endfunction()
######################################################################## ########################################################################
@ -215,7 +212,7 @@ endfunction()
# cxx_executable_with_flags(name cxx_flags libs srcs...) # cxx_executable_with_flags(name cxx_flags libs srcs...)
# #
# creates a named C++ executable that depends on the given libraries and # Creates a named C++ executable that depends on the given libraries and
# is built from the given source files with the given compiler flags. # is built from the given source files with the given compiler flags.
function(cxx_executable_with_flags name cxx_flags libs) function(cxx_executable_with_flags name cxx_flags libs)
add_executable(${name} ${ARGN}) add_executable(${name} ${ARGN})
@ -242,7 +239,7 @@ endfunction()
# cxx_executable(name dir lib srcs...) # cxx_executable(name dir lib srcs...)
# #
# creates a named target that depends on the given libs and is built # Creates a named target that depends on the given libs and is built
# from the given source files. dir/name.cc is implicitly included in # from the given source files. dir/name.cc is implicitly included in
# the source file list. # the source file list.
function(cxx_executable name dir libs) function(cxx_executable name dir libs)
@ -250,24 +247,13 @@ function(cxx_executable name dir libs)
${name} "${cxx_default}" "${libs}" "${dir}/${name}.cc" ${ARGN}) ${name} "${cxx_default}" "${libs}" "${dir}/${name}.cc" ${ARGN})
endfunction() endfunction()
# CMP0094 policy enables finding a Python executable in the LOCATION order, as if(gtest_build_tests)
# specified by the PATH environment variable. find_package(Python3)
if (POLICY CMP0094)
cmake_policy(SET CMP0094 NEW)
endif()
# Sets PYTHONINTERP_FOUND and PYTHON_EXECUTABLE.
if ("${CMAKE_VERSION}" VERSION_LESS "3.12.0")
find_package(PythonInterp)
else()
find_package(Python COMPONENTS Interpreter)
set(PYTHONINTERP_FOUND ${Python_Interpreter_FOUND})
set(PYTHON_EXECUTABLE ${Python_EXECUTABLE})
endif() endif()
# cxx_test_with_flags(name cxx_flags libs srcs...) # cxx_test_with_flags(name cxx_flags libs srcs...)
# #
# creates a named C++ test that depends on the given libs and is built # Creates a named C++ test that depends on the given libs and is built
# from the given source files with the given compiler flags. # from the given source files with the given compiler flags.
function(cxx_test_with_flags name cxx_flags libs) function(cxx_test_with_flags name cxx_flags libs)
cxx_executable_with_flags(${name} "${cxx_flags}" "${libs}" ${ARGN}) cxx_executable_with_flags(${name} "${cxx_flags}" "${libs}" ${ARGN})
@ -276,7 +262,7 @@ endfunction()
# cxx_test(name libs srcs...) # cxx_test(name libs srcs...)
# #
# creates a named test target that depends on the given libs and is # Creates a named test target that depends on the given libs and is
# built from the given source files. Unlike cxx_test_with_flags, # built from the given source files. Unlike cxx_test_with_flags,
# test/name.cc is already implicitly included in the source file list. # test/name.cc is already implicitly included in the source file list.
function(cxx_test name libs) function(cxx_test name libs)
@ -286,37 +272,25 @@ endfunction()
# py_test(name) # py_test(name)
# #
# creates a Python test with the given name whose main module is in # Creates a Python test with the given name whose main module is in
# test/name.py. It does nothing if Python is not installed. # test/name.py. It does nothing if Python is not installed.
function(py_test name) function(py_test name)
if (PYTHONINTERP_FOUND) if (NOT Python3_Interpreter_FOUND)
if ("${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}" VERSION_GREATER 3.1) return()
if (CMAKE_CONFIGURATION_TYPES)
# Multi-configuration build generators as for Visual Studio save
# output in a subdirectory of CMAKE_CURRENT_BINARY_DIR (Debug,
# Release etc.), so we have to provide it here.
add_test(NAME ${name}
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/${name}.py
--build_dir=${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG> ${ARGN})
else (CMAKE_CONFIGURATION_TYPES)
# Single-configuration build generators like Makefile generators
# don't have subdirs below CMAKE_CURRENT_BINARY_DIR.
add_test(NAME ${name}
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/${name}.py
--build_dir=${CMAKE_CURRENT_BINARY_DIR} ${ARGN})
endif (CMAKE_CONFIGURATION_TYPES)
else()
# ${CMAKE_CURRENT_BINARY_DIR} is known at configuration time, so we can
# directly bind it from cmake. ${CTEST_CONFIGURATION_TYPE} is known
# only at ctest runtime (by calling ctest -c <Configuration>), so
# we have to escape $ to delay variable substitution here.
add_test(NAME ${name}
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/${name}.py
--build_dir=${CMAKE_CURRENT_BINARY_DIR}/\${CTEST_CONFIGURATION_TYPE} ${ARGN})
endif() endif()
get_cmake_property(is_multi "GENERATOR_IS_MULTI_CONFIG")
set(build_dir "${CMAKE_CURRENT_BINARY_DIR}")
if (is_multi)
set(build_dir "${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG>")
endif()
add_test(NAME ${name}
COMMAND Python3::Interpreter ${CMAKE_CURRENT_SOURCE_DIR}/test/${name}.py
--build_dir=${build_dir} ${ARGN})
# Make the Python import path consistent between Bazel and CMake. # Make the Python import path consistent between Bazel and CMake.
set_tests_properties(${name} PROPERTIES ENVIRONMENT PYTHONPATH=${CMAKE_SOURCE_DIR}) set_tests_properties(${name} PROPERTIES ENVIRONMENT PYTHONPATH=${CMAKE_SOURCE_DIR})
endif(PYTHONINTERP_FOUND)
endfunction() endfunction()
# install_project(targets...) # install_project(targets...)
@ -325,21 +299,24 @@ endfunction()
function(install_project) function(install_project)
if(INSTALL_GTEST) if(INSTALL_GTEST)
install(DIRECTORY "${PROJECT_SOURCE_DIR}/include/" install(DIRECTORY "${PROJECT_SOURCE_DIR}/include/"
COMPONENT "${PROJECT_NAME}"
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}") DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}")
# Install the project targets. # Install the project targets.
install(TARGETS ${ARGN} install(TARGETS ${ARGN}
EXPORT ${targets_export_name} EXPORT ${targets_export_name}
COMPONENT "${PROJECT_NAME}"
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}") LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}")
if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC") if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
# Install PDBs # Install PDBs.
foreach(t ${ARGN}) foreach(t ${ARGN})
get_target_property(t_pdb_name ${t} COMPILE_PDB_NAME) get_target_property(t_pdb_name ${t} COMPILE_PDB_NAME)
get_target_property(t_pdb_name_debug ${t} COMPILE_PDB_NAME_DEBUG) get_target_property(t_pdb_name_debug ${t} COMPILE_PDB_NAME_DEBUG)
get_target_property(t_pdb_output_directory ${t} PDB_OUTPUT_DIRECTORY) get_target_property(t_pdb_output_directory ${t} PDB_OUTPUT_DIRECTORY)
install(FILES install(FILES
"${t_pdb_output_directory}/\${CMAKE_INSTALL_CONFIG_NAME}/$<$<CONFIG:Debug>:${t_pdb_name_debug}>$<$<NOT:$<CONFIG:Debug>>:${t_pdb_name}>.pdb" "${t_pdb_output_directory}/\${CMAKE_INSTALL_CONFIG_NAME}/$<$<CONFIG:Debug>:${t_pdb_name_debug}>$<$<NOT:$<CONFIG:Debug>>:${t_pdb_name}>.pdb"
COMPONENT "${PROJECT_NAME}"
DESTINATION ${CMAKE_INSTALL_LIBDIR} DESTINATION ${CMAKE_INSTALL_LIBDIR}
OPTIONAL) OPTIONAL)
endforeach() endforeach()
@ -350,6 +327,7 @@ function(install_project)
configure_file("${PROJECT_SOURCE_DIR}/cmake/${t}.pc.in" configure_file("${PROJECT_SOURCE_DIR}/cmake/${t}.pc.in"
"${configured_pc}" @ONLY) "${configured_pc}" @ONLY)
install(FILES "${configured_pc}" install(FILES "${configured_pc}"
COMPONENT "${PROJECT_NAME}"
DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig") DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
endforeach() endforeach()
endif() endif()

View File

@ -130,6 +130,13 @@ namespace testing {
// Expected: Foo() is even // Expected: Foo() is even
// Actual: it's 5 // Actual: it's 5
// //
// Returned AssertionResult objects may not be ignored.
// Note: Disabled for SWIG as it doesn't parse attributes correctly.
#if !defined(SWIG)
class [[nodiscard]] AssertionResult;
#endif // !SWIG
class GTEST_API_ AssertionResult { class GTEST_API_ AssertionResult {
public: public:
// Copy constructor. // Copy constructor.
@ -181,7 +188,7 @@ class GTEST_API_ AssertionResult {
// assertion's expectation). When nothing has been streamed into the // assertion's expectation). When nothing has been streamed into the
// object, returns an empty string. // object, returns an empty string.
const char* message() const { const char* message() const {
return message_.get() != nullptr ? message_->c_str() : ""; return message_ != nullptr ? message_->c_str() : "";
} }
// Deprecated; please use message() instead. // Deprecated; please use message() instead.
const char* failure_message() const { return message(); } const char* failure_message() const { return message(); }
@ -204,7 +211,7 @@ class GTEST_API_ AssertionResult {
private: private:
// Appends the contents of message to message_. // Appends the contents of message to message_.
void AppendMessage(const Message& a_message) { void AppendMessage(const Message& a_message) {
if (message_.get() == nullptr) message_.reset(new ::std::string); if (message_ == nullptr) message_ = ::std::make_unique<::std::string>();
message_->append(a_message.GetString().c_str()); message_->append(a_message.GetString().c_str());
} }

View File

@ -51,7 +51,7 @@ GTEST_DECLARE_string_(death_test_style);
namespace testing { namespace testing {
#if GTEST_HAS_DEATH_TEST #ifdef GTEST_HAS_DEATH_TEST
namespace internal { namespace internal {
@ -203,7 +203,7 @@ class GTEST_API_ ExitedWithCode {
const int exit_code_; const int exit_code_;
}; };
#if !GTEST_OS_WINDOWS && !GTEST_OS_FUCHSIA #if !defined(GTEST_OS_WINDOWS) && !defined(GTEST_OS_FUCHSIA)
// Tests that an exit code describes an exit due to termination by a // Tests that an exit code describes an exit due to termination by a
// given signal. // given signal.
class GTEST_API_ KilledBySignal { class GTEST_API_ KilledBySignal {
@ -293,8 +293,8 @@ class GTEST_API_ KilledBySignal {
// statement is compiled but not executed, to ensure that // statement is compiled but not executed, to ensure that
// EXPECT_DEATH_IF_SUPPORTED compiles with a certain // EXPECT_DEATH_IF_SUPPORTED compiles with a certain
// parameter if and only if EXPECT_DEATH compiles with it. // parameter if and only if EXPECT_DEATH compiles with it.
// regex - A regex that a macro such as EXPECT_DEATH would use to test // regex_or_matcher - A regex that a macro such as EXPECT_DEATH would use
// the output of statement. This parameter has to be // to test the output of statement. This parameter has to be
// compiled but not evaluated by this macro, to ensure that // compiled but not evaluated by this macro, to ensure that
// this macro only accepts expressions that a macro such as // this macro only accepts expressions that a macro such as
// EXPECT_DEATH would accept. // EXPECT_DEATH would accept.
@ -311,13 +311,13 @@ class GTEST_API_ KilledBySignal {
// statement unconditionally returns or throws. The Message constructor at // statement unconditionally returns or throws. The Message constructor at
// the end allows the syntax of streaming additional messages into the // the end allows the syntax of streaming additional messages into the
// macro, for compilational compatibility with EXPECT_DEATH/ASSERT_DEATH. // macro, for compilational compatibility with EXPECT_DEATH/ASSERT_DEATH.
#define GTEST_UNSUPPORTED_DEATH_TEST(statement, regex, terminator) \ #define GTEST_UNSUPPORTED_DEATH_TEST(statement, regex_or_matcher, terminator) \
GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
if (::testing::internal::AlwaysTrue()) { \ if (::testing::internal::AlwaysTrue()) { \
GTEST_LOG_(WARNING) << "Death tests are not supported on this platform.\n" \ GTEST_LOG_(WARNING) << "Death tests are not supported on this platform.\n" \
<< "Statement '" #statement "' cannot be verified."; \ << "Statement '" #statement "' cannot be verified."; \
} else if (::testing::internal::AlwaysFalse()) { \ } else if (::testing::internal::AlwaysFalse()) { \
::testing::internal::RE::PartialMatch(".*", (regex)); \ ::testing::internal::MakeDeathTestMatcher(regex_or_matcher); \
GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \ GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \
terminator; \ terminator; \
} else \ } else \
@ -328,7 +328,7 @@ class GTEST_API_ KilledBySignal {
// death tests are supported; otherwise they just issue a warning. This is // death tests are supported; otherwise they just issue a warning. This is
// useful when you are combining death test assertions with normal test // useful when you are combining death test assertions with normal test
// assertions in one test. // assertions in one test.
#if GTEST_HAS_DEATH_TEST #ifdef GTEST_HAS_DEATH_TEST
#define EXPECT_DEATH_IF_SUPPORTED(statement, regex) \ #define EXPECT_DEATH_IF_SUPPORTED(statement, regex) \
EXPECT_DEATH(statement, regex) EXPECT_DEATH(statement, regex)
#define ASSERT_DEATH_IF_SUPPORTED(statement, regex) \ #define ASSERT_DEATH_IF_SUPPORTED(statement, regex) \

View File

@ -40,6 +40,7 @@
#define GOOGLETEST_INCLUDE_GTEST_GTEST_MATCHERS_H_ #define GOOGLETEST_INCLUDE_GTEST_GTEST_MATCHERS_H_
#include <atomic> #include <atomic>
#include <functional>
#include <memory> #include <memory>
#include <ostream> #include <ostream>
#include <string> #include <string>
@ -66,10 +67,10 @@ namespace testing {
// To implement a matcher Foo for type T, define: // To implement a matcher Foo for type T, define:
// 1. a class FooMatcherMatcher that implements the matcher interface: // 1. a class FooMatcherMatcher that implements the matcher interface:
// using is_gtest_matcher = void; // using is_gtest_matcher = void;
// bool MatchAndExplain(const T&, std::ostream*); // bool MatchAndExplain(const T&, std::ostream*) const;
// (MatchResultListener* can also be used instead of std::ostream*) // (MatchResultListener* can also be used instead of std::ostream*)
// void DescribeTo(std::ostream*); // void DescribeTo(std::ostream*) const;
// void DescribeNegationTo(std::ostream*); // void DescribeNegationTo(std::ostream*) const;
// //
// 2. a factory function that creates a Matcher<T> object from a // 2. a factory function that creates a Matcher<T> object from a
// FooMatcherMatcher. // FooMatcherMatcher.
@ -106,13 +107,13 @@ class MatchResultListener {
MatchResultListener& operator=(const MatchResultListener&) = delete; MatchResultListener& operator=(const MatchResultListener&) = delete;
}; };
inline MatchResultListener::~MatchResultListener() {} inline MatchResultListener::~MatchResultListener() = default;
// An instance of a subclass of this knows how to describe itself as a // An instance of a subclass of this knows how to describe itself as a
// matcher. // matcher.
class GTEST_API_ MatcherDescriberInterface { class GTEST_API_ MatcherDescriberInterface {
public: public:
virtual ~MatcherDescriberInterface() {} virtual ~MatcherDescriberInterface() = default;
// Describes this matcher to an ostream. The function should print // Describes this matcher to an ostream. The function should print
// a verb phrase that describes the property a value matching this // a verb phrase that describes the property a value matching this
@ -178,43 +179,6 @@ class MatcherInterface : public MatcherDescriberInterface {
namespace internal { namespace internal {
struct AnyEq {
template <typename A, typename B>
bool operator()(const A& a, const B& b) const {
return a == b;
}
};
struct AnyNe {
template <typename A, typename B>
bool operator()(const A& a, const B& b) const {
return a != b;
}
};
struct AnyLt {
template <typename A, typename B>
bool operator()(const A& a, const B& b) const {
return a < b;
}
};
struct AnyGt {
template <typename A, typename B>
bool operator()(const A& a, const B& b) const {
return a > b;
}
};
struct AnyLe {
template <typename A, typename B>
bool operator()(const A& a, const B& b) const {
return a <= b;
}
};
struct AnyGe {
template <typename A, typename B>
bool operator()(const A& a, const B& b) const {
return a >= b;
}
};
// A match result listener that ignores the explanation. // A match result listener that ignores the explanation.
class DummyMatchResultListener : public MatchResultListener { class DummyMatchResultListener : public MatchResultListener {
public: public:
@ -530,7 +494,7 @@ template <>
class GTEST_API_ Matcher<const std::string&> class GTEST_API_ Matcher<const std::string&>
: public internal::MatcherBase<const std::string&> { : public internal::MatcherBase<const std::string&> {
public: public:
Matcher() {} Matcher() = default;
explicit Matcher(const MatcherInterface<const std::string&>* impl) explicit Matcher(const MatcherInterface<const std::string&>* impl)
: internal::MatcherBase<const std::string&>(impl) {} : internal::MatcherBase<const std::string&>(impl) {}
@ -552,7 +516,7 @@ template <>
class GTEST_API_ Matcher<std::string> class GTEST_API_ Matcher<std::string>
: public internal::MatcherBase<std::string> { : public internal::MatcherBase<std::string> {
public: public:
Matcher() {} Matcher() = default;
explicit Matcher(const MatcherInterface<const std::string&>* impl) explicit Matcher(const MatcherInterface<const std::string&>* impl)
: internal::MatcherBase<std::string>(impl) {} : internal::MatcherBase<std::string>(impl) {}
@ -580,7 +544,7 @@ template <>
class GTEST_API_ Matcher<const internal::StringView&> class GTEST_API_ Matcher<const internal::StringView&>
: public internal::MatcherBase<const internal::StringView&> { : public internal::MatcherBase<const internal::StringView&> {
public: public:
Matcher() {} Matcher() = default;
explicit Matcher(const MatcherInterface<const internal::StringView&>* impl) explicit Matcher(const MatcherInterface<const internal::StringView&>* impl)
: internal::MatcherBase<const internal::StringView&>(impl) {} : internal::MatcherBase<const internal::StringView&>(impl) {}
@ -606,7 +570,7 @@ template <>
class GTEST_API_ Matcher<internal::StringView> class GTEST_API_ Matcher<internal::StringView>
: public internal::MatcherBase<internal::StringView> { : public internal::MatcherBase<internal::StringView> {
public: public:
Matcher() {} Matcher() = default;
explicit Matcher(const MatcherInterface<const internal::StringView&>* impl) explicit Matcher(const MatcherInterface<const internal::StringView&>* impl)
: internal::MatcherBase<internal::StringView>(impl) {} : internal::MatcherBase<internal::StringView>(impl) {}
@ -758,54 +722,86 @@ class ComparisonBase {
}; };
template <typename Rhs> template <typename Rhs>
class EqMatcher : public ComparisonBase<EqMatcher<Rhs>, Rhs, AnyEq> { class EqMatcher : public ComparisonBase<EqMatcher<Rhs>, Rhs, std::equal_to<>> {
public: public:
explicit EqMatcher(const Rhs& rhs) explicit EqMatcher(const Rhs& rhs)
: ComparisonBase<EqMatcher<Rhs>, Rhs, AnyEq>(rhs) {} : ComparisonBase<EqMatcher<Rhs>, Rhs, std::equal_to<>>(rhs) {}
static const char* Desc() { return "is equal to"; } static const char* Desc() { return "is equal to"; }
static const char* NegatedDesc() { return "isn't equal to"; } static const char* NegatedDesc() { return "isn't equal to"; }
}; };
template <typename Rhs> template <typename Rhs>
class NeMatcher : public ComparisonBase<NeMatcher<Rhs>, Rhs, AnyNe> { class NeMatcher
: public ComparisonBase<NeMatcher<Rhs>, Rhs, std::not_equal_to<>> {
public: public:
explicit NeMatcher(const Rhs& rhs) explicit NeMatcher(const Rhs& rhs)
: ComparisonBase<NeMatcher<Rhs>, Rhs, AnyNe>(rhs) {} : ComparisonBase<NeMatcher<Rhs>, Rhs, std::not_equal_to<>>(rhs) {}
static const char* Desc() { return "isn't equal to"; } static const char* Desc() { return "isn't equal to"; }
static const char* NegatedDesc() { return "is equal to"; } static const char* NegatedDesc() { return "is equal to"; }
}; };
template <typename Rhs> template <typename Rhs>
class LtMatcher : public ComparisonBase<LtMatcher<Rhs>, Rhs, AnyLt> { class LtMatcher : public ComparisonBase<LtMatcher<Rhs>, Rhs, std::less<>> {
public: public:
explicit LtMatcher(const Rhs& rhs) explicit LtMatcher(const Rhs& rhs)
: ComparisonBase<LtMatcher<Rhs>, Rhs, AnyLt>(rhs) {} : ComparisonBase<LtMatcher<Rhs>, Rhs, std::less<>>(rhs) {}
static const char* Desc() { return "is <"; } static const char* Desc() { return "is <"; }
static const char* NegatedDesc() { return "isn't <"; } static const char* NegatedDesc() { return "isn't <"; }
}; };
template <typename Rhs> template <typename Rhs>
class GtMatcher : public ComparisonBase<GtMatcher<Rhs>, Rhs, AnyGt> { class GtMatcher : public ComparisonBase<GtMatcher<Rhs>, Rhs, std::greater<>> {
public: public:
explicit GtMatcher(const Rhs& rhs) explicit GtMatcher(const Rhs& rhs)
: ComparisonBase<GtMatcher<Rhs>, Rhs, AnyGt>(rhs) {} : ComparisonBase<GtMatcher<Rhs>, Rhs, std::greater<>>(rhs) {}
static const char* Desc() { return "is >"; } static const char* Desc() { return "is >"; }
static const char* NegatedDesc() { return "isn't >"; } static const char* NegatedDesc() { return "isn't >"; }
}; };
template <typename Rhs> template <typename Rhs>
class LeMatcher : public ComparisonBase<LeMatcher<Rhs>, Rhs, AnyLe> { class LeMatcher
: public ComparisonBase<LeMatcher<Rhs>, Rhs, std::less_equal<>> {
public: public:
explicit LeMatcher(const Rhs& rhs) explicit LeMatcher(const Rhs& rhs)
: ComparisonBase<LeMatcher<Rhs>, Rhs, AnyLe>(rhs) {} : ComparisonBase<LeMatcher<Rhs>, Rhs, std::less_equal<>>(rhs) {}
static const char* Desc() { return "is <="; } static const char* Desc() { return "is <="; }
static const char* NegatedDesc() { return "isn't <="; } static const char* NegatedDesc() { return "isn't <="; }
}; };
template <typename Rhs> template <typename Rhs>
class GeMatcher : public ComparisonBase<GeMatcher<Rhs>, Rhs, AnyGe> { class GeMatcher
: public ComparisonBase<GeMatcher<Rhs>, Rhs, std::greater_equal<>> {
public: public:
explicit GeMatcher(const Rhs& rhs) explicit GeMatcher(const Rhs& rhs)
: ComparisonBase<GeMatcher<Rhs>, Rhs, AnyGe>(rhs) {} : ComparisonBase<GeMatcher<Rhs>, Rhs, std::greater_equal<>>(rhs) {}
static const char* Desc() { return "is >="; } static const char* Desc() { return "is >="; }
static const char* NegatedDesc() { return "isn't >="; } static const char* NegatedDesc() { return "isn't >="; }
}; };
// Same as `EqMatcher<Rhs>`, except that the `rhs` is stored as `StoredRhs` and
// must be implicitly convertible to `Rhs`.
template <typename Rhs, typename StoredRhs>
class ImplicitCastEqMatcher {
public:
explicit ImplicitCastEqMatcher(const StoredRhs& rhs) : stored_rhs_(rhs) {}
using is_gtest_matcher = void;
template <typename Lhs>
bool MatchAndExplain(const Lhs& lhs, std::ostream*) const {
return lhs == rhs();
}
void DescribeTo(std::ostream* os) const {
*os << "is equal to ";
UniversalPrint(rhs(), os);
}
void DescribeNegationTo(std::ostream* os) const {
*os << "isn't equal to ";
UniversalPrint(rhs(), os);
}
private:
Rhs rhs() const { return ImplicitCast_<Rhs>(stored_rhs_); }
StoredRhs stored_rhs_;
};
template <typename T, typename = typename std::enable_if< template <typename T, typename = typename std::enable_if<
std::is_constructible<std::string, T>::value>::type> std::is_constructible<std::string, T>::value>::type>
using StringLike = T; using StringLike = T;

View File

@ -56,6 +56,13 @@
#include "gtest/internal/gtest-port.h" #include "gtest/internal/gtest-port.h"
#ifdef GTEST_HAS_ABSL
#include <type_traits>
#include "absl/strings/has_absl_stringify.h"
#include "absl/strings/str_cat.h"
#endif // GTEST_HAS_ABSL
GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \ GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \
/* class A needs to have dll-interface to be used by clients of class B */) /* class A needs to have dll-interface to be used by clients of class B */)
@ -111,8 +118,17 @@ class GTEST_API_ Message {
*ss_ << str; *ss_ << str;
} }
// Streams a non-pointer value to this object. // Streams a non-pointer value to this object. If building a version of
template <typename T> // GoogleTest with ABSL, this overload is only enabled if the value does not
// have an AbslStringify definition.
template <
typename T
#ifdef GTEST_HAS_ABSL
,
typename std::enable_if<!absl::HasAbslStringify<T>::value, // NOLINT
int>::type = 0
#endif // GTEST_HAS_ABSL
>
inline Message& operator<<(const T& val) { inline Message& operator<<(const T& val) {
// Some libraries overload << for STL containers. These // Some libraries overload << for STL containers. These
// overloads are defined in the global namespace instead of ::std. // overloads are defined in the global namespace instead of ::std.
@ -133,6 +149,21 @@ class GTEST_API_ Message {
return *this; return *this;
} }
#ifdef GTEST_HAS_ABSL
// Streams a non-pointer value with an AbslStringify definition to this
// object.
template <typename T,
typename std::enable_if<absl::HasAbslStringify<T>::value, // NOLINT
int>::type = 0>
inline Message& operator<<(const T& val) {
// ::operator<< is needed here for a similar reason as with the non-Abseil
// version above
using ::operator<<;
*ss_ << absl::StrCat(val);
return *this;
}
#endif // GTEST_HAS_ABSL
// Streams a pointer value to this object. // Streams a pointer value to this object.
// //
// This function is an overload of the previous one. When you // This function is an overload of the previous one. When you

View File

@ -174,11 +174,12 @@ TEST_P(DerivedTest, DoesBlah) {
#endif // 0 #endif // 0
#include <functional>
#include <iterator> #include <iterator>
#include <utility> #include <utility>
#include "gtest/internal/gtest-internal.h" #include "gtest/internal/gtest-internal.h"
#include "gtest/internal/gtest-param-util.h" #include "gtest/internal/gtest-param-util.h" // IWYU pragma: export
#include "gtest/internal/gtest-port.h" #include "gtest/internal/gtest-port.h"
namespace testing { namespace testing {
@ -413,7 +414,8 @@ internal::CartesianProductHolder<Generator...> Combine(const Generator&... g) {
// Synopsis: // Synopsis:
// ConvertGenerator<T>(gen) // ConvertGenerator<T>(gen)
// - returns a generator producing the same elements as generated by gen, but // - returns a generator producing the same elements as generated by gen, but
// each element is static_cast to type T before being returned // each T-typed element is static_cast to a type deduced from the interface
// that accepts this generator, and then returned
// //
// It is useful when using the Combine() function to get the generated // It is useful when using the Combine() function to get the generated
// parameters in a custom type instead of std::tuple // parameters in a custom type instead of std::tuple
@ -441,15 +443,71 @@ internal::CartesianProductHolder<Generator...> Combine(const Generator&... g) {
// Combine(Values("cat", "dog"), // Combine(Values("cat", "dog"),
// Values(BLACK, WHITE)))); // Values(BLACK, WHITE))));
// //
template <typename T> template <typename RequestedT>
internal::ParamConverterGenerator<T> ConvertGenerator( internal::ParamConverterGenerator<RequestedT> ConvertGenerator(
internal::ParamGenerator<T> gen) { internal::ParamGenerator<RequestedT> gen) {
return internal::ParamConverterGenerator<T>(gen); return internal::ParamConverterGenerator<RequestedT>(std::move(gen));
}
// As above, but takes a callable as a second argument. The callable converts
// the generated parameter to the test fixture's parameter type. This allows you
// to use a parameter type that does not have a converting constructor from the
// generated type.
//
// Example:
//
// This will instantiate tests in test suite AnimalTest each one with
// the parameter values tuple("cat", BLACK), tuple("cat", WHITE),
// tuple("dog", BLACK), and tuple("dog", WHITE):
//
// enum Color { BLACK, GRAY, WHITE };
// struct ParamType {
// std::string animal;
// Color color;
// };
// class AnimalTest
// : public testing::TestWithParam<ParamType> {...};
//
// TEST_P(AnimalTest, AnimalLooksNice) {...}
//
// INSTANTIATE_TEST_SUITE_P(
// AnimalVariations, AnimalTest,
// ConvertGenerator(Combine(Values("cat", "dog"), Values(BLACK, WHITE)),
// [](std::tuple<std::string, Color> t) {
// return ParamType{.animal = std::get<0>(t),
// .color = std::get<1>(t)};
// }));
//
template <typename T, int&... ExplicitArgumentBarrier, typename Gen,
typename Func,
typename StdFunction = decltype(std::function(std::declval<Func>()))>
internal::ParamConverterGenerator<T, StdFunction> ConvertGenerator(Gen&& gen,
Func&& f) {
return internal::ParamConverterGenerator<T, StdFunction>(
std::forward<Gen>(gen), std::forward<Func>(f));
}
// As above, but infers the T from the supplied std::function instead of
// having the caller specify it.
template <int&... ExplicitArgumentBarrier, typename Gen, typename Func,
typename StdFunction = decltype(std::function(std::declval<Func>()))>
auto ConvertGenerator(Gen&& gen, Func&& f) {
constexpr bool is_single_arg_std_function =
internal::IsSingleArgStdFunction<StdFunction>::value;
if constexpr (is_single_arg_std_function) {
return ConvertGenerator<
typename internal::FuncSingleParamType<StdFunction>::type>(
std::forward<Gen>(gen), std::forward<Func>(f));
} else {
static_assert(is_single_arg_std_function,
"The call signature must contain a single argument.");
}
} }
#define TEST_P(test_suite_name, test_name) \ #define TEST_P(test_suite_name, test_name) \
class GTEST_TEST_CLASS_NAME_(test_suite_name, test_name) \ class GTEST_TEST_CLASS_NAME_(test_suite_name, test_name) \
: public test_suite_name, private ::testing::internal::GTestNonCopyable {\ : public test_suite_name, \
private ::testing::internal::GTestNonCopyable { \
public: \ public: \
GTEST_TEST_CLASS_NAME_(test_suite_name, test_name)() {} \ GTEST_TEST_CLASS_NAME_(test_suite_name, test_name)() {} \
void TestBody() override; \ void TestBody() override; \
@ -468,7 +526,7 @@ internal::ParamConverterGenerator<T> ConvertGenerator(
::testing::internal::CodeLocation(__FILE__, __LINE__)); \ ::testing::internal::CodeLocation(__FILE__, __LINE__)); \
return 0; \ return 0; \
} \ } \
static int gtest_registering_dummy_ GTEST_ATTRIBUTE_UNUSED_; \ [[maybe_unused]] static int gtest_registering_dummy_; \
}; \ }; \
int GTEST_TEST_CLASS_NAME_(test_suite_name, \ int GTEST_TEST_CLASS_NAME_(test_suite_name, \
test_name)::gtest_registering_dummy_ = \ test_name)::gtest_registering_dummy_ = \
@ -513,8 +571,7 @@ internal::ParamConverterGenerator<T> ConvertGenerator(
::testing::internal::DefaultParamName<test_suite_name::ParamType>, \ ::testing::internal::DefaultParamName<test_suite_name::ParamType>, \
DUMMY_PARAM_))))(info); \ DUMMY_PARAM_))))(info); \
} \ } \
static int gtest_##prefix##test_suite_name##_dummy_ \ [[maybe_unused]] static int gtest_##prefix##test_suite_name##_dummy_ = \
GTEST_ATTRIBUTE_UNUSED_ = \
::testing::UnitTest::GetInstance() \ ::testing::UnitTest::GetInstance() \
->parameterized_test_registry() \ ->parameterized_test_registry() \
.GetTestSuitePatternHolder<test_suite_name>( \ .GetTestSuitePatternHolder<test_suite_name>( \
@ -523,8 +580,8 @@ internal::ParamConverterGenerator<T> ConvertGenerator(
->AddTestSuiteInstantiation( \ ->AddTestSuiteInstantiation( \
GTEST_STRINGIFY_(prefix), \ GTEST_STRINGIFY_(prefix), \
&gtest_##prefix##test_suite_name##_EvalGenerator_, \ &gtest_##prefix##test_suite_name##_EvalGenerator_, \
&gtest_##prefix##test_suite_name##_EvalGenerateName_, \ &gtest_##prefix##test_suite_name##_EvalGenerateName_, __FILE__, \
__FILE__, __LINE__) __LINE__)
// Allow Marking a Parameterized test class as not needing to be instantiated. // Allow Marking a Parameterized test class as not needing to be instantiated.
#define GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(T) \ #define GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(T) \

View File

@ -43,6 +43,9 @@
// 1. foo::PrintTo(const T&, ostream*) // 1. foo::PrintTo(const T&, ostream*)
// 2. operator<<(ostream&, const T&) defined in either foo or the // 2. operator<<(ostream&, const T&) defined in either foo or the
// global namespace. // global namespace.
// * Prefer AbslStringify(..) to operator<<(..), per https://abseil.io/tips/215.
// * Define foo::PrintTo(..) if the type already has AbslStringify(..), but an
// alternative presentation in test results is of interest.
// //
// However if T is an STL-style container then it is printed element-wise // However if T is an STL-style container then it is printed element-wise
// unless foo::PrintTo(const T&, ostream*) is defined. Note that // unless foo::PrintTo(const T&, ostream*) is defined. Note that
@ -101,20 +104,36 @@
#ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_PRINTERS_H_ #ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_PRINTERS_H_
#define GOOGLETEST_INCLUDE_GTEST_GTEST_PRINTERS_H_ #define GOOGLETEST_INCLUDE_GTEST_GTEST_PRINTERS_H_
#include <any>
#include <functional> #include <functional>
#include <memory> #include <memory>
#include <optional>
#include <ostream> // NOLINT #include <ostream> // NOLINT
#include <sstream> #include <sstream>
#include <string> #include <string>
#include <string_view>
#include <tuple> #include <tuple>
#include <type_traits> #include <type_traits>
#include <typeinfo> #include <typeinfo>
#include <utility> #include <utility>
#include <variant>
#include <vector> #include <vector>
#ifdef GTEST_HAS_ABSL
#include "absl/strings/has_absl_stringify.h"
#include "absl/strings/str_cat.h"
#endif // GTEST_HAS_ABSL
#include "gtest/internal/gtest-internal.h" #include "gtest/internal/gtest-internal.h"
#include "gtest/internal/gtest-port.h" #include "gtest/internal/gtest-port.h"
#if GTEST_INTERNAL_HAS_STD_SPAN
#include <span> // NOLINT
#endif // GTEST_INTERNAL_HAS_STD_SPAN
#if GTEST_INTERNAL_HAS_COMPARE_LIB
#include <compare> // NOLINT
#endif // GTEST_INTERNAL_HAS_COMPARE_LIB
namespace testing { namespace testing {
// Definitions in the internal* namespaces are subject to change without notice. // Definitions in the internal* namespaces are subject to change without notice.
@ -124,13 +143,32 @@ namespace internal {
template <typename T> template <typename T>
void UniversalPrint(const T& value, ::std::ostream* os); void UniversalPrint(const T& value, ::std::ostream* os);
template <typename T>
struct IsStdSpan {
static constexpr bool value = false;
};
#if GTEST_INTERNAL_HAS_STD_SPAN
template <typename E>
struct IsStdSpan<std::span<E>> {
static constexpr bool value = true;
};
#endif // GTEST_INTERNAL_HAS_STD_SPAN
// Used to print an STL-style container when the user doesn't define // Used to print an STL-style container when the user doesn't define
// a PrintTo() for it. // a PrintTo() for it.
//
// NOTE: Since std::span does not have const_iterator until C++23, it would
// fail IsContainerTest before C++23. However, IsContainerTest only uses
// the presence of const_iterator to avoid treating iterators as containers
// because of iterator::iterator. Which means std::span satisfies the *intended*
// condition of IsContainerTest.
struct ContainerPrinter { struct ContainerPrinter {
template <typename T, template <typename T,
typename = typename std::enable_if< typename = typename std::enable_if<
(sizeof(IsContainerTest<T>(0)) == sizeof(IsContainer)) && ((sizeof(IsContainerTest<T>(0)) == sizeof(IsContainer)) &&
!IsRecursiveContainer<T>::value>::type> !IsRecursiveContainer<T>::value) ||
IsStdSpan<T>::value>::type>
static void PrintValue(const T& container, std::ostream* os) { static void PrintValue(const T& container, std::ostream* os) {
const size_t kMaxCount = 32; // The maximum number of elements to print. const size_t kMaxCount = 32; // The maximum number of elements to print.
*os << '{'; *os << '{';
@ -206,12 +244,13 @@ struct StreamPrinter {
// Don't accept member pointers here. We'd print them via implicit // Don't accept member pointers here. We'd print them via implicit
// conversion to bool, which isn't useful. // conversion to bool, which isn't useful.
typename = typename std::enable_if< typename = typename std::enable_if<
!std::is_member_pointer<T>::value>::type, !std::is_member_pointer<T>::value>::type>
// Only accept types for which we can find a streaming operator via // Only accept types for which we can find a streaming operator via
// ADL (possibly involving implicit conversions). // ADL (possibly involving implicit conversions).
typename = decltype(std::declval<std::ostream&>() // (Use SFINAE via return type, because it seems GCC < 12 doesn't handle name
<< std::declval<const T&>())> // lookup properly when we do it in the template parameter list.)
static void PrintValue(const T& value, ::std::ostream* os) { static auto PrintValue(const T& value, ::std::ostream* os)
-> decltype((void)(*os << value)) {
// Call streaming operator found by ADL, possibly with implicit conversions // Call streaming operator found by ADL, possibly with implicit conversions
// of the arguments. // of the arguments.
*os << value; *os << value;
@ -259,6 +298,17 @@ struct ConvertibleToStringViewPrinter {
#endif #endif
}; };
#ifdef GTEST_HAS_ABSL
struct ConvertibleToAbslStringifyPrinter {
template <typename T,
typename = typename std::enable_if<
absl::HasAbslStringify<T>::value>::type> // NOLINT
static void PrintValue(const T& value, ::std::ostream* os) {
*os << absl::StrCat(value);
}
};
#endif // GTEST_HAS_ABSL
// Prints the given number of bytes in the given object to the given // Prints the given number of bytes in the given object to the given
// ostream. // ostream.
GTEST_API_ void PrintBytesInObjectTo(const unsigned char* obj_bytes, GTEST_API_ void PrintBytesInObjectTo(const unsigned char* obj_bytes,
@ -297,8 +347,8 @@ struct FindFirstPrinter<
// - Print containers (they have begin/end/etc). // - Print containers (they have begin/end/etc).
// - Print function pointers. // - Print function pointers.
// - Print object pointers. // - Print object pointers.
// - Use the stream operator, if available.
// - Print protocol buffers. // - Print protocol buffers.
// - Use the stream operator, if available.
// - Print types convertible to BiggestInt. // - Print types convertible to BiggestInt.
// - Print types convertible to StringView, if available. // - Print types convertible to StringView, if available.
// - Fallback to printing the raw bytes of the object. // - Fallback to printing the raw bytes of the object.
@ -306,9 +356,13 @@ template <typename T>
void PrintWithFallback(const T& value, ::std::ostream* os) { void PrintWithFallback(const T& value, ::std::ostream* os) {
using Printer = typename FindFirstPrinter< using Printer = typename FindFirstPrinter<
T, void, ContainerPrinter, FunctionPointerPrinter, PointerPrinter, T, void, ContainerPrinter, FunctionPointerPrinter, PointerPrinter,
ProtobufPrinter,
#ifdef GTEST_HAS_ABSL
ConvertibleToAbslStringifyPrinter,
#endif // GTEST_HAS_ABSL
internal_stream_operator_without_lexical_name_lookup::StreamPrinter, internal_stream_operator_without_lexical_name_lookup::StreamPrinter,
ProtobufPrinter, ConvertibleToIntegerPrinter, ConvertibleToIntegerPrinter, ConvertibleToStringViewPrinter,
ConvertibleToStringViewPrinter, RawBytesPrinter, FallbackPrinter>::type; RawBytesPrinter, FallbackPrinter>::type;
Printer::PrintValue(value, os); Printer::PrintValue(value, os);
} }
@ -471,11 +525,15 @@ GTEST_API_ void PrintTo(wchar_t wc, ::std::ostream* os);
GTEST_API_ void PrintTo(char32_t c, ::std::ostream* os); GTEST_API_ void PrintTo(char32_t c, ::std::ostream* os);
inline void PrintTo(char16_t c, ::std::ostream* os) { inline void PrintTo(char16_t c, ::std::ostream* os) {
PrintTo(ImplicitCast_<char32_t>(c), os); // TODO(b/418738869): Incorrect for values not representing valid codepoints.
// Also see https://github.com/google/googletest/issues/4762.
PrintTo(static_cast<char32_t>(c), os);
} }
#ifdef __cpp_char8_t #ifdef __cpp_lib_char8_t
inline void PrintTo(char8_t c, ::std::ostream* os) { inline void PrintTo(char8_t c, ::std::ostream* os) {
PrintTo(ImplicitCast_<char32_t>(c), os); // TODO(b/418738869): Incorrect for values not representing valid codepoints.
// Also see https://github.com/google/googletest/issues/4762.
PrintTo(static_cast<char32_t>(c), os);
} }
#endif #endif
@ -506,43 +564,63 @@ int AppropriateResolution(FloatType val) {
int full = std::numeric_limits<FloatType>::max_digits10; int full = std::numeric_limits<FloatType>::max_digits10;
if (val < 0) val = -val; if (val < 0) val = -val;
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wfloat-equal"
#endif
if (val < 1000000) { if (val < 1000000) {
FloatType mulfor6 = 1e10; FloatType mulfor6 = 1e10;
if (val >= 100000.0) { // 100,000 to 999,999 // Without these static casts, the template instantiation for float would
// fail to compile when -Wdouble-promotion is enabled, as the arithmetic and
// comparison logic would promote floats to doubles.
if (val >= static_cast<FloatType>(100000.0)) { // 100,000 to 999,999
mulfor6 = 1.0; mulfor6 = 1.0;
} else if (val >= 10000.0) { } else if (val >= static_cast<FloatType>(10000.0)) {
mulfor6 = 1e1; mulfor6 = 1e1;
} else if (val >= 1000.0) { } else if (val >= static_cast<FloatType>(1000.0)) {
mulfor6 = 1e2; mulfor6 = 1e2;
} else if (val >= 100.0) { } else if (val >= static_cast<FloatType>(100.0)) {
mulfor6 = 1e3; mulfor6 = 1e3;
} else if (val >= 10.0) { } else if (val >= static_cast<FloatType>(10.0)) {
mulfor6 = 1e4; mulfor6 = 1e4;
} else if (val >= 1.0) { } else if (val >= static_cast<FloatType>(1.0)) {
mulfor6 = 1e5; mulfor6 = 1e5;
} else if (val >= 0.1) { } else if (val >= static_cast<FloatType>(0.1)) {
mulfor6 = 1e6; mulfor6 = 1e6;
} else if (val >= 0.01) { } else if (val >= static_cast<FloatType>(0.01)) {
mulfor6 = 1e7; mulfor6 = 1e7;
} else if (val >= 0.001) { } else if (val >= static_cast<FloatType>(0.001)) {
mulfor6 = 1e8; mulfor6 = 1e8;
} else if (val >= 0.0001) { } else if (val >= static_cast<FloatType>(0.0001)) {
mulfor6 = 1e9; mulfor6 = 1e9;
} }
if (static_cast<int32_t>(val * mulfor6 + 0.5) / mulfor6 == val) return 6; if (static_cast<FloatType>(static_cast<int32_t>(
} else if (val < 1e10) { val * mulfor6 + (static_cast<FloatType>(0.5)))) /
FloatType divfor6 = 1.0; mulfor6 ==
if (val >= 1e9) { // 1,000,000,000 to 9,999,999,999 val)
return 6;
} else if (val < static_cast<FloatType>(1e10)) {
FloatType divfor6 = static_cast<FloatType>(1.0);
if (val >= static_cast<FloatType>(1e9)) { // 1,000,000,000 to 9,999,999,999
divfor6 = 10000; divfor6 = 10000;
} else if (val >= 1e8) { // 100,000,000 to 999,999,999 } else if (val >=
static_cast<FloatType>(1e8)) { // 100,000,000 to 999,999,999
divfor6 = 1000; divfor6 = 1000;
} else if (val >= 1e7) { // 10,000,000 to 99,999,999 } else if (val >=
static_cast<FloatType>(1e7)) { // 10,000,000 to 99,999,999
divfor6 = 100; divfor6 = 100;
} else if (val >= 1e6) { // 1,000,000 to 9,999,999 } else if (val >= static_cast<FloatType>(1e6)) { // 1,000,000 to 9,999,999
divfor6 = 10; divfor6 = 10;
} }
if (static_cast<int32_t>(val / divfor6 + 0.5) * divfor6 == val) return 6; if (static_cast<FloatType>(static_cast<int32_t>(
val / divfor6 + (static_cast<FloatType>(0.5)))) *
divfor6 ==
val)
return 6;
} }
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
return full; return full;
} }
@ -580,7 +658,7 @@ inline void PrintTo(const unsigned char* s, ::std::ostream* os) {
inline void PrintTo(unsigned char* s, ::std::ostream* os) { inline void PrintTo(unsigned char* s, ::std::ostream* os) {
PrintTo(ImplicitCast_<const void*>(s), os); PrintTo(ImplicitCast_<const void*>(s), os);
} }
#ifdef __cpp_char8_t #ifdef __cpp_lib_char8_t
// Overloads for u8 strings. // Overloads for u8 strings.
GTEST_API_ void PrintTo(const char8_t* s, ::std::ostream* os); GTEST_API_ void PrintTo(const char8_t* s, ::std::ostream* os);
inline void PrintTo(char8_t* s, ::std::ostream* os) { inline void PrintTo(char8_t* s, ::std::ostream* os) {
@ -625,44 +703,63 @@ void PrintRawArrayTo(const T a[], size_t count, ::std::ostream* os) {
} }
} }
// Overloads for ::std::string. // Overloads for ::std::string and ::std::string_view
GTEST_API_ void PrintStringTo(const ::std::string& s, ::std::ostream* os); GTEST_API_ void PrintStringTo(::std::string_view s, ::std::ostream* os);
inline void PrintTo(const ::std::string& s, ::std::ostream* os) { inline void PrintTo(const ::std::string& s, ::std::ostream* os) {
PrintStringTo(s, os); PrintStringTo(s, os);
} }
inline void PrintTo(::std::string_view s, ::std::ostream* os) {
PrintStringTo(s, os);
}
// Overloads for ::std::u8string // Overloads for ::std::u8string and ::std::u8string_view
#ifdef __cpp_lib_char8_t #ifdef __cpp_lib_char8_t
GTEST_API_ void PrintU8StringTo(const ::std::u8string& s, ::std::ostream* os); GTEST_API_ void PrintU8StringTo(::std::u8string_view s, ::std::ostream* os);
inline void PrintTo(const ::std::u8string& s, ::std::ostream* os) { inline void PrintTo(const ::std::u8string& s, ::std::ostream* os) {
PrintU8StringTo(s, os); PrintU8StringTo(s, os);
} }
inline void PrintTo(::std::u8string_view s, ::std::ostream* os) {
PrintU8StringTo(s, os);
}
#endif #endif
// Overloads for ::std::u16string // Overloads for ::std::u16string and ::std::u16string_view
GTEST_API_ void PrintU16StringTo(const ::std::u16string& s, ::std::ostream* os); GTEST_API_ void PrintU16StringTo(::std::u16string_view s, ::std::ostream* os);
inline void PrintTo(const ::std::u16string& s, ::std::ostream* os) { inline void PrintTo(const ::std::u16string& s, ::std::ostream* os) {
PrintU16StringTo(s, os); PrintU16StringTo(s, os);
} }
inline void PrintTo(::std::u16string_view s, ::std::ostream* os) {
PrintU16StringTo(s, os);
}
// Overloads for ::std::u32string // Overloads for ::std::u32string and ::std::u32string_view
GTEST_API_ void PrintU32StringTo(const ::std::u32string& s, ::std::ostream* os); GTEST_API_ void PrintU32StringTo(::std::u32string_view s, ::std::ostream* os);
inline void PrintTo(const ::std::u32string& s, ::std::ostream* os) { inline void PrintTo(const ::std::u32string& s, ::std::ostream* os) {
PrintU32StringTo(s, os); PrintU32StringTo(s, os);
} }
inline void PrintTo(::std::u32string_view s, ::std::ostream* os) {
PrintU32StringTo(s, os);
}
// Overloads for ::std::wstring. // Overloads for ::std::wstring and ::std::wstring_view
#if GTEST_HAS_STD_WSTRING #if GTEST_HAS_STD_WSTRING
GTEST_API_ void PrintWideStringTo(const ::std::wstring& s, ::std::ostream* os); GTEST_API_ void PrintWideStringTo(::std::wstring_view s, ::std::ostream* os);
inline void PrintTo(const ::std::wstring& s, ::std::ostream* os) { inline void PrintTo(const ::std::wstring& s, ::std::ostream* os) {
PrintWideStringTo(s, os); PrintWideStringTo(s, os);
} }
inline void PrintTo(::std::wstring_view s, ::std::ostream* os) {
PrintWideStringTo(s, os);
}
#endif // GTEST_HAS_STD_WSTRING #endif // GTEST_HAS_STD_WSTRING
#if GTEST_INTERNAL_HAS_STRING_VIEW #if GTEST_INTERNAL_HAS_STRING_VIEW
// Overload for internal::StringView. // Overload for internal::StringView. Needed for build configurations where
// internal::StringView is an alias for absl::string_view, but absl::string_view
// is a distinct type from std::string_view.
template <int&... ExplicitArgumentBarrier, typename T = internal::StringView,
std::enable_if_t<!std::is_same_v<T, ::std::string_view>, int> = 0>
inline void PrintTo(internal::StringView sp, ::std::ostream* os) { inline void PrintTo(internal::StringView sp, ::std::ostream* os) {
PrintTo(::std::string(sp), os); PrintStringTo(sp, os);
} }
#endif // GTEST_INTERNAL_HAS_STRING_VIEW #endif // GTEST_INTERNAL_HAS_STRING_VIEW
@ -716,6 +813,41 @@ void PrintTo(const std::shared_ptr<T>& ptr, std::ostream* os) {
(PrintSmartPointer<T>)(ptr, os, 0); (PrintSmartPointer<T>)(ptr, os, 0);
} }
#if GTEST_INTERNAL_HAS_COMPARE_LIB
template <typename T>
void PrintOrderingHelper(T ordering, std::ostream* os) {
if (ordering == T::less) {
*os << "(less)";
} else if (ordering == T::greater) {
*os << "(greater)";
} else if (ordering == T::equivalent) {
*os << "(equivalent)";
} else {
*os << "(unknown ordering)";
}
}
inline void PrintTo(std::strong_ordering ordering, std::ostream* os) {
if (ordering == std::strong_ordering::equal) {
*os << "(equal)";
} else {
PrintOrderingHelper(ordering, os);
}
}
inline void PrintTo(std::partial_ordering ordering, std::ostream* os) {
if (ordering == std::partial_ordering::unordered) {
*os << "(unordered)";
} else {
PrintOrderingHelper(ordering, os);
}
}
inline void PrintTo(std::weak_ordering ordering, std::ostream* os) {
PrintOrderingHelper(ordering, os);
}
#endif
// Helper function for printing a tuple. T must be instantiated with // Helper function for printing a tuple. T must be instantiated with
// a tuple type. // a tuple type.
template <typename T> template <typename T>
@ -785,14 +917,11 @@ class UniversalPrinter {
template <typename T> template <typename T>
class UniversalPrinter<const T> : public UniversalPrinter<T> {}; class UniversalPrinter<const T> : public UniversalPrinter<T> {};
#if GTEST_INTERNAL_HAS_ANY // Printer for std::any
// Printer for std::any / absl::any
template <> template <>
class UniversalPrinter<Any> { class UniversalPrinter<std::any> {
public: public:
static void Print(const Any& value, ::std::ostream* os) { static void Print(const std::any& value, ::std::ostream* os) {
if (value.has_value()) { if (value.has_value()) {
*os << "value of type " << GetTypeName(value); *os << "value of type " << GetTypeName(value);
} else { } else {
@ -801,7 +930,7 @@ class UniversalPrinter<Any> {
} }
private: private:
static std::string GetTypeName(const Any& value) { static std::string GetTypeName(const std::any& value) {
#if GTEST_HAS_RTTI #if GTEST_HAS_RTTI
return internal::GetTypeName(value.type()); return internal::GetTypeName(value.type());
#else #else
@ -811,16 +940,11 @@ class UniversalPrinter<Any> {
} }
}; };
#endif // GTEST_INTERNAL_HAS_ANY // Printer for std::optional
#if GTEST_INTERNAL_HAS_OPTIONAL
// Printer for std::optional / absl::optional
template <typename T> template <typename T>
class UniversalPrinter<Optional<T>> { class UniversalPrinter<std::optional<T>> {
public: public:
static void Print(const Optional<T>& value, ::std::ostream* os) { static void Print(const std::optional<T>& value, ::std::ostream* os) {
*os << '('; *os << '(';
if (!value) { if (!value) {
*os << "nullopt"; *os << "nullopt";
@ -832,29 +956,18 @@ class UniversalPrinter<Optional<T>> {
}; };
template <> template <>
class UniversalPrinter<decltype(Nullopt())> { class UniversalPrinter<std::nullopt_t> {
public: public:
static void Print(decltype(Nullopt()), ::std::ostream* os) { static void Print(std::nullopt_t, ::std::ostream* os) { *os << "(nullopt)"; }
*os << "(nullopt)";
}
}; };
#endif // GTEST_INTERNAL_HAS_OPTIONAL // Printer for std::variant
#if GTEST_INTERNAL_HAS_VARIANT
// Printer for std::variant / absl::variant
template <typename... T> template <typename... T>
class UniversalPrinter<Variant<T...>> { class UniversalPrinter<std::variant<T...>> {
public: public:
static void Print(const Variant<T...>& value, ::std::ostream* os) { static void Print(const std::variant<T...>& value, ::std::ostream* os) {
*os << '('; *os << '(';
#if GTEST_HAS_ABSL
absl::visit(Visitor{os, value.index()}, value);
#else
std::visit(Visitor{os, value.index()}, value); std::visit(Visitor{os, value.index()}, value);
#endif // GTEST_HAS_ABSL
*os << ')'; *os << ')';
} }
@ -871,8 +984,6 @@ class UniversalPrinter<Variant<T...>> {
}; };
}; };
#endif // GTEST_INTERNAL_HAS_VARIANT
// UniversalPrintArray(begin, len, os) prints an array of 'len' // UniversalPrintArray(begin, len, os) prints an array of 'len'
// elements, starting at address 'begin'. // elements, starting at address 'begin'.
template <typename T> template <typename T>
@ -900,7 +1011,7 @@ void UniversalPrintArray(const T* begin, size_t len, ::std::ostream* os) {
GTEST_API_ void UniversalPrintArray(const char* begin, size_t len, GTEST_API_ void UniversalPrintArray(const char* begin, size_t len,
::std::ostream* os); ::std::ostream* os);
#ifdef __cpp_char8_t #ifdef __cpp_lib_char8_t
// This overload prints a (const) char8_t array compactly. // This overload prints a (const) char8_t array compactly.
GTEST_API_ void UniversalPrintArray(const char8_t* begin, size_t len, GTEST_API_ void UniversalPrintArray(const char8_t* begin, size_t len,
::std::ostream* os); ::std::ostream* os);
@ -996,7 +1107,7 @@ template <>
class UniversalTersePrinter<char*> : public UniversalTersePrinter<const char*> { class UniversalTersePrinter<char*> : public UniversalTersePrinter<const char*> {
}; };
#ifdef __cpp_char8_t #ifdef __cpp_lib_char8_t
template <> template <>
class UniversalTersePrinter<const char8_t*> { class UniversalTersePrinter<const char8_t*> {
public: public:

View File

@ -133,7 +133,7 @@ std::ostream& operator<<(std::ostream& os, const TestPartResult& result);
// virtual. // virtual.
class GTEST_API_ TestPartResultArray { class GTEST_API_ TestPartResultArray {
public: public:
TestPartResultArray() {} TestPartResultArray() = default;
// Appends the given TestPartResult to the array. // Appends the given TestPartResult to the array.
void Append(const TestPartResult& result); void Append(const TestPartResult& result);
@ -154,7 +154,7 @@ class GTEST_API_ TestPartResultArray {
// This interface knows how to report a test part result. // This interface knows how to report a test part result.
class GTEST_API_ TestPartResultReporterInterface { class GTEST_API_ TestPartResultReporterInterface {
public: public:
virtual ~TestPartResultReporterInterface() {} virtual ~TestPartResultReporterInterface() = default;
virtual void ReportTestPartResult(const TestPartResult& result) = 0; virtual void ReportTestPartResult(const TestPartResult& result) = 0;
}; };

View File

@ -205,8 +205,8 @@ INSTANTIATE_TYPED_TEST_SUITE_P(My, FooTest, MyTypes);
typedef gtest_TypeParam_ TypeParam; \ typedef gtest_TypeParam_ TypeParam; \
void TestBody() override; \ void TestBody() override; \
}; \ }; \
static bool gtest_##CaseName##_##TestName##_registered_ \ [[maybe_unused]] static bool gtest_##CaseName##_##TestName##_registered_ = \
GTEST_ATTRIBUTE_UNUSED_ = ::testing::internal::TypeParameterizedTest< \ ::testing::internal::TypeParameterizedTest< \
CaseName, \ CaseName, \
::testing::internal::TemplateSel<GTEST_TEST_CLASS_NAME_(CaseName, \ ::testing::internal::TemplateSel<GTEST_TEST_CLASS_NAME_(CaseName, \
TestName)>, \ TestName)>, \
@ -276,7 +276,7 @@ INSTANTIATE_TYPED_TEST_SUITE_P(My, FooTest, MyTypes);
typedef gtest_TypeParam_ TypeParam; \ typedef gtest_TypeParam_ TypeParam; \
void TestBody() override; \ void TestBody() override; \
}; \ }; \
static bool gtest_##TestName##_defined_ GTEST_ATTRIBUTE_UNUSED_ = \ [[maybe_unused]] static bool gtest_##TestName##_defined_ = \
GTEST_TYPED_TEST_SUITE_P_STATE_(SuiteName).AddTestName( \ GTEST_TYPED_TEST_SUITE_P_STATE_(SuiteName).AddTestName( \
__FILE__, __LINE__, GTEST_STRINGIFY_(SuiteName), \ __FILE__, __LINE__, GTEST_STRINGIFY_(SuiteName), \
GTEST_STRINGIFY_(TestName)); \ GTEST_STRINGIFY_(TestName)); \
@ -290,8 +290,8 @@ INSTANTIATE_TYPED_TEST_SUITE_P(My, FooTest, MyTypes);
namespace GTEST_SUITE_NAMESPACE_(SuiteName) { \ namespace GTEST_SUITE_NAMESPACE_(SuiteName) { \
typedef ::testing::internal::Templates<__VA_ARGS__> gtest_AllTests_; \ typedef ::testing::internal::Templates<__VA_ARGS__> gtest_AllTests_; \
} \ } \
static const char* const GTEST_REGISTERED_TEST_NAMES_( \ [[maybe_unused]] static const char* const GTEST_REGISTERED_TEST_NAMES_( \
SuiteName) GTEST_ATTRIBUTE_UNUSED_ = \ SuiteName) = \
GTEST_TYPED_TEST_SUITE_P_STATE_(SuiteName).VerifyRegisteredTestNames( \ GTEST_TYPED_TEST_SUITE_P_STATE_(SuiteName).VerifyRegisteredTestNames( \
GTEST_STRINGIFY_(SuiteName), __FILE__, __LINE__, #__VA_ARGS__) GTEST_STRINGIFY_(SuiteName), __FILE__, __LINE__, #__VA_ARGS__)
@ -306,7 +306,7 @@ INSTANTIATE_TYPED_TEST_SUITE_P(My, FooTest, MyTypes);
#define INSTANTIATE_TYPED_TEST_SUITE_P(Prefix, SuiteName, Types, ...) \ #define INSTANTIATE_TYPED_TEST_SUITE_P(Prefix, SuiteName, Types, ...) \
static_assert(sizeof(GTEST_STRINGIFY_(Prefix)) > 1, \ static_assert(sizeof(GTEST_STRINGIFY_(Prefix)) > 1, \
"test-suit-prefix must not be empty"); \ "test-suit-prefix must not be empty"); \
static bool gtest_##Prefix##_##SuiteName GTEST_ATTRIBUTE_UNUSED_ = \ [[maybe_unused]] static bool gtest_##Prefix##_##SuiteName = \
::testing::internal::TypeParameterizedTestSuite< \ ::testing::internal::TypeParameterizedTestSuite< \
SuiteName, GTEST_SUITE_NAMESPACE_(SuiteName)::gtest_AllTests_, \ SuiteName, GTEST_SUITE_NAMESPACE_(SuiteName)::gtest_AllTests_, \
::testing::internal::GenerateTypeList<Types>::type>:: \ ::testing::internal::GenerateTypeList<Types>::type>:: \

View File

@ -51,7 +51,6 @@
#include <cstddef> #include <cstddef>
#include <cstdint> #include <cstdint>
#include <iomanip>
#include <limits> #include <limits>
#include <memory> #include <memory>
#include <ostream> #include <ostream>
@ -61,16 +60,16 @@
#include <type_traits> #include <type_traits>
#include <vector> #include <vector>
#include "gtest/gtest-assertion-result.h" #include "gtest/gtest-assertion-result.h" // IWYU pragma: export
#include "gtest/gtest-death-test.h" #include "gtest/gtest-death-test.h" // IWYU pragma: export
#include "gtest/gtest-matchers.h" #include "gtest/gtest-matchers.h" // IWYU pragma: export
#include "gtest/gtest-message.h" #include "gtest/gtest-message.h" // IWYU pragma: export
#include "gtest/gtest-param-test.h" #include "gtest/gtest-param-test.h" // IWYU pragma: export
#include "gtest/gtest-printers.h" #include "gtest/gtest-printers.h" // IWYU pragma: export
#include "gtest/gtest-test-part.h" #include "gtest/gtest-test-part.h" // IWYU pragma: export
#include "gtest/gtest-typed-test.h" #include "gtest/gtest-typed-test.h" // IWYU pragma: export
#include "gtest/gtest_pred_impl.h" #include "gtest/gtest_pred_impl.h" // IWYU pragma: export
#include "gtest/gtest_prod.h" #include "gtest/gtest_prod.h" // IWYU pragma: export
#include "gtest/internal/gtest-internal.h" #include "gtest/internal/gtest-internal.h"
#include "gtest/internal/gtest-string.h" #include "gtest/internal/gtest-string.h"
@ -166,11 +165,7 @@ namespace testing {
// Silence C4100 (unreferenced formal parameter) and 4805 // Silence C4100 (unreferenced formal parameter) and 4805
// unsafe mix of type 'const int' and type 'const bool' // unsafe mix of type 'const int' and type 'const bool'
#ifdef _MSC_VER GTEST_DISABLE_MSC_WARNINGS_PUSH_(4805 4100)
#pragma warning(push)
#pragma warning(disable : 4805)
#pragma warning(disable : 4100)
#endif
// The upper limit for valid stack trace depths. // The upper limit for valid stack trace depths.
const int kMaxStackTraceDepth = 100; const int kMaxStackTraceDepth = 100;
@ -201,8 +196,8 @@ std::set<std::string>* GetIgnoredParameterizedTestSuites();
class GTestNonCopyable { class GTestNonCopyable {
public: public:
GTestNonCopyable() = default; GTestNonCopyable() = default;
GTestNonCopyable(const GTestNonCopyable &) = delete; GTestNonCopyable(const GTestNonCopyable&) = delete;
GTestNonCopyable &operator=(const GTestNonCopyable &) = delete; GTestNonCopyable& operator=(const GTestNonCopyable&) = delete;
~GTestNonCopyable() = default; ~GTestNonCopyable() = default;
}; };
@ -301,7 +296,13 @@ class GTEST_API_ Test {
// SetUp/TearDown method of Environment objects registered with Google // SetUp/TearDown method of Environment objects registered with Google
// Test) will be output as attributes of the <testsuites> element. // Test) will be output as attributes of the <testsuites> element.
static void RecordProperty(const std::string& key, const std::string& value); static void RecordProperty(const std::string& key, const std::string& value);
static void RecordProperty(const std::string& key, int64_t value); // We do not define a custom serialization except for values that can be
// converted to int64_t, but other values could be logged in this way.
template <typename T, std::enable_if_t<std::is_convertible<T, int64_t>::value,
bool> = true>
static void RecordProperty(const std::string& key, const T& value) {
RecordProperty(key, (Message() << value).GetString());
}
protected: protected:
// Creates a Test object. // Creates a Test object.
@ -549,14 +550,14 @@ class GTEST_API_ TestInfo {
// Returns the name of the parameter type, or NULL if this is not a typed // Returns the name of the parameter type, or NULL if this is not a typed
// or a type-parameterized test. // or a type-parameterized test.
const char* type_param() const { const char* type_param() const {
if (type_param_.get() != nullptr) return type_param_->c_str(); if (type_param_ != nullptr) return type_param_->c_str();
return nullptr; return nullptr;
} }
// Returns the text representation of the value parameter, or NULL if this // Returns the text representation of the value parameter, or NULL if this
// is not a value-parameterized test. // is not a value-parameterized test.
const char* value_param() const { const char* value_param() const {
if (value_param_.get() != nullptr) return value_param_->c_str(); if (value_param_ != nullptr) return value_param_->c_str();
return nullptr; return nullptr;
} }
@ -598,7 +599,7 @@ class GTEST_API_ TestInfo {
const TestResult* result() const { return &result_; } const TestResult* result() const { return &result_; }
private: private:
#if GTEST_HAS_DEATH_TEST #ifdef GTEST_HAS_DEATH_TEST
friend class internal::DefaultDeathTestFactory; friend class internal::DefaultDeathTestFactory;
#endif // GTEST_HAS_DEATH_TEST #endif // GTEST_HAS_DEATH_TEST
friend class Test; friend class Test;
@ -606,7 +607,7 @@ class GTEST_API_ TestInfo {
friend class internal::UnitTestImpl; friend class internal::UnitTestImpl;
friend class internal::StreamingListenerTest; friend class internal::StreamingListenerTest;
friend TestInfo* internal::MakeAndRegisterTestInfo( friend TestInfo* internal::MakeAndRegisterTestInfo(
const char* test_suite_name, const char* name, const char* type_param, std::string test_suite_name, const char* name, const char* type_param,
const char* value_param, internal::CodeLocation code_location, const char* value_param, internal::CodeLocation code_location,
internal::TypeId fixture_class_id, internal::SetUpTestSuiteFunc set_up_tc, internal::TypeId fixture_class_id, internal::SetUpTestSuiteFunc set_up_tc,
internal::TearDownTestSuiteFunc tear_down_tc, internal::TearDownTestSuiteFunc tear_down_tc,
@ -614,7 +615,7 @@ class GTEST_API_ TestInfo {
// Constructs a TestInfo object. The newly constructed instance assumes // Constructs a TestInfo object. The newly constructed instance assumes
// ownership of the factory object. // ownership of the factory object.
TestInfo(const std::string& test_suite_name, const std::string& name, TestInfo(std::string test_suite_name, std::string name,
const char* a_type_param, // NULL if not a type-parameterized test const char* a_type_param, // NULL if not a type-parameterized test
const char* a_value_param, // NULL if not a value-parameterized test const char* a_value_param, // NULL if not a value-parameterized test
internal::CodeLocation a_code_location, internal::CodeLocation a_code_location,
@ -682,7 +683,7 @@ class GTEST_API_ TestSuite {
// this is not a type-parameterized test. // this is not a type-parameterized test.
// set_up_tc: pointer to the function that sets up the test suite // set_up_tc: pointer to the function that sets up the test suite
// tear_down_tc: pointer to the function that tears down the test suite // tear_down_tc: pointer to the function that tears down the test suite
TestSuite(const char* name, const char* a_type_param, TestSuite(const std::string& name, const char* a_type_param,
internal::SetUpTestSuiteFunc set_up_tc, internal::SetUpTestSuiteFunc set_up_tc,
internal::TearDownTestSuiteFunc tear_down_tc); internal::TearDownTestSuiteFunc tear_down_tc);
@ -695,7 +696,7 @@ class GTEST_API_ TestSuite {
// Returns the name of the parameter type, or NULL if this is not a // Returns the name of the parameter type, or NULL if this is not a
// type-parameterized test suite. // type-parameterized test suite.
const char* type_param() const { const char* type_param() const {
if (type_param_.get() != nullptr) return type_param_->c_str(); if (type_param_ != nullptr) return type_param_->c_str();
return nullptr; return nullptr;
} }
@ -892,7 +893,7 @@ class GTEST_API_ TestSuite {
class Environment { class Environment {
public: public:
// The d'tor is virtual as we need to subclass Environment. // The d'tor is virtual as we need to subclass Environment.
virtual ~Environment() {} virtual ~Environment() = default;
// Override this to define how to set up the environment. // Override this to define how to set up the environment.
virtual void SetUp() {} virtual void SetUp() {}
@ -923,7 +924,7 @@ class GTEST_API_ AssertionException
// the order the corresponding events are fired. // the order the corresponding events are fired.
class TestEventListener { class TestEventListener {
public: public:
virtual ~TestEventListener() {} virtual ~TestEventListener() = default;
// Fired before any test activity starts. // Fired before any test activity starts.
virtual void OnTestProgramStart(const UnitTest& unit_test) = 0; virtual void OnTestProgramStart(const UnitTest& unit_test) = 0;
@ -1053,6 +1054,10 @@ class GTEST_API_ TestEventListeners {
return default_xml_generator_; return default_xml_generator_;
} }
// Controls whether events will be forwarded by the repeater to the
// listeners in the list.
void SuppressEventForwarding(bool);
private: private:
friend class TestSuite; friend class TestSuite;
friend class TestInfo; friend class TestInfo;
@ -1082,7 +1087,6 @@ class GTEST_API_ TestEventListeners {
// Controls whether events will be forwarded by the repeater to the // Controls whether events will be forwarded by the repeater to the
// listeners in the list. // listeners in the list.
bool EventForwardingEnabled() const; bool EventForwardingEnabled() const;
void SuppressEventForwarding();
// The actual list of listeners. // The actual list of listeners.
internal::TestEventRepeater* repeater_; internal::TestEventRepeater* repeater_;
@ -1119,7 +1123,7 @@ class GTEST_API_ UnitTest {
// This method can only be called from the main thread. // This method can only be called from the main thread.
// //
// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
int Run() GTEST_MUST_USE_RESULT_; [[nodiscard]] int Run();
// Returns the working directory when the first TEST() or TEST_F() // Returns the working directory when the first TEST() or TEST_F()
// was executed. The UnitTest object owns the string. // was executed. The UnitTest object owns the string.
@ -1258,6 +1262,20 @@ class GTEST_API_ UnitTest {
// total_test_suite_count() - 1. If i is not in that range, returns NULL. // total_test_suite_count() - 1. If i is not in that range, returns NULL.
TestSuite* GetMutableTestSuite(int i); TestSuite* GetMutableTestSuite(int i);
// Invokes OsStackTrackGetterInterface::UponLeavingGTest. UponLeavingGTest()
// should be called immediately before Google Test calls user code. It saves
// some information about the current stack that CurrentStackTrace() will use
// to find and hide Google Test stack frames.
void UponLeavingGTest();
// Sets the TestSuite object for the test that's currently running.
void set_current_test_suite(TestSuite* a_current_test_suite)
GTEST_LOCK_EXCLUDED_(mutex_);
// Sets the TestInfo object for the test that's currently running.
void set_current_test_info(TestInfo* a_current_test_info)
GTEST_LOCK_EXCLUDED_(mutex_);
// Accessors for the implementation object. // Accessors for the implementation object.
internal::UnitTestImpl* impl() { return impl_; } internal::UnitTestImpl* impl() { return impl_; }
const internal::UnitTestImpl* impl() const { return impl_; } const internal::UnitTestImpl* impl() const { return impl_; }
@ -1266,6 +1284,8 @@ class GTEST_API_ UnitTest {
// members of UnitTest. // members of UnitTest.
friend class ScopedTrace; friend class ScopedTrace;
friend class Test; friend class Test;
friend class TestInfo;
friend class TestSuite;
friend class internal::AssertHelper; friend class internal::AssertHelper;
friend class internal::StreamingListenerTest; friend class internal::StreamingListenerTest;
friend class internal::UnitTestRecordPropertyTestHelper; friend class internal::UnitTestRecordPropertyTestHelper;
@ -1569,12 +1589,12 @@ AssertionResult CmpHelperFloatingPointEQ(const char* lhs_expression,
} }
::std::stringstream lhs_ss; ::std::stringstream lhs_ss;
lhs_ss << std::setprecision(std::numeric_limits<RawType>::digits10 + 2) lhs_ss.precision(std::numeric_limits<RawType>::digits10 + 2);
<< lhs_value; lhs_ss << lhs_value;
::std::stringstream rhs_ss; ::std::stringstream rhs_ss;
rhs_ss << std::setprecision(std::numeric_limits<RawType>::digits10 + 2) rhs_ss.precision(std::numeric_limits<RawType>::digits10 + 2);
<< rhs_value; rhs_ss << rhs_value;
return EqFailure(lhs_expression, rhs_expression, return EqFailure(lhs_expression, rhs_expression,
StringStreamToString(&lhs_ss), StringStreamToString(&rhs_ss), StringStreamToString(&lhs_ss), StringStreamToString(&rhs_ss),
@ -1590,6 +1610,8 @@ GTEST_API_ AssertionResult DoubleNearPredFormat(const char* expr1,
double val1, double val2, double val1, double val2,
double abs_error); double abs_error);
using GoogleTest_NotSupported_OnFunctionReturningNonVoid = void;
// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE. // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
// A class that enables one to stream messages to assertion macros // A class that enables one to stream messages to assertion macros
class GTEST_API_ AssertHelper { class GTEST_API_ AssertHelper {
@ -1601,7 +1623,8 @@ class GTEST_API_ AssertHelper {
// Message assignment is a semantic trick to enable assertion // Message assignment is a semantic trick to enable assertion
// streaming; see the GTEST_MESSAGE_ macro below. // streaming; see the GTEST_MESSAGE_ macro below.
void operator=(const Message& message) const; GoogleTest_NotSupported_OnFunctionReturningNonVoid operator=(
const Message& message) const;
private: private:
// We put our data in a struct so that the size of the AssertHelper class can // We put our data in a struct so that the size of the AssertHelper class can
@ -1669,11 +1692,11 @@ template <typename T>
class WithParamInterface { class WithParamInterface {
public: public:
typedef T ParamType; typedef T ParamType;
virtual ~WithParamInterface() {} virtual ~WithParamInterface() = default;
// The current parameter value. Is also available in the test fixture's // The current parameter value. Is also available in the test fixture's
// constructor. // constructor.
static const ParamType& GetParam() { [[nodiscard]] static const ParamType& GetParam() {
GTEST_CHECK_(parameter_ != nullptr) GTEST_CHECK_(parameter_ != nullptr)
<< "GetParam() can only be called inside a value-parameterized test " << "GetParam() can only be called inside a value-parameterized test "
<< "-- did you intend to write TEST_P instead of TEST_F?"; << "-- did you intend to write TEST_P instead of TEST_F?";
@ -1745,8 +1768,9 @@ class TestWithParam : public Test, public WithParamInterface<T> {};
// Define this macro to 1 to omit the definition of FAIL(), which is a // Define this macro to 1 to omit the definition of FAIL(), which is a
// generic name and clashes with some other libraries. // generic name and clashes with some other libraries.
#if !GTEST_DONT_DEFINE_FAIL #if !(defined(GTEST_DONT_DEFINE_FAIL) && GTEST_DONT_DEFINE_FAIL)
#define FAIL() GTEST_FAIL() #define FAIL() GTEST_FAIL()
#define FAIL_AT(file, line) GTEST_FAIL_AT(file, line)
#endif #endif
// Generates a success with a generic message. // Generates a success with a generic message.
@ -1754,7 +1778,7 @@ class TestWithParam : public Test, public WithParamInterface<T> {};
// Define this macro to 1 to omit the definition of SUCCEED(), which // Define this macro to 1 to omit the definition of SUCCEED(), which
// is a generic name and clashes with some other libraries. // is a generic name and clashes with some other libraries.
#if !GTEST_DONT_DEFINE_SUCCEED #if !(defined(GTEST_DONT_DEFINE_SUCCEED) && GTEST_DONT_DEFINE_SUCCEED)
#define SUCCEED() GTEST_SUCCEED() #define SUCCEED() GTEST_SUCCEED()
#endif #endif
@ -1798,19 +1822,19 @@ class TestWithParam : public Test, public WithParamInterface<T> {};
// Define these macros to 1 to omit the definition of the corresponding // Define these macros to 1 to omit the definition of the corresponding
// EXPECT or ASSERT, which clashes with some users' own code. // EXPECT or ASSERT, which clashes with some users' own code.
#if !GTEST_DONT_DEFINE_EXPECT_TRUE #if !(defined(GTEST_DONT_DEFINE_EXPECT_TRUE) && GTEST_DONT_DEFINE_EXPECT_TRUE)
#define EXPECT_TRUE(condition) GTEST_EXPECT_TRUE(condition) #define EXPECT_TRUE(condition) GTEST_EXPECT_TRUE(condition)
#endif #endif
#if !GTEST_DONT_DEFINE_EXPECT_FALSE #if !(defined(GTEST_DONT_DEFINE_EXPECT_FALSE) && GTEST_DONT_DEFINE_EXPECT_FALSE)
#define EXPECT_FALSE(condition) GTEST_EXPECT_FALSE(condition) #define EXPECT_FALSE(condition) GTEST_EXPECT_FALSE(condition)
#endif #endif
#if !GTEST_DONT_DEFINE_ASSERT_TRUE #if !(defined(GTEST_DONT_DEFINE_ASSERT_TRUE) && GTEST_DONT_DEFINE_ASSERT_TRUE)
#define ASSERT_TRUE(condition) GTEST_ASSERT_TRUE(condition) #define ASSERT_TRUE(condition) GTEST_ASSERT_TRUE(condition)
#endif #endif
#if !GTEST_DONT_DEFINE_ASSERT_FALSE #if !(defined(GTEST_DONT_DEFINE_ASSERT_FALSE) && GTEST_DONT_DEFINE_ASSERT_FALSE)
#define ASSERT_FALSE(condition) GTEST_ASSERT_FALSE(condition) #define ASSERT_FALSE(condition) GTEST_ASSERT_FALSE(condition)
#endif #endif
@ -1889,27 +1913,27 @@ class TestWithParam : public Test, public WithParamInterface<T> {};
// Define macro GTEST_DONT_DEFINE_ASSERT_XY to 1 to omit the definition of // Define macro GTEST_DONT_DEFINE_ASSERT_XY to 1 to omit the definition of
// ASSERT_XY(), which clashes with some users' own code. // ASSERT_XY(), which clashes with some users' own code.
#if !GTEST_DONT_DEFINE_ASSERT_EQ #if !(defined(GTEST_DONT_DEFINE_ASSERT_EQ) && GTEST_DONT_DEFINE_ASSERT_EQ)
#define ASSERT_EQ(val1, val2) GTEST_ASSERT_EQ(val1, val2) #define ASSERT_EQ(val1, val2) GTEST_ASSERT_EQ(val1, val2)
#endif #endif
#if !GTEST_DONT_DEFINE_ASSERT_NE #if !(defined(GTEST_DONT_DEFINE_ASSERT_NE) && GTEST_DONT_DEFINE_ASSERT_NE)
#define ASSERT_NE(val1, val2) GTEST_ASSERT_NE(val1, val2) #define ASSERT_NE(val1, val2) GTEST_ASSERT_NE(val1, val2)
#endif #endif
#if !GTEST_DONT_DEFINE_ASSERT_LE #if !(defined(GTEST_DONT_DEFINE_ASSERT_LE) && GTEST_DONT_DEFINE_ASSERT_LE)
#define ASSERT_LE(val1, val2) GTEST_ASSERT_LE(val1, val2) #define ASSERT_LE(val1, val2) GTEST_ASSERT_LE(val1, val2)
#endif #endif
#if !GTEST_DONT_DEFINE_ASSERT_LT #if !(defined(GTEST_DONT_DEFINE_ASSERT_LT) && GTEST_DONT_DEFINE_ASSERT_LT)
#define ASSERT_LT(val1, val2) GTEST_ASSERT_LT(val1, val2) #define ASSERT_LT(val1, val2) GTEST_ASSERT_LT(val1, val2)
#endif #endif
#if !GTEST_DONT_DEFINE_ASSERT_GE #if !(defined(GTEST_DONT_DEFINE_ASSERT_GE) && GTEST_DONT_DEFINE_ASSERT_GE)
#define ASSERT_GE(val1, val2) GTEST_ASSERT_GE(val1, val2) #define ASSERT_GE(val1, val2) GTEST_ASSERT_GE(val1, val2)
#endif #endif
#if !GTEST_DONT_DEFINE_ASSERT_GT #if !(defined(GTEST_DONT_DEFINE_ASSERT_GT) && GTEST_DONT_DEFINE_ASSERT_GT)
#define ASSERT_GT(val1, val2) GTEST_ASSERT_GT(val1, val2) #define ASSERT_GT(val1, val2) GTEST_ASSERT_GT(val1, val2)
#endif #endif
@ -1997,7 +2021,7 @@ GTEST_API_ AssertionResult FloatLE(const char* expr1, const char* expr2,
GTEST_API_ AssertionResult DoubleLE(const char* expr1, const char* expr2, GTEST_API_ AssertionResult DoubleLE(const char* expr1, const char* expr2,
double val1, double val2); double val1, double val2);
#if GTEST_OS_WINDOWS #ifdef GTEST_OS_WINDOWS
// Macros that test for HRESULT failure and success, these are only useful // Macros that test for HRESULT failure and success, these are only useful
// on Windows, and rely on Windows SDK macros and APIs to compile. // on Windows, and rely on Windows SDK macros and APIs to compile.
@ -2097,7 +2121,7 @@ class GTEST_API_ ScopedTrace {
// Therefore, a SCOPED_TRACE() would (correctly) only affect the // Therefore, a SCOPED_TRACE() would (correctly) only affect the
// assertions in its own thread. // assertions in its own thread.
#define SCOPED_TRACE(message) \ #define SCOPED_TRACE(message) \
::testing::ScopedTrace GTEST_CONCAT_TOKEN_(gtest_trace_, __LINE__)( \ const ::testing::ScopedTrace GTEST_CONCAT_TOKEN_(gtest_trace_, __LINE__)( \
__FILE__, __LINE__, (message)) __FILE__, __LINE__, (message))
// Compile-time assertion for type equality. // Compile-time assertion for type equality.
@ -2167,7 +2191,7 @@ constexpr bool StaticAssertTypeEq() noexcept {
// Define this macro to 1 to omit the definition of TEST(), which // Define this macro to 1 to omit the definition of TEST(), which
// is a generic name and clashes with some other libraries. // is a generic name and clashes with some other libraries.
#if !GTEST_DONT_DEFINE_TEST #if !(defined(GTEST_DONT_DEFINE_TEST) && GTEST_DONT_DEFINE_TEST)
#define TEST(test_suite_name, test_name) GTEST_TEST(test_suite_name, test_name) #define TEST(test_suite_name, test_name) GTEST_TEST(test_suite_name, test_name)
#endif #endif
@ -2199,7 +2223,7 @@ constexpr bool StaticAssertTypeEq() noexcept {
#define GTEST_TEST_F(test_fixture, test_name) \ #define GTEST_TEST_F(test_fixture, test_name) \
GTEST_TEST_(test_fixture, test_name, test_fixture, \ GTEST_TEST_(test_fixture, test_name, test_fixture, \
::testing::internal::GetTypeId<test_fixture>()) ::testing::internal::GetTypeId<test_fixture>())
#if !GTEST_DONT_DEFINE_TEST_F #if !(defined(GTEST_DONT_DEFINE_TEST_F) && GTEST_DONT_DEFINE_TEST_F)
#define TEST_F(test_fixture, test_name) GTEST_TEST_F(test_fixture, test_name) #define TEST_F(test_fixture, test_name) GTEST_TEST_F(test_fixture, test_name)
#endif #endif
@ -2214,9 +2238,7 @@ GTEST_API_ std::string TempDir();
// in it should be considered read-only. // in it should be considered read-only.
GTEST_API_ std::string SrcDir(); GTEST_API_ std::string SrcDir();
#ifdef _MSC_VER GTEST_DISABLE_MSC_WARNINGS_POP_() // 4805 4100
#pragma warning(pop)
#endif
// Dynamically registers a test with the framework. // Dynamically registers a test with the framework.
// //
@ -2305,11 +2327,12 @@ TestInfo* RegisterTest(const char* test_suite_name, const char* test_name,
// tests are successful, or 1 otherwise. // tests are successful, or 1 otherwise.
// //
// RUN_ALL_TESTS() should be invoked after the command line has been // RUN_ALL_TESTS() should be invoked after the command line has been
// parsed by InitGoogleTest(). // parsed by InitGoogleTest(). RUN_ALL_TESTS will tear down and delete any
// installed environments and should only be called once per binary.
// //
// This function was formerly a macro; thus, it is in the global // This function was formerly a macro; thus, it is in the global
// namespace and has an all-caps name. // namespace and has an all-caps name.
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_; [[nodiscard]] int RUN_ALL_TESTS();
inline int RUN_ALL_TESTS() { return ::testing::UnitTest::GetInstance()->Run(); } inline int RUN_ALL_TESTS() { return ::testing::UnitTest::GetInstance()->Run(); }

View File

@ -46,18 +46,39 @@
#include "gtest/gtest-matchers.h" #include "gtest/gtest-matchers.h"
#include "gtest/internal/gtest-internal.h" #include "gtest/internal/gtest-internal.h"
#include "gtest/internal/gtest-port.h"
GTEST_DECLARE_string_(internal_run_death_test); GTEST_DECLARE_string_(internal_run_death_test);
namespace testing { namespace testing {
namespace internal { namespace internal {
// Names of the flags (needed for parsing Google Test flags). // Name of the flag (needed for parsing Google Test flag).
const char kDeathTestStyleFlag[] = "death_test_style";
const char kDeathTestUseFork[] = "death_test_use_fork";
const char kInternalRunDeathTestFlag[] = "internal_run_death_test"; const char kInternalRunDeathTestFlag[] = "internal_run_death_test";
#if GTEST_HAS_DEATH_TEST // A string passed to EXPECT_DEATH (etc.) is caught by one of these overloads
// and interpreted as a regex (rather than an Eq matcher) for legacy
// compatibility.
inline Matcher<const ::std::string&> MakeDeathTestMatcher(
::testing::internal::RE regex) {
return ContainsRegex(regex.pattern());
}
inline Matcher<const ::std::string&> MakeDeathTestMatcher(const char* regex) {
return ContainsRegex(regex);
}
inline Matcher<const ::std::string&> MakeDeathTestMatcher(
const ::std::string& regex) {
return ContainsRegex(regex);
}
// If a Matcher<const ::std::string&> is passed to EXPECT_DEATH (etc.), it's
// used directly.
inline Matcher<const ::std::string&> MakeDeathTestMatcher(
Matcher<const ::std::string&> matcher) {
return matcher;
}
#ifdef GTEST_HAS_DEATH_TEST
GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \ GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \
/* class A needs to have dll-interface to be used by clients of class B */) /* class A needs to have dll-interface to be used by clients of class B */)
@ -73,7 +94,7 @@ GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \
// //
// exit status: The integer exit information in the format specified // exit status: The integer exit information in the format specified
// by wait(2) // by wait(2)
// exit code: The integer code passed to exit(3), _exit(2), or // exit code: The integer code passed to exit(3), _Exit(2), or
// returned from main() // returned from main()
class GTEST_API_ DeathTest { class GTEST_API_ DeathTest {
public: public:
@ -88,7 +109,7 @@ class GTEST_API_ DeathTest {
static bool Create(const char* statement, Matcher<const std::string&> matcher, static bool Create(const char* statement, Matcher<const std::string&> matcher,
const char* file, int line, DeathTest** test); const char* file, int line, DeathTest** test);
DeathTest(); DeathTest();
virtual ~DeathTest() {} virtual ~DeathTest() = default;
// A helper class that aborts a death test when it's deleted. // A helper class that aborts a death test when it's deleted.
class ReturnSentinel { class ReturnSentinel {
@ -153,7 +174,7 @@ GTEST_DISABLE_MSC_WARNINGS_POP_() // 4251
// Factory interface for death tests. May be mocked out for testing. // Factory interface for death tests. May be mocked out for testing.
class DeathTestFactory { class DeathTestFactory {
public: public:
virtual ~DeathTestFactory() {} virtual ~DeathTestFactory() = default;
virtual bool Create(const char* statement, virtual bool Create(const char* statement,
Matcher<const std::string&> matcher, const char* file, Matcher<const std::string&> matcher, const char* file,
int line, DeathTest** test) = 0; int line, DeathTest** test) = 0;
@ -170,28 +191,6 @@ class DefaultDeathTestFactory : public DeathTestFactory {
// by a signal, or exited normally with a nonzero exit code. // by a signal, or exited normally with a nonzero exit code.
GTEST_API_ bool ExitedUnsuccessfully(int exit_status); GTEST_API_ bool ExitedUnsuccessfully(int exit_status);
// A string passed to EXPECT_DEATH (etc.) is caught by one of these overloads
// and interpreted as a regex (rather than an Eq matcher) for legacy
// compatibility.
inline Matcher<const ::std::string&> MakeDeathTestMatcher(
::testing::internal::RE regex) {
return ContainsRegex(regex.pattern());
}
inline Matcher<const ::std::string&> MakeDeathTestMatcher(const char* regex) {
return ContainsRegex(regex);
}
inline Matcher<const ::std::string&> MakeDeathTestMatcher(
const ::std::string& regex) {
return ContainsRegex(regex);
}
// If a Matcher<const ::std::string&> is passed to EXPECT_DEATH (etc.), it's
// used directly.
inline Matcher<const ::std::string&> MakeDeathTestMatcher(
Matcher<const ::std::string&> matcher) {
return matcher;
}
// Traps C++ exceptions escaping statement and reports them as test // Traps C++ exceptions escaping statement and reports them as test
// failures. Note that trapping SEH exceptions is not implemented here. // failures. Note that trapping SEH exceptions is not implemented here.
#if GTEST_HAS_EXCEPTIONS #if GTEST_HAS_EXCEPTIONS
@ -238,7 +237,7 @@ inline Matcher<const ::std::string&> MakeDeathTestMatcher(
} \ } \
break; \ break; \
case ::testing::internal::DeathTest::EXECUTE_TEST: { \ case ::testing::internal::DeathTest::EXECUTE_TEST: { \
::testing::internal::DeathTest::ReturnSentinel gtest_sentinel( \ const ::testing::internal::DeathTest::ReturnSentinel gtest_sentinel( \
gtest_dt); \ gtest_dt); \
GTEST_EXECUTE_DEATH_TEST_STATEMENT_(statement, gtest_dt); \ GTEST_EXECUTE_DEATH_TEST_STATEMENT_(statement, gtest_dt); \
gtest_dt->Abort(::testing::internal::DeathTest::TEST_DID_NOT_DIE); \ gtest_dt->Abort(::testing::internal::DeathTest::TEST_DID_NOT_DIE); \

View File

@ -43,6 +43,7 @@
#define GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_FILEPATH_H_ #define GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_FILEPATH_H_
#include <string> #include <string>
#include <utility>
#include "gtest/internal/gtest-port.h" #include "gtest/internal/gtest-port.h"
#include "gtest/internal/gtest-string.h" #include "gtest/internal/gtest-string.h"
@ -70,8 +71,9 @@ class GTEST_API_ FilePath {
public: public:
FilePath() : pathname_("") {} FilePath() : pathname_("") {}
FilePath(const FilePath& rhs) : pathname_(rhs.pathname_) {} FilePath(const FilePath& rhs) : pathname_(rhs.pathname_) {}
FilePath(FilePath&& rhs) noexcept : pathname_(std::move(rhs.pathname_)) {}
explicit FilePath(const std::string& pathname) : pathname_(pathname) { explicit FilePath(std::string pathname) : pathname_(std::move(pathname)) {
Normalize(); Normalize();
} }
@ -79,6 +81,10 @@ class GTEST_API_ FilePath {
Set(rhs); Set(rhs);
return *this; return *this;
} }
FilePath& operator=(FilePath&& rhs) noexcept {
pathname_ = std::move(rhs.pathname_);
return *this;
}
void Set(const FilePath& rhs) { pathname_ = rhs.pathname_; } void Set(const FilePath& rhs) { pathname_ = rhs.pathname_; }

View File

@ -41,7 +41,7 @@
#include "gtest/internal/gtest-port.h" #include "gtest/internal/gtest-port.h"
#if GTEST_OS_LINUX #ifdef GTEST_OS_LINUX
#include <stdlib.h> #include <stdlib.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/wait.h> #include <sys/wait.h>
@ -58,7 +58,6 @@
#include <cstdint> #include <cstdint>
#include <functional> #include <functional>
#include <iomanip>
#include <limits> #include <limits>
#include <map> #include <map>
#include <set> #include <set>
@ -79,7 +78,7 @@
// //
// will result in the token foo__LINE__, instead of foo followed by // will result in the token foo__LINE__, instead of foo followed by
// the current line number. For more details, see // the current line number. For more details, see
// http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.6 // https://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.6
#define GTEST_CONCAT_TOKEN_(foo, bar) GTEST_CONCAT_TOKEN_IMPL_(foo, bar) #define GTEST_CONCAT_TOKEN_(foo, bar) GTEST_CONCAT_TOKEN_IMPL_(foo, bar)
#define GTEST_CONCAT_TOKEN_IMPL_(foo, bar) foo##bar #define GTEST_CONCAT_TOKEN_IMPL_(foo, bar) foo##bar
@ -170,7 +169,7 @@ namespace edit_distance {
// All edits cost the same, with replace having lower priority than // All edits cost the same, with replace having lower priority than
// add/remove. // add/remove.
// Simple implementation of the Wagner-Fischer algorithm. // Simple implementation of the Wagner-Fischer algorithm.
// See http://en.wikipedia.org/wiki/Wagner-Fischer_algorithm // See https://en.wikipedia.org/wiki/Wagner-Fischer_algorithm
enum EditType { kMatch, kAdd, kRemove, kReplace }; enum EditType { kMatch, kAdd, kRemove, kReplace };
GTEST_API_ std::vector<EditType> CalculateOptimalEdits( GTEST_API_ std::vector<EditType> CalculateOptimalEdits(
const std::vector<size_t>& left, const std::vector<size_t>& right); const std::vector<size_t>& left, const std::vector<size_t>& right);
@ -237,7 +236,7 @@ GTEST_API_ std::string GetBoolAssertionFailureMessage(
// For double, there are 11 exponent bits and 52 fraction bits. // For double, there are 11 exponent bits and 52 fraction bits.
// //
// More details can be found at // More details can be found at
// http://en.wikipedia.org/wiki/IEEE_floating-point_standard. // https://en.wikipedia.org/wiki/IEEE_floating-point_standard.
// //
// Template parameter: // Template parameter:
// //
@ -282,7 +281,7 @@ class FloatingPoint {
// bits. Therefore, 4 should be enough for ordinary use. // bits. Therefore, 4 should be enough for ordinary use.
// //
// See the following article for more details on ULP: // See the following article for more details on ULP:
// http://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/ // https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/
static const uint32_t kMaxUlps = 4; static const uint32_t kMaxUlps = 4;
// Constructs a FloatingPoint from a raw floating-point number. // Constructs a FloatingPoint from a raw floating-point number.
@ -291,38 +290,35 @@ class FloatingPoint {
// around may change its bits, although the new value is guaranteed // around may change its bits, although the new value is guaranteed
// to be also a NAN. Therefore, don't expect this constructor to // to be also a NAN. Therefore, don't expect this constructor to
// preserve the bits in x when x is a NAN. // preserve the bits in x when x is a NAN.
explicit FloatingPoint(const RawType& x) { u_.value_ = x; } explicit FloatingPoint(RawType x) { memcpy(&bits_, &x, sizeof(x)); }
// Static methods // Static methods
// Reinterprets a bit pattern as a floating-point number. // Reinterprets a bit pattern as a floating-point number.
// //
// This function is needed to test the AlmostEquals() method. // This function is needed to test the AlmostEquals() method.
static RawType ReinterpretBits(const Bits bits) { static RawType ReinterpretBits(Bits bits) {
FloatingPoint fp(0); RawType fp;
fp.u_.bits_ = bits; memcpy(&fp, &bits, sizeof(fp));
return fp.u_.value_; return fp;
} }
// Returns the floating-point number that represent positive infinity. // Returns the floating-point number that represent positive infinity.
static RawType Infinity() { return ReinterpretBits(kExponentBitMask); } static RawType Infinity() { return ReinterpretBits(kExponentBitMask); }
// Returns the maximum representable finite floating-point number.
static RawType Max();
// Non-static methods // Non-static methods
// Returns the bits that represents this number. // Returns the bits that represents this number.
const Bits& bits() const { return u_.bits_; } const Bits& bits() const { return bits_; }
// Returns the exponent bits of this number. // Returns the exponent bits of this number.
Bits exponent_bits() const { return kExponentBitMask & u_.bits_; } Bits exponent_bits() const { return kExponentBitMask & bits_; }
// Returns the fraction bits of this number. // Returns the fraction bits of this number.
Bits fraction_bits() const { return kFractionBitMask & u_.bits_; } Bits fraction_bits() const { return kFractionBitMask & bits_; }
// Returns the sign bit of this number. // Returns the sign bit of this number.
Bits sign_bit() const { return kSignBitMask & u_.bits_; } Bits sign_bit() const { return kSignBitMask & bits_; }
// Returns true if and only if this is NAN (not a number). // Returns true if and only if this is NAN (not a number).
bool is_nan() const { bool is_nan() const {
@ -336,23 +332,16 @@ class FloatingPoint {
// //
// - returns false if either number is (or both are) NAN. // - returns false if either number is (or both are) NAN.
// - treats really large numbers as almost equal to infinity. // - treats really large numbers as almost equal to infinity.
// - thinks +0.0 and -0.0 are 0 DLP's apart. // - thinks +0.0 and -0.0 are 0 ULP's apart.
bool AlmostEquals(const FloatingPoint& rhs) const { bool AlmostEquals(const FloatingPoint& rhs) const {
// The IEEE standard says that any comparison operation involving // The IEEE standard says that any comparison operation involving
// a NAN must return false. // a NAN must return false.
if (is_nan() || rhs.is_nan()) return false; if (is_nan() || rhs.is_nan()) return false;
return DistanceBetweenSignAndMagnitudeNumbers(u_.bits_, rhs.u_.bits_) <= return DistanceBetweenSignAndMagnitudeNumbers(bits_, rhs.bits_) <= kMaxUlps;
kMaxUlps;
} }
private: private:
// The data type used to store the actual floating-point number.
union FloatingPointUnion {
RawType value_; // The raw floating-point number.
Bits bits_; // The bits that represent the number.
};
// Converts an integer from the sign-and-magnitude representation to // Converts an integer from the sign-and-magnitude representation to
// the biased representation. More precisely, let N be 2 to the // the biased representation. More precisely, let N be 2 to the
// power of (kBitCount - 1), an integer x is represented by the // power of (kBitCount - 1), an integer x is represented by the
@ -366,9 +355,9 @@ class FloatingPoint {
// N - 1 (the biggest number representable using // N - 1 (the biggest number representable using
// sign-and-magnitude) is represented by 2N - 1. // sign-and-magnitude) is represented by 2N - 1.
// //
// Read http://en.wikipedia.org/wiki/Signed_number_representations // Read https://en.wikipedia.org/wiki/Signed_number_representations
// for more details on signed number representations. // for more details on signed number representations.
static Bits SignAndMagnitudeToBiased(const Bits& sam) { static Bits SignAndMagnitudeToBiased(Bits sam) {
if (kSignBitMask & sam) { if (kSignBitMask & sam) {
// sam represents a negative number. // sam represents a negative number.
return ~sam + 1; return ~sam + 1;
@ -380,27 +369,15 @@ class FloatingPoint {
// Given two numbers in the sign-and-magnitude representation, // Given two numbers in the sign-and-magnitude representation,
// returns the distance between them as an unsigned number. // returns the distance between them as an unsigned number.
static Bits DistanceBetweenSignAndMagnitudeNumbers(const Bits& sam1, static Bits DistanceBetweenSignAndMagnitudeNumbers(Bits sam1, Bits sam2) {
const Bits& sam2) {
const Bits biased1 = SignAndMagnitudeToBiased(sam1); const Bits biased1 = SignAndMagnitudeToBiased(sam1);
const Bits biased2 = SignAndMagnitudeToBiased(sam2); const Bits biased2 = SignAndMagnitudeToBiased(sam2);
return (biased1 >= biased2) ? (biased1 - biased2) : (biased2 - biased1); return (biased1 >= biased2) ? (biased1 - biased2) : (biased2 - biased1);
} }
FloatingPointUnion u_; Bits bits_; // The bits that represent the number.
}; };
// We cannot use std::numeric_limits<T>::max() as it clashes with the max()
// macro defined by <windows.h>.
template <>
inline float FloatingPoint<float>::Max() {
return FLT_MAX;
}
template <>
inline double FloatingPoint<double>::Max() {
return DBL_MAX;
}
// Typedefs the instances of the FloatingPoint template class that we // Typedefs the instances of the FloatingPoint template class that we
// care to use. // care to use.
typedef FloatingPoint<float> Float; typedef FloatingPoint<float> Float;
@ -449,7 +426,7 @@ GTEST_API_ TypeId GetTestTypeId();
// of a Test object. // of a Test object.
class TestFactoryBase { class TestFactoryBase {
public: public:
virtual ~TestFactoryBase() {} virtual ~TestFactoryBase() = default;
// Creates a test instance to run. The instance is both created and destroyed // Creates a test instance to run. The instance is both created and destroyed
// within TestInfoImpl::Run() // within TestInfoImpl::Run()
@ -471,7 +448,7 @@ class TestFactoryImpl : public TestFactoryBase {
Test* CreateTest() override { return new TestClass; } Test* CreateTest() override { return new TestClass; }
}; };
#if GTEST_OS_WINDOWS #ifdef GTEST_OS_WINDOWS
// Predicate-formatters for implementing the HRESULT checking macros // Predicate-formatters for implementing the HRESULT checking macros
// {ASSERT|EXPECT}_HRESULT_{SUCCEEDED|FAILED} // {ASSERT|EXPECT}_HRESULT_{SUCCEEDED|FAILED}
@ -489,8 +466,8 @@ using SetUpTestSuiteFunc = void (*)();
using TearDownTestSuiteFunc = void (*)(); using TearDownTestSuiteFunc = void (*)();
struct CodeLocation { struct CodeLocation {
CodeLocation(const std::string& a_file, int a_line) CodeLocation(std::string a_file, int a_line)
: file(a_file), line(a_line) {} : file(std::move(a_file)), line(a_line) {}
std::string file; std::string file;
int line; int line;
@ -570,7 +547,7 @@ struct SuiteApiResolver : T {
// type_param: the name of the test's type parameter, or NULL if // type_param: the name of the test's type parameter, or NULL if
// this is not a typed or a type-parameterized test. // this is not a typed or a type-parameterized test.
// value_param: text representation of the test's value parameter, // value_param: text representation of the test's value parameter,
// or NULL if this is not a type-parameterized test. // or NULL if this is not a value-parameterized test.
// code_location: code location where the test is defined // code_location: code location where the test is defined
// fixture_class_id: ID of the test fixture class // fixture_class_id: ID of the test fixture class
// set_up_tc: pointer to the function that sets up the test suite // set_up_tc: pointer to the function that sets up the test suite
@ -579,7 +556,7 @@ struct SuiteApiResolver : T {
// The newly created TestInfo instance will assume // The newly created TestInfo instance will assume
// ownership of the factory object. // ownership of the factory object.
GTEST_API_ TestInfo* MakeAndRegisterTestInfo( GTEST_API_ TestInfo* MakeAndRegisterTestInfo(
const char* test_suite_name, const char* name, const char* type_param, std::string test_suite_name, const char* name, const char* type_param,
const char* value_param, CodeLocation code_location, const char* value_param, CodeLocation code_location,
TypeId fixture_class_id, SetUpTestSuiteFunc set_up_tc, TypeId fixture_class_id, SetUpTestSuiteFunc set_up_tc,
TearDownTestSuiteFunc tear_down_tc, TestFactoryBase* factory); TearDownTestSuiteFunc tear_down_tc, TestFactoryBase* factory);
@ -610,8 +587,7 @@ class GTEST_API_ TypedTestSuitePState {
fflush(stderr); fflush(stderr);
posix::Abort(); posix::Abort();
} }
registered_tests_.insert( registered_tests_.emplace(test_name, CodeLocation(file, line));
::std::make_pair(test_name, CodeLocation(file, line)));
return true; return true;
} }
@ -715,7 +691,7 @@ class TypeParameterizedTest {
// specified in INSTANTIATE_TYPED_TEST_SUITE_P(Prefix, TestSuite, // specified in INSTANTIATE_TYPED_TEST_SUITE_P(Prefix, TestSuite,
// Types). Valid values for 'index' are [0, N - 1] where N is the // Types). Valid values for 'index' are [0, N - 1] where N is the
// length of Types. // length of Types.
static bool Register(const char* prefix, const CodeLocation& code_location, static bool Register(const char* prefix, CodeLocation code_location,
const char* case_name, const char* test_names, int index, const char* case_name, const char* test_names, int index,
const std::vector<std::string>& type_names = const std::vector<std::string>& type_names =
GenerateNames<DefaultNameGenerator, Types>()) { GenerateNames<DefaultNameGenerator, Types>()) {
@ -727,8 +703,7 @@ class TypeParameterizedTest {
// list. // list.
MakeAndRegisterTestInfo( MakeAndRegisterTestInfo(
(std::string(prefix) + (prefix[0] == '\0' ? "" : "/") + case_name + (std::string(prefix) + (prefix[0] == '\0' ? "" : "/") + case_name +
"/" + type_names[static_cast<size_t>(index)]) "/" + type_names[static_cast<size_t>(index)]),
.c_str(),
StripTrailingSpaces(GetPrefixUntilComma(test_names)).c_str(), StripTrailingSpaces(GetPrefixUntilComma(test_names)).c_str(),
GetTypeName<Type>().c_str(), GetTypeName<Type>().c_str(),
nullptr, // No value parameter. nullptr, // No value parameter.
@ -740,13 +715,9 @@ class TypeParameterizedTest {
new TestFactoryImpl<TestClass>); new TestFactoryImpl<TestClass>);
// Next, recurses (at compile time) with the tail of the type list. // Next, recurses (at compile time) with the tail of the type list.
return TypeParameterizedTest<Fixture, TestSel, return TypeParameterizedTest<Fixture, TestSel, typename Types::Tail>::
typename Types::Tail>::Register(prefix, Register(prefix, std::move(code_location), case_name, test_names,
code_location, index + 1, type_names);
case_name,
test_names,
index + 1,
type_names);
} }
}; };
@ -754,7 +725,7 @@ class TypeParameterizedTest {
template <GTEST_TEMPLATE_ Fixture, class TestSel> template <GTEST_TEMPLATE_ Fixture, class TestSel>
class TypeParameterizedTest<Fixture, TestSel, internal::None> { class TypeParameterizedTest<Fixture, TestSel, internal::None> {
public: public:
static bool Register(const char* /*prefix*/, const CodeLocation&, static bool Register(const char* /*prefix*/, CodeLocation,
const char* /*case_name*/, const char* /*test_names*/, const char* /*case_name*/, const char* /*test_names*/,
int /*index*/, int /*index*/,
const std::vector<std::string>& = const std::vector<std::string>& =
@ -801,7 +772,8 @@ class TypeParameterizedTestSuite {
// Next, recurses (at compile time) with the tail of the test list. // Next, recurses (at compile time) with the tail of the test list.
return TypeParameterizedTestSuite<Fixture, typename Tests::Tail, return TypeParameterizedTestSuite<Fixture, typename Tests::Tail,
Types>::Register(prefix, code_location, Types>::Register(prefix,
std::move(code_location),
state, case_name, state, case_name,
SkipComma(test_names), SkipComma(test_names),
type_names); type_names);
@ -914,9 +886,6 @@ class HasDebugStringAndShortDebugString {
HasDebugStringType::value && HasShortDebugStringType::value; HasDebugStringType::value && HasShortDebugStringType::value;
}; };
template <typename T>
constexpr bool HasDebugStringAndShortDebugString<T>::value;
// When the compiler sees expression IsContainerTest<C>(0), if C is an // When the compiler sees expression IsContainerTest<C>(0), if C is an
// STL-style container class, the first overload of IsContainerTest // STL-style container class, the first overload of IsContainerTest
// will be viable (since both C::iterator* and C::const_iterator* are // will be viable (since both C::iterator* and C::const_iterator* are
@ -1155,40 +1124,6 @@ class NativeArray {
void (NativeArray::*clone_)(const Element*, size_t); void (NativeArray::*clone_)(const Element*, size_t);
}; };
// Backport of std::index_sequence.
template <size_t... Is>
struct IndexSequence {
using type = IndexSequence;
};
// Double the IndexSequence, and one if plus_one is true.
template <bool plus_one, typename T, size_t sizeofT>
struct DoubleSequence;
template <size_t... I, size_t sizeofT>
struct DoubleSequence<true, IndexSequence<I...>, sizeofT> {
using type = IndexSequence<I..., (sizeofT + I)..., 2 * sizeofT>;
};
template <size_t... I, size_t sizeofT>
struct DoubleSequence<false, IndexSequence<I...>, sizeofT> {
using type = IndexSequence<I..., (sizeofT + I)...>;
};
// Backport of std::make_index_sequence.
// It uses O(ln(N)) instantiation depth.
template <size_t N>
struct MakeIndexSequenceImpl
: DoubleSequence<N % 2 == 1, typename MakeIndexSequenceImpl<N / 2>::type,
N / 2>::type {};
template <>
struct MakeIndexSequenceImpl<0> : IndexSequence<> {};
template <size_t N>
using MakeIndexSequence = typename MakeIndexSequenceImpl<N>::type;
template <typename... T>
using IndexSequenceFor = typename MakeIndexSequence<sizeof...(T)>::type;
template <size_t> template <size_t>
struct Ignore { struct Ignore {
Ignore(...); // NOLINT Ignore(...); // NOLINT
@ -1197,7 +1132,7 @@ struct Ignore {
template <typename> template <typename>
struct ElemFromListImpl; struct ElemFromListImpl;
template <size_t... I> template <size_t... I>
struct ElemFromListImpl<IndexSequence<I...>> { struct ElemFromListImpl<std::index_sequence<I...>> {
// We make Ignore a template to solve a problem with MSVC. // We make Ignore a template to solve a problem with MSVC.
// A non-template Ignore would work fine with `decltype(Ignore(I))...`, but // A non-template Ignore would work fine with `decltype(Ignore(I))...`, but
// MSVC doesn't understand how to deal with that pack expansion. // MSVC doesn't understand how to deal with that pack expansion.
@ -1208,8 +1143,7 @@ struct ElemFromListImpl<IndexSequence<I...>> {
template <size_t N, typename... T> template <size_t N, typename... T>
struct ElemFromList { struct ElemFromList {
using type = using type = decltype(ElemFromListImpl<std::make_index_sequence<N>>::Apply(
decltype(ElemFromListImpl<typename MakeIndexSequence<N>::type>::Apply(
static_cast<T (*)()>(nullptr)...)); static_cast<T (*)()>(nullptr)...));
}; };
@ -1235,9 +1169,9 @@ template <typename Derived, typename Idx>
struct FlatTupleBase; struct FlatTupleBase;
template <size_t... Idx, typename... T> template <size_t... Idx, typename... T>
struct FlatTupleBase<FlatTuple<T...>, IndexSequence<Idx...>> struct FlatTupleBase<FlatTuple<T...>, std::index_sequence<Idx...>>
: FlatTupleElemBase<FlatTuple<T...>, Idx>... { : FlatTupleElemBase<FlatTuple<T...>, Idx>... {
using Indices = IndexSequence<Idx...>; using Indices = std::index_sequence<Idx...>;
FlatTupleBase() = default; FlatTupleBase() = default;
template <typename... Args> template <typename... Args>
explicit FlatTupleBase(FlatTupleConstructTag, Args&&... args) explicit FlatTupleBase(FlatTupleConstructTag, Args&&... args)
@ -1272,14 +1206,15 @@ struct FlatTupleBase<FlatTuple<T...>, IndexSequence<Idx...>>
// implementations. // implementations.
// FlatTuple and ElemFromList are not recursive and have a fixed depth // FlatTuple and ElemFromList are not recursive and have a fixed depth
// regardless of T... // regardless of T...
// MakeIndexSequence, on the other hand, it is recursive but with an // std::make_index_sequence, on the other hand, it is recursive but with an
// instantiation depth of O(ln(N)). // instantiation depth of O(ln(N)).
template <typename... T> template <typename... T>
class FlatTuple class FlatTuple
: private FlatTupleBase<FlatTuple<T...>, : private FlatTupleBase<FlatTuple<T...>,
typename MakeIndexSequence<sizeof...(T)>::type> { std::make_index_sequence<sizeof...(T)>> {
using Indices = typename FlatTupleBase< using Indices =
FlatTuple<T...>, typename MakeIndexSequence<sizeof...(T)>::type>::Indices; typename FlatTupleBase<FlatTuple<T...>,
std::make_index_sequence<sizeof...(T)>>::Indices;
public: public:
FlatTuple() = default; FlatTuple() = default;
@ -1293,30 +1228,40 @@ class FlatTuple
// Utility functions to be called with static_assert to induce deprecation // Utility functions to be called with static_assert to induce deprecation
// warnings. // warnings.
GTEST_INTERNAL_DEPRECATED( [[deprecated(
"INSTANTIATE_TEST_CASE_P is deprecated, please use " "INSTANTIATE_TEST_CASE_P is deprecated, please use "
"INSTANTIATE_TEST_SUITE_P") "INSTANTIATE_TEST_SUITE_P")]]
constexpr bool InstantiateTestCase_P_IsDeprecated() { return true; } constexpr bool InstantiateTestCase_P_IsDeprecated() {
return true;
}
GTEST_INTERNAL_DEPRECATED( [[deprecated(
"TYPED_TEST_CASE_P is deprecated, please use " "TYPED_TEST_CASE_P is deprecated, please use "
"TYPED_TEST_SUITE_P") "TYPED_TEST_SUITE_P")]]
constexpr bool TypedTestCase_P_IsDeprecated() { return true; } constexpr bool TypedTestCase_P_IsDeprecated() {
return true;
}
GTEST_INTERNAL_DEPRECATED( [[deprecated(
"TYPED_TEST_CASE is deprecated, please use " "TYPED_TEST_CASE is deprecated, please use "
"TYPED_TEST_SUITE") "TYPED_TEST_SUITE")]]
constexpr bool TypedTestCaseIsDeprecated() { return true; } constexpr bool TypedTestCaseIsDeprecated() {
return true;
}
GTEST_INTERNAL_DEPRECATED( [[deprecated(
"REGISTER_TYPED_TEST_CASE_P is deprecated, please use " "REGISTER_TYPED_TEST_CASE_P is deprecated, please use "
"REGISTER_TYPED_TEST_SUITE_P") "REGISTER_TYPED_TEST_SUITE_P")]]
constexpr bool RegisterTypedTestCase_P_IsDeprecated() { return true; } constexpr bool RegisterTypedTestCase_P_IsDeprecated() {
return true;
}
GTEST_INTERNAL_DEPRECATED( [[deprecated(
"INSTANTIATE_TYPED_TEST_CASE_P is deprecated, please use " "INSTANTIATE_TYPED_TEST_CASE_P is deprecated, please use "
"INSTANTIATE_TYPED_TEST_SUITE_P") "INSTANTIATE_TYPED_TEST_SUITE_P")]]
constexpr bool InstantiateTypedTestCase_P_IsDeprecated() { return true; } constexpr bool InstantiateTypedTestCase_P_IsDeprecated() {
return true;
}
} // namespace internal } // namespace internal
} // namespace testing } // namespace testing
@ -1512,12 +1457,13 @@ class NeverThrown {
#define GTEST_TEST_NO_FATAL_FAILURE_(statement, fail) \ #define GTEST_TEST_NO_FATAL_FAILURE_(statement, fail) \
GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
if (::testing::internal::AlwaysTrue()) { \ if (::testing::internal::AlwaysTrue()) { \
::testing::internal::HasNewFatalFailureHelper gtest_fatal_failure_checker; \ const ::testing::internal::HasNewFatalFailureHelper \
gtest_fatal_failure_checker; \
GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \ GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \
if (gtest_fatal_failure_checker.has_new_fatal_failure()) { \ if (gtest_fatal_failure_checker.has_new_fatal_failure()) { \
goto GTEST_CONCAT_TOKEN_(gtest_label_testnofatal_, __LINE__); \ goto GTEST_CONCAT_TOKEN_(gtest_label_testnofatal_, __LINE__); \
} \ } \
} else \ } else /* NOLINT */ \
GTEST_CONCAT_TOKEN_(gtest_label_testnofatal_, __LINE__) \ GTEST_CONCAT_TOKEN_(gtest_label_testnofatal_, __LINE__) \
: fail("Expected: " #statement \ : fail("Expected: " #statement \
" doesn't generate new fatal " \ " doesn't generate new fatal " \
@ -1552,7 +1498,7 @@ class NeverThrown {
\ \
private: \ private: \
void TestBody() override; \ void TestBody() override; \
static ::testing::TestInfo* const test_info_ GTEST_ATTRIBUTE_UNUSED_; \ [[maybe_unused]] static ::testing::TestInfo* const test_info_; \
}; \ }; \
\ \
::testing::TestInfo* const GTEST_TEST_CLASS_NAME_(test_suite_name, \ ::testing::TestInfo* const GTEST_TEST_CLASS_NAME_(test_suite_name, \

View File

@ -39,6 +39,7 @@
#include <ctype.h> #include <ctype.h>
#include <cassert> #include <cassert>
#include <functional>
#include <iterator> #include <iterator>
#include <map> #include <map>
#include <memory> #include <memory>
@ -47,6 +48,7 @@
#include <string> #include <string>
#include <tuple> #include <tuple>
#include <type_traits> #include <type_traits>
#include <unordered_map>
#include <utility> #include <utility>
#include <vector> #include <vector>
@ -85,7 +87,7 @@ namespace internal {
// TEST_P macro is used to define two tests with the same name // TEST_P macro is used to define two tests with the same name
// but in different namespaces. // but in different namespaces.
GTEST_API_ void ReportInvalidTestSuiteType(const char* test_suite_name, GTEST_API_ void ReportInvalidTestSuiteType(const char* test_suite_name,
CodeLocation code_location); const CodeLocation& code_location);
template <typename> template <typename>
class ParamGeneratorInterface; class ParamGeneratorInterface;
@ -97,7 +99,7 @@ class ParamGenerator;
template <typename T> template <typename T>
class ParamIteratorInterface { class ParamIteratorInterface {
public: public:
virtual ~ParamIteratorInterface() {} virtual ~ParamIteratorInterface() = default;
// A pointer to the base generator instance. // A pointer to the base generator instance.
// Used only for the purposes of iterator comparison // Used only for the purposes of iterator comparison
// to make sure that two iterators belong to the same generator. // to make sure that two iterators belong to the same generator.
@ -171,7 +173,7 @@ class ParamGeneratorInterface {
public: public:
typedef T ParamType; typedef T ParamType;
virtual ~ParamGeneratorInterface() {} virtual ~ParamGeneratorInterface() = default;
// Generator interface definition // Generator interface definition
virtual ParamIteratorInterface<T>* Begin() const = 0; virtual ParamIteratorInterface<T>* Begin() const = 0;
@ -215,7 +217,7 @@ class RangeGenerator : public ParamGeneratorInterface<T> {
end_(end), end_(end),
step_(step), step_(step),
end_index_(CalculateEndIndex(begin, end, step)) {} end_index_(CalculateEndIndex(begin, end, step)) {}
~RangeGenerator() override {} ~RangeGenerator() override = default;
ParamIteratorInterface<T>* Begin() const override { ParamIteratorInterface<T>* Begin() const override {
return new Iterator(this, begin_, 0, step_); return new Iterator(this, begin_, 0, step_);
@ -230,7 +232,7 @@ class RangeGenerator : public ParamGeneratorInterface<T> {
Iterator(const ParamGeneratorInterface<T>* base, T value, int index, Iterator(const ParamGeneratorInterface<T>* base, T value, int index,
IncrementT step) IncrementT step)
: base_(base), value_(value), index_(index), step_(step) {} : base_(base), value_(value), index_(index), step_(step) {}
~Iterator() override {} ~Iterator() override = default;
const ParamGeneratorInterface<T>* BaseGenerator() const override { const ParamGeneratorInterface<T>* BaseGenerator() const override {
return base_; return base_;
@ -299,7 +301,7 @@ class ValuesInIteratorRangeGenerator : public ParamGeneratorInterface<T> {
template <typename ForwardIterator> template <typename ForwardIterator>
ValuesInIteratorRangeGenerator(ForwardIterator begin, ForwardIterator end) ValuesInIteratorRangeGenerator(ForwardIterator begin, ForwardIterator end)
: container_(begin, end) {} : container_(begin, end) {}
~ValuesInIteratorRangeGenerator() override {} ~ValuesInIteratorRangeGenerator() override = default;
ParamIteratorInterface<T>* Begin() const override { ParamIteratorInterface<T>* Begin() const override {
return new Iterator(this, container_.begin()); return new Iterator(this, container_.begin());
@ -316,7 +318,7 @@ class ValuesInIteratorRangeGenerator : public ParamGeneratorInterface<T> {
Iterator(const ParamGeneratorInterface<T>* base, Iterator(const ParamGeneratorInterface<T>* base,
typename ContainerType::const_iterator iterator) typename ContainerType::const_iterator iterator)
: base_(base), iterator_(iterator) {} : base_(base), iterator_(iterator) {}
~Iterator() override {} ~Iterator() override = default;
const ParamGeneratorInterface<T>* BaseGenerator() const override { const ParamGeneratorInterface<T>* BaseGenerator() const override {
return base_; return base_;
@ -379,9 +381,7 @@ class ValuesInIteratorRangeGenerator : public ParamGeneratorInterface<T> {
// integer test parameter index. // integer test parameter index.
template <class ParamType> template <class ParamType>
std::string DefaultParamName(const TestParamInfo<ParamType>& info) { std::string DefaultParamName(const TestParamInfo<ParamType>& info) {
Message name_stream; return std::to_string(info.index);
name_stream << info.index;
return name_stream.GetString();
} }
template <typename T = int> template <typename T = int>
@ -420,7 +420,7 @@ class ParameterizedTestFactory : public TestFactoryBase {
template <class ParamType> template <class ParamType>
class TestMetaFactoryBase { class TestMetaFactoryBase {
public: public:
virtual ~TestMetaFactoryBase() {} virtual ~TestMetaFactoryBase() = default;
virtual TestFactoryBase* CreateTestFactory(ParamType parameter) = 0; virtual TestFactoryBase* CreateTestFactory(ParamType parameter) = 0;
}; };
@ -439,7 +439,7 @@ class TestMetaFactory
public: public:
using ParamType = typename TestSuite::ParamType; using ParamType = typename TestSuite::ParamType;
TestMetaFactory() {} TestMetaFactory() = default;
TestFactoryBase* CreateTestFactory(ParamType parameter) override { TestFactoryBase* CreateTestFactory(ParamType parameter) override {
return new ParameterizedTestFactory<TestSuite>(parameter); return new ParameterizedTestFactory<TestSuite>(parameter);
@ -462,7 +462,7 @@ class TestMetaFactory
// and calls RegisterTests() on each of them when asked. // and calls RegisterTests() on each of them when asked.
class ParameterizedTestSuiteInfoBase { class ParameterizedTestSuiteInfoBase {
public: public:
virtual ~ParameterizedTestSuiteInfoBase() {} virtual ~ParameterizedTestSuiteInfoBase() = default;
// Base part of test suite name for display purposes. // Base part of test suite name for display purposes.
virtual const std::string& GetTestSuiteName() const = 0; virtual const std::string& GetTestSuiteName() const = 0;
@ -513,9 +513,10 @@ class ParameterizedTestSuiteInfo : public ParameterizedTestSuiteInfoBase {
typedef ParamGenerator<ParamType>(GeneratorCreationFunc)(); typedef ParamGenerator<ParamType>(GeneratorCreationFunc)();
using ParamNameGeneratorFunc = std::string(const TestParamInfo<ParamType>&); using ParamNameGeneratorFunc = std::string(const TestParamInfo<ParamType>&);
explicit ParameterizedTestSuiteInfo(const char* name, explicit ParameterizedTestSuiteInfo(std::string name,
CodeLocation code_location) CodeLocation code_location)
: test_suite_name_(name), code_location_(code_location) {} : test_suite_name_(std::move(name)),
code_location_(std::move(code_location)) {}
// Test suite base name for display purposes. // Test suite base name for display purposes.
const std::string& GetTestSuiteName() const override { const std::string& GetTestSuiteName() const override {
@ -529,20 +530,20 @@ class ParameterizedTestSuiteInfo : public ParameterizedTestSuiteInfoBase {
// prefix). test_base_name is the name of an individual test without // prefix). test_base_name is the name of an individual test without
// parameter index. For the test SequenceA/FooTest.DoBar/1 FooTest is // parameter index. For the test SequenceA/FooTest.DoBar/1 FooTest is
// test suite base name and DoBar is test base name. // test suite base name and DoBar is test base name.
void AddTestPattern(const char* test_suite_name, const char* test_base_name, void AddTestPattern(const char*, const char* test_base_name,
TestMetaFactoryBase<ParamType>* meta_factory, TestMetaFactoryBase<ParamType>* meta_factory,
CodeLocation code_location) { CodeLocation code_location) {
tests_.push_back(std::shared_ptr<TestInfo>(new TestInfo( tests_.emplace_back(
test_suite_name, test_base_name, meta_factory, code_location))); new TestInfo(test_base_name, meta_factory, std::move(code_location)));
} }
// INSTANTIATE_TEST_SUITE_P macro uses AddGenerator() to record information // INSTANTIATE_TEST_SUITE_P macro uses AddGenerator() to record information
// about a generator. // about a generator.
int AddTestSuiteInstantiation(const std::string& instantiation_name, int AddTestSuiteInstantiation(std::string instantiation_name,
GeneratorCreationFunc* func, GeneratorCreationFunc* func,
ParamNameGeneratorFunc* name_func, ParamNameGeneratorFunc* name_func,
const char* file, int line) { const char* file, int line) {
instantiations_.push_back( instantiations_.emplace_back(std::move(instantiation_name), func, name_func,
InstantiationInfo(instantiation_name, func, name_func, file, line)); file, line);
return 0; // Return value used only to run this method in namespace scope. return 0; // Return value used only to run this method in namespace scope.
} }
// UnitTest class invokes this method to register tests in this test suite // UnitTest class invokes this method to register tests in this test suite
@ -553,60 +554,61 @@ class ParameterizedTestSuiteInfo : public ParameterizedTestSuiteInfoBase {
void RegisterTests() override { void RegisterTests() override {
bool generated_instantiations = false; bool generated_instantiations = false;
for (typename TestInfoContainer::iterator test_it = tests_.begin();
test_it != tests_.end(); ++test_it) {
std::shared_ptr<TestInfo> test_info = *test_it;
for (typename InstantiationContainer::iterator gen_it =
instantiations_.begin();
gen_it != instantiations_.end(); ++gen_it) {
const std::string& instantiation_name = gen_it->name;
ParamGenerator<ParamType> generator((*gen_it->generator)());
ParamNameGeneratorFunc* name_func = gen_it->name_func;
const char* file = gen_it->file;
int line = gen_it->line;
std::string test_suite_name; std::string test_suite_name;
std::string test_name;
for (const std::shared_ptr<TestInfo>& test_info : tests_) {
for (const InstantiationInfo& instantiation : instantiations_) {
const std::string& instantiation_name = instantiation.name;
ParamGenerator<ParamType> generator((*instantiation.generator)());
ParamNameGeneratorFunc* name_func = instantiation.name_func;
const char* file = instantiation.file;
int line = instantiation.line;
if (!instantiation_name.empty()) if (!instantiation_name.empty())
test_suite_name = instantiation_name + "/"; test_suite_name = instantiation_name + "/";
test_suite_name += test_info->test_suite_base_name; else
test_suite_name.clear();
test_suite_name += test_suite_name_;
size_t i = 0; size_t i = 0;
std::set<std::string> test_param_names; std::set<std::string> test_param_names;
for (typename ParamGenerator<ParamType>::iterator param_it = for (const auto& param : generator) {
generator.begin();
param_it != generator.end(); ++param_it, ++i) {
generated_instantiations = true; generated_instantiations = true;
Message test_name_stream; test_name.clear();
std::string param_name = std::string param_name =
name_func(TestParamInfo<ParamType>(*param_it, i)); name_func(TestParamInfo<ParamType>(param, i));
GTEST_CHECK_(IsValidParamName(param_name)) GTEST_CHECK_(IsValidParamName(param_name))
<< "Parameterized test name '" << param_name << "Parameterized test name '" << param_name
<< "' is invalid, in " << file << " line " << line << std::endl; << "' is invalid (contains spaces, dashes, or any "
"non-alphanumeric characters other than underscores), in "
<< file << " line " << line << "" << std::endl;
GTEST_CHECK_(test_param_names.count(param_name) == 0) GTEST_CHECK_(test_param_names.count(param_name) == 0)
<< "Duplicate parameterized test name '" << param_name << "', in " << "Duplicate parameterized test name '" << param_name << "', in "
<< file << " line " << line << std::endl; << file << " line " << line << std::endl;
test_param_names.insert(param_name);
if (!test_info->test_base_name.empty()) { if (!test_info->test_base_name.empty()) {
test_name_stream << test_info->test_base_name << "/"; test_name.append(test_info->test_base_name).append("/");
} }
test_name_stream << param_name; test_name += param_name;
test_param_names.insert(std::move(param_name));
MakeAndRegisterTestInfo( MakeAndRegisterTestInfo(
test_suite_name.c_str(), test_name_stream.GetString().c_str(), test_suite_name, test_name.c_str(),
nullptr, // No type parameter. nullptr, // No type parameter.
PrintToString(*param_it).c_str(), test_info->code_location, PrintToString(param).c_str(), test_info->code_location,
GetTestSuiteTypeId(), GetTestSuiteTypeId(),
SuiteApiResolver<TestSuite>::GetSetUpCaseOrSuite(file, line), SuiteApiResolver<TestSuite>::GetSetUpCaseOrSuite(file, line),
SuiteApiResolver<TestSuite>::GetTearDownCaseOrSuite(file, line), SuiteApiResolver<TestSuite>::GetTearDownCaseOrSuite(file, line),
test_info->test_meta_factory->CreateTestFactory(*param_it)); test_info->test_meta_factory->CreateTestFactory(param));
} // for param_it ++i;
} // for gen_it } // for param
} // for test_it } // for instantiation
} // for test_info
if (!generated_instantiations) { if (!generated_instantiations) {
// There are no generaotrs, or they all generate nothing ... // There are no generaotrs, or they all generate nothing ...
@ -619,15 +621,13 @@ class ParameterizedTestSuiteInfo : public ParameterizedTestSuiteInfoBase {
// LocalTestInfo structure keeps information about a single test registered // LocalTestInfo structure keeps information about a single test registered
// with TEST_P macro. // with TEST_P macro.
struct TestInfo { struct TestInfo {
TestInfo(const char* a_test_suite_base_name, const char* a_test_base_name, TestInfo(const char* a_test_base_name,
TestMetaFactoryBase<ParamType>* a_test_meta_factory, TestMetaFactoryBase<ParamType>* a_test_meta_factory,
CodeLocation a_code_location) CodeLocation a_code_location)
: test_suite_base_name(a_test_suite_base_name), : test_base_name(a_test_base_name),
test_base_name(a_test_base_name),
test_meta_factory(a_test_meta_factory), test_meta_factory(a_test_meta_factory),
code_location(a_code_location) {} code_location(std::move(a_code_location)) {}
const std::string test_suite_base_name;
const std::string test_base_name; const std::string test_base_name;
const std::unique_ptr<TestMetaFactoryBase<ParamType>> test_meta_factory; const std::unique_ptr<TestMetaFactoryBase<ParamType>> test_meta_factory;
const CodeLocation code_location; const CodeLocation code_location;
@ -637,11 +637,10 @@ class ParameterizedTestSuiteInfo : public ParameterizedTestSuiteInfoBase {
// <Instantiation name, Sequence generator creation function, // <Instantiation name, Sequence generator creation function,
// Name generator function, Source file, Source line> // Name generator function, Source file, Source line>
struct InstantiationInfo { struct InstantiationInfo {
InstantiationInfo(const std::string& name_in, InstantiationInfo(std::string name_in, GeneratorCreationFunc* generator_in,
GeneratorCreationFunc* generator_in,
ParamNameGeneratorFunc* name_func_in, const char* file_in, ParamNameGeneratorFunc* name_func_in, const char* file_in,
int line_in) int line_in)
: name(name_in), : name(std::move(name_in)),
generator(generator_in), generator(generator_in),
name_func(name_func_in), name_func(name_func_in),
file(file_in), file(file_in),
@ -691,7 +690,7 @@ using ParameterizedTestCaseInfo = ParameterizedTestSuiteInfo<TestCase>;
// ParameterizedTestSuiteInfo descriptors. // ParameterizedTestSuiteInfo descriptors.
class ParameterizedTestSuiteRegistry { class ParameterizedTestSuiteRegistry {
public: public:
ParameterizedTestSuiteRegistry() {} ParameterizedTestSuiteRegistry() = default;
~ParameterizedTestSuiteRegistry() { ~ParameterizedTestSuiteRegistry() {
for (auto& test_suite_info : test_suite_infos_) { for (auto& test_suite_info : test_suite_infos_) {
delete test_suite_info; delete test_suite_info;
@ -702,29 +701,32 @@ class ParameterizedTestSuiteRegistry {
// tests and instantiations of a particular test suite. // tests and instantiations of a particular test suite.
template <class TestSuite> template <class TestSuite>
ParameterizedTestSuiteInfo<TestSuite>* GetTestSuitePatternHolder( ParameterizedTestSuiteInfo<TestSuite>* GetTestSuitePatternHolder(
const char* test_suite_name, CodeLocation code_location) { std::string test_suite_name, CodeLocation code_location) {
ParameterizedTestSuiteInfo<TestSuite>* typed_test_info = nullptr; ParameterizedTestSuiteInfo<TestSuite>* typed_test_info = nullptr;
for (auto& test_suite_info : test_suite_infos_) {
if (test_suite_info->GetTestSuiteName() == test_suite_name) { auto item_it = suite_name_to_info_index_.find(test_suite_name);
if (item_it != suite_name_to_info_index_.end()) {
auto* test_suite_info = test_suite_infos_[item_it->second];
if (test_suite_info->GetTestSuiteTypeId() != GetTypeId<TestSuite>()) { if (test_suite_info->GetTestSuiteTypeId() != GetTypeId<TestSuite>()) {
// Complain about incorrect usage of Google Test facilities // Complain about incorrect usage of Google Test facilities
// and terminate the program since we cannot guaranty correct // and terminate the program since we cannot guaranty correct
// test suite setup and tear-down in this case. // test suite setup and tear-down in this case.
ReportInvalidTestSuiteType(test_suite_name, code_location); ReportInvalidTestSuiteType(test_suite_name.c_str(), code_location);
posix::Abort(); posix::Abort();
} else { } else {
// At this point we are sure that the object we found is of the same // At this point we are sure that the object we found is of the same
// type we are looking for, so we downcast it to that type // type we are looking for, so we downcast it to that type
// without further checks. // without further checks.
typed_test_info = CheckedDowncastToActualType< typed_test_info =
ParameterizedTestSuiteInfo<TestSuite>>(test_suite_info); CheckedDowncastToActualType<ParameterizedTestSuiteInfo<TestSuite>>(
} test_suite_info);
break;
} }
} }
if (typed_test_info == nullptr) { if (typed_test_info == nullptr) {
typed_test_info = new ParameterizedTestSuiteInfo<TestSuite>( typed_test_info = new ParameterizedTestSuiteInfo<TestSuite>(
test_suite_name, code_location); test_suite_name, std::move(code_location));
suite_name_to_info_index_.emplace(std::move(test_suite_name),
test_suite_infos_.size());
test_suite_infos_.push_back(typed_test_info); test_suite_infos_.push_back(typed_test_info);
} }
return typed_test_info; return typed_test_info;
@ -738,8 +740,9 @@ class ParameterizedTestSuiteRegistry {
#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_
template <class TestCase> template <class TestCase>
ParameterizedTestCaseInfo<TestCase>* GetTestCasePatternHolder( ParameterizedTestCaseInfo<TestCase>* GetTestCasePatternHolder(
const char* test_case_name, CodeLocation code_location) { std::string test_case_name, CodeLocation code_location) {
return GetTestSuitePatternHolder<TestCase>(test_case_name, code_location); return GetTestSuitePatternHolder<TestCase>(std::move(test_case_name),
std::move(code_location));
} }
#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ #endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_
@ -748,6 +751,7 @@ class ParameterizedTestSuiteRegistry {
using TestSuiteInfoContainer = ::std::vector<ParameterizedTestSuiteInfoBase*>; using TestSuiteInfoContainer = ::std::vector<ParameterizedTestSuiteInfoBase*>;
TestSuiteInfoContainer test_suite_infos_; TestSuiteInfoContainer test_suite_infos_;
::std::unordered_map<std::string, size_t> suite_name_to_info_index_;
ParameterizedTestSuiteRegistry(const ParameterizedTestSuiteRegistry&) = ParameterizedTestSuiteRegistry(const ParameterizedTestSuiteRegistry&) =
delete; delete;
@ -774,7 +778,7 @@ class TypeParameterizedTestSuiteRegistry {
private: private:
struct TypeParameterizedTestSuiteInfo { struct TypeParameterizedTestSuiteInfo {
explicit TypeParameterizedTestSuiteInfo(CodeLocation c) explicit TypeParameterizedTestSuiteInfo(CodeLocation c)
: code_location(c), instantiated(false) {} : code_location(std::move(c)), instantiated(false) {}
CodeLocation code_location; CodeLocation code_location;
bool instantiated; bool instantiated;
@ -794,10 +798,7 @@ internal::ParamGenerator<typename Container::value_type> ValuesIn(
namespace internal { namespace internal {
// Used in the Values() function to provide polymorphic capabilities. // Used in the Values() function to provide polymorphic capabilities.
#ifdef _MSC_VER GTEST_DISABLE_MSC_WARNINGS_PUSH_(4100)
#pragma warning(push)
#pragma warning(disable : 4100)
#endif
template <typename... Ts> template <typename... Ts>
class ValueArray { class ValueArray {
@ -806,21 +807,19 @@ class ValueArray {
template <typename T> template <typename T>
operator ParamGenerator<T>() const { // NOLINT operator ParamGenerator<T>() const { // NOLINT
return ValuesIn(MakeVector<T>(MakeIndexSequence<sizeof...(Ts)>())); return ValuesIn(MakeVector<T>(std::make_index_sequence<sizeof...(Ts)>()));
} }
private: private:
template <typename T, size_t... I> template <typename T, size_t... I>
std::vector<T> MakeVector(IndexSequence<I...>) const { std::vector<T> MakeVector(std::index_sequence<I...>) const {
return std::vector<T>{static_cast<T>(v_.template Get<I>())...}; return std::vector<T>{static_cast<T>(v_.template Get<I>())...};
} }
FlatTuple<Ts...> v_; FlatTuple<Ts...> v_;
}; };
#ifdef _MSC_VER GTEST_DISABLE_MSC_WARNINGS_POP_() // 4100
#pragma warning(pop)
#endif
template <typename... T> template <typename... T>
class CartesianProductGenerator class CartesianProductGenerator
@ -830,7 +829,7 @@ class CartesianProductGenerator
CartesianProductGenerator(const std::tuple<ParamGenerator<T>...>& g) CartesianProductGenerator(const std::tuple<ParamGenerator<T>...>& g)
: generators_(g) {} : generators_(g) {}
~CartesianProductGenerator() override {} ~CartesianProductGenerator() override = default;
ParamIteratorInterface<ParamType>* Begin() const override { ParamIteratorInterface<ParamType>* Begin() const override {
return new Iterator(this, generators_, false); return new Iterator(this, generators_, false);
@ -843,7 +842,7 @@ class CartesianProductGenerator
template <class I> template <class I>
class IteratorImpl; class IteratorImpl;
template <size_t... I> template <size_t... I>
class IteratorImpl<IndexSequence<I...>> class IteratorImpl<std::index_sequence<I...>>
: public ParamIteratorInterface<ParamType> { : public ParamIteratorInterface<ParamType> {
public: public:
IteratorImpl(const ParamGeneratorInterface<ParamType>* base, IteratorImpl(const ParamGeneratorInterface<ParamType>* base,
@ -855,7 +854,7 @@ class CartesianProductGenerator
current_(is_end ? end_ : begin_) { current_(is_end ? end_ : begin_) {
ComputeCurrentValue(); ComputeCurrentValue();
} }
~IteratorImpl() override {} ~IteratorImpl() override = default;
const ParamGeneratorInterface<ParamType>* BaseGenerator() const override { const ParamGeneratorInterface<ParamType>* BaseGenerator() const override {
return base_; return base_;
@ -934,7 +933,7 @@ class CartesianProductGenerator
std::shared_ptr<ParamType> current_value_; std::shared_ptr<ParamType> current_value_;
}; };
using Iterator = IteratorImpl<typename MakeIndexSequence<sizeof...(T)>::type>; using Iterator = IteratorImpl<std::make_index_sequence<sizeof...(T)>>;
std::tuple<ParamGenerator<T>...> generators_; std::tuple<ParamGenerator<T>...> generators_;
}; };
@ -953,11 +952,11 @@ class CartesianProductHolder {
std::tuple<Gen...> generators_; std::tuple<Gen...> generators_;
}; };
template <typename From, typename To> template <typename From, typename To, typename Func>
class ParamGeneratorConverter : public ParamGeneratorInterface<To> { class ParamGeneratorConverter : public ParamGeneratorInterface<To> {
public: public:
ParamGeneratorConverter(ParamGenerator<From> gen) // NOLINT ParamGeneratorConverter(ParamGenerator<From> gen, Func converter) // NOLINT
: generator_(std::move(gen)) {} : generator_(std::move(gen)), converter_(std::move(converter)) {}
ParamIteratorInterface<To>* Begin() const override { ParamIteratorInterface<To>* Begin() const override {
return new Iterator(this, generator_.begin(), generator_.end()); return new Iterator(this, generator_.begin(), generator_.end());
@ -966,22 +965,32 @@ class ParamGeneratorConverter : public ParamGeneratorInterface<To> {
return new Iterator(this, generator_.end(), generator_.end()); return new Iterator(this, generator_.end(), generator_.end());
} }
// Returns the std::function wrapping the user-supplied converter callable. It
// is used by the iterator (see class Iterator below) to convert the object
// (of type FROM) returned by the ParamGenerator to an object of a type that
// can be static_cast to type TO.
const Func& TypeConverter() const { return converter_; }
private: private:
class Iterator : public ParamIteratorInterface<To> { class Iterator : public ParamIteratorInterface<To> {
public: public:
Iterator(const ParamGeneratorInterface<To>* base, ParamIterator<From> it, Iterator(const ParamGeneratorConverter* base, ParamIterator<From> it,
ParamIterator<From> end) ParamIterator<From> end)
: base_(base), it_(it), end_(end) { : base_(base), it_(it), end_(end) {
if (it_ != end_) value_ = std::make_shared<To>(static_cast<To>(*it_)); if (it_ != end_)
value_ =
std::make_shared<To>(static_cast<To>(base->TypeConverter()(*it_)));
} }
~Iterator() override {} ~Iterator() override = default;
const ParamGeneratorInterface<To>* BaseGenerator() const override { const ParamGeneratorInterface<To>* BaseGenerator() const override {
return base_; return base_;
} }
void Advance() override { void Advance() override {
++it_; ++it_;
if (it_ != end_) value_ = std::make_shared<To>(static_cast<To>(*it_)); if (it_ != end_)
value_ =
std::make_shared<To>(static_cast<To>(base_->TypeConverter()(*it_)));
} }
ParamIteratorInterface<To>* Clone() const override { ParamIteratorInterface<To>* Clone() const override {
return new Iterator(*this); return new Iterator(*this);
@ -1001,30 +1010,54 @@ class ParamGeneratorConverter : public ParamGeneratorInterface<To> {
private: private:
Iterator(const Iterator& other) = default; Iterator(const Iterator& other) = default;
const ParamGeneratorInterface<To>* const base_; const ParamGeneratorConverter* const base_;
ParamIterator<From> it_; ParamIterator<From> it_;
ParamIterator<From> end_; ParamIterator<From> end_;
std::shared_ptr<To> value_; std::shared_ptr<To> value_;
}; // class ParamGeneratorConverter::Iterator }; // class ParamGeneratorConverter::Iterator
ParamGenerator<From> generator_; ParamGenerator<From> generator_;
Func converter_;
}; // class ParamGeneratorConverter }; // class ParamGeneratorConverter
template <class Gen> template <class GeneratedT,
typename StdFunction =
std::function<const GeneratedT&(const GeneratedT&)>>
class ParamConverterGenerator { class ParamConverterGenerator {
public: public:
ParamConverterGenerator(ParamGenerator<Gen> g) // NOLINT ParamConverterGenerator(ParamGenerator<GeneratedT> g) // NOLINT
: generator_(std::move(g)) {} : generator_(std::move(g)), converter_(Identity) {}
ParamConverterGenerator(ParamGenerator<GeneratedT> g, StdFunction converter)
: generator_(std::move(g)), converter_(std::move(converter)) {}
template <typename T> template <typename T>
operator ParamGenerator<T>() const { // NOLINT operator ParamGenerator<T>() const { // NOLINT
return ParamGenerator<T>(new ParamGeneratorConverter<Gen, T>(generator_)); return ParamGenerator<T>(
new ParamGeneratorConverter<GeneratedT, T, StdFunction>(generator_,
converter_));
} }
private: private:
ParamGenerator<Gen> generator_; static const GeneratedT& Identity(const GeneratedT& v) { return v; }
ParamGenerator<GeneratedT> generator_;
StdFunction converter_;
}; };
// Template to determine the param type of a single-param std::function.
template <typename T>
struct FuncSingleParamType;
template <typename R, typename P>
struct FuncSingleParamType<std::function<R(P)>> {
using type = std::remove_cv_t<std::remove_reference_t<P>>;
};
template <typename T>
struct IsSingleArgStdFunction : public std::false_type {};
template <typename R, typename P>
struct IsSingleArgStdFunction<std::function<R(P)>> : public std::true_type {};
} // namespace internal } // namespace internal
} // namespace testing } // namespace testing

View File

@ -56,6 +56,8 @@
#elif WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_TV_TITLE) #elif WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_TV_TITLE)
#define GTEST_OS_WINDOWS_PHONE 1 #define GTEST_OS_WINDOWS_PHONE 1
#define GTEST_OS_WINDOWS_TV_TITLE 1 #define GTEST_OS_WINDOWS_TV_TITLE 1
#elif WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_GAMES)
#define GTEST_OS_WINDOWS_GAMES 1
#else #else
// WINAPI_FAMILY defined but no known partition matched. // WINAPI_FAMILY defined but no known partition matched.
// Default to desktop. // Default to desktop.
@ -113,6 +115,10 @@
#define GTEST_OS_XTENSA 1 #define GTEST_OS_XTENSA 1
#elif defined(__hexagon__) #elif defined(__hexagon__)
#define GTEST_OS_QURT 1 #define GTEST_OS_QURT 1
#elif defined(CPU_QN9090) || defined(CPU_QN9090HN)
#define GTEST_OS_NXP_QN9090 1
#elif defined(NRF52)
#define GTEST_OS_NRF52 1
#endif // __CYGWIN__ #endif // __CYGWIN__
#endif // GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_ARCH_H_ #endif // GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_ARCH_H_

View File

@ -161,10 +161,10 @@
// NOT define them. // NOT define them.
// //
// These macros are public so that portable tests can be written. // These macros are public so that portable tests can be written.
// Such tests typically surround code using a feature with an #if // Such tests typically surround code using a feature with an #ifdef
// which controls that code. For example: // which controls that code. For example:
// //
// #if GTEST_HAS_DEATH_TEST // #ifdef GTEST_HAS_DEATH_TEST
// EXPECT_DEATH(DoSomethingDeadly()); // EXPECT_DEATH(DoSomethingDeadly());
// #endif // #endif
// //
@ -178,6 +178,7 @@
// define themselves. // define themselves.
// GTEST_USES_SIMPLE_RE - our own simple regex is used; // GTEST_USES_SIMPLE_RE - our own simple regex is used;
// the above RE\b(s) are mutually exclusive. // the above RE\b(s) are mutually exclusive.
// GTEST_HAS_ABSL - Google Test is compiled with Abseil.
// Misc public macros // Misc public macros
// ------------------ // ------------------
@ -193,25 +194,19 @@
// //
// Macros for basic C++ coding: // Macros for basic C++ coding:
// GTEST_AMBIGUOUS_ELSE_BLOCKER_ - for disabling a gcc warning. // GTEST_AMBIGUOUS_ELSE_BLOCKER_ - for disabling a gcc warning.
// GTEST_ATTRIBUTE_UNUSED_ - declares that a class' instances or a
// variable don't have to be used.
// GTEST_MUST_USE_RESULT_ - declares that a function's result must be used.
// GTEST_INTENTIONAL_CONST_COND_PUSH_ - start code section where MSVC C4127 is // GTEST_INTENTIONAL_CONST_COND_PUSH_ - start code section where MSVC C4127 is
// suppressed (constant conditional). // suppressed (constant conditional).
// GTEST_INTENTIONAL_CONST_COND_POP_ - finish code section where MSVC C4127 // GTEST_INTENTIONAL_CONST_COND_POP_ - finish code section where MSVC C4127
// is suppressed. // is suppressed.
// GTEST_INTERNAL_HAS_ANY - for enabling UniversalPrinter<std::any> or // GTEST_INTERNAL_HAS_STD_SPAN - for enabling UniversalPrinter<std::span>
// UniversalPrinter<absl::any> specializations. // specializations. Always defined to 0 or 1
// GTEST_INTERNAL_HAS_OPTIONAL - for enabling UniversalPrinter<std::optional> // GTEST_USE_OWN_FLAGFILE_FLAG_ - Always defined to 0 or 1.
// or // GTEST_HAS_CXXABI_H_ - Always defined to 0 or 1.
// UniversalPrinter<absl::optional> // GTEST_CAN_STREAM_RESULTS_ - Always defined to 0 or 1.
// specializations. // GTEST_HAS_ALT_PATH_SEP_ - Always defined to 0 or 1.
// GTEST_INTERNAL_HAS_STRING_VIEW - for enabling Matcher<std::string_view> or // GTEST_WIDE_STRING_USES_UTF16_ - Always defined to 0 or 1.
// Matcher<absl::string_view> // GTEST_HAS_MUTEX_AND_THREAD_LOCAL_ - Always defined to 0 or 1.
// specializations. // GTEST_HAS_NOTIFICATION_- Always defined to 0 or 1.
// GTEST_INTERNAL_HAS_VARIANT - for enabling UniversalPrinter<std::variant> or
// UniversalPrinter<absl::variant>
// specializations.
// //
// Synchronization: // Synchronization:
// Mutex, MutexLock, ThreadLocal, GetThreadCount() // Mutex, MutexLock, ThreadLocal, GetThreadCount()
@ -251,11 +246,6 @@
// BoolFromGTestEnv() - parses a bool environment variable. // BoolFromGTestEnv() - parses a bool environment variable.
// Int32FromGTestEnv() - parses an int32_t environment variable. // Int32FromGTestEnv() - parses an int32_t environment variable.
// StringFromGTestEnv() - parses a string environment variable. // StringFromGTestEnv() - parses a string environment variable.
//
// Deprecation warnings:
// GTEST_INTERNAL_DEPRECATED(message) - attribute marking a function as
// deprecated; calling a marked function
// should generate a compiler warning
// The definition of GTEST_INTERNAL_CPLUSPLUS_LANG comes first because it can // The definition of GTEST_INTERNAL_CPLUSPLUS_LANG comes first because it can
// potentially be used as an #include guard. // potentially be used as an #include guard.
@ -266,8 +256,28 @@
#endif #endif
#if !defined(GTEST_INTERNAL_CPLUSPLUS_LANG) || \ #if !defined(GTEST_INTERNAL_CPLUSPLUS_LANG) || \
GTEST_INTERNAL_CPLUSPLUS_LANG < 201402L GTEST_INTERNAL_CPLUSPLUS_LANG < 201703L
#error C++ versions less than C++14 are not supported. #error C++ versions less than C++17 are not supported.
#endif
// MSVC >= 19.11 (VS 2017 Update 3) supports __has_include.
#ifdef __has_include
#define GTEST_INTERNAL_HAS_INCLUDE __has_include
#else
#define GTEST_INTERNAL_HAS_INCLUDE(...) 0
#endif
// Detect C++ feature test macros as gracefully as possible.
// MSVC >= 19.15, Clang >= 3.4.1, and GCC >= 4.1.2 support feature test macros.
//
// GCC15 warns that <ciso646> is deprecated in C++17 and suggests using
// <version> instead, even though <version> is not available in C++17 mode prior
// to GCC9.
#if GTEST_INTERNAL_CPLUSPLUS_LANG >= 202002L || \
GTEST_INTERNAL_HAS_INCLUDE(<version>)
#include <version> // C++20 or <version> support.
#else
#include <ciso646> // Pre-C++20
#endif #endif
#include <ctype.h> // for isspace, etc #include <ctype.h> // for isspace, etc
@ -303,7 +313,16 @@
#include "gtest/internal/custom/gtest-port.h" #include "gtest/internal/custom/gtest-port.h"
#include "gtest/internal/gtest-port-arch.h" #include "gtest/internal/gtest-port-arch.h"
#if GTEST_HAS_ABSL #ifndef GTEST_HAS_MUTEX_AND_THREAD_LOCAL_
#define GTEST_HAS_MUTEX_AND_THREAD_LOCAL_ 0
#endif
#ifndef GTEST_HAS_NOTIFICATION_
#define GTEST_HAS_NOTIFICATION_ 0
#endif
#if defined(GTEST_HAS_ABSL) && !defined(GTEST_NO_ABSL_FLAGS)
#define GTEST_INTERNAL_HAS_ABSL_FLAGS // Used only in this file.
#include "absl/flags/declare.h" #include "absl/flags/declare.h"
#include "absl/flags/flag.h" #include "absl/flags/flag.h"
#include "absl/flags/reflection.h" #include "absl/flags/reflection.h"
@ -361,13 +380,13 @@
// Brings in definitions for functions used in the testing::internal::posix // Brings in definitions for functions used in the testing::internal::posix
// namespace (read, write, close, chdir, isatty, stat). We do not currently // namespace (read, write, close, chdir, isatty, stat). We do not currently
// use them on Windows Mobile. // use them on Windows Mobile.
#if GTEST_OS_WINDOWS #ifdef GTEST_OS_WINDOWS
#if !GTEST_OS_WINDOWS_MOBILE #ifndef GTEST_OS_WINDOWS_MOBILE
#include <direct.h> #include <direct.h>
#include <io.h> #include <io.h>
#endif #endif
// In order to avoid having to include <windows.h>, use forward declaration // In order to avoid having to include <windows.h>, use forward declaration
#if GTEST_OS_WINDOWS_MINGW && !defined(__MINGW64_VERSION_MAJOR) #if defined(GTEST_OS_WINDOWS_MINGW) && !defined(__MINGW64_VERSION_MAJOR)
// MinGW defined _CRITICAL_SECTION and _RTL_CRITICAL_SECTION as two // MinGW defined _CRITICAL_SECTION and _RTL_CRITICAL_SECTION as two
// separate (equivalent) structs, instead of using typedef // separate (equivalent) structs, instead of using typedef
typedef struct _CRITICAL_SECTION GTEST_CRITICAL_SECTION; typedef struct _CRITICAL_SECTION GTEST_CRITICAL_SECTION;
@ -377,7 +396,7 @@ typedef struct _CRITICAL_SECTION GTEST_CRITICAL_SECTION;
// WindowsTypesTest.CRITICAL_SECTIONIs_RTL_CRITICAL_SECTION. // WindowsTypesTest.CRITICAL_SECTIONIs_RTL_CRITICAL_SECTION.
typedef struct _RTL_CRITICAL_SECTION GTEST_CRITICAL_SECTION; typedef struct _RTL_CRITICAL_SECTION GTEST_CRITICAL_SECTION;
#endif #endif
#elif GTEST_OS_XTENSA #elif defined(GTEST_OS_XTENSA)
#include <unistd.h> #include <unistd.h>
// Xtensa toolchains define strcasecmp in the string.h header instead of // Xtensa toolchains define strcasecmp in the string.h header instead of
// strings.h. string.h is already included. // strings.h. string.h is already included.
@ -389,7 +408,7 @@ typedef struct _RTL_CRITICAL_SECTION GTEST_CRITICAL_SECTION;
#include <unistd.h> #include <unistd.h>
#endif // GTEST_OS_WINDOWS #endif // GTEST_OS_WINDOWS
#if GTEST_OS_LINUX_ANDROID #ifdef GTEST_OS_LINUX_ANDROID
// Used to define __ANDROID_API__ matching the target NDK API level. // Used to define __ANDROID_API__ matching the target NDK API level.
#include <android/api-level.h> // NOLINT #include <android/api-level.h> // NOLINT
#endif #endif
@ -397,17 +416,21 @@ typedef struct _RTL_CRITICAL_SECTION GTEST_CRITICAL_SECTION;
// Defines this to true if and only if Google Test can use POSIX regular // Defines this to true if and only if Google Test can use POSIX regular
// expressions. // expressions.
#ifndef GTEST_HAS_POSIX_RE #ifndef GTEST_HAS_POSIX_RE
#if GTEST_OS_LINUX_ANDROID #ifdef GTEST_OS_LINUX_ANDROID
// On Android, <regex.h> is only available starting with Gingerbread. // On Android, <regex.h> is only available starting with Gingerbread.
#define GTEST_HAS_POSIX_RE (__ANDROID_API__ >= 9) #define GTEST_HAS_POSIX_RE (__ANDROID_API__ >= 9)
#else #else
#define GTEST_HAS_POSIX_RE \ #if !(defined(GTEST_OS_WINDOWS) || defined(GTEST_OS_XTENSA) || \
!(GTEST_OS_WINDOWS || GTEST_OS_XTENSA || GTEST_OS_QURT) defined(GTEST_OS_QURT))
#define GTEST_HAS_POSIX_RE 1
#else
#define GTEST_HAS_POSIX_RE 0
#endif #endif
#endif // GTEST_OS_LINUX_ANDROID
#endif #endif
// Select the regular expression implementation. // Select the regular expression implementation.
#if GTEST_HAS_ABSL #ifdef GTEST_HAS_ABSL
// When using Abseil, RE2 is required. // When using Abseil, RE2 is required.
#include "absl/strings/string_view.h" #include "absl/strings/string_view.h"
#include "re2/re2.h" #include "re2/re2.h"
@ -443,8 +466,12 @@ typedef struct _RTL_CRITICAL_SECTION GTEST_CRITICAL_SECTION;
// cleanups prior to that. To reliably check for C++ exception availability with // cleanups prior to that. To reliably check for C++ exception availability with
// clang, check for // clang, check for
// __EXCEPTIONS && __has_feature(cxx_exceptions). // __EXCEPTIONS && __has_feature(cxx_exceptions).
#define GTEST_HAS_EXCEPTIONS (__EXCEPTIONS && __has_feature(cxx_exceptions)) #if defined(__EXCEPTIONS) && __EXCEPTIONS && __has_feature(cxx_exceptions)
#elif defined(__GNUC__) && __EXCEPTIONS #define GTEST_HAS_EXCEPTIONS 1
#else
#define GTEST_HAS_EXCEPTIONS 0
#endif
#elif defined(__GNUC__) && defined(__EXCEPTIONS) && __EXCEPTIONS
// gcc defines __EXCEPTIONS to 1 if and only if exceptions are enabled. // gcc defines __EXCEPTIONS to 1 if and only if exceptions are enabled.
#define GTEST_HAS_EXCEPTIONS 1 #define GTEST_HAS_EXCEPTIONS 1
#elif defined(__SUNPRO_CC) #elif defined(__SUNPRO_CC)
@ -452,7 +479,7 @@ typedef struct _RTL_CRITICAL_SECTION GTEST_CRITICAL_SECTION;
// detecting whether they are enabled or not. Therefore, we assume that // detecting whether they are enabled or not. Therefore, we assume that
// they are enabled unless the user tells us otherwise. // they are enabled unless the user tells us otherwise.
#define GTEST_HAS_EXCEPTIONS 1 #define GTEST_HAS_EXCEPTIONS 1
#elif defined(__IBMCPP__) && __EXCEPTIONS #elif defined(__IBMCPP__) && defined(__EXCEPTIONS) && __EXCEPTIONS
// xlC defines __EXCEPTIONS to 1 if and only if exceptions are enabled. // xlC defines __EXCEPTIONS to 1 if and only if exceptions are enabled.
#define GTEST_HAS_EXCEPTIONS 1 #define GTEST_HAS_EXCEPTIONS 1
#elif defined(__HP_aCC) #elif defined(__HP_aCC)
@ -472,11 +499,15 @@ typedef struct _RTL_CRITICAL_SECTION GTEST_CRITICAL_SECTION;
// Cygwin 1.7 and below doesn't support ::std::wstring. // Cygwin 1.7 and below doesn't support ::std::wstring.
// Solaris' libc++ doesn't support it either. Android has // Solaris' libc++ doesn't support it either. Android has
// no support for it at least as recent as Froyo (2.2). // no support for it at least as recent as Froyo (2.2).
#define GTEST_HAS_STD_WSTRING \ #if (!(defined(GTEST_OS_LINUX_ANDROID) || defined(GTEST_OS_CYGWIN) || \
(!(GTEST_OS_LINUX_ANDROID || GTEST_OS_CYGWIN || GTEST_OS_SOLARIS || \ defined(GTEST_OS_SOLARIS) || defined(GTEST_OS_HAIKU) || \
GTEST_OS_HAIKU || GTEST_OS_ESP32 || GTEST_OS_ESP8266 || \ defined(GTEST_OS_ESP32) || defined(GTEST_OS_ESP8266) || \
GTEST_OS_XTENSA || GTEST_OS_QURT)) defined(GTEST_OS_XTENSA) || defined(GTEST_OS_QURT) || \
defined(GTEST_OS_NXP_QN9090) || defined(GTEST_OS_NRF52)))
#define GTEST_HAS_STD_WSTRING 1
#else
#define GTEST_HAS_STD_WSTRING 0
#endif
#endif // GTEST_HAS_STD_WSTRING #endif // GTEST_HAS_STD_WSTRING
#ifndef GTEST_HAS_FILE_SYSTEM #ifndef GTEST_HAS_FILE_SYSTEM
@ -506,7 +537,8 @@ typedef struct _RTL_CRITICAL_SECTION GTEST_CRITICAL_SECTION;
// -frtti -fno-exceptions, the build fails at link time with undefined // -frtti -fno-exceptions, the build fails at link time with undefined
// references to __cxa_bad_typeid. Note sure if STL or toolchain bug, // references to __cxa_bad_typeid. Note sure if STL or toolchain bug,
// so disable RTTI when detected. // so disable RTTI when detected.
#if GTEST_OS_LINUX_ANDROID && defined(_STLPORT_MAJOR) && !defined(__EXCEPTIONS) #if defined(GTEST_OS_LINUX_ANDROID) && defined(_STLPORT_MAJOR) && \
!defined(__EXCEPTIONS)
#define GTEST_HAS_RTTI 0 #define GTEST_HAS_RTTI 0
#else #else
#define GTEST_HAS_RTTI 1 #define GTEST_HAS_RTTI 1
@ -554,11 +586,18 @@ typedef struct _RTL_CRITICAL_SECTION GTEST_CRITICAL_SECTION;
// //
// To disable threading support in Google Test, add -DGTEST_HAS_PTHREAD=0 // To disable threading support in Google Test, add -DGTEST_HAS_PTHREAD=0
// to your compiler flags. // to your compiler flags.
#define GTEST_HAS_PTHREAD \ #if (defined(GTEST_OS_LINUX) || defined(GTEST_OS_MAC) || \
(GTEST_OS_LINUX || GTEST_OS_MAC || GTEST_OS_HPUX || GTEST_OS_QNX || \ defined(GTEST_OS_HPUX) || defined(GTEST_OS_QNX) || \
GTEST_OS_FREEBSD || GTEST_OS_NACL || GTEST_OS_NETBSD || GTEST_OS_FUCHSIA || \ defined(GTEST_OS_FREEBSD) || defined(GTEST_OS_NACL) || \
GTEST_OS_DRAGONFLY || GTEST_OS_GNU_KFREEBSD || GTEST_OS_OPENBSD || \ defined(GTEST_OS_NETBSD) || defined(GTEST_OS_FUCHSIA) || \
GTEST_OS_HAIKU || GTEST_OS_GNU_HURD) defined(GTEST_OS_DRAGONFLY) || defined(GTEST_OS_GNU_KFREEBSD) || \
defined(GTEST_OS_OPENBSD) || defined(GTEST_OS_HAIKU) || \
defined(GTEST_OS_GNU_HURD) || defined(GTEST_OS_SOLARIS) || \
defined(GTEST_OS_AIX) || defined(GTEST_OS_ZOS))
#define GTEST_HAS_PTHREAD 1
#else
#define GTEST_HAS_PTHREAD 0
#endif
#endif // GTEST_HAS_PTHREAD #endif // GTEST_HAS_PTHREAD
#if GTEST_HAS_PTHREAD #if GTEST_HAS_PTHREAD
@ -573,12 +612,12 @@ typedef struct _RTL_CRITICAL_SECTION GTEST_CRITICAL_SECTION;
// Determines whether clone(2) is supported. // Determines whether clone(2) is supported.
// Usually it will only be available on Linux, excluding // Usually it will only be available on Linux, excluding
// Linux on the Itanium architecture. // Linux on the Itanium architecture.
// Also see http://linux.die.net/man/2/clone. // Also see https://linux.die.net/man/2/clone.
#ifndef GTEST_HAS_CLONE #ifndef GTEST_HAS_CLONE
// The user didn't tell us, so we need to figure it out. // The user didn't tell us, so we need to figure it out.
#if GTEST_OS_LINUX && !defined(__ia64__) #if defined(GTEST_OS_LINUX) && !defined(__ia64__)
#if GTEST_OS_LINUX_ANDROID #if defined(GTEST_OS_LINUX_ANDROID)
// On Android, clone() became available at different API levels for each 32-bit // On Android, clone() became available at different API levels for each 32-bit
// architecture. // architecture.
#if defined(__LP64__) || (defined(__arm__) && __ANDROID_API__ >= 9) || \ #if defined(__LP64__) || (defined(__arm__) && __ANDROID_API__ >= 9) || \
@ -603,9 +642,10 @@ typedef struct _RTL_CRITICAL_SECTION GTEST_CRITICAL_SECTION;
// By default, we assume that stream redirection is supported on all // By default, we assume that stream redirection is supported on all
// platforms except known mobile / embedded ones. Also, if the port doesn't have // platforms except known mobile / embedded ones. Also, if the port doesn't have
// a file system, stream redirection is not supported. // a file system, stream redirection is not supported.
#if GTEST_OS_WINDOWS_MOBILE || GTEST_OS_WINDOWS_PHONE || \ #if defined(GTEST_OS_WINDOWS_MOBILE) || defined(GTEST_OS_WINDOWS_PHONE) || \
GTEST_OS_WINDOWS_RT || GTEST_OS_ESP8266 || GTEST_OS_XTENSA || \ defined(GTEST_OS_WINDOWS_RT) || defined(GTEST_OS_WINDOWS_GAMES) || \
GTEST_OS_QURT || !GTEST_HAS_FILE_SYSTEM defined(GTEST_OS_ESP8266) || defined(GTEST_OS_XTENSA) || \
defined(GTEST_OS_QURT) || !GTEST_HAS_FILE_SYSTEM
#define GTEST_HAS_STREAM_REDIRECTION 0 #define GTEST_HAS_STREAM_REDIRECTION 0
#else #else
#define GTEST_HAS_STREAM_REDIRECTION 1 #define GTEST_HAS_STREAM_REDIRECTION 1
@ -614,13 +654,16 @@ typedef struct _RTL_CRITICAL_SECTION GTEST_CRITICAL_SECTION;
// Determines whether to support death tests. // Determines whether to support death tests.
// pops up a dialog window that cannot be suppressed programmatically. // pops up a dialog window that cannot be suppressed programmatically.
#if (GTEST_OS_LINUX || GTEST_OS_CYGWIN || GTEST_OS_SOLARIS || \ #if (defined(GTEST_OS_LINUX) || defined(GTEST_OS_CYGWIN) || \
(GTEST_OS_MAC && !GTEST_OS_IOS) || \ defined(GTEST_OS_SOLARIS) || defined(GTEST_OS_ZOS) || \
(GTEST_OS_WINDOWS_DESKTOP && _MSC_VER) || GTEST_OS_WINDOWS_MINGW || \ (defined(GTEST_OS_MAC) && !defined(GTEST_OS_IOS)) || \
GTEST_OS_AIX || GTEST_OS_HPUX || GTEST_OS_OPENBSD || GTEST_OS_QNX || \ (defined(GTEST_OS_WINDOWS_DESKTOP) && _MSC_VER) || \
GTEST_OS_FREEBSD || GTEST_OS_NETBSD || GTEST_OS_FUCHSIA || \ defined(GTEST_OS_WINDOWS_MINGW) || defined(GTEST_OS_AIX) || \
GTEST_OS_DRAGONFLY || GTEST_OS_GNU_KFREEBSD || GTEST_OS_HAIKU || \ defined(GTEST_OS_HPUX) || defined(GTEST_OS_OPENBSD) || \
GTEST_OS_GNU_HURD) defined(GTEST_OS_QNX) || defined(GTEST_OS_FREEBSD) || \
defined(GTEST_OS_NETBSD) || defined(GTEST_OS_FUCHSIA) || \
defined(GTEST_OS_DRAGONFLY) || defined(GTEST_OS_GNU_KFREEBSD) || \
defined(GTEST_OS_HAIKU) || defined(GTEST_OS_GNU_HURD))
// Death tests require a file system to work properly. // Death tests require a file system to work properly.
#if GTEST_HAS_FILE_SYSTEM #if GTEST_HAS_FILE_SYSTEM
#define GTEST_HAS_DEATH_TEST 1 #define GTEST_HAS_DEATH_TEST 1
@ -638,14 +681,21 @@ typedef struct _RTL_CRITICAL_SECTION GTEST_CRITICAL_SECTION;
#endif #endif
// Determines whether the system compiler uses UTF-16 for encoding wide strings. // Determines whether the system compiler uses UTF-16 for encoding wide strings.
#define GTEST_WIDE_STRING_USES_UTF16_ \ #if defined(GTEST_OS_WINDOWS) || defined(GTEST_OS_CYGWIN) || \
(GTEST_OS_WINDOWS || GTEST_OS_CYGWIN || GTEST_OS_AIX || GTEST_OS_OS2) defined(GTEST_OS_AIX) || defined(GTEST_OS_OS2)
#define GTEST_WIDE_STRING_USES_UTF16_ 1
#else
#define GTEST_WIDE_STRING_USES_UTF16_ 0
#endif
// Determines whether test results can be streamed to a socket. // Determines whether test results can be streamed to a socket.
#if GTEST_OS_LINUX || GTEST_OS_GNU_KFREEBSD || GTEST_OS_DRAGONFLY || \ #if defined(GTEST_OS_LINUX) || defined(GTEST_OS_GNU_KFREEBSD) || \
GTEST_OS_FREEBSD || GTEST_OS_NETBSD || GTEST_OS_OPENBSD || \ defined(GTEST_OS_DRAGONFLY) || defined(GTEST_OS_FREEBSD) || \
GTEST_OS_GNU_HURD defined(GTEST_OS_NETBSD) || defined(GTEST_OS_OPENBSD) || \
defined(GTEST_OS_GNU_HURD) || defined(GTEST_OS_MAC)
#define GTEST_CAN_STREAM_RESULTS_ 1 #define GTEST_CAN_STREAM_RESULTS_ 1
#else
#define GTEST_CAN_STREAM_RESULTS_ 0
#endif #endif
// Defines some utility macros. // Defines some utility macros.
@ -683,6 +733,20 @@ typedef struct _RTL_CRITICAL_SECTION GTEST_CRITICAL_SECTION;
#define GTEST_HAVE_ATTRIBUTE_(x) 0 #define GTEST_HAVE_ATTRIBUTE_(x) 0
#endif #endif
// GTEST_INTERNAL_HAVE_CPP_ATTRIBUTE
//
// A function-like feature checking macro that accepts C++11 style attributes.
// It's a wrapper around `__has_cpp_attribute`, defined by ISO C++ SD-6
// (https://en.cppreference.com/w/cpp/experimental/feature_test). If we don't
// find `__has_cpp_attribute`, will evaluate to 0.
#if defined(__has_cpp_attribute)
// NOTE: requiring __cplusplus above should not be necessary, but
// works around https://bugs.llvm.org/show_bug.cgi?id=23435.
#define GTEST_INTERNAL_HAVE_CPP_ATTRIBUTE(x) __has_cpp_attribute(x)
#else
#define GTEST_INTERNAL_HAVE_CPP_ATTRIBUTE(x) 0
#endif
// GTEST_HAVE_FEATURE_ // GTEST_HAVE_FEATURE_
// //
// A function-like feature checking macro that is a wrapper around // A function-like feature checking macro that is a wrapper around
@ -693,17 +757,6 @@ typedef struct _RTL_CRITICAL_SECTION GTEST_CRITICAL_SECTION;
#define GTEST_HAVE_FEATURE_(x) 0 #define GTEST_HAVE_FEATURE_(x) 0
#endif #endif
// Use this annotation after a variable or parameter declaration to tell the
// compiler the variable/parameter does not have to be used.
// Example:
//
// GTEST_ATTRIBUTE_UNUSED_ int foo = bar();
#if GTEST_HAVE_ATTRIBUTE_(unused)
#define GTEST_ATTRIBUTE_UNUSED_ __attribute__((unused))
#else
#define GTEST_ATTRIBUTE_UNUSED_
#endif
// Use this annotation before a function that takes a printf format string. // Use this annotation before a function that takes a printf format string.
#if GTEST_HAVE_ATTRIBUTE_(format) && defined(__MINGW_PRINTF_FORMAT) #if GTEST_HAVE_ATTRIBUTE_(format) && defined(__MINGW_PRINTF_FORMAT)
// MinGW has two different printf implementations. Ensure the format macro // MinGW has two different printf implementations. Ensure the format macro
@ -718,17 +771,6 @@ typedef struct _RTL_CRITICAL_SECTION GTEST_CRITICAL_SECTION;
#define GTEST_ATTRIBUTE_PRINTF_(string_index, first_to_check) #define GTEST_ATTRIBUTE_PRINTF_(string_index, first_to_check)
#endif #endif
// Tell the compiler to warn about unused return values for functions declared
// with this macro. The macro should be used on function declarations
// following the argument list:
//
// Sprocket* AllocateSprocket() GTEST_MUST_USE_RESULT_;
#if GTEST_HAVE_ATTRIBUTE_(warn_unused_result)
#define GTEST_MUST_USE_RESULT_ __attribute__((warn_unused_result))
#else
#define GTEST_MUST_USE_RESULT_
#endif
// MS C++ compiler emits warning when a conditional expression is compile time // MS C++ compiler emits warning when a conditional expression is compile time
// constant. In some contexts this warning is false positive and needs to be // constant. In some contexts this warning is false positive and needs to be
// suppressed. Use the following two macros in such cases: // suppressed. Use the following two macros in such cases:
@ -759,14 +801,16 @@ typedef struct _RTL_CRITICAL_SECTION GTEST_CRITICAL_SECTION;
#ifndef GTEST_IS_THREADSAFE #ifndef GTEST_IS_THREADSAFE
#define GTEST_IS_THREADSAFE \ #if (GTEST_HAS_MUTEX_AND_THREAD_LOCAL_ || \
(GTEST_HAS_MUTEX_AND_THREAD_LOCAL_ || \ (defined(GTEST_OS_WINDOWS) && !defined(GTEST_OS_WINDOWS_PHONE) && \
(GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_PHONE && !GTEST_OS_WINDOWS_RT) || \ !defined(GTEST_OS_WINDOWS_RT)) || \
GTEST_HAS_PTHREAD) GTEST_HAS_PTHREAD)
#define GTEST_IS_THREADSAFE 1
#endif
#endif // GTEST_IS_THREADSAFE #endif // GTEST_IS_THREADSAFE
#if GTEST_IS_THREADSAFE #ifdef GTEST_IS_THREADSAFE
// Some platforms don't support including these threading related headers. // Some platforms don't support including these threading related headers.
#include <condition_variable> // NOLINT #include <condition_variable> // NOLINT
#include <mutex> // NOLINT #include <mutex> // NOLINT
@ -778,9 +822,9 @@ typedef struct _RTL_CRITICAL_SECTION GTEST_CRITICAL_SECTION;
#ifndef GTEST_API_ #ifndef GTEST_API_
#ifdef _MSC_VER #ifdef _MSC_VER
#if GTEST_LINKED_AS_SHARED_LIBRARY #if defined(GTEST_LINKED_AS_SHARED_LIBRARY) && GTEST_LINKED_AS_SHARED_LIBRARY
#define GTEST_API_ __declspec(dllimport) #define GTEST_API_ __declspec(dllimport)
#elif GTEST_CREATE_SHARED_LIBRARY #elif defined(GTEST_CREATE_SHARED_LIBRARY) && GTEST_CREATE_SHARED_LIBRARY
#define GTEST_API_ __declspec(dllexport) #define GTEST_API_ __declspec(dllexport)
#endif #endif
#elif GTEST_HAVE_ATTRIBUTE_(visibility) #elif GTEST_HAVE_ATTRIBUTE_(visibility)
@ -808,7 +852,7 @@ typedef struct _RTL_CRITICAL_SECTION GTEST_CRITICAL_SECTION;
// Ask the compiler not to perform tail call optimization inside // Ask the compiler not to perform tail call optimization inside
// the marked function. // the marked function.
#define GTEST_NO_TAIL_CALL_ __attribute__((disable_tail_calls)) #define GTEST_NO_TAIL_CALL_ __attribute__((disable_tail_calls))
#elif __GNUC__ #elif defined(__GNUC__) && !defined(__NVCOMPILER)
#define GTEST_NO_TAIL_CALL_ \ #define GTEST_NO_TAIL_CALL_ \
__attribute__((optimize("no-optimize-sibling-calls"))) __attribute__((optimize("no-optimize-sibling-calls")))
#else #else
@ -871,9 +915,11 @@ using std::tuple_size;
namespace internal { namespace internal {
// A secret type that Google Test users don't know about. It has no // A secret type that Google Test users don't know about. It has no
// definition on purpose. Therefore it's impossible to create a // accessible constructors on purpose. Therefore it's impossible to create a
// Secret object, which is what we want. // Secret object, which is what we want.
class Secret; class Secret {
Secret(const Secret&) = delete;
};
// A helper for suppressing warnings on constant condition. It just // A helper for suppressing warnings on constant condition. It just
// returns 'condition'. // returns 'condition'.
@ -881,7 +927,7 @@ GTEST_API_ bool IsTrue(bool condition);
// Defines RE. // Defines RE.
#if GTEST_USES_RE2 #ifdef GTEST_USES_RE2
// This is almost `using RE = ::RE2`, except it is copy-constructible, and it // This is almost `using RE = ::RE2`, except it is copy-constructible, and it
// needs to disambiguate the `std::string`, `absl::string_view`, and `const // needs to disambiguate the `std::string`, `absl::string_view`, and `const
@ -906,7 +952,9 @@ class GTEST_API_ RE {
RE2 regex_; RE2 regex_;
}; };
#elif GTEST_USES_POSIX_RE || GTEST_USES_SIMPLE_RE #elif defined(GTEST_USES_POSIX_RE) || defined(GTEST_USES_SIMPLE_RE)
GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \
/* class A needs to have dll-interface to be used by clients of class B */)
// A simple C++ wrapper for <regex.h>. It uses the POSIX Extended // A simple C++ wrapper for <regex.h>. It uses the POSIX Extended
// Regular Expression syntax. // Regular Expression syntax.
@ -923,7 +971,7 @@ class GTEST_API_ RE {
~RE(); ~RE();
// Returns the string representation of the regex. // Returns the string representation of the regex.
const char* pattern() const { return pattern_; } const char* pattern() const { return pattern_.c_str(); }
// FullMatch(str, re) returns true if and only if regular expression re // FullMatch(str, re) returns true if and only if regular expression re
// matches the entire str. // matches the entire str.
@ -941,21 +989,21 @@ class GTEST_API_ RE {
private: private:
void Init(const char* regex); void Init(const char* regex);
const char* pattern_; std::string pattern_;
bool is_valid_; bool is_valid_;
#if GTEST_USES_POSIX_RE #ifdef GTEST_USES_POSIX_RE
regex_t full_regex_; // For FullMatch(). regex_t full_regex_; // For FullMatch().
regex_t partial_regex_; // For PartialMatch(). regex_t partial_regex_; // For PartialMatch().
#else // GTEST_USES_SIMPLE_RE #else // GTEST_USES_SIMPLE_RE
const char* full_pattern_; // For FullMatch(); std::string full_pattern_; // For FullMatch();
#endif #endif
}; };
GTEST_DISABLE_MSC_WARNINGS_POP_() // 4251
#endif // ::testing::internal::RE implementation #endif // ::testing::internal::RE implementation
// Formats a source file path and a line number as they would appear // Formats a source file path and a line number as they would appear
@ -1088,47 +1136,6 @@ inline To ImplicitCast_(To x) {
return x; return x;
} }
// When you upcast (that is, cast a pointer from type Foo to type
// SuperclassOfFoo), it's fine to use ImplicitCast_<>, since upcasts
// always succeed. When you downcast (that is, cast a pointer from
// type Foo to type SubclassOfFoo), static_cast<> isn't safe, because
// how do you know the pointer is really of type SubclassOfFoo? It
// could be a bare Foo, or of type DifferentSubclassOfFoo. Thus,
// when you downcast, you should use this macro. In debug mode, we
// use dynamic_cast<> to double-check the downcast is legal (we die
// if it's not). In normal mode, we do the efficient static_cast<>
// instead. Thus, it's important to test in debug mode to make sure
// the cast is legal!
// This is the only place in the code we should use dynamic_cast<>.
// In particular, you SHOULDN'T be using dynamic_cast<> in order to
// do RTTI (eg code like this:
// if (dynamic_cast<Subclass1>(foo)) HandleASubclass1Object(foo);
// if (dynamic_cast<Subclass2>(foo)) HandleASubclass2Object(foo);
// You should design the code some other way not to need this.
//
// This relatively ugly name is intentional. It prevents clashes with
// similar functions users may have (e.g., down_cast). The internal
// namespace alone is not enough because the function can be found by ADL.
template <typename To, typename From> // use like this: DownCast_<T*>(foo);
inline To DownCast_(From* f) { // so we only accept pointers
// Ensures that To is a sub-type of From *. This test is here only
// for compile-time type checking, and has no overhead in an
// optimized build at run-time, as it will be optimized away
// completely.
GTEST_INTENTIONAL_CONST_COND_PUSH_()
if (false) {
GTEST_INTENTIONAL_CONST_COND_POP_()
const To to = nullptr;
::testing::internal::ImplicitCast_<From*>(to);
}
#if GTEST_HAS_RTTI
// RTTI: debug mode only!
GTEST_CHECK_(f == nullptr || dynamic_cast<To>(f) != nullptr);
#endif
return static_cast<To>(f);
}
// Downcasts the pointer of type Base to Derived. // Downcasts the pointer of type Base to Derived.
// Derived must be a subclass of Base. The parameter MUST // Derived must be a subclass of Base. The parameter MUST
// point to a class of type Derived, not any subclass of it. // point to a class of type Derived, not any subclass of it.
@ -1136,17 +1143,12 @@ inline To DownCast_(From* f) { // so we only accept pointers
// check to enforce this. // check to enforce this.
template <class Derived, class Base> template <class Derived, class Base>
Derived* CheckedDowncastToActualType(Base* base) { Derived* CheckedDowncastToActualType(Base* base) {
static_assert(std::is_base_of<Base, Derived>::value,
"target type not derived from source type");
#if GTEST_HAS_RTTI #if GTEST_HAS_RTTI
GTEST_CHECK_(typeid(*base) == typeid(Derived)); GTEST_CHECK_(base == nullptr || dynamic_cast<Derived*>(base) != nullptr);
#endif
#if GTEST_HAS_DOWNCAST_
return ::down_cast<Derived*>(base);
#elif GTEST_HAS_RTTI
return dynamic_cast<Derived*>(base); // NOLINT
#else
return static_cast<Derived*>(base); // Poor man's downcast.
#endif #endif
return static_cast<Derived*>(base);
} }
#if GTEST_HAS_STREAM_REDIRECTION #if GTEST_HAS_STREAM_REDIRECTION
@ -1172,7 +1174,7 @@ GTEST_API_ std::string ReadEntireFile(FILE* file);
// All command line arguments. // All command line arguments.
GTEST_API_ std::vector<std::string> GetArgvs(); GTEST_API_ std::vector<std::string> GetArgvs();
#if GTEST_HAS_DEATH_TEST #ifdef GTEST_HAS_DEATH_TEST
std::vector<std::string> GetInjectableArgvs(); std::vector<std::string> GetInjectableArgvs();
// Deprecated: pass the args vector by value instead. // Deprecated: pass the args vector by value instead.
@ -1183,9 +1185,9 @@ void ClearInjectableArgvs();
#endif // GTEST_HAS_DEATH_TEST #endif // GTEST_HAS_DEATH_TEST
// Defines synchronization primitives. // Defines synchronization primitives.
#if GTEST_IS_THREADSAFE #ifdef GTEST_IS_THREADSAFE
#if GTEST_OS_WINDOWS #ifdef GTEST_OS_WINDOWS
// Provides leak-safe Windows kernel handle ownership. // Provides leak-safe Windows kernel handle ownership.
// Used in death tests and in threading support. // Used in death tests and in threading support.
class GTEST_API_ AutoHandle { class GTEST_API_ AutoHandle {
@ -1264,7 +1266,7 @@ GTEST_DISABLE_MSC_WARNINGS_POP_() // 4251
// On MinGW, we can have both GTEST_OS_WINDOWS and GTEST_HAS_PTHREAD // On MinGW, we can have both GTEST_OS_WINDOWS and GTEST_HAS_PTHREAD
// defined, but we don't want to use MinGW's pthreads implementation, which // defined, but we don't want to use MinGW's pthreads implementation, which
// has conformance problems with some versions of the POSIX standard. // has conformance problems with some versions of the POSIX standard.
#if GTEST_HAS_PTHREAD && !GTEST_OS_WINDOWS_MINGW #if GTEST_HAS_PTHREAD && !defined(GTEST_OS_WINDOWS_MINGW)
// As a C-function, ThreadFuncWithCLinkage cannot be templated itself. // As a C-function, ThreadFuncWithCLinkage cannot be templated itself.
// Consequently, it cannot select a correct instantiation of ThreadWithParam // Consequently, it cannot select a correct instantiation of ThreadWithParam
@ -1273,7 +1275,7 @@ GTEST_DISABLE_MSC_WARNINGS_POP_() // 4251
// problem. // problem.
class ThreadWithParamBase { class ThreadWithParamBase {
public: public:
virtual ~ThreadWithParamBase() {} virtual ~ThreadWithParamBase() = default;
virtual void Run() = 0; virtual void Run() = 0;
}; };
@ -1350,7 +1352,8 @@ class ThreadWithParam : public ThreadWithParamBase {
// Mutex and ThreadLocal have already been imported into the namespace. // Mutex and ThreadLocal have already been imported into the namespace.
// Nothing to do here. // Nothing to do here.
#elif GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_PHONE && !GTEST_OS_WINDOWS_RT #elif defined(GTEST_OS_WINDOWS) && !defined(GTEST_OS_WINDOWS_PHONE) && \
!defined(GTEST_OS_WINDOWS_RT)
// Mutex implements mutex on Windows platforms. It is used in conjunction // Mutex implements mutex on Windows platforms. It is used in conjunction
// with class MutexLock: // with class MutexLock:
@ -1734,7 +1737,7 @@ typedef GTestMutexLock MutexLock;
// ThreadLocalValueHolderBase. // ThreadLocalValueHolderBase.
class GTEST_API_ ThreadLocalValueHolderBase { class GTEST_API_ ThreadLocalValueHolderBase {
public: public:
virtual ~ThreadLocalValueHolderBase() {} virtual ~ThreadLocalValueHolderBase() = default;
}; };
// Called by pthread to delete thread-local data stored by // Called by pthread to delete thread-local data stored by
@ -1806,8 +1809,8 @@ class GTEST_API_ ThreadLocal {
class ValueHolderFactory { class ValueHolderFactory {
public: public:
ValueHolderFactory() {} ValueHolderFactory() = default;
virtual ~ValueHolderFactory() {} virtual ~ValueHolderFactory() = default;
virtual ValueHolder* MakeNewHolder() const = 0; virtual ValueHolder* MakeNewHolder() const = 0;
private: private:
@ -1817,7 +1820,7 @@ class GTEST_API_ ThreadLocal {
class DefaultValueHolderFactory : public ValueHolderFactory { class DefaultValueHolderFactory : public ValueHolderFactory {
public: public:
DefaultValueHolderFactory() {} DefaultValueHolderFactory() = default;
ValueHolder* MakeNewHolder() const override { return new ValueHolder(); } ValueHolder* MakeNewHolder() const override { return new ValueHolder(); }
private: private:
@ -1903,7 +1906,7 @@ class GTEST_API_ ThreadLocal {
// we cannot detect it. // we cannot detect it.
GTEST_API_ size_t GetThreadCount(); GTEST_API_ size_t GetThreadCount();
#if GTEST_OS_WINDOWS #ifdef GTEST_OS_WINDOWS
#define GTEST_PATH_SEP_ "\\" #define GTEST_PATH_SEP_ "\\"
#define GTEST_HAS_ALT_PATH_SEP_ 1 #define GTEST_HAS_ALT_PATH_SEP_ 1
#else #else
@ -1939,7 +1942,7 @@ inline bool IsUpper(char ch) {
inline bool IsXDigit(char ch) { inline bool IsXDigit(char ch) {
return isxdigit(static_cast<unsigned char>(ch)) != 0; return isxdigit(static_cast<unsigned char>(ch)) != 0;
} }
#ifdef __cpp_char8_t #ifdef __cpp_lib_char8_t
inline bool IsXDigit(char8_t ch) { inline bool IsXDigit(char8_t ch) {
return isxdigit(static_cast<unsigned char>(ch)) != 0; return isxdigit(static_cast<unsigned char>(ch)) != 0;
} }
@ -1979,63 +1982,70 @@ inline std::string StripTrailingSpaces(std::string str) {
namespace posix { namespace posix {
// File system porting. // File system porting.
#if GTEST_HAS_FILE_SYSTEM // Note: Not every I/O-related function is related to file systems, so don't
#if GTEST_OS_WINDOWS // just disable all of them here. For example, fileno() and isatty(), etc. must
// always be available in order to detect if a pipe points to a terminal.
#ifdef GTEST_OS_WINDOWS
typedef struct _stat StatStruct; typedef struct _stat StatStruct;
#if GTEST_OS_WINDOWS_MOBILE #ifdef GTEST_OS_WINDOWS_MOBILE
inline int FileNo(FILE* file) { return reinterpret_cast<int>(_fileno(file)); } inline int FileNo(FILE* file) { return reinterpret_cast<int>(_fileno(file)); }
// Stat(), RmDir(), and IsDir() are not needed on Windows CE at this // Stat(), RmDir(), and IsDir() are not needed on Windows CE at this
// time and thus not defined there. // time and thus not defined there.
#else #else
inline int FileNo(FILE* file) { return _fileno(file); } inline int FileNo(FILE* file) { return _fileno(file); }
#if GTEST_HAS_FILE_SYSTEM
inline int Stat(const char* path, StatStruct* buf) { return _stat(path, buf); } inline int Stat(const char* path, StatStruct* buf) { return _stat(path, buf); }
inline int RmDir(const char* dir) { return _rmdir(dir); } inline int RmDir(const char* dir) { return _rmdir(dir); }
inline bool IsDir(const StatStruct& st) { return (_S_IFDIR & st.st_mode) != 0; } inline bool IsDir(const StatStruct& st) { return (_S_IFDIR & st.st_mode) != 0; }
#endif
#endif // GTEST_OS_WINDOWS_MOBILE #endif // GTEST_OS_WINDOWS_MOBILE
#elif GTEST_OS_ESP8266 #elif defined(GTEST_OS_ESP8266)
typedef struct stat StatStruct; typedef struct stat StatStruct;
inline int FileNo(FILE* file) { return fileno(file); } inline int FileNo(FILE* file) { return fileno(file); }
#if GTEST_HAS_FILE_SYSTEM
inline int Stat(const char* path, StatStruct* buf) { inline int Stat(const char* path, StatStruct* buf) {
// stat function not implemented on ESP8266 // stat function not implemented on ESP8266
return 0; return 0;
} }
inline int RmDir(const char* dir) { return rmdir(dir); } inline int RmDir(const char* dir) { return rmdir(dir); }
inline bool IsDir(const StatStruct& st) { return S_ISDIR(st.st_mode); } inline bool IsDir(const StatStruct& st) { return S_ISDIR(st.st_mode); }
#endif
#else #else
typedef struct stat StatStruct; typedef struct stat StatStruct;
inline int FileNo(FILE* file) { return fileno(file); } inline int FileNo(FILE* file) { return fileno(file); }
#if GTEST_HAS_FILE_SYSTEM
inline int Stat(const char* path, StatStruct* buf) { return stat(path, buf); } inline int Stat(const char* path, StatStruct* buf) { return stat(path, buf); }
#if GTEST_OS_QURT #ifdef GTEST_OS_QURT
// QuRT doesn't support any directory functions, including rmdir // QuRT doesn't support any directory functions, including rmdir
inline int RmDir(const char*) { return 0; } inline int RmDir(const char*) { return 0; }
#else #else
inline int RmDir(const char* dir) { return rmdir(dir); } inline int RmDir(const char* dir) { return rmdir(dir); }
#endif #endif
inline bool IsDir(const StatStruct& st) { return S_ISDIR(st.st_mode); } inline bool IsDir(const StatStruct& st) { return S_ISDIR(st.st_mode); }
#endif
#endif // GTEST_OS_WINDOWS #endif // GTEST_OS_WINDOWS
#endif // GTEST_HAS_FILE_SYSTEM
// Other functions with a different name on Windows. // Other functions with a different name on Windows.
#if GTEST_OS_WINDOWS #ifdef GTEST_OS_WINDOWS
#ifdef __BORLANDC__ #ifdef __BORLANDC__
inline int DoIsATTY(int fd) { return isatty(fd); } inline int DoIsATTY(int fd) { return isatty(fd); }
inline int StrCaseCmp(const char* s1, const char* s2) { inline int StrCaseCmp(const char* s1, const char* s2) {
return stricmp(s1, s2); return stricmp(s1, s2);
} }
inline char* StrDup(const char* src) { return strdup(src); }
#else // !__BORLANDC__ #else // !__BORLANDC__
#if GTEST_OS_WINDOWS_MOBILE || GTEST_OS_ZOS || GTEST_OS_IOS || \ #if defined(GTEST_OS_WINDOWS_MOBILE) || defined(GTEST_OS_ZOS) || \
GTEST_OS_WINDOWS_PHONE || GTEST_OS_WINDOWS_RT || defined(ESP_PLATFORM) defined(GTEST_OS_IOS) || defined(GTEST_OS_WINDOWS_PHONE) || \
defined(GTEST_OS_WINDOWS_RT) || defined(ESP_PLATFORM)
inline int DoIsATTY(int /* fd */) { return 0; } inline int DoIsATTY(int /* fd */) { return 0; }
#else #else
inline int DoIsATTY(int fd) { return _isatty(fd); } inline int DoIsATTY(int fd) { return _isatty(fd); }
@ -2043,24 +2053,14 @@ inline int DoIsATTY(int fd) { return _isatty(fd); }
inline int StrCaseCmp(const char* s1, const char* s2) { inline int StrCaseCmp(const char* s1, const char* s2) {
return _stricmp(s1, s2); return _stricmp(s1, s2);
} }
inline char* StrDup(const char* src) { return _strdup(src); }
#endif // __BORLANDC__ #endif // __BORLANDC__
#elif GTEST_OS_ESP8266
inline int DoIsATTY(int fd) { return isatty(fd); }
inline int StrCaseCmp(const char* s1, const char* s2) {
return strcasecmp(s1, s2);
}
inline char* StrDup(const char* src) { return strdup(src); }
#else #else
inline int DoIsATTY(int fd) { return isatty(fd); } inline int DoIsATTY(int fd) { return isatty(fd); }
inline int StrCaseCmp(const char* s1, const char* s2) { inline int StrCaseCmp(const char* s1, const char* s2) {
return strcasecmp(s1, s2); return strcasecmp(s1, s2);
} }
inline char* StrDup(const char* src) { return strdup(src); }
#endif // GTEST_OS_WINDOWS #endif // GTEST_OS_WINDOWS
@ -2083,13 +2083,14 @@ GTEST_DISABLE_MSC_DEPRECATED_PUSH_()
// StrError() aren't needed on Windows CE at this time and thus not // StrError() aren't needed on Windows CE at this time and thus not
// defined there. // defined there.
#if GTEST_HAS_FILE_SYSTEM #if GTEST_HAS_FILE_SYSTEM
#if !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_WINDOWS_PHONE && \ #if !defined(GTEST_OS_WINDOWS_MOBILE) && !defined(GTEST_OS_WINDOWS_PHONE) && \
!GTEST_OS_WINDOWS_RT && !GTEST_OS_ESP8266 && !GTEST_OS_XTENSA && \ !defined(GTEST_OS_WINDOWS_RT) && !defined(GTEST_OS_WINDOWS_GAMES) && \
!GTEST_OS_QURT !defined(GTEST_OS_ESP8266) && !defined(GTEST_OS_XTENSA) && \
!defined(GTEST_OS_QURT)
inline int ChDir(const char* dir) { return chdir(dir); } inline int ChDir(const char* dir) { return chdir(dir); }
#endif #endif
inline FILE* FOpen(const char* path, const char* mode) { inline FILE* FOpen(const char* path, const char* mode) {
#if GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MINGW #if defined(GTEST_OS_WINDOWS) && !defined(GTEST_OS_WINDOWS_MINGW)
struct wchar_codecvt : public std::codecvt<wchar_t, char, std::mbstate_t> {}; struct wchar_codecvt : public std::codecvt<wchar_t, char, std::mbstate_t> {};
std::wstring_convert<wchar_codecvt> converter; std::wstring_convert<wchar_codecvt> converter;
std::wstring wide_path = converter.from_bytes(path); std::wstring wide_path = converter.from_bytes(path);
@ -2099,14 +2100,14 @@ inline FILE* FOpen(const char* path, const char* mode) {
return fopen(path, mode); return fopen(path, mode);
#endif // GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MINGW #endif // GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MINGW
} }
#if !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_QURT #if !defined(GTEST_OS_WINDOWS_MOBILE) && !defined(GTEST_OS_QURT)
inline FILE* FReopen(const char* path, const char* mode, FILE* stream) { inline FILE* FReopen(const char* path, const char* mode, FILE* stream) {
return freopen(path, mode, stream); return freopen(path, mode, stream);
} }
inline FILE* FDOpen(int fd, const char* mode) { return fdopen(fd, mode); } inline FILE* FDOpen(int fd, const char* mode) { return fdopen(fd, mode); }
#endif // !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_QURT #endif // !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_QURT
inline int FClose(FILE* fp) { return fclose(fp); } inline int FClose(FILE* fp) { return fclose(fp); }
#if !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_QURT #if !defined(GTEST_OS_WINDOWS_MOBILE) && !defined(GTEST_OS_QURT)
inline int Read(int fd, void* buf, unsigned int count) { inline int Read(int fd, void* buf, unsigned int count) {
return static_cast<int>(read(fd, buf, count)); return static_cast<int>(read(fd, buf, count));
} }
@ -2117,14 +2118,14 @@ inline int Close(int fd) { return close(fd); }
#endif // !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_QURT #endif // !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_QURT
#endif // GTEST_HAS_FILE_SYSTEM #endif // GTEST_HAS_FILE_SYSTEM
#if !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_QURT #if !defined(GTEST_OS_WINDOWS_MOBILE) && !defined(GTEST_OS_QURT)
inline const char* StrError(int errnum) { return strerror(errnum); } inline const char* StrError(int errnum) { return strerror(errnum); }
#endif // !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_QURT #endif // !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_QURT
inline const char* GetEnv(const char* name) { inline const char* GetEnv(const char* name) {
#if GTEST_OS_WINDOWS_MOBILE || GTEST_OS_WINDOWS_PHONE || \ #if defined(GTEST_OS_WINDOWS_MOBILE) || defined(GTEST_OS_WINDOWS_PHONE) || \
GTEST_OS_WINDOWS_RT || GTEST_OS_ESP8266 || GTEST_OS_XTENSA || \ defined(GTEST_OS_ESP8266) || defined(GTEST_OS_XTENSA) || \
GTEST_OS_QURT defined(GTEST_OS_QURT)
// We are on an embedded platform, which has no environment variables. // We are on an embedded platform, which has no environment variables.
static_cast<void>(name); // To prevent 'unused argument' warning. static_cast<void>(name); // To prevent 'unused argument' warning.
return nullptr; return nullptr;
@ -2140,7 +2141,7 @@ inline const char* GetEnv(const char* name) {
GTEST_DISABLE_MSC_DEPRECATED_POP_() GTEST_DISABLE_MSC_DEPRECATED_POP_()
#if GTEST_OS_WINDOWS_MOBILE #ifdef GTEST_OS_WINDOWS_MOBILE
// Windows CE has no C library. The abort() function is used in // Windows CE has no C library. The abort() function is used in
// several places in Google Test. This implementation provides a reasonable // several places in Google Test. This implementation provides a reasonable
// imitation of standard behaviour. // imitation of standard behaviour.
@ -2156,7 +2157,7 @@ GTEST_DISABLE_MSC_DEPRECATED_POP_()
// MSVC-based platforms. We map the GTEST_SNPRINTF_ macro to the appropriate // MSVC-based platforms. We map the GTEST_SNPRINTF_ macro to the appropriate
// function in order to achieve that. We use macro definition here because // function in order to achieve that. We use macro definition here because
// snprintf is a variadic function. // snprintf is a variadic function.
#if defined(_MSC_VER) && !GTEST_OS_WINDOWS_MOBILE #if defined(_MSC_VER) && !defined(GTEST_OS_WINDOWS_MOBILE)
// MSVC 2005 and above support variadic macros. // MSVC 2005 and above support variadic macros.
#define GTEST_SNPRINTF_(buffer, size, format, ...) \ #define GTEST_SNPRINTF_(buffer, size, format, ...) \
_snprintf_s(buffer, size, size, format, __VA_ARGS__) _snprintf_s(buffer, size, size, format, __VA_ARGS__)
@ -2229,7 +2230,7 @@ using TimeInMillis = int64_t; // Represents time in milliseconds.
#endif // !defined(GTEST_FLAG) #endif // !defined(GTEST_FLAG)
// Pick a command line flags implementation. // Pick a command line flags implementation.
#if GTEST_HAS_ABSL #ifdef GTEST_INTERNAL_HAS_ABSL_FLAGS
// Macros for defining flags. // Macros for defining flags.
#define GTEST_DEFINE_bool_(name, default_val, doc) \ #define GTEST_DEFINE_bool_(name, default_val, doc) \
@ -2254,7 +2255,8 @@ using TimeInMillis = int64_t; // Represents time in milliseconds.
(void)(::absl::SetFlag(&GTEST_FLAG(name), value)) (void)(::absl::SetFlag(&GTEST_FLAG(name), value))
#define GTEST_USE_OWN_FLAGFILE_FLAG_ 0 #define GTEST_USE_OWN_FLAGFILE_FLAG_ 0
#else // GTEST_HAS_ABSL #undef GTEST_INTERNAL_HAS_ABSL_FLAGS
#else // ndef GTEST_INTERNAL_HAS_ABSL_FLAGS
// Macros for defining flags. // Macros for defining flags.
#define GTEST_DEFINE_bool_(name, default_val, doc) \ #define GTEST_DEFINE_bool_(name, default_val, doc) \
@ -2296,7 +2298,7 @@ using TimeInMillis = int64_t; // Represents time in milliseconds.
#define GTEST_FLAG_SET(name, value) (void)(::testing::GTEST_FLAG(name) = value) #define GTEST_FLAG_SET(name, value) (void)(::testing::GTEST_FLAG(name) = value)
#define GTEST_USE_OWN_FLAGFILE_FLAG_ 1 #define GTEST_USE_OWN_FLAGFILE_FLAG_ 1
#endif // GTEST_HAS_ABSL #endif // GTEST_INTERNAL_HAS_ABSL_FLAGS
// Thread annotations // Thread annotations
#if !defined(GTEST_EXCLUSIVE_LOCK_REQUIRED_) #if !defined(GTEST_EXCLUSIVE_LOCK_REQUIRED_)
@ -2320,87 +2322,16 @@ const char* StringFromGTestEnv(const char* flag, const char* default_val);
} // namespace internal } // namespace internal
} // namespace testing } // namespace testing
#if !defined(GTEST_INTERNAL_DEPRECATED) #if defined(__cpp_lib_span) || (GTEST_INTERNAL_HAS_INCLUDE(<span>) && \
GTEST_INTERNAL_CPLUSPLUS_LANG >= 202002L)
#define GTEST_INTERNAL_HAS_STD_SPAN 1
#endif // __cpp_lib_span
// Internal Macro to mark an API deprecated, for googletest usage only #ifndef GTEST_INTERNAL_HAS_STD_SPAN
// Usage: class GTEST_INTERNAL_DEPRECATED(message) MyClass or #define GTEST_INTERNAL_HAS_STD_SPAN 0
// GTEST_INTERNAL_DEPRECATED(message) <return_type> myFunction(); Every usage of
// a deprecated entity will trigger a warning when compiled with
// `-Wdeprecated-declarations` option (clang, gcc, any __GNUC__ compiler).
// For msvc /W3 option will need to be used
// Note that for 'other' compilers this macro evaluates to nothing to prevent
// compilations errors.
#if defined(_MSC_VER)
#define GTEST_INTERNAL_DEPRECATED(message) __declspec(deprecated(message))
#elif defined(__GNUC__)
#define GTEST_INTERNAL_DEPRECATED(message) __attribute__((deprecated(message)))
#else
#define GTEST_INTERNAL_DEPRECATED(message)
#endif #endif
#endif // !defined(GTEST_INTERNAL_DEPRECATED) #ifdef GTEST_HAS_ABSL
#if GTEST_HAS_ABSL
// Always use absl::any for UniversalPrinter<> specializations if googletest
// is built with absl support.
#define GTEST_INTERNAL_HAS_ANY 1
#include "absl/types/any.h"
namespace testing {
namespace internal {
using Any = ::absl::any;
} // namespace internal
} // namespace testing
#else
#ifdef __has_include
#if __has_include(<any>) && __cplusplus >= 201703L
// Otherwise for C++17 and higher use std::any for UniversalPrinter<>
// specializations.
#define GTEST_INTERNAL_HAS_ANY 1
#include <any>
namespace testing {
namespace internal {
using Any = ::std::any;
} // namespace internal
} // namespace testing
// The case where absl is configured NOT to alias std::any is not
// supported.
#endif // __has_include(<any>) && __cplusplus >= 201703L
#endif // __has_include
#endif // GTEST_HAS_ABSL
#if GTEST_HAS_ABSL
// Always use absl::optional for UniversalPrinter<> specializations if
// googletest is built with absl support.
#define GTEST_INTERNAL_HAS_OPTIONAL 1
#include "absl/types/optional.h"
namespace testing {
namespace internal {
template <typename T>
using Optional = ::absl::optional<T>;
inline ::absl::nullopt_t Nullopt() { return ::absl::nullopt; }
} // namespace internal
} // namespace testing
#else
#ifdef __has_include
#if __has_include(<optional>) && __cplusplus >= 201703L
// Otherwise for C++17 and higher use std::optional for UniversalPrinter<>
// specializations.
#define GTEST_INTERNAL_HAS_OPTIONAL 1
#include <optional>
namespace testing {
namespace internal {
template <typename T>
using Optional = ::std::optional<T>;
inline ::std::nullopt_t Nullopt() { return ::std::nullopt; }
} // namespace internal
} // namespace testing
// The case where absl is configured NOT to alias std::optional is not
// supported.
#endif // __has_include(<optional>) && __cplusplus >= 201703L
#endif // __has_include
#endif // GTEST_HAS_ABSL
#if GTEST_HAS_ABSL
// Always use absl::string_view for Matcher<> specializations if googletest // Always use absl::string_view for Matcher<> specializations if googletest
// is built with absl support. // is built with absl support.
#define GTEST_INTERNAL_HAS_STRING_VIEW 1 #define GTEST_INTERNAL_HAS_STRING_VIEW 1
@ -2411,8 +2342,9 @@ using StringView = ::absl::string_view;
} // namespace internal } // namespace internal
} // namespace testing } // namespace testing
#else #else
#ifdef __has_include #if defined(__cpp_lib_string_view) || \
#if __has_include(<string_view>) && __cplusplus >= 201703L (GTEST_INTERNAL_HAS_INCLUDE(<string_view>) && \
GTEST_INTERNAL_CPLUSPLUS_LANG >= 201703L)
// Otherwise for C++17 and higher use std::string_view for Matcher<> // Otherwise for C++17 and higher use std::string_view for Matcher<>
// specializations. // specializations.
#define GTEST_INTERNAL_HAS_STRING_VIEW 1 #define GTEST_INTERNAL_HAS_STRING_VIEW 1
@ -2424,37 +2356,19 @@ using StringView = ::std::string_view;
} // namespace testing } // namespace testing
// The case where absl is configured NOT to alias std::string_view is not // The case where absl is configured NOT to alias std::string_view is not
// supported. // supported.
#endif // __has_include(<string_view>) && __cplusplus >= 201703L #endif // __cpp_lib_string_view
#endif // __has_include
#endif // GTEST_HAS_ABSL #endif // GTEST_HAS_ABSL
#if GTEST_HAS_ABSL #ifndef GTEST_INTERNAL_HAS_STRING_VIEW
// Always use absl::variant for UniversalPrinter<> specializations if googletest #define GTEST_INTERNAL_HAS_STRING_VIEW 0
// is built with absl support. #endif
#define GTEST_INTERNAL_HAS_VARIANT 1
#include "absl/types/variant.h" #if (defined(__cpp_lib_three_way_comparison) || \
namespace testing { (GTEST_INTERNAL_HAS_INCLUDE(<compare>) && \
namespace internal { GTEST_INTERNAL_CPLUSPLUS_LANG >= 201907L))
template <typename... T> #define GTEST_INTERNAL_HAS_COMPARE_LIB 1
using Variant = ::absl::variant<T...>;
} // namespace internal
} // namespace testing
#else #else
#ifdef __has_include #define GTEST_INTERNAL_HAS_COMPARE_LIB 0
#if __has_include(<variant>) && __cplusplus >= 201703L #endif
// Otherwise for C++17 and higher use std::variant for UniversalPrinter<>
// specializations.
#define GTEST_INTERNAL_HAS_VARIANT 1
#include <variant>
namespace testing {
namespace internal {
template <typename... T>
using Variant = ::std::variant<T...>;
} // namespace internal
} // namespace testing
// The case where absl is configured NOT to alias std::variant is not supported.
#endif // __has_include(<variant>) && __cplusplus >= 201703L
#endif // __has_include
#endif // GTEST_HAS_ABSL
#endif // GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_ #endif // GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_

View File

@ -73,7 +73,7 @@ class GTEST_API_ String {
// memory using malloc(). // memory using malloc().
static const char* CloneCString(const char* c_str); static const char* CloneCString(const char* c_str);
#if GTEST_OS_WINDOWS_MOBILE #ifdef GTEST_OS_WINDOWS_MOBILE
// Windows CE does not have the 'ANSI' versions of Win32 APIs. To be // Windows CE does not have the 'ANSI' versions of Win32 APIs. To be
// able to pass strings to Win32 APIs on CE we need to convert them // able to pass strings to Win32 APIs on CE we need to convert them
// to 'Unicode', UTF-16. // to 'Unicode', UTF-16.

View File

@ -67,6 +67,22 @@ inline std::string CanonicalizeForStdLibVersioning(std::string s) {
s.erase(strlen("std"), end - strlen("std")); s.erase(strlen("std"), end - strlen("std"));
} }
} }
// Strip redundant spaces in typename to match MSVC
// For example, std::pair<int, bool> -> std::pair<int,bool>
static const char to_search[] = ", ";
const char replace_char = ',';
size_t pos = 0;
while (true) {
// Get the next occurrence from the current position
pos = s.find(to_search, pos);
if (pos == std::string::npos) {
break;
}
// Replace this occurrence of substring
s.replace(pos, strlen(to_search), 1, replace_char);
++pos;
}
return s; return s;
} }
@ -85,6 +101,20 @@ inline std::string GetTypeName(const std::type_info& type) {
const std::string name_str(status == 0 ? readable_name : name); const std::string name_str(status == 0 ? readable_name : name);
free(readable_name); free(readable_name);
return CanonicalizeForStdLibVersioning(name_str); return CanonicalizeForStdLibVersioning(name_str);
#elif defined(_MSC_VER)
// Strip struct and class due to differences between
// MSVC and other compilers. std::pair<int,bool> is printed as
// "struct std::pair<int,bool>" when using MSVC vs "std::pair<int, bool>" with
// other compilers.
std::string s = name;
// Only strip the leading "struct " and "class ", so uses rfind == 0 to
// ensure that
if (s.rfind("struct ", 0) == 0) {
s = s.substr(strlen("struct "));
} else if (s.rfind("class ", 0) == 0) {
s = s.substr(strlen("class "));
}
return s;
#else #else
return name; return name;
#endif // GTEST_HAS_CXXABI_H_ || __HP_aCC #endif // GTEST_HAS_CXXABI_H_ || __HP_aCC

View File

@ -39,7 +39,7 @@
// The prime table interface. // The prime table interface.
class PrimeTable { class PrimeTable {
public: public:
virtual ~PrimeTable() {} virtual ~PrimeTable() = default;
// Returns true if and only if n is a prime number. // Returns true if and only if n is a prime number.
virtual bool IsPrime(int n) const = 0; virtual bool IsPrime(int n) const = 0;
@ -78,8 +78,9 @@ class PreCalculatedPrimeTable : public PrimeTable {
public: public:
// 'max' specifies the maximum number the prime table holds. // 'max' specifies the maximum number the prime table holds.
explicit PreCalculatedPrimeTable(int max) explicit PreCalculatedPrimeTable(int max)
: is_prime_size_(max + 1), is_prime_(new bool[max + 1]) { : is_prime_size_(std::max(1, max + 1)),
CalculatePrimesUpTo(max); is_prime_(new bool[static_cast<size_t>(is_prime_size_)]) {
CalculatePrimesUpTo(is_prime_size_ - 1);
} }
~PreCalculatedPrimeTable() override { delete[] is_prime_; } ~PreCalculatedPrimeTable() override { delete[] is_prime_; }

View File

@ -38,7 +38,6 @@ using ::testing::InitGoogleTest;
using ::testing::Test; using ::testing::Test;
using ::testing::TestEventListeners; using ::testing::TestEventListeners;
using ::testing::TestInfo; using ::testing::TestInfo;
using ::testing::TestPartResult;
using ::testing::UnitTest; using ::testing::UnitTest;
namespace { namespace {

View File

@ -32,6 +32,8 @@
// and each test is given one combination as a parameter. // and each test is given one combination as a parameter.
// Use class definitions to test from this header. // Use class definitions to test from this header.
#include <tuple>
#include "prime_tables.h" #include "prime_tables.h"
#include "gtest/gtest.h" #include "gtest/gtest.h"
namespace { namespace {

View File

@ -40,7 +40,6 @@ using ::testing::Test;
using ::testing::TestEventListeners; using ::testing::TestEventListeners;
using ::testing::TestInfo; using ::testing::TestInfo;
using ::testing::TestPartResult; using ::testing::TestPartResult;
using ::testing::TestSuite;
using ::testing::UnitTest; using ::testing::UnitTest;
namespace { namespace {
// Provides alternative output mode which produces minimal amount of // Provides alternative output mode which produces minimal amount of

View File

@ -44,7 +44,7 @@ namespace testing {
// Used in EXPECT_TRUE/FALSE(assertion_result). // Used in EXPECT_TRUE/FALSE(assertion_result).
AssertionResult::AssertionResult(const AssertionResult& other) AssertionResult::AssertionResult(const AssertionResult& other)
: success_(other.success_), : success_(other.success_),
message_(other.message_.get() != nullptr message_(other.message_ != nullptr
? new ::std::string(*other.message_) ? new ::std::string(*other.message_)
: static_cast< ::std::string*>(nullptr)) {} : static_cast< ::std::string*>(nullptr)) {}
@ -58,7 +58,7 @@ void AssertionResult::swap(AssertionResult& other) {
// Returns the assertion's negation. Used with EXPECT/ASSERT_FALSE. // Returns the assertion's negation. Used with EXPECT/ASSERT_FALSE.
AssertionResult AssertionResult::operator!() const { AssertionResult AssertionResult::operator!() const {
AssertionResult negation(!success_); AssertionResult negation(!success_);
if (message_.get() != nullptr) negation << *message_; if (message_ != nullptr) negation << *message_;
return negation; return negation;
} }

View File

@ -32,15 +32,21 @@
#include "gtest/gtest-death-test.h" #include "gtest/gtest-death-test.h"
#include <stdlib.h>
#include <functional> #include <functional>
#include <memory>
#include <sstream>
#include <string>
#include <utility> #include <utility>
#include <vector>
#include "gtest/internal/custom/gtest.h" #include "gtest/internal/custom/gtest.h"
#include "gtest/internal/gtest-port.h" #include "gtest/internal/gtest-port.h"
#if GTEST_HAS_DEATH_TEST #ifdef GTEST_HAS_DEATH_TEST
#if GTEST_OS_MAC #ifdef GTEST_OS_MAC
#include <crt_externs.h> #include <crt_externs.h>
#endif // GTEST_OS_MAC #endif // GTEST_OS_MAC
@ -48,24 +54,24 @@
#include <fcntl.h> #include <fcntl.h>
#include <limits.h> #include <limits.h>
#if GTEST_OS_LINUX #ifdef GTEST_OS_LINUX
#include <signal.h> #include <signal.h>
#endif // GTEST_OS_LINUX #endif // GTEST_OS_LINUX
#include <stdarg.h> #include <stdarg.h>
#if GTEST_OS_WINDOWS #ifdef GTEST_OS_WINDOWS
#include <windows.h> #include <windows.h>
#else #else
#include <sys/mman.h> #include <sys/mman.h>
#include <sys/wait.h> #include <sys/wait.h>
#endif // GTEST_OS_WINDOWS #endif // GTEST_OS_WINDOWS
#if GTEST_OS_QNX #ifdef GTEST_OS_QNX
#include <spawn.h> #include <spawn.h>
#endif // GTEST_OS_QNX #endif // GTEST_OS_QNX
#if GTEST_OS_FUCHSIA #ifdef GTEST_OS_FUCHSIA
#include <lib/fdio/fd.h> #include <lib/fdio/fd.h>
#include <lib/fdio/io.h> #include <lib/fdio/io.h>
#include <lib/fdio/spawn.h> #include <lib/fdio/spawn.h>
@ -111,7 +117,7 @@ GTEST_DEFINE_string_(
GTEST_DEFINE_bool_( GTEST_DEFINE_bool_(
death_test_use_fork, death_test_use_fork,
testing::internal::BoolFromGTestEnv("death_test_use_fork", false), testing::internal::BoolFromGTestEnv("death_test_use_fork", false),
"Instructs to use fork()/_exit() instead of clone() in death tests. " "Instructs to use fork()/_Exit() instead of clone() in death tests. "
"Ignored and always uses fork() on POSIX systems where clone() is not " "Ignored and always uses fork() on POSIX systems where clone() is not "
"implemented. Useful when running under valgrind or similar tools if " "implemented. Useful when running under valgrind or similar tools if "
"those do not support clone(). Valgrind 3.3.1 will just fail if " "those do not support clone(). Valgrind 3.3.1 will just fail if "
@ -131,13 +137,13 @@ GTEST_DEFINE_string_(
namespace testing { namespace testing {
#if GTEST_HAS_DEATH_TEST #ifdef GTEST_HAS_DEATH_TEST
namespace internal { namespace internal {
// Valid only for fast death tests. Indicates the code is running in the // Valid only for fast death tests. Indicates the code is running in the
// child process of a fast style death test. // child process of a fast style death test.
#if !GTEST_OS_WINDOWS && !GTEST_OS_FUCHSIA #if !defined(GTEST_OS_WINDOWS) && !defined(GTEST_OS_FUCHSIA)
static bool g_in_fast_death_test_child = false; static bool g_in_fast_death_test_child = false;
#endif #endif
@ -147,7 +153,7 @@ static bool g_in_fast_death_test_child = false;
// tests. IMPORTANT: This is an internal utility. Using it may break the // tests. IMPORTANT: This is an internal utility. Using it may break the
// implementation of death tests. User code MUST NOT use it. // implementation of death tests. User code MUST NOT use it.
bool InDeathTestChild() { bool InDeathTestChild() {
#if GTEST_OS_WINDOWS || GTEST_OS_FUCHSIA #if defined(GTEST_OS_WINDOWS) || defined(GTEST_OS_FUCHSIA)
// On Windows and Fuchsia, death tests are thread-safe regardless of the value // On Windows and Fuchsia, death tests are thread-safe regardless of the value
// of the death_test_style flag. // of the death_test_style flag.
@ -169,7 +175,7 @@ ExitedWithCode::ExitedWithCode(int exit_code) : exit_code_(exit_code) {}
// ExitedWithCode function-call operator. // ExitedWithCode function-call operator.
bool ExitedWithCode::operator()(int exit_status) const { bool ExitedWithCode::operator()(int exit_status) const {
#if GTEST_OS_WINDOWS || GTEST_OS_FUCHSIA #if defined(GTEST_OS_WINDOWS) || defined(GTEST_OS_FUCHSIA)
return exit_status == exit_code_; return exit_status == exit_code_;
@ -180,7 +186,7 @@ bool ExitedWithCode::operator()(int exit_status) const {
#endif // GTEST_OS_WINDOWS || GTEST_OS_FUCHSIA #endif // GTEST_OS_WINDOWS || GTEST_OS_FUCHSIA
} }
#if !GTEST_OS_WINDOWS && !GTEST_OS_FUCHSIA #if !defined(GTEST_OS_WINDOWS) && !defined(GTEST_OS_FUCHSIA)
// KilledBySignal constructor. // KilledBySignal constructor.
KilledBySignal::KilledBySignal(int signum) : signum_(signum) {} KilledBySignal::KilledBySignal(int signum) : signum_(signum) {}
@ -207,7 +213,7 @@ namespace internal {
static std::string ExitSummary(int exit_code) { static std::string ExitSummary(int exit_code) {
Message m; Message m;
#if GTEST_OS_WINDOWS || GTEST_OS_FUCHSIA #if defined(GTEST_OS_WINDOWS) || defined(GTEST_OS_FUCHSIA)
m << "Exited with exit status " << exit_code; m << "Exited with exit status " << exit_code;
@ -234,7 +240,7 @@ bool ExitedUnsuccessfully(int exit_status) {
return !ExitedWithCode(0)(exit_status); return !ExitedWithCode(0)(exit_status);
} }
#if !GTEST_OS_WINDOWS && !GTEST_OS_FUCHSIA #if !defined(GTEST_OS_WINDOWS) && !defined(GTEST_OS_FUCHSIA)
// Generates a textual failure message when a death test finds more than // Generates a textual failure message when a death test finds more than
// one thread running, or cannot determine the number of threads, prior // one thread running, or cannot determine the number of threads, prior
// to executing the given statement. It is the responsibility of the // to executing the given statement. It is the responsibility of the
@ -263,7 +269,7 @@ static const char kDeathTestReturned = 'R';
static const char kDeathTestThrew = 'T'; static const char kDeathTestThrew = 'T';
static const char kDeathTestInternalError = 'I'; static const char kDeathTestInternalError = 'I';
#if GTEST_OS_FUCHSIA #ifdef GTEST_OS_FUCHSIA
// File descriptor used for the pipe in the child process. // File descriptor used for the pipe in the child process.
static const int kFuchsiaReadPipeFd = 3; static const int kFuchsiaReadPipeFd = 3;
@ -295,7 +301,7 @@ enum DeathTestOutcome { IN_PROGRESS, DIED, LIVED, RETURNED, THREW };
fputc(kDeathTestInternalError, parent); fputc(kDeathTestInternalError, parent);
fprintf(parent, "%s", message.c_str()); fprintf(parent, "%s", message.c_str());
fflush(parent); fflush(parent);
_exit(1); _Exit(1);
} else { } else {
fprintf(stderr, "%s", message.c_str()); fprintf(stderr, "%s", message.c_str());
fflush(stderr); fflush(stderr);
@ -507,7 +513,7 @@ std::string DeathTestImpl::GetErrorLogs() { return GetCapturedStderr(); }
// Signals that the death test code which should have exited, didn't. // Signals that the death test code which should have exited, didn't.
// Should be called only in a death test child process. // Should be called only in a death test child process.
// Writes a status byte to the child's status file descriptor, then // Writes a status byte to the child's status file descriptor, then
// calls _exit(1). // calls _Exit(1).
void DeathTestImpl::Abort(AbortReason reason) { void DeathTestImpl::Abort(AbortReason reason) {
// The parent process considers the death test to be a failure if // The parent process considers the death test to be a failure if
// it finds any data in our pipe. So, here we write a single flag byte // it finds any data in our pipe. So, here we write a single flag byte
@ -519,13 +525,13 @@ void DeathTestImpl::Abort(AbortReason reason) {
GTEST_DEATH_TEST_CHECK_SYSCALL_(posix::Write(write_fd(), &status_ch, 1)); GTEST_DEATH_TEST_CHECK_SYSCALL_(posix::Write(write_fd(), &status_ch, 1));
// We are leaking the descriptor here because on some platforms (i.e., // We are leaking the descriptor here because on some platforms (i.e.,
// when built as Windows DLL), destructors of global objects will still // when built as Windows DLL), destructors of global objects will still
// run after calling _exit(). On such systems, write_fd_ will be // run after calling _Exit(). On such systems, write_fd_ will be
// indirectly closed from the destructor of UnitTestImpl, causing double // indirectly closed from the destructor of UnitTestImpl, causing double
// close if it is also closed here. On debug configurations, double close // close if it is also closed here. On debug configurations, double close
// may assert. As there are no in-process buffers to flush here, we are // may assert. As there are no in-process buffers to flush here, we are
// relying on the OS to close the descriptor after the process terminates // relying on the OS to close the descriptor after the process terminates
// when the destructors are not run. // when the destructors are not run.
_exit(1); // Exits w/o any normal exit hooks (we were supposed to crash) _Exit(1); // Exits w/o any normal exit hooks (we were supposed to crash)
} }
// Returns an indented copy of stderr output for a death test. // Returns an indented copy of stderr output for a death test.
@ -621,7 +627,21 @@ bool DeathTestImpl::Passed(bool status_ok) {
return success; return success;
} }
#if GTEST_OS_WINDOWS #ifndef GTEST_OS_WINDOWS
// Note: The return value points into args, so the return value's lifetime is
// bound to that of args.
static std::vector<char*> CreateArgvFromArgs(std::vector<std::string>& args) {
std::vector<char*> result;
result.reserve(args.size() + 1);
for (auto& arg : args) {
result.push_back(&arg[0]);
}
result.push_back(nullptr); // Extra null terminator.
return result;
}
#endif
#ifdef GTEST_OS_WINDOWS
// WindowsDeathTest implements death tests on Windows. Due to the // WindowsDeathTest implements death tests on Windows. Due to the
// specifics of starting new processes on Windows, death tests there are // specifics of starting new processes on Windows, death tests there are
// always threadsafe, and Google Test considers the // always threadsafe, and Google Test considers the
@ -765,7 +785,7 @@ DeathTest::TestRole WindowsDeathTest::AssumeRole() {
StreamableToString(static_cast<unsigned int>(::GetCurrentProcessId())) + StreamableToString(static_cast<unsigned int>(::GetCurrentProcessId())) +
// size_t has the same width as pointers on both 32-bit and 64-bit // size_t has the same width as pointers on both 32-bit and 64-bit
// Windows platforms. // Windows platforms.
// See http://msdn.microsoft.com/en-us/library/tcxf1dw6.aspx. // See https://msdn.microsoft.com/en-us/library/tcxf1dw6.aspx.
"|" + StreamableToString(reinterpret_cast<size_t>(write_handle)) + "|" + "|" + StreamableToString(reinterpret_cast<size_t>(write_handle)) + "|" +
StreamableToString(reinterpret_cast<size_t>(event_handle_.Get())); StreamableToString(reinterpret_cast<size_t>(event_handle_.Get()));
@ -808,7 +828,7 @@ DeathTest::TestRole WindowsDeathTest::AssumeRole() {
return OVERSEE_TEST; return OVERSEE_TEST;
} }
#elif GTEST_OS_FUCHSIA #elif defined(GTEST_OS_FUCHSIA)
class FuchsiaDeathTest : public DeathTestImpl { class FuchsiaDeathTest : public DeathTestImpl {
public: public:
@ -836,36 +856,6 @@ class FuchsiaDeathTest : public DeathTestImpl {
zx::socket stderr_socket_; zx::socket stderr_socket_;
}; };
// Utility class for accumulating command-line arguments.
class Arguments {
public:
Arguments() { args_.push_back(nullptr); }
~Arguments() {
for (std::vector<char*>::iterator i = args_.begin(); i != args_.end();
++i) {
free(*i);
}
}
void AddArgument(const char* argument) {
args_.insert(args_.end() - 1, posix::StrDup(argument));
}
template <typename Str>
void AddArguments(const ::std::vector<Str>& arguments) {
for (typename ::std::vector<Str>::const_iterator i = arguments.begin();
i != arguments.end(); ++i) {
args_.insert(args_.end() - 1, posix::StrDup(i->c_str()));
}
}
char* const* Argv() { return &args_[0]; }
int size() { return static_cast<int>(args_.size()) - 1; }
private:
std::vector<char*> args_;
};
// Waits for the child in a death test to exit, returning its exit // Waits for the child in a death test to exit, returning its exit
// status, or 0 if no child process exists. As a side effect, sets the // status, or 0 if no child process exists. As a side effect, sets the
// outcome data member. // outcome data member.
@ -986,10 +976,10 @@ DeathTest::TestRole FuchsiaDeathTest::AssumeRole() {
kInternalRunDeathTestFlag + "=" + file_ + kInternalRunDeathTestFlag + "=" + file_ +
"|" + StreamableToString(line_) + "|" + "|" + StreamableToString(line_) + "|" +
StreamableToString(death_test_index); StreamableToString(death_test_index);
Arguments args;
args.AddArguments(GetInjectableArgvs()); std::vector<std::string> args = GetInjectableArgvs();
args.AddArgument(filter_flag.c_str()); args.push_back(filter_flag);
args.AddArgument(internal_flag.c_str()); args.push_back(internal_flag);
// Build the pipe for communication with the child. // Build the pipe for communication with the child.
zx_status_t status; zx_status_t status;
@ -1041,8 +1031,14 @@ DeathTest::TestRole FuchsiaDeathTest::AssumeRole() {
GTEST_DEATH_TEST_CHECK_(status == ZX_OK); GTEST_DEATH_TEST_CHECK_(status == ZX_OK);
// Spawn the child process. // Spawn the child process.
status = fdio_spawn_etc(child_job, FDIO_SPAWN_CLONE_ALL, args.Argv()[0], // Note: The test component must have `fuchsia.process.Launcher` declared
args.Argv(), nullptr, 2, spawn_actions, // in its manifest. (Fuchsia integration tests require creating a
// "Fuchsia Test Component" which contains a "Fuchsia Component Manifest")
// Launching processes is a privileged operation in Fuchsia, and the
// declaration indicates that the ability is required for the component.
std::vector<char*> argv = CreateArgvFromArgs(args);
status = fdio_spawn_etc(child_job, FDIO_SPAWN_CLONE_ALL, argv[0], argv.data(),
nullptr, 2, spawn_actions,
child_process_.reset_and_get_address(), nullptr); child_process_.reset_and_get_address(), nullptr);
GTEST_DEATH_TEST_CHECK_(status == ZX_OK); GTEST_DEATH_TEST_CHECK_(status == ZX_OK);
@ -1134,7 +1130,7 @@ DeathTest::TestRole NoExecDeathTest::AssumeRole() {
LogToStderr(); LogToStderr();
// Event forwarding to the listeners of event listener API mush be shut // Event forwarding to the listeners of event listener API mush be shut
// down in death test subprocesses. // down in death test subprocesses.
GetUnitTestImpl()->listeners()->SuppressEventForwarding(); GetUnitTestImpl()->listeners()->SuppressEventForwarding(true);
g_in_fast_death_test_child = true; g_in_fast_death_test_child = true;
return EXECUTE_TEST; return EXECUTE_TEST;
} else { } else {
@ -1173,34 +1169,6 @@ class ExecDeathTest : public ForkingDeathTest {
const int line_; const int line_;
}; };
// Utility class for accumulating command-line arguments.
class Arguments {
public:
Arguments() { args_.push_back(nullptr); }
~Arguments() {
for (std::vector<char*>::iterator i = args_.begin(); i != args_.end();
++i) {
free(*i);
}
}
void AddArgument(const char* argument) {
args_.insert(args_.end() - 1, posix::StrDup(argument));
}
template <typename Str>
void AddArguments(const ::std::vector<Str>& arguments) {
for (typename ::std::vector<Str>::const_iterator i = arguments.begin();
i != arguments.end(); ++i) {
args_.insert(args_.end() - 1, posix::StrDup(i->c_str()));
}
}
char* const* Argv() { return &args_[0]; }
private:
std::vector<char*> args_;
};
// A struct that encompasses the arguments to the child process of a // A struct that encompasses the arguments to the child process of a
// threadsafe-style death test process. // threadsafe-style death test process.
struct ExecDeathTestArgs { struct ExecDeathTestArgs {
@ -1208,7 +1176,7 @@ struct ExecDeathTestArgs {
int close_fd; // File descriptor to close; the read end of a pipe int close_fd; // File descriptor to close; the read end of a pipe
}; };
#if GTEST_OS_QNX #ifdef GTEST_OS_QNX
extern "C" char** environ; extern "C" char** environ;
#else // GTEST_OS_QNX #else // GTEST_OS_QNX
// The main function for a threadsafe-style death test child process. // The main function for a threadsafe-style death test child process.
@ -1289,7 +1257,7 @@ static pid_t ExecDeathTestSpawnChild(char* const* argv, int close_fd) {
ExecDeathTestArgs args = {argv, close_fd}; ExecDeathTestArgs args = {argv, close_fd};
pid_t child_pid = -1; pid_t child_pid = -1;
#if GTEST_OS_QNX #ifdef GTEST_OS_QNX
// Obtains the current directory and sets it to be closed in the child // Obtains the current directory and sets it to be closed in the child
// process. // process.
const int cwd_fd = open(".", O_RDONLY); const int cwd_fd = open(".", O_RDONLY);
@ -1320,7 +1288,7 @@ static pid_t ExecDeathTestSpawnChild(char* const* argv, int close_fd) {
GTEST_DEATH_TEST_CHECK_SYSCALL_(close(cwd_fd)); GTEST_DEATH_TEST_CHECK_SYSCALL_(close(cwd_fd));
#else // GTEST_OS_QNX #else // GTEST_OS_QNX
#if GTEST_OS_LINUX #ifdef GTEST_OS_LINUX
// When a SIGPROF signal is received while fork() or clone() are executing, // When a SIGPROF signal is received while fork() or clone() are executing,
// the process may hang. To avoid this, we ignore SIGPROF here and re-enable // the process may hang. To avoid this, we ignore SIGPROF here and re-enable
// it after the call to fork()/clone() is complete. // it after the call to fork()/clone() is complete.
@ -1367,11 +1335,10 @@ static pid_t ExecDeathTestSpawnChild(char* const* argv, int close_fd) {
#endif // GTEST_HAS_CLONE #endif // GTEST_HAS_CLONE
if (use_fork && (child_pid = fork()) == 0) { if (use_fork && (child_pid = fork()) == 0) {
ExecDeathTestChildMain(&args); _Exit(ExecDeathTestChildMain(&args));
_exit(0);
} }
#endif // GTEST_OS_QNX #endif // GTEST_OS_QNX
#if GTEST_OS_LINUX #ifdef GTEST_OS_LINUX
GTEST_DEATH_TEST_CHECK_SYSCALL_( GTEST_DEATH_TEST_CHECK_SYSCALL_(
sigaction(SIGPROF, &saved_sigprof_action, nullptr)); sigaction(SIGPROF, &saved_sigprof_action, nullptr));
#endif // GTEST_OS_LINUX #endif // GTEST_OS_LINUX
@ -1410,10 +1377,9 @@ DeathTest::TestRole ExecDeathTest::AssumeRole() {
StreamableToString(line_) + "|" + StreamableToString(line_) + "|" +
StreamableToString(death_test_index) + "|" + StreamableToString(death_test_index) + "|" +
StreamableToString(pipe_fd[1]); StreamableToString(pipe_fd[1]);
Arguments args; std::vector<std::string> args = GetArgvsForDeathTestChildProcess();
args.AddArguments(GetArgvsForDeathTestChildProcess()); args.push_back(filter_flag);
args.AddArgument(filter_flag.c_str()); args.push_back(internal_flag);
args.AddArgument(internal_flag.c_str());
DeathTest::set_last_death_test_message(""); DeathTest::set_last_death_test_message("");
@ -1422,7 +1388,8 @@ DeathTest::TestRole ExecDeathTest::AssumeRole() {
// is necessary. // is necessary.
FlushInfoLog(); FlushInfoLog();
const pid_t child_pid = ExecDeathTestSpawnChild(args.Argv(), pipe_fd[0]); std::vector<char*> argv = CreateArgvFromArgs(args);
const pid_t child_pid = ExecDeathTestSpawnChild(argv.data(), pipe_fd[0]);
GTEST_DEATH_TEST_CHECK_SYSCALL_(close(pipe_fd[1])); GTEST_DEATH_TEST_CHECK_SYSCALL_(close(pipe_fd[1]));
set_child_pid(child_pid); set_child_pid(child_pid);
set_read_fd(pipe_fd[0]); set_read_fd(pipe_fd[0]);
@ -1463,14 +1430,14 @@ bool DefaultDeathTestFactory::Create(const char* statement,
} }
} }
#if GTEST_OS_WINDOWS #ifdef GTEST_OS_WINDOWS
if (GTEST_FLAG_GET(death_test_style) == "threadsafe" || if (GTEST_FLAG_GET(death_test_style) == "threadsafe" ||
GTEST_FLAG_GET(death_test_style) == "fast") { GTEST_FLAG_GET(death_test_style) == "fast") {
*test = new WindowsDeathTest(statement, std::move(matcher), file, line); *test = new WindowsDeathTest(statement, std::move(matcher), file, line);
} }
#elif GTEST_OS_FUCHSIA #elif defined(GTEST_OS_FUCHSIA)
if (GTEST_FLAG_GET(death_test_style) == "threadsafe" || if (GTEST_FLAG_GET(death_test_style) == "threadsafe" ||
GTEST_FLAG_GET(death_test_style) == "fast") { GTEST_FLAG_GET(death_test_style) == "fast") {
@ -1497,7 +1464,7 @@ bool DefaultDeathTestFactory::Create(const char* statement,
return true; return true;
} }
#if GTEST_OS_WINDOWS #ifdef GTEST_OS_WINDOWS
// Recreates the pipe and event handles from the provided parameters, // Recreates the pipe and event handles from the provided parameters,
// signals the event, and returns a file descriptor wrapped around the pipe // signals the event, and returns a file descriptor wrapped around the pipe
// handle. This function is called in the child process only. // handle. This function is called in the child process only.
@ -1564,7 +1531,7 @@ static int GetStatusFileDescriptor(unsigned int parent_process_id,
// initialized from the GTEST_FLAG(internal_run_death_test) flag if // initialized from the GTEST_FLAG(internal_run_death_test) flag if
// the flag is specified; otherwise returns NULL. // the flag is specified; otherwise returns NULL.
InternalRunDeathTestFlag* ParseInternalRunDeathTestFlag() { InternalRunDeathTestFlag* ParseInternalRunDeathTestFlag() {
if (GTEST_FLAG_GET(internal_run_death_test) == "") return nullptr; if (GTEST_FLAG_GET(internal_run_death_test).empty()) return nullptr;
// GTEST_HAS_DEATH_TEST implies that we have ::std::string, so we // GTEST_HAS_DEATH_TEST implies that we have ::std::string, so we
// can use it here. // can use it here.
@ -1574,7 +1541,7 @@ InternalRunDeathTestFlag* ParseInternalRunDeathTestFlag() {
SplitString(GTEST_FLAG_GET(internal_run_death_test), '|', &fields); SplitString(GTEST_FLAG_GET(internal_run_death_test), '|', &fields);
int write_fd = -1; int write_fd = -1;
#if GTEST_OS_WINDOWS #ifdef GTEST_OS_WINDOWS
unsigned int parent_process_id = 0; unsigned int parent_process_id = 0;
size_t write_handle_as_size_t = 0; size_t write_handle_as_size_t = 0;
@ -1591,7 +1558,7 @@ InternalRunDeathTestFlag* ParseInternalRunDeathTestFlag() {
write_fd = GetStatusFileDescriptor(parent_process_id, write_handle_as_size_t, write_fd = GetStatusFileDescriptor(parent_process_id, write_handle_as_size_t,
event_handle_as_size_t); event_handle_as_size_t);
#elif GTEST_OS_FUCHSIA #elif defined(GTEST_OS_FUCHSIA)
if (fields.size() != 3 || !ParseNaturalNumber(fields[1], &line) || if (fields.size() != 3 || !ParseNaturalNumber(fields[1], &line) ||
!ParseNaturalNumber(fields[2], &index)) { !ParseNaturalNumber(fields[2], &index)) {

View File

@ -31,12 +31,15 @@
#include <stdlib.h> #include <stdlib.h>
#include <iterator>
#include <string>
#include "gtest/gtest-message.h" #include "gtest/gtest-message.h"
#include "gtest/internal/gtest-port.h" #include "gtest/internal/gtest-port.h"
#if GTEST_OS_WINDOWS_MOBILE #ifdef GTEST_OS_WINDOWS_MOBILE
#include <windows.h> #include <windows.h>
#elif GTEST_OS_WINDOWS #elif defined(GTEST_OS_WINDOWS)
#include <direct.h> #include <direct.h>
#include <io.h> #include <io.h>
#else #else
@ -47,7 +50,7 @@
#include "gtest/internal/gtest-string.h" #include "gtest/internal/gtest-string.h"
#if GTEST_OS_WINDOWS #ifdef GTEST_OS_WINDOWS
#define GTEST_PATH_MAX_ _MAX_PATH #define GTEST_PATH_MAX_ _MAX_PATH
#elif defined(PATH_MAX) #elif defined(PATH_MAX)
#define GTEST_PATH_MAX_ PATH_MAX #define GTEST_PATH_MAX_ PATH_MAX
@ -62,7 +65,7 @@
namespace testing { namespace testing {
namespace internal { namespace internal {
#if GTEST_OS_WINDOWS #ifdef GTEST_OS_WINDOWS
// On Windows, '\\' is the standard path separator, but many tools and the // On Windows, '\\' is the standard path separator, but many tools and the
// Windows API also accept '/' as an alternate path separator. Unless otherwise // Windows API also accept '/' as an alternate path separator. Unless otherwise
// noted, a file path can contain either kind of path separators, or a mixture // noted, a file path can contain either kind of path separators, or a mixture
@ -70,7 +73,7 @@ namespace internal {
const char kPathSeparator = '\\'; const char kPathSeparator = '\\';
const char kAlternatePathSeparator = '/'; const char kAlternatePathSeparator = '/';
const char kAlternatePathSeparatorString[] = "/"; const char kAlternatePathSeparatorString[] = "/";
#if GTEST_OS_WINDOWS_MOBILE #ifdef GTEST_OS_WINDOWS_MOBILE
// Windows CE doesn't have a current directory. You should not use // Windows CE doesn't have a current directory. You should not use
// the current directory in tests on Windows CE, but this at least // the current directory in tests on Windows CE, but this at least
// provides a reasonable fallback. // provides a reasonable fallback.
@ -96,19 +99,21 @@ static bool IsPathSeparator(char c) {
// Returns the current working directory, or "" if unsuccessful. // Returns the current working directory, or "" if unsuccessful.
FilePath FilePath::GetCurrentDir() { FilePath FilePath::GetCurrentDir() {
#if GTEST_OS_WINDOWS_MOBILE || GTEST_OS_WINDOWS_PHONE || \ #if defined(GTEST_OS_WINDOWS_MOBILE) || defined(GTEST_OS_WINDOWS_PHONE) || \
GTEST_OS_WINDOWS_RT || GTEST_OS_ESP8266 || GTEST_OS_ESP32 || \ defined(GTEST_OS_WINDOWS_RT) || defined(GTEST_OS_ESP8266) || \
GTEST_OS_XTENSA || GTEST_OS_QURT defined(GTEST_OS_ESP32) || defined(GTEST_OS_XTENSA) || \
defined(GTEST_OS_QURT) || defined(GTEST_OS_NXP_QN9090) || \
defined(GTEST_OS_NRF52)
// These platforms do not have a current directory, so we just return // These platforms do not have a current directory, so we just return
// something reasonable. // something reasonable.
return FilePath(kCurrentDirectoryString); return FilePath(kCurrentDirectoryString);
#elif GTEST_OS_WINDOWS #elif defined(GTEST_OS_WINDOWS)
char cwd[GTEST_PATH_MAX_ + 1] = {'\0'}; char cwd[GTEST_PATH_MAX_ + 1] = {'\0'};
return FilePath(_getcwd(cwd, sizeof(cwd)) == nullptr ? "" : cwd); return FilePath(_getcwd(cwd, sizeof(cwd)) == nullptr ? "" : cwd);
#else #else
char cwd[GTEST_PATH_MAX_ + 1] = {'\0'}; char cwd[GTEST_PATH_MAX_ + 1] = {'\0'};
char* result = getcwd(cwd, sizeof(cwd)); char* result = getcwd(cwd, sizeof(cwd));
#if GTEST_OS_NACL #ifdef GTEST_OS_NACL
// getcwd will likely fail in NaCl due to the sandbox, so return something // getcwd will likely fail in NaCl due to the sandbox, so return something
// reasonable. The user may have provided a shim implementation for getcwd, // reasonable. The user may have provided a shim implementation for getcwd,
// however, so fallback only when failure is detected. // however, so fallback only when failure is detected.
@ -148,20 +153,19 @@ const char* FilePath::FindLastPathSeparator() const {
} }
size_t FilePath::CalculateRootLength() const { size_t FilePath::CalculateRootLength() const {
const auto &path = pathname_; const auto& path = pathname_;
auto s = path.begin(); auto s = path.begin();
auto end = path.end(); auto end = path.end();
#if GTEST_OS_WINDOWS #ifdef GTEST_OS_WINDOWS
if (end - s >= 2 && s[1] == ':' && if (end - s >= 2 && s[1] == ':' && (end - s == 2 || IsPathSeparator(s[2])) &&
(end - s == 2 || IsPathSeparator(s[2])) &&
(('A' <= s[0] && s[0] <= 'Z') || ('a' <= s[0] && s[0] <= 'z'))) { (('A' <= s[0] && s[0] <= 'Z') || ('a' <= s[0] && s[0] <= 'z'))) {
// A typical absolute path like "C:\Windows" or "D:" // A typical absolute path like "C:\Windows" or "D:"
s += 2; s += 2;
if (s != end) { if (s != end) {
++s; ++s;
} }
} else if (end - s >= 3 && IsPathSeparator(*s) && IsPathSeparator(*(s + 1)) } else if (end - s >= 3 && IsPathSeparator(*s) && IsPathSeparator(*(s + 1)) &&
&& !IsPathSeparator(*(s + 2))) { !IsPathSeparator(*(s + 2))) {
// Move past the "\\" prefix in a UNC path like "\\Server\Share\Folder" // Move past the "\\" prefix in a UNC path like "\\Server\Share\Folder"
s += 2; s += 2;
// Skip 2 components and their following separators ("Server\" and "Share\") // Skip 2 components and their following separators ("Server\" and "Share\")
@ -245,7 +249,7 @@ FilePath FilePath::ConcatPaths(const FilePath& directory,
// Returns true if pathname describes something findable in the file-system, // Returns true if pathname describes something findable in the file-system,
// either a file, directory, or whatever. // either a file, directory, or whatever.
bool FilePath::FileOrDirectoryExists() const { bool FilePath::FileOrDirectoryExists() const {
#if GTEST_OS_WINDOWS_MOBILE #ifdef GTEST_OS_WINDOWS_MOBILE
LPCWSTR unicode = String::AnsiToUtf16(pathname_.c_str()); LPCWSTR unicode = String::AnsiToUtf16(pathname_.c_str());
const DWORD attributes = GetFileAttributes(unicode); const DWORD attributes = GetFileAttributes(unicode);
delete[] unicode; delete[] unicode;
@ -260,7 +264,7 @@ bool FilePath::FileOrDirectoryExists() const {
// that exists. // that exists.
bool FilePath::DirectoryExists() const { bool FilePath::DirectoryExists() const {
bool result = false; bool result = false;
#if GTEST_OS_WINDOWS #ifdef GTEST_OS_WINDOWS
// Don't strip off trailing separator if path is a root directory on // Don't strip off trailing separator if path is a root directory on
// Windows (like "C:\\"). // Windows (like "C:\\").
const FilePath& path(IsRootDirectory() ? *this const FilePath& path(IsRootDirectory() ? *this
@ -269,7 +273,7 @@ bool FilePath::DirectoryExists() const {
const FilePath& path(*this); const FilePath& path(*this);
#endif #endif
#if GTEST_OS_WINDOWS_MOBILE #ifdef GTEST_OS_WINDOWS_MOBILE
LPCWSTR unicode = String::AnsiToUtf16(path.c_str()); LPCWSTR unicode = String::AnsiToUtf16(path.c_str());
const DWORD attributes = GetFileAttributes(unicode); const DWORD attributes = GetFileAttributes(unicode);
delete[] unicode; delete[] unicode;
@ -295,9 +299,7 @@ bool FilePath::IsRootDirectory() const {
} }
// Returns true if pathname describes an absolute path. // Returns true if pathname describes an absolute path.
bool FilePath::IsAbsolutePath() const { bool FilePath::IsAbsolutePath() const { return CalculateRootLength() > 0; }
return CalculateRootLength() > 0;
}
// Returns a pathname for a file that does not currently exist. The pathname // Returns a pathname for a file that does not currently exist. The pathname
// will be directory/base_name.extension or // will be directory/base_name.extension or
@ -334,7 +336,7 @@ bool FilePath::CreateDirectoriesRecursively() const {
return false; return false;
} }
if (pathname_.length() == 0 || this->DirectoryExists()) { if (pathname_.empty() || this->DirectoryExists()) {
return true; return true;
} }
@ -347,14 +349,16 @@ bool FilePath::CreateDirectoriesRecursively() const {
// directory for any reason, including if the parent directory does not // directory for any reason, including if the parent directory does not
// exist. Not named "CreateDirectory" because that's a macro on Windows. // exist. Not named "CreateDirectory" because that's a macro on Windows.
bool FilePath::CreateFolder() const { bool FilePath::CreateFolder() const {
#if GTEST_OS_WINDOWS_MOBILE #ifdef GTEST_OS_WINDOWS_MOBILE
FilePath removed_sep(this->RemoveTrailingPathSeparator()); FilePath removed_sep(this->RemoveTrailingPathSeparator());
LPCWSTR unicode = String::AnsiToUtf16(removed_sep.c_str()); LPCWSTR unicode = String::AnsiToUtf16(removed_sep.c_str());
int result = CreateDirectory(unicode, nullptr) ? 0 : -1; int result = CreateDirectory(unicode, nullptr) ? 0 : -1;
delete[] unicode; delete[] unicode;
#elif GTEST_OS_WINDOWS #elif defined(GTEST_OS_WINDOWS)
int result = _mkdir(pathname_.c_str()); int result = _mkdir(pathname_.c_str());
#elif GTEST_OS_ESP8266 || GTEST_OS_XTENSA || GTEST_OS_QURT #elif defined(GTEST_OS_ESP8266) || defined(GTEST_OS_XTENSA) || \
defined(GTEST_OS_QURT) || defined(GTEST_OS_NXP_QN9090) || \
defined(GTEST_OS_NRF52)
// do nothing // do nothing
int result = 0; int result = 0;
#else #else
@ -383,7 +387,7 @@ void FilePath::Normalize() {
auto out = pathname_.begin(); auto out = pathname_.begin();
auto i = pathname_.cbegin(); auto i = pathname_.cbegin();
#if GTEST_OS_WINDOWS #ifdef GTEST_OS_WINDOWS
// UNC paths are treated specially // UNC paths are treated specially
if (pathname_.end() - i >= 3 && IsPathSeparator(*i) && if (pathname_.end() - i >= 3 && IsPathSeparator(*i) &&
IsPathSeparator(*(i + 1)) && !IsPathSeparator(*(i + 2))) { IsPathSeparator(*(i + 1)) && !IsPathSeparator(*(i + 2))) {

View File

@ -46,6 +46,7 @@
#include <memory> #include <memory>
#include <set> #include <set>
#include <string> #include <string>
#include <unordered_map>
#include <vector> #include <vector>
#include "gtest/internal/gtest-port.h" #include "gtest/internal/gtest-port.h"
@ -55,7 +56,7 @@
#include <netdb.h> // NOLINT #include <netdb.h> // NOLINT
#endif #endif
#if GTEST_OS_WINDOWS #ifdef GTEST_OS_WINDOWS
#include <windows.h> // NOLINT #include <windows.h> // NOLINT
#endif // GTEST_OS_WINDOWS #endif // GTEST_OS_WINDOWS
@ -92,7 +93,8 @@ GTEST_API_ TimeInMillis GetTimeInMillis();
// Returns true if and only if Google Test should use colors in the output. // Returns true if and only if Google Test should use colors in the output.
GTEST_API_ bool ShouldUseColor(bool stdout_is_tty); GTEST_API_ bool ShouldUseColor(bool stdout_is_tty);
// Formats the given time in milliseconds as seconds. // Formats the given time in milliseconds as seconds. If the input is an exact N
// seconds, the output has a trailing decimal point (e.g., "N." instead of "N").
GTEST_API_ std::string FormatTimeInMillisAsSeconds(TimeInMillis ms); GTEST_API_ std::string FormatTimeInMillisAsSeconds(TimeInMillis ms);
// Converts the given time in milliseconds to a date string in the ISO 8601 // Converts the given time in milliseconds to a date string in the ISO 8601
@ -311,7 +313,7 @@ void ShuffleRange(internal::Random* random, int begin, int end,
<< begin << ", " << size << "]."; << begin << ", " << size << "].";
// Fisher-Yates shuffle, from // Fisher-Yates shuffle, from
// http://en.wikipedia.org/wiki/Fisher-Yates_shuffle // https://en.wikipedia.org/wiki/Fisher-Yates_shuffle
for (int range_width = end - begin; range_width >= 2; range_width--) { for (int range_width = end - begin; range_width >= 2; range_width--) {
const int last_in_range = begin + range_width - 1; const int last_in_range = begin + range_width - 1;
const int selected = const int selected =
@ -383,13 +385,13 @@ class GTEST_API_ UnitTestOptions {
static bool FilterMatchesTest(const std::string& test_suite_name, static bool FilterMatchesTest(const std::string& test_suite_name,
const std::string& test_name); const std::string& test_name);
#if GTEST_OS_WINDOWS #ifdef GTEST_OS_WINDOWS
// Function for supporting the gtest_catch_exception flag. // Function for supporting the gtest_catch_exception flag.
// Returns EXCEPTION_EXECUTE_HANDLER if Google Test should handle the // Returns EXCEPTION_EXECUTE_HANDLER if given SEH exception was handled, or
// given SEH exception, or EXCEPTION_CONTINUE_SEARCH otherwise. // EXCEPTION_CONTINUE_SEARCH otherwise.
// This function is useful as an __except condition. // This function is useful as an __except condition.
static int GTestShouldProcessSEH(DWORD exception_code); static int GTestProcessSEH(DWORD seh_code, const char* location);
#endif // GTEST_OS_WINDOWS #endif // GTEST_OS_WINDOWS
// Returns true if "name" matches the ':' separated list of glob-style // Returns true if "name" matches the ':' separated list of glob-style
@ -406,8 +408,8 @@ GTEST_API_ FilePath GetCurrentExecutableName();
// The role interface for getting the OS stack trace as a string. // The role interface for getting the OS stack trace as a string.
class OsStackTraceGetterInterface { class OsStackTraceGetterInterface {
public: public:
OsStackTraceGetterInterface() {} OsStackTraceGetterInterface() = default;
virtual ~OsStackTraceGetterInterface() {} virtual ~OsStackTraceGetterInterface() = default;
// Returns the current OS stack trace as an std::string. Parameters: // Returns the current OS stack trace as an std::string. Parameters:
// //
@ -435,13 +437,13 @@ class OsStackTraceGetterInterface {
// A working implementation of the OsStackTraceGetterInterface interface. // A working implementation of the OsStackTraceGetterInterface interface.
class OsStackTraceGetter : public OsStackTraceGetterInterface { class OsStackTraceGetter : public OsStackTraceGetterInterface {
public: public:
OsStackTraceGetter() {} OsStackTraceGetter() = default;
std::string CurrentStackTrace(int max_depth, int skip_count) override; std::string CurrentStackTrace(int max_depth, int skip_count) override;
void UponLeavingGTest() override; void UponLeavingGTest() override;
private: private:
#if GTEST_HAS_ABSL #ifdef GTEST_HAS_ABSL
Mutex mutex_; // Protects all internal state. Mutex mutex_; // Protects all internal state.
// We save the stack frame below the frame that calls user code. // We save the stack frame below the frame that calls user code.
@ -648,13 +650,15 @@ class GTEST_API_ UnitTestImpl {
// this is not a typed or a type-parameterized test. // this is not a typed or a type-parameterized test.
// set_up_tc: pointer to the function that sets up the test suite // set_up_tc: pointer to the function that sets up the test suite
// tear_down_tc: pointer to the function that tears down the test suite // tear_down_tc: pointer to the function that tears down the test suite
TestSuite* GetTestSuite(const char* test_suite_name, const char* type_param, TestSuite* GetTestSuite(const std::string& test_suite_name,
const char* type_param,
internal::SetUpTestSuiteFunc set_up_tc, internal::SetUpTestSuiteFunc set_up_tc,
internal::TearDownTestSuiteFunc tear_down_tc); internal::TearDownTestSuiteFunc tear_down_tc);
// Legacy API is deprecated but still available // Legacy API is deprecated but still available
#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_
TestCase* GetTestCase(const char* test_case_name, const char* type_param, TestCase* GetTestCase(const std::string& test_case_name,
const char* type_param,
internal::SetUpTestSuiteFunc set_up_tc, internal::SetUpTestSuiteFunc set_up_tc,
internal::TearDownTestSuiteFunc tear_down_tc) { internal::TearDownTestSuiteFunc tear_down_tc) {
return GetTestSuite(test_case_name, type_param, set_up_tc, tear_down_tc); return GetTestSuite(test_case_name, type_param, set_up_tc, tear_down_tc);
@ -671,7 +675,7 @@ class GTEST_API_ UnitTestImpl {
void AddTestInfo(internal::SetUpTestSuiteFunc set_up_tc, void AddTestInfo(internal::SetUpTestSuiteFunc set_up_tc,
internal::TearDownTestSuiteFunc tear_down_tc, internal::TearDownTestSuiteFunc tear_down_tc,
TestInfo* test_info) { TestInfo* test_info) {
#if GTEST_HAS_DEATH_TEST #if GTEST_HAS_FILE_SYSTEM
// In order to support thread-safe death tests, we need to // In order to support thread-safe death tests, we need to
// remember the original working directory when the test program // remember the original working directory when the test program
// was first invoked. We cannot do this in RUN_ALL_TESTS(), as // was first invoked. We cannot do this in RUN_ALL_TESTS(), as
@ -680,13 +684,13 @@ class GTEST_API_ UnitTestImpl {
// AddTestInfo(), which is called to register a TEST or TEST_F // AddTestInfo(), which is called to register a TEST or TEST_F
// before main() is reached. // before main() is reached.
if (original_working_dir_.IsEmpty()) { if (original_working_dir_.IsEmpty()) {
original_working_dir_.Set(FilePath::GetCurrentDir()); original_working_dir_ = FilePath::GetCurrentDir();
GTEST_CHECK_(!original_working_dir_.IsEmpty()) GTEST_CHECK_(!original_working_dir_.IsEmpty())
<< "Failed to get the current working directory."; << "Failed to get the current working directory.";
} }
#endif // GTEST_HAS_DEATH_TEST #endif // GTEST_HAS_FILE_SYSTEM
GetTestSuite(test_info->test_suite_name(), test_info->type_param(), GetTestSuite(test_info->test_suite_name_, test_info->type_param(),
set_up_tc, tear_down_tc) set_up_tc, tear_down_tc)
->AddTestInfo(test_info); ->AddTestInfo(test_info);
} }
@ -708,18 +712,6 @@ class GTEST_API_ UnitTestImpl {
return type_parameterized_test_registry_; return type_parameterized_test_registry_;
} }
// Sets the TestSuite object for the test that's currently running.
void set_current_test_suite(TestSuite* a_current_test_suite) {
current_test_suite_ = a_current_test_suite;
}
// Sets the TestInfo object for the test that's currently running. If
// current_test_info is NULL, the assertion results will be stored in
// ad_hoc_test_result_.
void set_current_test_info(TestInfo* a_current_test_info) {
current_test_info_ = a_current_test_info;
}
// Registers all parameterized tests defined using TEST_P and // Registers all parameterized tests defined using TEST_P and
// INSTANTIATE_TEST_SUITE_P, creating regular tests for each test/parameter // INSTANTIATE_TEST_SUITE_P, creating regular tests for each test/parameter
// combination. This method can be called more then once; it has guards // combination. This method can be called more then once; it has guards
@ -777,7 +769,7 @@ class GTEST_API_ UnitTestImpl {
return gtest_trace_stack_.get(); return gtest_trace_stack_.get();
} }
#if GTEST_HAS_DEATH_TEST #ifdef GTEST_HAS_DEATH_TEST
void InitDeathTestSubprocessControlInfo() { void InitDeathTestSubprocessControlInfo() {
internal_run_death_test_flag_.reset(ParseInternalRunDeathTestFlag()); internal_run_death_test_flag_.reset(ParseInternalRunDeathTestFlag());
} }
@ -834,12 +826,34 @@ class GTEST_API_ UnitTestImpl {
bool catch_exceptions() const { return catch_exceptions_; } bool catch_exceptions() const { return catch_exceptions_; }
private: private:
// Returns true if a warning should be issued if no tests match the test
// filter flag.
bool ShouldWarnIfNoTestsMatchFilter() const;
struct CompareTestSuitesByPointer {
bool operator()(const TestSuite* lhs, const TestSuite* rhs) const {
return lhs->name_ < rhs->name_;
}
};
friend class ::testing::UnitTest; friend class ::testing::UnitTest;
// Used by UnitTest::Run() to capture the state of // Used by UnitTest::Run() to capture the state of
// GTEST_FLAG(catch_exceptions) at the moment it starts. // GTEST_FLAG(catch_exceptions) at the moment it starts.
void set_catch_exceptions(bool value) { catch_exceptions_ = value; } void set_catch_exceptions(bool value) { catch_exceptions_ = value; }
// Sets the TestSuite object for the test that's currently running.
void set_current_test_suite(TestSuite* a_current_test_suite) {
current_test_suite_ = a_current_test_suite;
}
// Sets the TestInfo object for the test that's currently running. If
// current_test_info is NULL, the assertion results will be stored in
// ad_hoc_test_result_.
void set_current_test_info(TestInfo* a_current_test_info) {
current_test_info_ = a_current_test_info;
}
// The UnitTest object that owns this implementation object. // The UnitTest object that owns this implementation object.
UnitTest* const parent_; UnitTest* const parent_;
@ -872,6 +886,9 @@ class GTEST_API_ UnitTestImpl {
// elements in the vector. // elements in the vector.
std::vector<TestSuite*> test_suites_; std::vector<TestSuite*> test_suites_;
// The set of TestSuites by name.
std::unordered_map<std::string, TestSuite*> test_suites_by_name_;
// Provides a level of indirection for the test suite list to allow // Provides a level of indirection for the test suite list to allow
// easy shuffling and restoring the test suite order. The i-th // easy shuffling and restoring the test suite order. The i-th
// element of this vector is the index of the i-th test suite in the // element of this vector is the index of the i-th test suite in the
@ -942,7 +959,7 @@ class GTEST_API_ UnitTestImpl {
// How long the test took to run, in milliseconds. // How long the test took to run, in milliseconds.
TimeInMillis elapsed_time_; TimeInMillis elapsed_time_;
#if GTEST_HAS_DEATH_TEST #ifdef GTEST_HAS_DEATH_TEST
// The decomposed components of the gtest_internal_run_death_test flag, // The decomposed components of the gtest_internal_run_death_test flag,
// parsed when RUN_ALL_TESTS is called. // parsed when RUN_ALL_TESTS is called.
std::unique_ptr<InternalRunDeathTestFlag> internal_run_death_test_flag_; std::unique_ptr<InternalRunDeathTestFlag> internal_run_death_test_flag_;
@ -966,7 +983,7 @@ inline UnitTestImpl* GetUnitTestImpl() {
return UnitTest::GetInstance()->impl(); return UnitTest::GetInstance()->impl();
} }
#if GTEST_USES_SIMPLE_RE #ifdef GTEST_USES_SIMPLE_RE
// Internal helper functions for implementing the simple regular // Internal helper functions for implementing the simple regular
// expression matcher. // expression matcher.
@ -992,7 +1009,7 @@ GTEST_API_ bool MatchRegexAnywhere(const char* regex, const char* str);
GTEST_API_ void ParseGoogleTestFlagsOnly(int* argc, char** argv); GTEST_API_ void ParseGoogleTestFlagsOnly(int* argc, char** argv);
GTEST_API_ void ParseGoogleTestFlagsOnly(int* argc, wchar_t** argv); GTEST_API_ void ParseGoogleTestFlagsOnly(int* argc, wchar_t** argv);
#if GTEST_HAS_DEATH_TEST #ifdef GTEST_HAS_DEATH_TEST
// Returns the message describing the last system error, regardless of the // Returns the message describing the last system error, regardless of the
// platform. // platform.
@ -1063,7 +1080,7 @@ class StreamingListener : public EmptyTestEventListener {
// Abstract base class for writing strings to a socket. // Abstract base class for writing strings to a socket.
class AbstractSocketWriter { class AbstractSocketWriter {
public: public:
virtual ~AbstractSocketWriter() {} virtual ~AbstractSocketWriter() = default;
// Sends a string to the socket. // Sends a string to the socket.
virtual void Send(const std::string& message) = 0; virtual void Send(const std::string& message) = 0;

View File

@ -37,8 +37,12 @@
#include <cstdint> #include <cstdint>
#include <fstream> #include <fstream>
#include <memory> #include <memory>
#include <ostream>
#include <string>
#include <utility>
#include <vector>
#if GTEST_OS_WINDOWS #ifdef GTEST_OS_WINDOWS
#include <io.h> #include <io.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <windows.h> #include <windows.h>
@ -51,32 +55,34 @@
#include <unistd.h> #include <unistd.h>
#endif // GTEST_OS_WINDOWS #endif // GTEST_OS_WINDOWS
#if GTEST_OS_MAC #ifdef GTEST_OS_MAC
#include <mach/mach_init.h> #include <mach/mach_init.h>
#include <mach/task.h> #include <mach/task.h>
#include <mach/vm_map.h> #include <mach/vm_map.h>
#endif // GTEST_OS_MAC #endif // GTEST_OS_MAC
#if GTEST_OS_DRAGONFLY || GTEST_OS_FREEBSD || GTEST_OS_GNU_KFREEBSD || \ #if defined(GTEST_OS_DRAGONFLY) || defined(GTEST_OS_FREEBSD) || \
GTEST_OS_NETBSD || GTEST_OS_OPENBSD defined(GTEST_OS_GNU_KFREEBSD) || defined(GTEST_OS_NETBSD) || \
defined(GTEST_OS_OPENBSD)
#include <sys/sysctl.h> #include <sys/sysctl.h>
#if GTEST_OS_DRAGONFLY || GTEST_OS_FREEBSD || GTEST_OS_GNU_KFREEBSD #if defined(GTEST_OS_DRAGONFLY) || defined(GTEST_OS_FREEBSD) || \
defined(GTEST_OS_GNU_KFREEBSD)
#include <sys/user.h> #include <sys/user.h>
#endif #endif
#endif #endif
#if GTEST_OS_QNX #ifdef GTEST_OS_QNX
#include <devctl.h> #include <devctl.h>
#include <fcntl.h> #include <fcntl.h>
#include <sys/procfs.h> #include <sys/procfs.h>
#endif // GTEST_OS_QNX #endif // GTEST_OS_QNX
#if GTEST_OS_AIX #ifdef GTEST_OS_AIX
#include <procinfo.h> #include <procinfo.h>
#include <sys/types.h> #include <sys/types.h>
#endif // GTEST_OS_AIX #endif // GTEST_OS_AIX
#if GTEST_OS_FUCHSIA #ifdef GTEST_OS_FUCHSIA
#include <zircon/process.h> #include <zircon/process.h>
#include <zircon/syscalls.h> #include <zircon/syscalls.h>
#endif // GTEST_OS_FUCHSIA #endif // GTEST_OS_FUCHSIA
@ -90,7 +96,7 @@
namespace testing { namespace testing {
namespace internal { namespace internal {
#if GTEST_OS_LINUX || GTEST_OS_GNU_HURD #if defined(GTEST_OS_LINUX) || defined(GTEST_OS_GNU_HURD)
namespace { namespace {
template <typename T> template <typename T>
@ -113,7 +119,7 @@ size_t GetThreadCount() {
return ReadProcFileField<size_t>(filename, 19); return ReadProcFileField<size_t>(filename, 19);
} }
#elif GTEST_OS_MAC #elif defined(GTEST_OS_MAC)
size_t GetThreadCount() { size_t GetThreadCount() {
const task_t task = mach_task_self(); const task_t task = mach_task_self();
@ -131,20 +137,20 @@ size_t GetThreadCount() {
} }
} }
#elif GTEST_OS_DRAGONFLY || GTEST_OS_FREEBSD || GTEST_OS_GNU_KFREEBSD || \ #elif defined(GTEST_OS_DRAGONFLY) || defined(GTEST_OS_FREEBSD) || \
GTEST_OS_NETBSD defined(GTEST_OS_GNU_KFREEBSD) || defined(GTEST_OS_NETBSD)
#if GTEST_OS_NETBSD #ifdef GTEST_OS_NETBSD
#undef KERN_PROC #undef KERN_PROC
#define KERN_PROC KERN_PROC2 #define KERN_PROC KERN_PROC2
#define kinfo_proc kinfo_proc2 #define kinfo_proc kinfo_proc2
#endif #endif
#if GTEST_OS_DRAGONFLY #ifdef GTEST_OS_DRAGONFLY
#define KP_NLWP(kp) (kp.kp_nthreads) #define KP_NLWP(kp) (kp.kp_nthreads)
#elif GTEST_OS_FREEBSD || GTEST_OS_GNU_KFREEBSD #elif defined(GTEST_OS_FREEBSD) || defined(GTEST_OS_GNU_KFREEBSD)
#define KP_NLWP(kp) (kp.ki_numthreads) #define KP_NLWP(kp) (kp.ki_numthreads)
#elif GTEST_OS_NETBSD #elif defined(GTEST_OS_NETBSD)
#define KP_NLWP(kp) (kp.p_nlwps) #define KP_NLWP(kp) (kp.p_nlwps)
#endif #endif
@ -156,7 +162,7 @@ size_t GetThreadCount() {
KERN_PROC, KERN_PROC,
KERN_PROC_PID, KERN_PROC_PID,
getpid(), getpid(),
#if GTEST_OS_NETBSD #ifdef GTEST_OS_NETBSD
sizeof(struct kinfo_proc), sizeof(struct kinfo_proc),
1, 1,
#endif #endif
@ -169,7 +175,7 @@ size_t GetThreadCount() {
} }
return static_cast<size_t>(KP_NLWP(info)); return static_cast<size_t>(KP_NLWP(info));
} }
#elif GTEST_OS_OPENBSD #elif defined(GTEST_OS_OPENBSD)
// Returns the number of threads running in the process, or 0 to indicate that // Returns the number of threads running in the process, or 0 to indicate that
// we cannot detect it. // we cannot detect it.
@ -193,8 +199,8 @@ size_t GetThreadCount() {
mib[5] = static_cast<int>(size / static_cast<size_t>(mib[4])); mib[5] = static_cast<int>(size / static_cast<size_t>(mib[4]));
// populate array of structs // populate array of structs
struct kinfo_proc info[mib[5]]; std::vector<struct kinfo_proc> info(mib[5]);
if (sysctl(mib, miblen, &info, &size, NULL, 0)) { if (sysctl(mib, miblen, info.data(), &size, NULL, 0)) {
return 0; return 0;
} }
@ -206,7 +212,7 @@ size_t GetThreadCount() {
return nthreads; return nthreads;
} }
#elif GTEST_OS_QNX #elif defined(GTEST_OS_QNX)
// Returns the number of threads running in the process, or 0 to indicate that // Returns the number of threads running in the process, or 0 to indicate that
// we cannot detect it. // we cannot detect it.
@ -226,7 +232,7 @@ size_t GetThreadCount() {
} }
} }
#elif GTEST_OS_AIX #elif defined(GTEST_OS_AIX)
size_t GetThreadCount() { size_t GetThreadCount() {
struct procentry64 entry; struct procentry64 entry;
@ -239,7 +245,7 @@ size_t GetThreadCount() {
} }
} }
#elif GTEST_OS_FUCHSIA #elif defined(GTEST_OS_FUCHSIA)
size_t GetThreadCount() { size_t GetThreadCount() {
int dummy_buffer; int dummy_buffer;
@ -264,7 +270,7 @@ size_t GetThreadCount() {
#endif // GTEST_OS_LINUX #endif // GTEST_OS_LINUX
#if GTEST_IS_THREADSAFE && GTEST_OS_WINDOWS #if defined(GTEST_IS_THREADSAFE) && defined(GTEST_OS_WINDOWS)
AutoHandle::AutoHandle() : handle_(INVALID_HANDLE_VALUE) {} AutoHandle::AutoHandle() : handle_(INVALID_HANDLE_VALUE) {}
@ -581,9 +587,11 @@ class ThreadLocalRegistryImpl {
// thread's ID. // thread's ID.
typedef std::map<DWORD, ThreadLocalValues> ThreadIdToThreadLocals; typedef std::map<DWORD, ThreadLocalValues> ThreadIdToThreadLocals;
// Holds the thread id and thread handle that we pass from struct WatcherThreadParams {
// StartWatcherThreadFor to WatcherThreadFunc. DWORD thread_id;
typedef std::pair<DWORD, HANDLE> ThreadIdAndHandle; HANDLE handle;
Notification has_initialized;
};
static void StartWatcherThreadFor(DWORD thread_id) { static void StartWatcherThreadFor(DWORD thread_id) {
// The returned handle will be kept in thread_map and closed by // The returned handle will be kept in thread_map and closed by
@ -591,14 +599,19 @@ class ThreadLocalRegistryImpl {
HANDLE thread = HANDLE thread =
::OpenThread(SYNCHRONIZE | THREAD_QUERY_INFORMATION, FALSE, thread_id); ::OpenThread(SYNCHRONIZE | THREAD_QUERY_INFORMATION, FALSE, thread_id);
GTEST_CHECK_(thread != nullptr); GTEST_CHECK_(thread != nullptr);
WatcherThreadParams* watcher_thread_params = new WatcherThreadParams;
watcher_thread_params->thread_id = thread_id;
watcher_thread_params->handle = thread;
// We need to pass a valid thread ID pointer into CreateThread for it // We need to pass a valid thread ID pointer into CreateThread for it
// to work correctly under Win98. // to work correctly under Win98.
DWORD watcher_thread_id; DWORD watcher_thread_id;
HANDLE watcher_thread = ::CreateThread( HANDLE watcher_thread =
nullptr, // Default security. ::CreateThread(nullptr, // Default security.
0, // Default stack size 0, // Default stack size
&ThreadLocalRegistryImpl::WatcherThreadFunc, &ThreadLocalRegistryImpl::WatcherThreadFunc,
reinterpret_cast<LPVOID>(new ThreadIdAndHandle(thread_id, thread)), reinterpret_cast<LPVOID>(watcher_thread_params),
CREATE_SUSPENDED, &watcher_thread_id); CREATE_SUSPENDED, &watcher_thread_id);
GTEST_CHECK_(watcher_thread != nullptr) GTEST_CHECK_(watcher_thread != nullptr)
<< "CreateThread failed with error " << ::GetLastError() << "."; << "CreateThread failed with error " << ::GetLastError() << ".";
@ -608,17 +621,25 @@ class ThreadLocalRegistryImpl {
::GetThreadPriority(::GetCurrentThread())); ::GetThreadPriority(::GetCurrentThread()));
::ResumeThread(watcher_thread); ::ResumeThread(watcher_thread);
::CloseHandle(watcher_thread); ::CloseHandle(watcher_thread);
// Wait for the watcher thread to start to avoid race conditions.
// One specific race condition that can happen is that we have returned
// from main and have started to tear down, the newly spawned watcher
// thread may access already-freed variables, like global shared_ptrs.
watcher_thread_params->has_initialized.WaitForNotification();
} }
// Monitors exit from a given thread and notifies those // Monitors exit from a given thread and notifies those
// ThreadIdToThreadLocals about thread termination. // ThreadIdToThreadLocals about thread termination.
static DWORD WINAPI WatcherThreadFunc(LPVOID param) { static DWORD WINAPI WatcherThreadFunc(LPVOID param) {
const ThreadIdAndHandle* tah = WatcherThreadParams* watcher_thread_params =
reinterpret_cast<const ThreadIdAndHandle*>(param); reinterpret_cast<WatcherThreadParams*>(param);
GTEST_CHECK_(::WaitForSingleObject(tah->second, INFINITE) == WAIT_OBJECT_0); watcher_thread_params->has_initialized.Notify();
OnThreadExit(tah->first); GTEST_CHECK_(::WaitForSingleObject(watcher_thread_params->handle,
::CloseHandle(tah->second); INFINITE) == WAIT_OBJECT_0);
delete tah; OnThreadExit(watcher_thread_params->thread_id);
::CloseHandle(watcher_thread_params->handle);
delete watcher_thread_params;
return 0; return 0;
} }
@ -655,7 +676,7 @@ void ThreadLocalRegistry::OnThreadLocalDestroyed(
#endif // GTEST_IS_THREADSAFE && GTEST_OS_WINDOWS #endif // GTEST_IS_THREADSAFE && GTEST_OS_WINDOWS
#if GTEST_USES_POSIX_RE #ifdef GTEST_USES_POSIX_RE
// Implements RE. Currently only needed for death tests. // Implements RE. Currently only needed for death tests.
@ -668,7 +689,6 @@ RE::~RE() {
regfree(&partial_regex_); regfree(&partial_regex_);
regfree(&full_regex_); regfree(&full_regex_);
} }
free(const_cast<char*>(pattern_));
} }
// Returns true if and only if regular expression re matches the entire str. // Returns true if and only if regular expression re matches the entire str.
@ -690,7 +710,18 @@ bool RE::PartialMatch(const char* str, const RE& re) {
// Initializes an RE from its string representation. // Initializes an RE from its string representation.
void RE::Init(const char* regex) { void RE::Init(const char* regex) {
pattern_ = posix::StrDup(regex); pattern_ = regex;
// NetBSD (and Android, which takes its regex implemntation from NetBSD) does
// not include the GNU regex extensions (such as Perl style character classes
// like \w) in REG_EXTENDED. REG_EXTENDED is only specified to include the
// [[:alpha:]] style character classes. Enable REG_GNU wherever it is defined
// so users can use those extensions.
#if defined(REG_GNU)
constexpr int reg_flags = REG_EXTENDED | REG_GNU;
#else
constexpr int reg_flags = REG_EXTENDED;
#endif
// Reserves enough bytes to hold the regular expression used for a // Reserves enough bytes to hold the regular expression used for a
// full match. // full match.
@ -698,7 +729,7 @@ void RE::Init(const char* regex) {
char* const full_pattern = new char[full_regex_len]; char* const full_pattern = new char[full_regex_len];
snprintf(full_pattern, full_regex_len, "^(%s)$", regex); snprintf(full_pattern, full_regex_len, "^(%s)$", regex);
is_valid_ = regcomp(&full_regex_, full_pattern, REG_EXTENDED) == 0; is_valid_ = regcomp(&full_regex_, full_pattern, reg_flags) == 0;
// We want to call regcomp(&partial_regex_, ...) even if the // We want to call regcomp(&partial_regex_, ...) even if the
// previous expression returns false. Otherwise partial_regex_ may // previous expression returns false. Otherwise partial_regex_ may
// not be properly initialized can may cause trouble when it's // not be properly initialized can may cause trouble when it's
@ -709,7 +740,7 @@ void RE::Init(const char* regex) {
// regex. We change it to an equivalent form "()" to be safe. // regex. We change it to an equivalent form "()" to be safe.
if (is_valid_) { if (is_valid_) {
const char* const partial_regex = (*regex == '\0') ? "()" : regex; const char* const partial_regex = (*regex == '\0') ? "()" : regex;
is_valid_ = regcomp(&partial_regex_, partial_regex, REG_EXTENDED) == 0; is_valid_ = regcomp(&partial_regex_, partial_regex, reg_flags) == 0;
} }
EXPECT_TRUE(is_valid_) EXPECT_TRUE(is_valid_)
<< "Regular expression \"" << regex << "Regular expression \"" << regex
@ -718,7 +749,7 @@ void RE::Init(const char* regex) {
delete[] full_pattern; delete[] full_pattern;
} }
#elif GTEST_USES_SIMPLE_RE #elif defined(GTEST_USES_SIMPLE_RE)
// Returns true if and only if ch appears anywhere in str (excluding the // Returns true if and only if ch appears anywhere in str (excluding the
// terminating '\0' character). // terminating '\0' character).
@ -920,27 +951,26 @@ bool MatchRegexAnywhere(const char* regex, const char* str) {
// Implements the RE class. // Implements the RE class.
RE::~RE() { RE::~RE() = default;
free(const_cast<char*>(pattern_));
free(const_cast<char*>(full_pattern_));
}
// Returns true if and only if regular expression re matches the entire str. // Returns true if and only if regular expression re matches the entire str.
bool RE::FullMatch(const char* str, const RE& re) { bool RE::FullMatch(const char* str, const RE& re) {
return re.is_valid_ && MatchRegexAnywhere(re.full_pattern_, str); return re.is_valid_ && MatchRegexAnywhere(re.full_pattern_.c_str(), str);
} }
// Returns true if and only if regular expression re matches a substring of // Returns true if and only if regular expression re matches a substring of
// str (including str itself). // str (including str itself).
bool RE::PartialMatch(const char* str, const RE& re) { bool RE::PartialMatch(const char* str, const RE& re) {
return re.is_valid_ && MatchRegexAnywhere(re.pattern_, str); return re.is_valid_ && MatchRegexAnywhere(re.pattern_.c_str(), str);
} }
// Initializes an RE from its string representation. // Initializes an RE from its string representation.
void RE::Init(const char* regex) { void RE::Init(const char* regex) {
pattern_ = full_pattern_ = nullptr; full_pattern_.clear();
pattern_.clear();
if (regex != nullptr) { if (regex != nullptr) {
pattern_ = posix::StrDup(regex); pattern_ = regex;
} }
is_valid_ = ValidateRegex(regex); is_valid_ = ValidateRegex(regex);
@ -949,25 +979,19 @@ void RE::Init(const char* regex) {
return; return;
} }
const size_t len = strlen(regex);
// Reserves enough bytes to hold the regular expression used for a // Reserves enough bytes to hold the regular expression used for a
// full match: we need space to prepend a '^', append a '$', and // full match: we need space to prepend a '^' and append a '$'.
// terminate the string with '\0'. full_pattern_.reserve(pattern_.size() + 2);
char* buffer = static_cast<char*>(malloc(len + 3));
full_pattern_ = buffer;
if (*regex != '^') if (pattern_.empty() || pattern_.front() != '^') {
*buffer++ = '^'; // Makes sure full_pattern_ starts with '^'. full_pattern_.push_back('^'); // Makes sure full_pattern_ starts with '^'.
}
// We don't use snprintf or strncpy, as they trigger a warning when full_pattern_.append(pattern_);
// compiled with VC++ 8.0.
memcpy(buffer, regex, len);
buffer += len;
if (len == 0 || regex[len - 1] != '$') if (pattern_.empty() || pattern_.back() != '$') {
*buffer++ = '$'; // Makes sure full_pattern_ ends with '$'. full_pattern_.push_back('$'); // Makes sure full_pattern_ ends with '$'.
}
*buffer = '\0';
} }
#endif // GTEST_USES_POSIX_RE #endif // GTEST_USES_POSIX_RE
@ -1024,18 +1048,28 @@ GTestLog::~GTestLog() {
} }
} }
#if GTEST_HAS_STREAM_REDIRECTION
// Disable Microsoft deprecation warnings for POSIX functions called from // Disable Microsoft deprecation warnings for POSIX functions called from
// this class (creat, dup, dup2, and close) // this class (creat, dup, dup2, and close)
GTEST_DISABLE_MSC_DEPRECATED_PUSH_() GTEST_DISABLE_MSC_DEPRECATED_PUSH_()
#if GTEST_HAS_STREAM_REDIRECTION namespace {
#if defined(GTEST_OS_LINUX_ANDROID) || defined(GTEST_OS_IOS)
bool EndsWithPathSeparator(const std::string& path) {
return !path.empty() && path.back() == GTEST_PATH_SEP_[0];
}
#endif
} // namespace
// Object that captures an output stream (stdout/stderr). // Object that captures an output stream (stdout/stderr).
class CapturedStream { class CapturedStream {
public: public:
// The ctor redirects the stream to a temporary file. // The ctor redirects the stream to a temporary file.
explicit CapturedStream(int fd) : fd_(fd), uncaptured_fd_(dup(fd)) { explicit CapturedStream(int fd) : fd_(fd), uncaptured_fd_(dup(fd)) {
#if GTEST_OS_WINDOWS #ifdef GTEST_OS_WINDOWS
char temp_dir_path[MAX_PATH + 1] = {'\0'}; // NOLINT char temp_dir_path[MAX_PATH + 1] = {'\0'}; // NOLINT
char temp_file_path[MAX_PATH + 1] = {'\0'}; // NOLINT char temp_file_path[MAX_PATH + 1] = {'\0'}; // NOLINT
@ -1054,7 +1088,7 @@ class CapturedStream {
// directory, so we create the temporary file in a temporary directory. // directory, so we create the temporary file in a temporary directory.
std::string name_template; std::string name_template;
#if GTEST_OS_LINUX_ANDROID #ifdef GTEST_OS_LINUX_ANDROID
// Note: Android applications are expected to call the framework's // Note: Android applications are expected to call the framework's
// Context.getExternalStorageDirectory() method through JNI to get // Context.getExternalStorageDirectory() method through JNI to get
// the location of the world-writable SD Card directory. However, // the location of the world-writable SD Card directory. However,
@ -1066,8 +1100,14 @@ class CapturedStream {
// The location /data/local/tmp is directly accessible from native code. // The location /data/local/tmp is directly accessible from native code.
// '/sdcard' and other variants cannot be relied on, as they are not // '/sdcard' and other variants cannot be relied on, as they are not
// guaranteed to be mounted, or may have a delay in mounting. // guaranteed to be mounted, or may have a delay in mounting.
name_template = "/data/local/tmp/"; //
#elif GTEST_OS_IOS // However, prefer using the TMPDIR environment variable if set, as newer
// devices may have /data/local/tmp read-only.
name_template = TempDir();
if (!EndsWithPathSeparator(name_template))
name_template.push_back(GTEST_PATH_SEP_[0]);
#elif defined(GTEST_OS_IOS)
char user_temp_dir[PATH_MAX + 1]; char user_temp_dir[PATH_MAX + 1];
// Documented alternative to NSTemporaryDirectory() (for obtaining creating // Documented alternative to NSTemporaryDirectory() (for obtaining creating
@ -1086,7 +1126,7 @@ class CapturedStream {
::confstr(_CS_DARWIN_USER_TEMP_DIR, user_temp_dir, sizeof(user_temp_dir)); ::confstr(_CS_DARWIN_USER_TEMP_DIR, user_temp_dir, sizeof(user_temp_dir));
name_template = user_temp_dir; name_template = user_temp_dir;
if (name_template.back() != GTEST_PATH_SEP_[0]) if (!EndsWithPathSeparator(name_template))
name_template.push_back(GTEST_PATH_SEP_[0]); name_template.push_back(GTEST_PATH_SEP_[0]);
#else #else
name_template = "/tmp/"; name_template = "/tmp/";
@ -1227,7 +1267,7 @@ std::string ReadEntireFile(FILE* file) {
return content; return content;
} }
#if GTEST_HAS_DEATH_TEST #ifdef GTEST_HAS_DEATH_TEST
static const std::vector<std::string>* g_injected_test_argvs = static const std::vector<std::string>* g_injected_test_argvs =
nullptr; // Owned. nullptr; // Owned.
@ -1254,7 +1294,7 @@ void ClearInjectableArgvs() {
} }
#endif // GTEST_HAS_DEATH_TEST #endif // GTEST_HAS_DEATH_TEST
#if GTEST_OS_WINDOWS_MOBILE #ifdef GTEST_OS_WINDOWS_MOBILE
namespace posix { namespace posix {
void Abort() { void Abort() {
DebugBreak(); DebugBreak();
@ -1308,8 +1348,8 @@ bool ParseInt32(const Message& src_text, const char* str, int32_t* value) {
) { ) {
Message msg; Message msg;
msg << "WARNING: " << src_text msg << "WARNING: " << src_text
<< " is expected to be a 32-bit integer, but actually" << " is expected to be a 32-bit integer, but actually" << " has value "
<< " has value " << str << ", which overflows.\n"; << str << ", which overflows.\n";
printf("%s", msg.GetString().c_str()); printf("%s", msg.GetString().c_str());
fflush(stdout); fflush(stdout);
return false; return false;

Some files were not shown because too many files have changed in this diff Show More