mirror of
https://github.com/google/googletest.git
synced 2025-12-06 16:57:00 +08:00
Compare commits
262 Commits
v1.14.0-pr
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1b96fa13f5 | ||
|
|
085af2cc08 | ||
|
|
37678c92fb | ||
|
|
dedab73a68 | ||
|
|
6ec14dfd8c | ||
|
|
17d335d7c7 | ||
|
|
4fe3307fb2 | ||
|
|
b2b9072ecb | ||
|
|
e17e37a115 | ||
|
|
8dbd60f7d5 | ||
|
|
2ce9d8f2e8 | ||
|
|
279f847946 | ||
|
|
de1c609262 | ||
|
|
9706f75b8f | ||
|
|
50b8600c63 | ||
|
|
0934b7e112 | ||
|
|
4969d0ad54 | ||
|
|
9df216cc9d | ||
|
|
7917641ff9 | ||
|
|
eb2d85edd0 | ||
|
|
6986c2b575 | ||
|
|
a05c091507 | ||
|
|
244cec869d | ||
|
|
373af2e3df | ||
|
|
32f9f4c82a | ||
|
|
7e17b15f15 | ||
|
|
309dab8d4b | ||
|
|
3983f67e32 | ||
|
|
c67de11737 | ||
|
|
a45468c0fc | ||
|
|
f8ed0e687c | ||
|
|
35b75a2cba | ||
|
|
175c1b55cf | ||
|
|
1aeec48a1d | ||
|
|
0fe21ac6ef | ||
|
|
fd15f51d57 | ||
|
|
6230d316e1 | ||
|
|
28e9d1f267 | ||
|
|
7e2c425db2 | ||
|
|
e9092b12dc | ||
|
|
7427a6b5e3 | ||
|
|
7da55820cc | ||
|
|
3abc68be30 | ||
|
|
09ffd00153 | ||
|
|
6aa03e6774 | ||
|
|
16d4f8eff6 | ||
|
|
bac6a8fd8a | ||
|
|
fa8438ae6b | ||
|
|
571930618f | ||
|
|
9f79a9597a | ||
|
|
8b8ef3ff0d | ||
|
|
90a4152114 | ||
|
|
04ee1b4f2a | ||
|
|
54915d462b | ||
|
|
59c924bc47 | ||
|
|
00b2154e8e | ||
|
|
cd430b47a5 | ||
|
|
155b337c93 | ||
|
|
e90fe24856 | ||
|
|
50a9511f50 | ||
|
|
52204f78f9 | ||
|
|
2ae29b52fd | ||
|
|
c2ceb2b09b | ||
|
|
e7b26b7246 | ||
|
|
3af834740f | ||
|
|
4902ea2d7c | ||
|
|
4ee4b17bf5 | ||
|
|
0bdccf4aa2 | ||
|
|
e88cb95b92 | ||
|
|
24a9e940d4 | ||
|
|
72189081ca | ||
|
|
144d335538 | ||
|
|
e5669fdffc | ||
|
|
54501746a6 | ||
|
|
3fbe4db9a3 | ||
|
|
a6ce08abf7 | ||
|
|
c00fd25b71 | ||
|
|
4a00a24fff | ||
|
|
a866428a78 | ||
|
|
445e9bd8d0 | ||
|
|
e5443e5c65 | ||
|
|
e235eb34c6 | ||
|
|
66d7401378 | ||
|
|
b4aae50ce1 | ||
|
|
2b6b042a77 | ||
|
|
e4ece4881d | ||
|
|
005254dae2 | ||
|
|
504ea69cf7 | ||
|
|
4bbf80823c | ||
|
|
7d76a231b0 | ||
|
|
e54519b094 | ||
|
|
f3c355f9dd | ||
|
|
79219e26e0 | ||
|
|
d122c0d435 | ||
|
|
35d0c36560 | ||
|
|
7927f8e93d | ||
|
|
d144031940 | ||
|
|
1204d63444 | ||
|
|
5ed2186395 | ||
|
|
df1544bcee | ||
|
|
62df7bdbc1 | ||
|
|
71815bbf7d | ||
|
|
a1e255a582 | ||
|
|
6dae7eb4a5 | ||
|
|
0953a17a42 | ||
|
|
ff233bdd4c | ||
|
|
3e3b44c300 | ||
|
|
ffa31aec1c | ||
|
|
5bcb2d78a1 | ||
|
|
352788321f | ||
|
|
57e107a10e | ||
|
|
cee1ba1f24 | ||
|
|
9ff2450a56 | ||
|
|
b62593aceb | ||
|
|
d49a665484 | ||
|
|
417158b8bc | ||
|
|
7321f950df | ||
|
|
4b21f1abdd | ||
|
|
b4aaf97d8f | ||
|
|
3ef16ef8b3 | ||
|
|
34ad51b3dc | ||
|
|
1d17ea141d | ||
|
|
a7f443b80b | ||
|
|
305e5a238b | ||
|
|
9b4993ca7d | ||
|
|
c8393f8554 | ||
|
|
33af80a883 | ||
|
|
fa6de7f438 | ||
|
|
2d684efe87 | ||
|
|
2d16ed055d | ||
|
|
2954cb8d87 | ||
|
|
5035ef3473 | ||
|
|
fbc93e61c4 | ||
|
|
d83fee138a | ||
|
|
5a37b517ad | ||
|
|
5197b1a8e6 | ||
|
|
b1a777f319 | ||
|
|
3d73dee972 | ||
|
|
f10e11fb27 | ||
|
|
0af976647f | ||
|
|
61db1e1740 | ||
|
|
d3a29ff624 | ||
|
|
f16770d63d | ||
|
|
ec7b38672c | ||
|
|
a2f106c7e2 | ||
|
|
c4fec74514 | ||
|
|
77afe8e014 | ||
|
|
a7678dd0db | ||
|
|
f7a6bba24c | ||
|
|
53ee0fc0b2 | ||
|
|
f1269cc220 | ||
|
|
eff443c6ef | ||
|
|
a1a608348e | ||
|
|
c231e6f5b1 | ||
|
|
b479e7a3c1 | ||
|
|
e1a38bc370 | ||
|
|
31993dfa6b | ||
|
|
b9059aaa4c | ||
|
|
926f681a90 | ||
|
|
e4fdb87e76 | ||
|
|
e15c5a51b8 | ||
|
|
3b6d48e8d5 | ||
|
|
814ba36338 | ||
|
|
4c676b94b8 | ||
|
|
9d43b27f7a | ||
|
|
db38b59fa0 | ||
|
|
5df0241ea4 | ||
|
|
9756ee7cba | ||
|
|
6eb225cb88 | ||
|
|
f5448b3d71 | ||
|
|
b75ecf1bed | ||
|
|
96519a4019 | ||
|
|
64be1c79fa | ||
|
|
48729681ad | ||
|
|
456574145c | ||
|
|
3cb7ce90b3 | ||
|
|
6fdb4c303f | ||
|
|
fc0076ffc4 | ||
|
|
6a5938233b | ||
|
|
563ebf1769 | ||
|
|
1cab76c7b8 | ||
|
|
bd30c39d61 | ||
|
|
ffc477e705 | ||
|
|
96cd50c082 | ||
|
|
f6f920d2ba | ||
|
|
cfe5076a8b | ||
|
|
778badf63a | ||
|
|
f57505314b | ||
|
|
7c07a86369 | ||
|
|
7f409cb3c0 | ||
|
|
3b26f7e53b | ||
|
|
dda72ef321 | ||
|
|
dddb219c3e | ||
|
|
96eadf659f | ||
|
|
cde9a745a6 | ||
|
|
8495449f07 | ||
|
|
530d5c8c84 | ||
|
|
eb80f759d5 | ||
|
|
b3a9ba2b8e | ||
|
|
8760db154a | ||
|
|
48b373f6e0 | ||
|
|
76bb2afb8b | ||
|
|
b10fad38c4 | ||
|
|
5b7fd63d6d | ||
|
|
7f036c5563 | ||
|
|
518387203b | ||
|
|
116b7e5528 | ||
|
|
9bb354fa83 | ||
|
|
829c19901d | ||
|
|
2dd1c13195 | ||
|
|
beb552fb47 | ||
|
|
a6d7fa8c0c | ||
|
|
2d09ed35cf | ||
|
|
e47544ad31 | ||
|
|
5bd0f08e0d | ||
|
|
e40661d89b | ||
|
|
80306a7b5e | ||
|
|
167a2255ae | ||
|
|
e27b194c27 | ||
|
|
adc5145386 | ||
|
|
8be20cce69 | ||
|
|
d1467f5813 | ||
|
|
728ec52d21 | ||
|
|
af5ddbde4d | ||
|
|
eab0e7e289 | ||
|
|
b86bf840d1 | ||
|
|
edf9b4964d | ||
|
|
8a6feabf04 | ||
|
|
460ae98267 | ||
|
|
f0eae4b399 | ||
|
|
cb5cd96bca | ||
|
|
c374da15c5 | ||
|
|
61332bd7e8 | ||
|
|
762d3a7f66 | ||
|
|
c759a58a2d | ||
|
|
722daa3da6 | ||
|
|
9fce548044 | ||
|
|
14d05f4708 | ||
|
|
5e47c767ff | ||
|
|
f42da0e443 | ||
|
|
e24cced08d | ||
|
|
987e225614 | ||
|
|
dd9a956904 | ||
|
|
6513d0272d | ||
|
|
fc234d6d18 | ||
|
|
d76e9e0dd9 | ||
|
|
8ca57f194a | ||
|
|
5b5ef29950 | ||
|
|
622ee5cf29 | ||
|
|
619601c5e2 | ||
|
|
7e33b6a1c4 | ||
|
|
89b25572db | ||
|
|
77485ff004 | ||
|
|
22eb2de1ef | ||
|
|
46db91ef6f | ||
|
|
6abc9e3d79 | ||
|
|
9ef5e82269 | ||
|
|
455fcb7773 | ||
|
|
429432e341 | ||
|
|
843976e4f5 | ||
|
|
2f83a6e8f8 | ||
|
|
b42fa26690 |
43
.github/workflows/gtest-ci.yml
vendored
43
.github/workflows/gtest-ci.yml
vendored
@ -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 ...
|
|
||||||
1
.gitignore
vendored
1
.gitignore
vendored
@ -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
|
||||||
|
|
||||||
|
|||||||
55
BUILD.bazel
55
BUILD.bazel
@ -30,6 +30,8 @@
|
|||||||
#
|
#
|
||||||
# Bazel Build for Google C++ Testing Framework(Google Test)
|
# Bazel Build for Google C++ Testing Framework(Google Test)
|
||||||
|
|
||||||
|
load("@rules_cc//cc:defs.bzl", "cc_library", "cc_test")
|
||||||
|
|
||||||
package(default_visibility = ["//visibility:public"])
|
package(default_visibility = ["//visibility:public"])
|
||||||
|
|
||||||
licenses(["notice"])
|
licenses(["notice"])
|
||||||
@ -56,6 +58,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 +85,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,24 +144,41 @@ cc_library(
|
|||||||
}),
|
}),
|
||||||
deps = select({
|
deps = select({
|
||||||
":has_absl": [
|
":has_absl": [
|
||||||
"@com_google_absl//absl/container:flat_hash_set",
|
"@abseil-cpp//absl/container:flat_hash_set",
|
||||||
"@com_google_absl//absl/debugging:failure_signal_handler",
|
"@abseil-cpp//absl/debugging:failure_signal_handler",
|
||||||
"@com_google_absl//absl/debugging:stacktrace",
|
"@abseil-cpp//absl/debugging:stacktrace",
|
||||||
"@com_google_absl//absl/debugging:symbolize",
|
"@abseil-cpp//absl/debugging:symbolize",
|
||||||
"@com_google_absl//absl/flags:flag",
|
"@abseil-cpp//absl/flags:flag",
|
||||||
"@com_google_absl//absl/flags:parse",
|
"@abseil-cpp//absl/flags:parse",
|
||||||
"@com_google_absl//absl/flags:reflection",
|
"@abseil-cpp//absl/flags:reflection",
|
||||||
"@com_google_absl//absl/flags:usage",
|
"@abseil-cpp//absl/flags:usage",
|
||||||
"@com_google_absl//absl/strings",
|
"@abseil-cpp//absl/strings",
|
||||||
"@com_google_absl//absl/types:any",
|
"@re2",
|
||||||
"@com_google_absl//absl/types:optional",
|
],
|
||||||
"@com_google_absl//absl/types:variant",
|
"//conditions:default": [],
|
||||||
"@com_googlesource_code_re2//:re2",
|
}) + 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/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"],
|
||||||
|
|||||||
@ -1,10 +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.13)
|
cmake_minimum_required(VERSION 3.16)
|
||||||
|
|
||||||
project(googletest-distribution)
|
project(googletest-distribution)
|
||||||
set(GOOGLETEST_VERSION 1.14.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)
|
||||||
@ -15,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()
|
||||||
|
|||||||
@ -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
|
||||||
|
|||||||
@ -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>
|
||||||
|
|||||||
81
MODULE.bazel
Normal file
81
MODULE.bazel
Normal file
@ -0,0 +1,81 @@
|
|||||||
|
# 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 = "20250814.0",
|
||||||
|
)
|
||||||
|
bazel_dep(
|
||||||
|
name = "platforms",
|
||||||
|
version = "0.0.11",
|
||||||
|
)
|
||||||
|
bazel_dep(
|
||||||
|
name = "re2",
|
||||||
|
version = "2024-07-02.bcr.1",
|
||||||
|
)
|
||||||
|
|
||||||
|
bazel_dep(
|
||||||
|
name = "rules_cc",
|
||||||
|
version = "0.2.8"
|
||||||
|
)
|
||||||
|
|
||||||
|
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")
|
||||||
30
README.md
30
README.md
@ -2,40 +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).
|
|
||||||
We do publish occasional semantic versions, tagged with
|
|
||||||
`v${major}.${minor}.${patch}` (e.g. `v1.13.0`).
|
|
||||||
|
|
||||||
#### 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.13.0
|
#### Release 1.17.0
|
||||||
|
|
||||||
[Release 1.13.0](https://github.com/google/googletest/releases/tag/v1.13.0) is
|
[Release 1.17.0](https://github.com/google/googletest/releases/tag/v1.17.0) is
|
||||||
now available.
|
now available.
|
||||||
|
|
||||||
The 1.13.x branch requires at least C++14.
|
The 1.17.x branch
|
||||||
|
[requires at least C++17](https://opensource.google/documentation/policies/cplusplus-support#c_language_standard).
|
||||||
|
|
||||||
#### Continuous Integration
|
#### Continuous Integration
|
||||||
|
|
||||||
We use Google's internal systems for continuous integration. \
|
We use Google's internal systems for continuous integration.
|
||||||
GitHub Actions were added for the convenience of open-source contributors. They
|
|
||||||
are exclusively maintained by the open-source community and not used by the
|
|
||||||
GoogleTest team.
|
|
||||||
|
|
||||||
#### 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!
|
||||||
|
|
||||||
@ -100,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
|
||||||
|
|
||||||
|
|||||||
64
WORKSPACE
64
WORKSPACE
@ -1,4 +1,34 @@
|
|||||||
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")
|
load("//:googletest_deps.bzl", "googletest_deps")
|
||||||
googletest_deps()
|
googletest_deps()
|
||||||
@ -6,22 +36,26 @@ 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 = "rules_python", # 2023-07-31T20:39:27Z
|
name = "rules_python",
|
||||||
sha256 = "1250b59a33c591a1c4ba68c62e95fc88a84c334ec35a2e23f46cbc1b9a5a8b55",
|
sha256 = "2cc26bbd53854ceb76dd42a834b1002cd4ba7f8df35440cf03482e045affc244",
|
||||||
strip_prefix = "rules_python-e355becc30275939d87116a4ec83dad4bb50d9e1",
|
strip_prefix = "rules_python-1.3.0",
|
||||||
urls = ["https://github.com/bazelbuild/rules_python/archive/e355becc30275939d87116a4ec83dad4bb50d9e1.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()
|
||||||
|
|
||||||
http_archive(
|
http_archive(
|
||||||
name = "bazel_skylib", # 2023-05-31T19:24:07Z
|
name = "platforms",
|
||||||
sha256 = "08c0386f45821ce246bbbf77503c973246ed6ee5c3463e41efc197fa9bc3a7f4",
|
urls = [
|
||||||
strip_prefix = "bazel-skylib-288731ef9f7f688932bd50e704a91a45ec185f9b",
|
"https://mirror.bazel.build/github.com/bazelbuild/platforms/releases/download/0.0.11/platforms-0.0.11.tar.gz",
|
||||||
urls = ["https://github.com/bazelbuild/bazel-skylib/archive/288731ef9f7f688932bd50e704a91a45ec185f9b.zip"],
|
"https://github.com/bazelbuild/platforms/releases/download/0.0.11/platforms-0.0.11.tar.gz",
|
||||||
|
],
|
||||||
|
sha256 = "29742e87275809b5e598dc2f04d86960cc7a55b3067d97221c9abbc9926bff0f",
|
||||||
)
|
)
|
||||||
|
|
||||||
http_archive(
|
load("@bazel_features//:deps.bzl", "bazel_features_deps")
|
||||||
name = "platforms", # 2023-07-28T19:44:27Z
|
bazel_features_deps()
|
||||||
sha256 = "40eb313613ff00a5c03eed20aba58890046f4d38dec7344f00bb9a8867853526",
|
|
||||||
strip_prefix = "platforms-4ad40ef271da8176d4fc0194d2089b8a76e19d7b",
|
load("@rules_cc//cc:extensions.bzl", "compatibility_proxy_repo")
|
||||||
urls = ["https://github.com/bazelbuild/platforms/archive/4ad40ef271da8176d4fc0194d2089b8a76e19d7b.zip"],
|
compatibility_proxy_repo()
|
||||||
)
|
|
||||||
|
|||||||
35
WORKSPACE.bzlmod
Normal file
35
WORKSPACE.bzlmod
Normal 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.
|
||||||
@ -31,39 +31,67 @@
|
|||||||
|
|
||||||
set -euox pipefail
|
set -euox pipefail
|
||||||
|
|
||||||
readonly LINUX_LATEST_CONTAINER="gcr.io/google.com/absl-177019/linux_hybrid-latest:20230217"
|
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:20230120"
|
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
|
||||||
time docker run \
|
|
||||||
--volume="${GTEST_ROOT}:/src:ro" \
|
# Test CMake + GCC
|
||||||
--tmpfs="/build:exec" \
|
for cmake_off_on in OFF ON; do
|
||||||
--workdir="/build" \
|
time docker run \
|
||||||
--rm \
|
--volume="${GTEST_ROOT}:/src:ro" \
|
||||||
--env="CC=${cc}" \
|
--tmpfs="/build:exec" \
|
||||||
--env=CXXFLAGS="-Werror -Wdeprecated" \
|
--workdir="/build" \
|
||||||
${LINUX_LATEST_CONTAINER} \
|
--rm \
|
||||||
/bin/bash -c "
|
--env="CC=/usr/local/bin/gcc" \
|
||||||
cmake /src \
|
--env=CXXFLAGS="-Werror -Wdeprecated" \
|
||||||
-DCMAKE_CXX_STANDARD=14 \
|
${LINUX_LATEST_CONTAINER} \
|
||||||
-Dgtest_build_samples=ON \
|
/bin/bash -c "
|
||||||
-Dgtest_build_tests=ON \
|
cmake /src \
|
||||||
-Dgmock_build_tests=ON \
|
-DCMAKE_CXX_STANDARD=17 \
|
||||||
-Dcxx_no_exception=${cmake_off_on} \
|
-Dgtest_build_samples=ON \
|
||||||
-Dcxx_no_rtti=${cmake_off_on} && \
|
-Dgtest_build_tests=ON \
|
||||||
make -j$(nproc) && \
|
-Dgmock_build_tests=ON \
|
||||||
ctest -j$(nproc) --output-on-failure"
|
-Dcxx_no_exception=${cmake_off_on} \
|
||||||
done
|
-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_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
|
done
|
||||||
|
|
||||||
# Do one test with an older version of GCC
|
# Do one test with an older version of GCC
|
||||||
@ -72,19 +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="-Wundef" \
|
--copt=\"-Wundef\" \
|
||||||
--copt="-Wno-error=pragmas" \
|
--copt=\"-Wno-error=pragmas\" \
|
||||||
--distdir="/bazel-distdir" \
|
--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
|
||||||
@ -95,18 +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} \
|
||||||
/usr/local/bin/bazel test ... \
|
/bin/bash --login -c "
|
||||||
--copt="-Wall" \
|
/usr/local/bin/bazel test ... \
|
||||||
--copt="-Werror" \
|
--copt=\"-Wall\" \
|
||||||
--copt="-Wuninitialized" \
|
--copt=\"-Werror\" \
|
||||||
--copt="-Wundef" \
|
--copt=\"-Wuninitialized\" \
|
||||||
--define="absl=${absl}" \
|
--copt=\"-Wundef\" \
|
||||||
--distdir="/bazel-distdir" \
|
--define=\"absl=${absl}\" \
|
||||||
--features=external_include_paths \
|
--enable_bzlmod=true \
|
||||||
--keep_going \
|
--features=external_include_paths \
|
||||||
--show_timestamps \
|
--keep_going \
|
||||||
--test_output=errors
|
--show_timestamps \
|
||||||
|
--test_output=errors \
|
||||||
|
${BAZEL_EXTRA_ARGS:-}"
|
||||||
done
|
done
|
||||||
done
|
done
|
||||||
|
|
||||||
@ -119,19 +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} \
|
||||||
/usr/local/bin/bazel test ... \
|
/bin/bash --login -c "
|
||||||
--copt="--gcc-toolchain=/usr/local" \
|
/usr/local/bin/bazel test ... \
|
||||||
--copt="-Wall" \
|
--copt=\"--gcc-toolchain=/usr/local\" \
|
||||||
--copt="-Werror" \
|
--copt=\"-Wall\" \
|
||||||
--copt="-Wuninitialized" \
|
--copt=\"-Werror\" \
|
||||||
--copt="-Wundef" \
|
--copt=\"-Wuninitialized\" \
|
||||||
--define="absl=${absl}" \
|
--copt=\"-Wundef\" \
|
||||||
--distdir="/bazel-distdir" \
|
--define=\"absl=${absl}\" \
|
||||||
--features=external_include_paths \
|
--enable_bzlmod=true \
|
||||||
--keep_going \
|
--features=external_include_paths \
|
||||||
--linkopt="--gcc-toolchain=/usr/local" \
|
--keep_going \
|
||||||
--show_timestamps \
|
--linkopt=\"--gcc-toolchain=/usr/local\" \
|
||||||
--test_output=errors
|
--show_timestamps \
|
||||||
|
--test_output=errors \
|
||||||
|
${BAZEL_EXTRA_ARGS:-}"
|
||||||
done
|
done
|
||||||
done
|
done
|
||||||
|
|||||||
@ -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,16 +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 "${HOME}/"
|
||||||
|
BAZEL_EXTRA_ARGS="--vendor_dir=${HOME}/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" \
|
||||||
--copt="-Wundef" \
|
--copt="-Wundef" \
|
||||||
--cxxopt="-std=c++14" \
|
--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
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
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:\Python34;%PATH%
|
SET PATH=C:\Python34;%PATH%
|
||||||
SET BAZEL_PYTHON=C:\python34\python.exe
|
SET BAZEL_PYTHON=C:\python34\python.exe
|
||||||
@ -11,21 +11,18 @@ SET CTEST_OUTPUT_ON_FAILURE=1
|
|||||||
SET CMAKE_BUILD_PARALLEL_LEVEL=16
|
SET CMAKE_BUILD_PARALLEL_LEVEL=16
|
||||||
SET CTEST_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
|
:: CMake
|
||||||
MKDIR cmake_msvc2022
|
SET CMAKE_BUILD_PATH=cmake_msvc2022
|
||||||
CD cmake_msvc2022
|
MKDIR %CMAKE_BUILD_PATH%
|
||||||
|
CD %CMAKE_BUILD_PATH%
|
||||||
|
|
||||||
%CMAKE_BIN% .. ^
|
%CMAKE_BIN% %GTEST_ROOT% ^
|
||||||
-G "Visual Studio 17 2022" ^
|
-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 ^
|
||||||
@ -40,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_msvc2022
|
RMDIR /S /Q %CMAKE_BUILD_PATH%
|
||||||
|
|
||||||
:: ----------------------------------------------------------------------------
|
:: ----------------------------------------------------------------------------
|
||||||
:: Bazel
|
:: Bazel
|
||||||
|
|
||||||
|
:: The default home directory on Kokoro is a long path which causes errors
|
||||||
|
:: 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
|
SET BAZEL_VS=C:\Program Files\Microsoft Visual Studio\2022\Community
|
||||||
%BAZEL_EXE% test ... ^
|
|
||||||
|
:: 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
|
||||||
|
|||||||
@ -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">
|
||||||
|
|||||||
200
docs/advanced.md
200
docs/advanced.md
@ -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,7 +298,7 @@ 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
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -349,7 +349,8 @@ void AbslStringify(Sink& sink, EnumWithStringify e) {
|
|||||||
{: .callout .note}
|
{: .callout .note}
|
||||||
Note: `AbslStringify()` utilizes a generic "sink" buffer to construct its
|
Note: `AbslStringify()` utilizes a generic "sink" buffer to construct its
|
||||||
string. For more information about supported operations on `AbslStringify()`'s
|
string. For more information about supported operations on `AbslStringify()`'s
|
||||||
sink, see go/abslstringify.
|
sink, see
|
||||||
|
[the `AbslStringify()` documentation](https://abseil.io/docs/cpp/guides/abslstringify).
|
||||||
|
|
||||||
`AbslStringify()` can also use `absl::StrFormat`'s catch-all `%v` type specifier
|
`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
|
within its own format strings to perform type deduction. `Point` above could be
|
||||||
@ -403,7 +404,53 @@ EXPECT_TRUE(IsCorrectPointIntVector(point_ints))
|
|||||||
```
|
```
|
||||||
|
|
||||||
For more details regarding `AbslStringify()` and its integration with other
|
For more details regarding `AbslStringify()` and its integration with other
|
||||||
libraries, see go/abslstringify.
|
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
|
||||||
|
|
||||||
@ -416,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"
|
||||||
@ -462,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.
|
||||||
|
|
||||||
@ -503,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
|
||||||
@ -727,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++
|
||||||
@ -899,10 +907,10 @@ also supports per-test-suite set-up/tear-down. To use it:
|
|||||||
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
|
||||||
@ -1004,11 +1012,21 @@ calling the `::testing::AddGlobalTestEnvironment()` function:
|
|||||||
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
|
||||||
@ -1432,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
|
||||||
@ -1455,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:
|
||||||
@ -1804,7 +1821,7 @@ 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.
|
||||||
@ -1913,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
|
||||||
@ -2171,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": {
|
||||||
@ -2372,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
|
||||||
|
|||||||
70
docs/faq.md
70
docs/faq.md
@ -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++
|
||||||
|
|||||||
@ -130,7 +130,7 @@ TEST(BarTest, DoesThis) {
|
|||||||
## Setting Default Actions {#OnCall}
|
## Setting Default Actions {#OnCall}
|
||||||
|
|
||||||
gMock has a **built-in default action** for any function that returns `void`,
|
gMock has a **built-in default action** for any function that returns `void`,
|
||||||
`bool`, a numeric value, or a pointer. In C++11, it will additionally returns
|
`bool`, a numeric value, or a pointer. In C++11, it additionally returns
|
||||||
the default-constructed value, if one exists for the given type.
|
the default-constructed value, if one exists for the given type.
|
||||||
|
|
||||||
To customize the default action for functions with return type `T`, use
|
To customize the default action for functions with return type `T`, use
|
||||||
|
|||||||
@ -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));
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
@ -900,15 +900,16 @@ using ::testing::Not;
|
|||||||
|
|
||||||
Matchers are function objects, and parametrized matchers can be composed just
|
Matchers are function objects, and parametrized matchers can be composed just
|
||||||
like any other function. However because their types can be long and rarely
|
like any other function. However because their types can be long and rarely
|
||||||
provide meaningful information, it can be easier to express them with C++14
|
provide meaningful information, it can be easier to express them with template
|
||||||
generic lambdas to avoid specifying types. For example,
|
parameters and `auto`. For example,
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
using ::testing::Contains;
|
using ::testing::Contains;
|
||||||
using ::testing::Property;
|
using ::testing::Property;
|
||||||
|
|
||||||
inline constexpr auto HasFoo = [](const auto& f) {
|
template <typename SubMatcher>
|
||||||
return Property("foo", &MyClass::foo, Contains(f));
|
inline constexpr auto HasFoo(const SubMatcher& sub_matcher) {
|
||||||
|
return Property("foo", &MyClass::foo, Contains(sub_matcher));
|
||||||
};
|
};
|
||||||
...
|
...
|
||||||
EXPECT_THAT(x, HasFoo("blah"));
|
EXPECT_THAT(x, HasFoo("blah"));
|
||||||
@ -936,8 +937,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.
|
||||||
|
|
||||||
@ -1927,6 +1928,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
|
||||||
@ -3306,7 +3313,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; }
|
||||||
@ -3344,6 +3351,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:
|
||||||
@ -3377,21 +3386,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
|
||||||
|
|
||||||
@ -3532,10 +3568,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:
|
||||||
|
|
||||||
@ -3627,8 +3668,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
|
||||||
|
|
||||||
@ -3754,6 +3803,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
|
||||||
|
|||||||
@ -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:
|
||||||
|
|
||||||
@ -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
|
||||||
|
|||||||
@ -50,7 +50,7 @@ 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.
|
||||||
|
|
||||||
@ -68,13 +68,13 @@ 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
|
||||||
|
|
||||||
@ -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
|
||||||
@ -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.
|
||||||
|
|
||||||
@ -326,11 +327,9 @@ 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.
|
||||||
@ -402,7 +401,7 @@ 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.
|
||||||
|
|
||||||
|
|||||||
@ -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, you’ll create the `WORKSPACE` file to specify dependencies. A common and
|
Next, you’ll 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
|
||||||
@ -92,30 +82,33 @@ following contents:
|
|||||||
|
|
||||||
```
|
```
|
||||||
cc_test(
|
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}
|
{: .callout .note}
|
||||||
NOTE: In the example below, we assume Clang or GCC and set `--cxxopt=-std=c++14`
|
NOTE: In the example below, we assume Clang or GCC and set `--cxxopt=-std=c++17`
|
||||||
to ensure that GoogleTest is compiled as C++14 instead of the compiler's default
|
to ensure that GoogleTest is compiled as C++17 instead of the compiler's default
|
||||||
setting (which could be C++11). For MSVC, the equivalent would be
|
setting. For MSVC, the equivalent would be `--cxxopt=/std:c++17`. See
|
||||||
`--cxxopt=/std:c++14`. See [Supported Platforms](platforms.md) for more details
|
[Supported Platforms](platforms.md) for more details on supported language
|
||||||
on supported language versions.
|
versions.
|
||||||
|
|
||||||
Now you can build and run your test:
|
Now you can build and run your test:
|
||||||
|
|
||||||
<pre>
|
<pre>
|
||||||
<strong>my_workspace$ bazel test --cxxopt=-std=c++14 --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:
|
||||||
|
|||||||
@ -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,8 @@ 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)
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||||
|
|
||||||
include(FetchContent)
|
include(FetchContent)
|
||||||
|
|||||||
@ -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`:
|
||||||
|
|||||||
@ -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
|
||||||
@ -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,
|
||||||
|
|||||||
@ -42,11 +42,14 @@ 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). |
|
||||||
| `Optional(m)` | `argument` is `optional<>` that contains a value matching `m`. (For testing whether an `optional<>` is set, check for equality with `nullopt`. You may need to use `Eq(nullopt)` if the inner type doesn't have `==`.)|
|
| `Optional(m)` | `argument` is `optional<>` that contains a value matching `m`. (For testing whether an `optional<>` is set, check for equality with `nullopt`. You may need to use `Eq(nullopt)` if the inner type doesn't have `==`.)|
|
||||||
| `VariantWith<T>(m)` | `argument` is `variant<>` that holds the alternative of type T with a value matching `m`. |
|
| `VariantWith<T>(m)` | `argument` is `variant<>` that holds the alternative of type T with a value matching `m`. |
|
||||||
|
| `AnyWith<T>(m)` | `argument` is `any<>` that holds a value of type T with a value matching `m`. |
|
||||||
| `Ref(variable)` | `argument` is a reference to `variable`. |
|
| `Ref(variable)` | `argument` is a reference to `variable`. |
|
||||||
| `TypedEq<type>(value)` | `argument` has type `type` and is equal to `value`. You may need to use this instead of `Eq(value)` when the mock function is overloaded. |
|
| `TypedEq<type>(value)` | `argument` has type `type` and is equal to `value`. You may need to use this instead of `Eq(value)` when the mock function is overloaded. |
|
||||||
|
|
||||||
@ -109,6 +112,33 @@ 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 +201,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 +224,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 |
|
||||||
|
|||||||
@ -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}
|
||||||
|
|
||||||
|
|||||||
@ -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,7 @@ 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
|
||||||
@ -136,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` — a `double` — 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`*.
|
||||||
@ -168,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.
|
||||||
|
|
||||||
@ -277,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:
|
||||||
@ -1317,7 +1433,9 @@ tests.
|
|||||||
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.
|
||||||
|
|
||||||
|
|||||||
61
fake_fuchsia_sdk.bzl
Normal file
61
fake_fuchsia_sdk.bzl
Normal 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},
|
||||||
|
)
|
||||||
@ -5,7 +5,7 @@
|
|||||||
# CMake build script for Google Mock.
|
# CMake build script for Google Mock.
|
||||||
#
|
#
|
||||||
# To run the tests for Google Mock itself on Linux, use 'make test' or
|
# To run the tests for Google Mock itself on Linux, use 'make test' or
|
||||||
# ctest. You can select which tests to run using 'ctest -R regex'.
|
# ctest. You can select which tests to run using 'ctest -R regex'.
|
||||||
# For more options, run 'ctest --help'.
|
# For more options, run 'ctest --help'.
|
||||||
|
|
||||||
option(gmock_build_tests "Build all of Google Mock's own tests." OFF)
|
option(gmock_build_tests "Build all of Google Mock's own tests." OFF)
|
||||||
@ -44,7 +44,7 @@ if (COMMAND set_up_hermetic_build)
|
|||||||
endif()
|
endif()
|
||||||
|
|
||||||
# Instructs CMake to process Google Test's CMakeLists.txt and add its
|
# Instructs CMake to process Google Test's CMakeLists.txt and add its
|
||||||
# targets to the current scope. We are placing Google Test's binary
|
# targets to the current scope. We are placing Google Test's binary
|
||||||
# directory in a subdirectory of our own as VC compilation may break
|
# directory in a subdirectory of our own as VC compilation may break
|
||||||
# if they are the same (the default).
|
# if they are the same (the default).
|
||||||
add_subdirectory("${gtest_dir}" "${gmock_BINARY_DIR}/${gtest_dir}")
|
add_subdirectory("${gtest_dir}" "${gmock_BINARY_DIR}/${gtest_dir}")
|
||||||
@ -60,25 +60,26 @@ else()
|
|||||||
endif()
|
endif()
|
||||||
|
|
||||||
# Although Google Test's CMakeLists.txt calls this function, the
|
# Although Google Test's CMakeLists.txt calls this function, the
|
||||||
# changes there don't affect the current scope. Therefore we have to
|
# changes there don't affect the current scope. Therefore we have to
|
||||||
# call it again here.
|
# call it again here.
|
||||||
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})
|
||||||
|
|
||||||
########################################################################
|
########################################################################
|
||||||
#
|
#
|
||||||
# Defines the gmock & gmock_main libraries. User tests should link
|
# Defines the gmock & gmock_main libraries. User tests should link
|
||||||
# with one of them.
|
# with one of them.
|
||||||
|
|
||||||
# Google Mock libraries. We build them using more strict warnings than what
|
# Google Mock libraries. We build them using more strict warnings than what
|
||||||
# are used for other targets, to ensure that Google Mock can be compiled by
|
# are used for other targets, to ensure that Google Mock can be compiled by
|
||||||
# a user aggressive about warnings.
|
# a user aggressive about warnings.
|
||||||
if (MSVC)
|
if (MSVC)
|
||||||
@ -111,7 +112,7 @@ target_include_directories(gmock_main SYSTEM INTERFACE
|
|||||||
|
|
||||||
########################################################################
|
########################################################################
|
||||||
#
|
#
|
||||||
# Install rules
|
# Install rules.
|
||||||
install_project(gmock gmock_main)
|
install_project(gmock gmock_main)
|
||||||
|
|
||||||
########################################################################
|
########################################################################
|
||||||
@ -121,8 +122,8 @@ install_project(gmock gmock_main)
|
|||||||
# You can skip this section if you aren't interested in testing
|
# You can skip this section if you aren't interested in testing
|
||||||
# Google Mock itself.
|
# Google Mock itself.
|
||||||
#
|
#
|
||||||
# The tests are not built by default. To build them, set the
|
# The tests are not built by default. To build them, set the
|
||||||
# gmock_build_tests option to ON. You can do it by running ccmake
|
# gmock_build_tests option to ON. You can do it by running ccmake
|
||||||
# or specifying the -Dgmock_build_tests=ON flag when running cmake.
|
# or specifying the -Dgmock_build_tests=ON flag when running cmake.
|
||||||
|
|
||||||
if (gmock_build_tests)
|
if (gmock_build_tests)
|
||||||
@ -187,7 +188,7 @@ if (gmock_build_tests)
|
|||||||
cxx_shared_library(shared_gmock_main "${cxx_default}"
|
cxx_shared_library(shared_gmock_main "${cxx_default}"
|
||||||
"${gtest_dir}/src/gtest-all.cc" src/gmock-all.cc src/gmock_main.cc)
|
"${gtest_dir}/src/gtest-all.cc" src/gmock-all.cc src/gmock_main.cc)
|
||||||
|
|
||||||
# Tests that a binary can be built with Google Mock as a shared library. On
|
# Tests that a binary can be built with Google Mock as a shared library. On
|
||||||
# some system configurations, it may not possible to run the binary without
|
# some system configurations, it may not possible to run the binary without
|
||||||
# knowing more details about the system configurations. We do not try to run
|
# knowing more details about the system configurations. We do not try to run
|
||||||
# this binary. To get a more robust shared library coverage, configure with
|
# this binary. To get a more robust shared library coverage, configure with
|
||||||
|
|||||||
@ -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.
|
||||||
|
|||||||
@ -135,6 +135,7 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <exception>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
@ -175,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
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -828,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;
|
||||||
};
|
};
|
||||||
@ -1444,6 +1455,30 @@ struct WithArgsAction {
|
|||||||
return OA{std::move(inner_action)};
|
return OA{std::move(inner_action)};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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<
|
||||||
|
std::is_convertible<const InnerAction&,
|
||||||
|
OnceAction<R(internal::TupleElement<
|
||||||
|
I, std::tuple<Args...>>...)>>::value,
|
||||||
|
int>::type = 0>
|
||||||
|
operator OnceAction<R(Args...)>() const& { // NOLINT
|
||||||
|
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 <
|
template <
|
||||||
typename R, typename... Args,
|
typename R, typename... Args,
|
||||||
typename std::enable_if<
|
typename std::enable_if<
|
||||||
@ -1486,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,
|
||||||
@ -1494,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<
|
||||||
@ -1573,16 +1624,16 @@ 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
|
||||||
typename std::enable_if<
|
// the rest support conversion to OnceAction.
|
||||||
conjunction<
|
template <
|
||||||
// Both the initial action and the rest must support
|
typename R, typename... Args,
|
||||||
// conversion to OnceAction.
|
typename std::enable_if<
|
||||||
std::is_convertible<
|
conjunction<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,
|
||||||
int>::type = 0>
|
int>::type = 0>
|
||||||
operator OnceAction<R(Args...)>() && { // NOLINT
|
operator OnceAction<R(Args...)>() && { // NOLINT
|
||||||
// Return an action that first calls the initial action with arguments
|
// Return an action that first calls the initial action with arguments
|
||||||
// filtered through InitialActionArgType, then forwards arguments directly
|
// filtered through InitialActionArgType, then forwards arguments directly
|
||||||
@ -1605,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,
|
||||||
@ -1674,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;
|
||||||
@ -1713,11 +1796,24 @@ struct SetArrayArgumentAction {
|
|||||||
};
|
};
|
||||||
|
|
||||||
template <size_t k>
|
template <size_t k>
|
||||||
struct DeleteArgAction {
|
class DeleteArgAction {
|
||||||
|
public:
|
||||||
template <typename... Args>
|
template <typename... Args>
|
||||||
void operator()(const Args&... args) const {
|
void operator()(const Args&... args) const {
|
||||||
delete std::get<k>(std::tie(args...));
|
DoDelete(std::get<k>(std::tie(args...)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
template <typename T>
|
||||||
|
static void DoDelete(T* ptr) {
|
||||||
|
delete ptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
[[deprecated(
|
||||||
|
"DeleteArg<N> used for a non-pointer argument, it was likely migrated "
|
||||||
|
"to a smart pointer type. This action should be removed.")]]
|
||||||
|
static void DoDelete(T&) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename Ptr>
|
template <typename Ptr>
|
||||||
@ -1740,6 +1836,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
|
||||||
@ -1776,6 +1879,13 @@ struct ThrowAction {
|
|||||||
// EXPECT_CALL(mock, Bar(5, _, _)).WillOnce(Invoke(DistanceToOrigin));
|
// EXPECT_CALL(mock, Bar(5, _, _)).WillOnce(Invoke(DistanceToOrigin));
|
||||||
typedef internal::IgnoredValue Unused;
|
typedef internal::IgnoredValue Unused;
|
||||||
|
|
||||||
|
// Deprecated single-argument DoAll.
|
||||||
|
template <typename Action>
|
||||||
|
GTEST_INTERNAL_DEPRECATE_AND_INLINE("Avoid using DoAll() for single actions")
|
||||||
|
typename std::decay<Action>::type DoAll(Action&& action) {
|
||||||
|
return std::forward<Action>(action);
|
||||||
|
}
|
||||||
|
|
||||||
// Creates an action that does actions a1, a2, ..., sequentially in
|
// Creates an action that does actions a1, a2, ..., sequentially in
|
||||||
// each invocation. All but the last action will have a readonly view of the
|
// each invocation. All but the last action will have a readonly view of the
|
||||||
// arguments.
|
// arguments.
|
||||||
@ -1941,10 +2051,11 @@ PolymorphicAction<internal::SetErrnoAndReturnAction<T>> SetErrnoAndReturn(
|
|||||||
// Various overloads for Invoke().
|
// Various overloads for Invoke().
|
||||||
|
|
||||||
// Legacy function.
|
// Legacy function.
|
||||||
// Actions can now be implicitly constructed from callables. No need to create
|
|
||||||
// wrapper objects.
|
|
||||||
// This function exists for backwards compatibility.
|
// This function exists for backwards compatibility.
|
||||||
template <typename FunctionImpl>
|
template <typename FunctionImpl>
|
||||||
|
GTEST_INTERNAL_DEPRECATE_AND_INLINE(
|
||||||
|
"Actions can now be implicitly constructed from callables. No need to "
|
||||||
|
"create wrapper objects using Invoke().")
|
||||||
typename std::decay<FunctionImpl>::type Invoke(FunctionImpl&& function_impl) {
|
typename std::decay<FunctionImpl>::type Invoke(FunctionImpl&& function_impl) {
|
||||||
return std::forward<FunctionImpl>(function_impl);
|
return std::forward<FunctionImpl>(function_impl);
|
||||||
}
|
}
|
||||||
@ -2017,6 +2128,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>
|
||||||
@ -2056,13 +2174,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 {
|
||||||
@ -2111,13 +2239,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
|
||||||
@ -2150,9 +2278,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
|
||||||
@ -2217,8 +2345,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)) { \
|
||||||
@ -2253,7 +2381,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_> \
|
||||||
|
|||||||
@ -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) ||
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@ -521,9 +521,8 @@
|
|||||||
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 \
|
##value_params \
|
||||||
@ -551,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_( \
|
||||||
@ -592,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
|
||||||
std::get<index>(std::forward_as_tuple(std::forward<Args>(args)...)),
|
-> decltype(internal::InvokeArgument(
|
||||||
std::declval<const Params &>()...)) {
|
std::get<index>(std::forward_as_tuple(std::forward<Args>(args)...)),
|
||||||
|
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...);
|
||||||
});
|
});
|
||||||
|
|||||||
@ -868,7 +868,7 @@ class GTEST_API_ ExpectationBase {
|
|||||||
Clause last_clause_;
|
Clause last_clause_;
|
||||||
mutable bool action_count_checked_; // Under mutex_.
|
mutable bool action_count_checked_; // Under mutex_.
|
||||||
mutable Mutex mutex_; // Protects action_count_checked_.
|
mutable Mutex mutex_; // Protects action_count_checked_.
|
||||||
}; // class ExpectationBase
|
}; // class ExpectationBase
|
||||||
|
|
||||||
template <typename F>
|
template <typename F>
|
||||||
class TypedExpectation;
|
class TypedExpectation;
|
||||||
@ -1292,10 +1292,10 @@ class MockSpec {
|
|||||||
: function_mocker_(function_mocker), matchers_(matchers) {}
|
: function_mocker_(function_mocker), matchers_(matchers) {}
|
||||||
|
|
||||||
// Adds a new default action spec to the function mocker and returns
|
// Adds a new default action spec to the function mocker and returns
|
||||||
// the newly created spec.
|
// the newly created spec. .WillByDefault() must be called on the returned
|
||||||
internal::OnCallSpec<F>& InternalDefaultActionSetAt(const char* file,
|
// object.
|
||||||
int line, const char* obj,
|
[[nodiscard]] internal::OnCallSpec<F>& InternalDefaultActionSetAt(
|
||||||
const char* call) {
|
const char* file, int line, const char* obj, const char* call) {
|
||||||
LogWithLocation(internal::kInfo, file, line,
|
LogWithLocation(internal::kInfo, file, line,
|
||||||
std::string("ON_CALL(") + obj + ", " + call + ") invoked");
|
std::string("ON_CALL(") + obj + ", " + call + ") invoked");
|
||||||
return function_mocker_->AddNewOnCallSpec(file, line, matchers_);
|
return function_mocker_->AddNewOnCallSpec(file, line, matchers_);
|
||||||
@ -1467,7 +1467,7 @@ class FunctionMocker<R(Args...)> final : public UntypedFunctionMockerBase {
|
|||||||
// function have been satisfied. If not, it will report Google Test
|
// function have been satisfied. If not, it will report Google Test
|
||||||
// non-fatal failures for the violations.
|
// non-fatal failures for the violations.
|
||||||
~FunctionMocker() override GTEST_LOCK_EXCLUDED_(g_gmock_mutex) {
|
~FunctionMocker() override GTEST_LOCK_EXCLUDED_(g_gmock_mutex) {
|
||||||
MutexLock l(&g_gmock_mutex);
|
MutexLock l(g_gmock_mutex);
|
||||||
VerifyAndClearExpectationsLocked();
|
VerifyAndClearExpectationsLocked();
|
||||||
Mock::UnregisterLocked(this);
|
Mock::UnregisterLocked(this);
|
||||||
ClearDefaultActionsLocked();
|
ClearDefaultActionsLocked();
|
||||||
@ -1530,7 +1530,7 @@ class FunctionMocker<R(Args...)> final : public UntypedFunctionMockerBase {
|
|||||||
UntypedOnCallSpecs specs_to_delete;
|
UntypedOnCallSpecs specs_to_delete;
|
||||||
untyped_on_call_specs_.swap(specs_to_delete);
|
untyped_on_call_specs_.swap(specs_to_delete);
|
||||||
|
|
||||||
g_gmock_mutex.Unlock();
|
g_gmock_mutex.unlock();
|
||||||
for (UntypedOnCallSpecs::const_iterator it = specs_to_delete.begin();
|
for (UntypedOnCallSpecs::const_iterator it = specs_to_delete.begin();
|
||||||
it != specs_to_delete.end(); ++it) {
|
it != specs_to_delete.end(); ++it) {
|
||||||
delete static_cast<const OnCallSpec<F>*>(*it);
|
delete static_cast<const OnCallSpec<F>*>(*it);
|
||||||
@ -1538,7 +1538,7 @@ class FunctionMocker<R(Args...)> final : public UntypedFunctionMockerBase {
|
|||||||
|
|
||||||
// Lock the mutex again, since the caller expects it to be locked when we
|
// Lock the mutex again, since the caller expects it to be locked when we
|
||||||
// return.
|
// return.
|
||||||
g_gmock_mutex.Lock();
|
g_gmock_mutex.lock();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the result of invoking this mock function with the given
|
// Returns the result of invoking this mock function with the given
|
||||||
@ -1646,7 +1646,7 @@ class FunctionMocker<R(Args...)> final : public UntypedFunctionMockerBase {
|
|||||||
GTEST_LOCK_EXCLUDED_(g_gmock_mutex) {
|
GTEST_LOCK_EXCLUDED_(g_gmock_mutex) {
|
||||||
const ArgumentTuple& args =
|
const ArgumentTuple& args =
|
||||||
*static_cast<const ArgumentTuple*>(untyped_args);
|
*static_cast<const ArgumentTuple*>(untyped_args);
|
||||||
MutexLock l(&g_gmock_mutex);
|
MutexLock l(g_gmock_mutex);
|
||||||
TypedExpectation<F>* exp = this->FindMatchingExpectationLocked(args);
|
TypedExpectation<F>* exp = this->FindMatchingExpectationLocked(args);
|
||||||
if (exp == nullptr) { // A match wasn't found.
|
if (exp == nullptr) { // A match wasn't found.
|
||||||
this->FormatUnexpectedCallMessageLocked(args, what, why);
|
this->FormatUnexpectedCallMessageLocked(args, what, why);
|
||||||
@ -1838,9 +1838,8 @@ R FunctionMocker<R(Args...)>::InvokeWith(ArgumentTuple&& args)
|
|||||||
// Doing so slows down compilation dramatically because the *constructor* of
|
// Doing so slows down compilation dramatically because the *constructor* of
|
||||||
// std::function<T> is re-instantiated with different template
|
// std::function<T> is re-instantiated with different template
|
||||||
// parameters each time.
|
// parameters each time.
|
||||||
const UninterestingCallCleanupHandler report_uninteresting_call = {
|
const UninterestingCallCleanupHandler report_uninteresting_call = {reaction,
|
||||||
reaction, ss
|
ss};
|
||||||
};
|
|
||||||
|
|
||||||
return PerformActionAndPrintResult(nullptr, std::move(args), ss.str(), ss);
|
return PerformActionAndPrintResult(nullptr, std::move(args), ss.str(), ss);
|
||||||
}
|
}
|
||||||
@ -1890,8 +1889,7 @@ R FunctionMocker<R(Args...)>::InvokeWith(ArgumentTuple&& args)
|
|||||||
// std::function<T> is re-instantiated with different template
|
// std::function<T> is re-instantiated with different template
|
||||||
// parameters each time.
|
// parameters each time.
|
||||||
const FailureCleanupHandler handle_failures = {
|
const FailureCleanupHandler handle_failures = {
|
||||||
ss, why, loc, untyped_expectation, found, is_excessive
|
ss, why, loc, untyped_expectation, found, is_excessive};
|
||||||
};
|
|
||||||
|
|
||||||
return PerformActionAndPrintResult(untyped_action, std::move(args), ss.str(),
|
return PerformActionAndPrintResult(untyped_action, std::move(args), ss.str(),
|
||||||
ss);
|
ss);
|
||||||
|
|||||||
@ -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"
|
||||||
|
|
||||||
|
|||||||
@ -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"
|
||||||
@ -297,7 +298,7 @@ GTEST_API_ void Log(LogSeverity severity, const std::string& message,
|
|||||||
//
|
//
|
||||||
class WithoutMatchers {
|
class WithoutMatchers {
|
||||||
private:
|
private:
|
||||||
WithoutMatchers() {}
|
WithoutMatchers() = default;
|
||||||
friend GTEST_API_ WithoutMatchers GetWithoutMatchers();
|
friend GTEST_API_ WithoutMatchers GetWithoutMatchers();
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -420,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))...);
|
||||||
@ -428,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)
|
||||||
std::forward<F>(f), std::forward<Tuple>(args),
|
-> decltype(ApplyImpl(
|
||||||
MakeIndexSequence<std::tuple_size<
|
std::forward<F>(f), std::forward<Tuple>(args),
|
||||||
typename std::remove_reference<Tuple>::type>::value>())) {
|
std::make_index_sequence<std::tuple_size<
|
||||||
|
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>());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -465,11 +467,6 @@ struct Function<R(Args...)> {
|
|||||||
using MakeResultIgnoredValue = IgnoredValue(Args...);
|
using MakeResultIgnoredValue = IgnoredValue(Args...);
|
||||||
};
|
};
|
||||||
|
|
||||||
#ifdef GTEST_INTERNAL_NEED_REDUNDANT_CONSTEXPR_DECL
|
|
||||||
template <typename R, typename... Args>
|
|
||||||
constexpr size_t Function<R(Args...)>::ArgumentCount;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// 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
|
||||||
|
|||||||
@ -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"
|
||||||
|
|
||||||
#ifdef 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.
|
||||||
#ifdef 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_
|
||||||
|
|||||||
@ -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
|
||||||
|
|||||||
@ -44,6 +44,7 @@
|
|||||||
#include <iostream>
|
#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"
|
||||||
@ -155,7 +156,7 @@ GTEST_API_ void Log(LogSeverity severity, const std::string& message,
|
|||||||
if (!LogIsVisible(severity)) return;
|
if (!LogIsVisible(severity)) return;
|
||||||
|
|
||||||
// Ensures that logs from different threads don't interleave.
|
// Ensures that logs from different threads don't interleave.
|
||||||
MutexLock l(&g_log_mutex);
|
MutexLock l(g_log_mutex);
|
||||||
|
|
||||||
if (severity == kWarning) {
|
if (severity == kWarning) {
|
||||||
// Prints a GMOCK WARNING marker to make the warnings easily searchable.
|
// Prints a GMOCK WARNING marker to make the warnings easily searchable.
|
||||||
@ -211,14 +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 {
|
return {
|
||||||
{UnBase64Impl(UndoWebSafeEncoding(static_cast<char>(I)), base64, 0)...}};
|
{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[] =
|
||||||
|
|||||||
@ -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 &&
|
||||||
// The element count doesn't match. If the container is empty,
|
matrix.LhsSize() != matrix.RhsSize();
|
||||||
// there's no need to explain anything as Google Mock already
|
if (is_exact_match_with_size_discrepency) {
|
||||||
// prints the empty container. Otherwise we just need to show
|
// The element count doesn't match. If the container is empty,
|
||||||
// how many elements there actually are.
|
// there's no need to explain anything as Google Mock already
|
||||||
if (matrix.LhsSize() != 0 && listener->IsInterested()) {
|
// prints the empty container. Otherwise we just need to show
|
||||||
*listener << "which has " << Elements(matrix.LhsSize());
|
// how many elements there actually are.
|
||||||
}
|
if (matrix.LhsSize() != 0 && listener->IsInterested()) {
|
||||||
return false;
|
*listener << "which has " << Elements(matrix.LhsSize()) << "\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
||||||
|
|
||||||
|
|||||||
@ -212,7 +212,7 @@ void ExpectationBase::CheckActionCountIfNotDone() const
|
|||||||
GTEST_LOCK_EXCLUDED_(mutex_) {
|
GTEST_LOCK_EXCLUDED_(mutex_) {
|
||||||
bool should_check = false;
|
bool should_check = false;
|
||||||
{
|
{
|
||||||
MutexLock l(&mutex_);
|
MutexLock l(mutex_);
|
||||||
if (!action_count_checked_) {
|
if (!action_count_checked_) {
|
||||||
action_count_checked_ = true;
|
action_count_checked_ = true;
|
||||||
should_check = true;
|
should_check = true;
|
||||||
@ -293,7 +293,7 @@ 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/"
|
||||||
@ -318,7 +318,7 @@ UntypedFunctionMockerBase::~UntypedFunctionMockerBase() = default;
|
|||||||
void UntypedFunctionMockerBase::RegisterOwner(const void* mock_obj)
|
void UntypedFunctionMockerBase::RegisterOwner(const void* mock_obj)
|
||||||
GTEST_LOCK_EXCLUDED_(g_gmock_mutex) {
|
GTEST_LOCK_EXCLUDED_(g_gmock_mutex) {
|
||||||
{
|
{
|
||||||
MutexLock l(&g_gmock_mutex);
|
MutexLock l(g_gmock_mutex);
|
||||||
mock_obj_ = mock_obj;
|
mock_obj_ = mock_obj;
|
||||||
}
|
}
|
||||||
Mock::Register(mock_obj, this);
|
Mock::Register(mock_obj, this);
|
||||||
@ -332,7 +332,7 @@ void UntypedFunctionMockerBase::SetOwnerAndName(const void* mock_obj,
|
|||||||
GTEST_LOCK_EXCLUDED_(g_gmock_mutex) {
|
GTEST_LOCK_EXCLUDED_(g_gmock_mutex) {
|
||||||
// We protect name_ under g_gmock_mutex in case this mock function
|
// We protect name_ under g_gmock_mutex in case this mock function
|
||||||
// is called from two threads concurrently.
|
// is called from two threads concurrently.
|
||||||
MutexLock l(&g_gmock_mutex);
|
MutexLock l(g_gmock_mutex);
|
||||||
mock_obj_ = mock_obj;
|
mock_obj_ = mock_obj;
|
||||||
name_ = name;
|
name_ = name;
|
||||||
}
|
}
|
||||||
@ -345,7 +345,7 @@ const void* UntypedFunctionMockerBase::MockObject() const
|
|||||||
{
|
{
|
||||||
// We protect mock_obj_ under g_gmock_mutex in case this mock
|
// We protect mock_obj_ under g_gmock_mutex in case this mock
|
||||||
// function is called from two threads concurrently.
|
// function is called from two threads concurrently.
|
||||||
MutexLock l(&g_gmock_mutex);
|
MutexLock l(g_gmock_mutex);
|
||||||
Assert(mock_obj_ != nullptr, __FILE__, __LINE__,
|
Assert(mock_obj_ != nullptr, __FILE__, __LINE__,
|
||||||
"MockObject() must not be called before RegisterOwner() or "
|
"MockObject() must not be called before RegisterOwner() or "
|
||||||
"SetOwnerAndName() has been called.");
|
"SetOwnerAndName() has been called.");
|
||||||
@ -362,7 +362,7 @@ const char* UntypedFunctionMockerBase::Name() const
|
|||||||
{
|
{
|
||||||
// We protect name_ under g_gmock_mutex in case this mock
|
// We protect name_ under g_gmock_mutex in case this mock
|
||||||
// function is called from two threads concurrently.
|
// function is called from two threads concurrently.
|
||||||
MutexLock l(&g_gmock_mutex);
|
MutexLock l(g_gmock_mutex);
|
||||||
Assert(name_ != nullptr, __FILE__, __LINE__,
|
Assert(name_ != nullptr, __FILE__, __LINE__,
|
||||||
"Name() must not be called before SetOwnerAndName() has "
|
"Name() must not be called before SetOwnerAndName() has "
|
||||||
"been called.");
|
"been called.");
|
||||||
@ -436,9 +436,9 @@ bool UntypedFunctionMockerBase::VerifyAndClearExpectationsLocked()
|
|||||||
UntypedExpectations expectations_to_delete;
|
UntypedExpectations expectations_to_delete;
|
||||||
untyped_expectations_.swap(expectations_to_delete);
|
untyped_expectations_.swap(expectations_to_delete);
|
||||||
|
|
||||||
g_gmock_mutex.Unlock();
|
g_gmock_mutex.unlock();
|
||||||
expectations_to_delete.clear();
|
expectations_to_delete.clear();
|
||||||
g_gmock_mutex.Lock();
|
g_gmock_mutex.lock();
|
||||||
|
|
||||||
return expectations_met;
|
return expectations_met;
|
||||||
}
|
}
|
||||||
@ -490,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();
|
||||||
@ -530,7 +531,7 @@ class MockObjectRegistry {
|
|||||||
#ifdef 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
|
||||||
}
|
}
|
||||||
@ -558,7 +559,7 @@ UninterestingCallReactionMap() {
|
|||||||
void SetReactionOnUninterestingCalls(uintptr_t mock_obj,
|
void SetReactionOnUninterestingCalls(uintptr_t mock_obj,
|
||||||
internal::CallReaction reaction)
|
internal::CallReaction reaction)
|
||||||
GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) {
|
GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) {
|
||||||
internal::MutexLock l(&internal::g_gmock_mutex);
|
internal::MutexLock l(internal::g_gmock_mutex);
|
||||||
UninterestingCallReactionMap()[mock_obj] = reaction;
|
UninterestingCallReactionMap()[mock_obj] = reaction;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -589,7 +590,7 @@ void Mock::FailUninterestingCalls(uintptr_t mock_obj)
|
|||||||
// entry in the call-reaction table should be removed.
|
// entry in the call-reaction table should be removed.
|
||||||
void Mock::UnregisterCallReaction(uintptr_t mock_obj)
|
void Mock::UnregisterCallReaction(uintptr_t mock_obj)
|
||||||
GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) {
|
GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) {
|
||||||
internal::MutexLock l(&internal::g_gmock_mutex);
|
internal::MutexLock l(internal::g_gmock_mutex);
|
||||||
UninterestingCallReactionMap().erase(static_cast<uintptr_t>(mock_obj));
|
UninterestingCallReactionMap().erase(static_cast<uintptr_t>(mock_obj));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -597,7 +598,7 @@ void Mock::UnregisterCallReaction(uintptr_t mock_obj)
|
|||||||
// made on the given mock object.
|
// made on the given mock object.
|
||||||
internal::CallReaction Mock::GetReactionOnUninterestingCalls(
|
internal::CallReaction Mock::GetReactionOnUninterestingCalls(
|
||||||
const void* mock_obj) GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) {
|
const void* mock_obj) GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) {
|
||||||
internal::MutexLock l(&internal::g_gmock_mutex);
|
internal::MutexLock l(internal::g_gmock_mutex);
|
||||||
return (UninterestingCallReactionMap().count(
|
return (UninterestingCallReactionMap().count(
|
||||||
reinterpret_cast<uintptr_t>(mock_obj)) == 0)
|
reinterpret_cast<uintptr_t>(mock_obj)) == 0)
|
||||||
? internal::intToCallReaction(
|
? internal::intToCallReaction(
|
||||||
@ -610,7 +611,7 @@ internal::CallReaction Mock::GetReactionOnUninterestingCalls(
|
|||||||
// objects.
|
// objects.
|
||||||
void Mock::AllowLeak(const void* mock_obj)
|
void Mock::AllowLeak(const void* mock_obj)
|
||||||
GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) {
|
GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) {
|
||||||
internal::MutexLock l(&internal::g_gmock_mutex);
|
internal::MutexLock l(internal::g_gmock_mutex);
|
||||||
g_mock_object_registry.states()[mock_obj].leakable = true;
|
g_mock_object_registry.states()[mock_obj].leakable = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -619,7 +620,7 @@ void Mock::AllowLeak(const void* mock_obj)
|
|||||||
// Test non-fatal failures and returns false.
|
// Test non-fatal failures and returns false.
|
||||||
bool Mock::VerifyAndClearExpectations(void* mock_obj)
|
bool Mock::VerifyAndClearExpectations(void* mock_obj)
|
||||||
GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) {
|
GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) {
|
||||||
internal::MutexLock l(&internal::g_gmock_mutex);
|
internal::MutexLock l(internal::g_gmock_mutex);
|
||||||
return VerifyAndClearExpectationsLocked(mock_obj);
|
return VerifyAndClearExpectationsLocked(mock_obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -628,7 +629,7 @@ bool Mock::VerifyAndClearExpectations(void* mock_obj)
|
|||||||
// verification was successful.
|
// verification was successful.
|
||||||
bool Mock::VerifyAndClear(void* mock_obj)
|
bool Mock::VerifyAndClear(void* mock_obj)
|
||||||
GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) {
|
GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) {
|
||||||
internal::MutexLock l(&internal::g_gmock_mutex);
|
internal::MutexLock l(internal::g_gmock_mutex);
|
||||||
ClearDefaultActionsLocked(mock_obj);
|
ClearDefaultActionsLocked(mock_obj);
|
||||||
return VerifyAndClearExpectationsLocked(mock_obj);
|
return VerifyAndClearExpectationsLocked(mock_obj);
|
||||||
}
|
}
|
||||||
@ -678,7 +679,7 @@ bool Mock::IsStrict(void* mock_obj)
|
|||||||
void Mock::Register(const void* mock_obj,
|
void Mock::Register(const void* mock_obj,
|
||||||
internal::UntypedFunctionMockerBase* mocker)
|
internal::UntypedFunctionMockerBase* mocker)
|
||||||
GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) {
|
GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) {
|
||||||
internal::MutexLock l(&internal::g_gmock_mutex);
|
internal::MutexLock l(internal::g_gmock_mutex);
|
||||||
g_mock_object_registry.states()[mock_obj].function_mockers.insert(mocker);
|
g_mock_object_registry.states()[mock_obj].function_mockers.insert(mocker);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -688,7 +689,7 @@ void Mock::Register(const void* mock_obj,
|
|||||||
void Mock::RegisterUseByOnCallOrExpectCall(const void* mock_obj,
|
void Mock::RegisterUseByOnCallOrExpectCall(const void* mock_obj,
|
||||||
const char* file, int line)
|
const char* file, int line)
|
||||||
GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) {
|
GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) {
|
||||||
internal::MutexLock l(&internal::g_gmock_mutex);
|
internal::MutexLock l(internal::g_gmock_mutex);
|
||||||
MockObjectState& state = g_mock_object_registry.states()[mock_obj];
|
MockObjectState& state = g_mock_object_registry.states()[mock_obj];
|
||||||
if (state.first_used_file == nullptr) {
|
if (state.first_used_file == nullptr) {
|
||||||
state.first_used_file = file;
|
state.first_used_file = file;
|
||||||
|
|||||||
@ -30,6 +30,7 @@
|
|||||||
#
|
#
|
||||||
# Bazel Build for Google C++ Testing Framework(Google Test)-googlemock
|
# Bazel Build for Google C++ Testing Framework(Google Test)-googlemock
|
||||||
|
|
||||||
|
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_test")
|
||||||
load("@rules_python//python:defs.bzl", "py_library", "py_test")
|
load("@rules_python//python:defs.bzl", "py_library", "py_test")
|
||||||
|
|
||||||
licenses(["notice"])
|
licenses(["notice"])
|
||||||
|
|||||||
@ -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,8 +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(GTEST_INTERNAL_CPLUSPLUS_LANG) && \
|
// TODO(b/396121064) - Fix this test under MSVC
|
||||||
GTEST_INTERNAL_CPLUSPLUS_LANG >= 201703L
|
#ifndef _MSC_VER
|
||||||
{
|
{
|
||||||
struct NonMoveable {
|
struct NonMoveable {
|
||||||
NonMoveable() = default;
|
NonMoveable() = default;
|
||||||
@ -244,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.
|
||||||
@ -441,8 +441,8 @@ 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) {
|
||||||
@ -505,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
|
||||||
@ -1030,8 +1030,7 @@ void VoidFunc(bool /* flag */) {}
|
|||||||
|
|
||||||
TEST(DoDefaultDeathTest, DiesIfUsedInCompositeAction) {
|
TEST(DoDefaultDeathTest, DiesIfUsedInCompositeAction) {
|
||||||
MockClass mock;
|
MockClass mock;
|
||||||
EXPECT_CALL(mock, IntFunc(_))
|
EXPECT_CALL(mock, IntFunc(_)).WillRepeatedly(DoAll(VoidFunc, DoDefault()));
|
||||||
.WillRepeatedly(DoAll(Invoke(VoidFunc), DoDefault()));
|
|
||||||
|
|
||||||
// Ideally we should verify the error message as well. Sadly,
|
// Ideally we should verify the error message as well. Sadly,
|
||||||
// EXPECT_DEATH() can only capture stderr, while Google Mock's
|
// EXPECT_DEATH() can only capture stderr, while Google Mock's
|
||||||
@ -1282,7 +1281,7 @@ int ReturnOne() {
|
|||||||
|
|
||||||
TEST(IgnoreResultTest, MonomorphicAction) {
|
TEST(IgnoreResultTest, MonomorphicAction) {
|
||||||
g_done = false;
|
g_done = false;
|
||||||
Action<void()> a = IgnoreResult(Invoke(ReturnOne));
|
Action<void()> a = IgnoreResult(&ReturnOne);
|
||||||
a.Perform(std::make_tuple());
|
a.Perform(std::make_tuple());
|
||||||
EXPECT_TRUE(g_done);
|
EXPECT_TRUE(g_done);
|
||||||
}
|
}
|
||||||
@ -1297,7 +1296,7 @@ MyNonDefaultConstructible ReturnMyNonDefaultConstructible(double /* x */) {
|
|||||||
TEST(IgnoreResultTest, ActionReturningClass) {
|
TEST(IgnoreResultTest, ActionReturningClass) {
|
||||||
g_done = false;
|
g_done = false;
|
||||||
Action<void(int)> a =
|
Action<void(int)> a =
|
||||||
IgnoreResult(Invoke(ReturnMyNonDefaultConstructible)); // NOLINT
|
IgnoreResult(&ReturnMyNonDefaultConstructible); // NOLINT
|
||||||
a.Perform(std::make_tuple(2));
|
a.Perform(std::make_tuple(2));
|
||||||
EXPECT_TRUE(g_done);
|
EXPECT_TRUE(g_done);
|
||||||
}
|
}
|
||||||
@ -1477,9 +1476,50 @@ TEST(DoAll, SupportsTypeErasedActions) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// A multi-action 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.
|
||||||
|
//
|
||||||
|
// Single-arg DoAll just returns its argument, so will prefer the Action<F>
|
||||||
|
// overload for WillOnce.
|
||||||
|
TEST(DoAll, ConvertibleToOnceActionWithUserProvidedActionConversion) {
|
||||||
|
// Final action.
|
||||||
|
struct CustomFinal final {
|
||||||
|
operator Action<int()>() { // NOLINT
|
||||||
|
return Return(17);
|
||||||
|
}
|
||||||
|
|
||||||
|
operator Action<int(int, char)>() { // NOLINT
|
||||||
|
return Return(19);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// 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>(Unary);
|
||||||
EXPECT_TRUE(a.Perform(std::make_tuple(1.5, -1)));
|
EXPECT_TRUE(a.Perform(std::make_tuple(1.5, -1)));
|
||||||
EXPECT_FALSE(a.Perform(std::make_tuple(1.5, 1)));
|
EXPECT_FALSE(a.Perform(std::make_tuple(1.5, 1)));
|
||||||
}
|
}
|
||||||
@ -1487,7 +1527,7 @@ TEST(WithArgsTest, OneArg) {
|
|||||||
// Tests using WithArgs with an action that takes 2 arguments.
|
// Tests using WithArgs with an action that takes 2 arguments.
|
||||||
TEST(WithArgsTest, TwoArgs) {
|
TEST(WithArgsTest, TwoArgs) {
|
||||||
Action<const char*(const char* s, double x, short n)> a = // NOLINT
|
Action<const char*(const char* s, double x, short n)> a = // NOLINT
|
||||||
WithArgs<0, 2>(Invoke(Binary));
|
WithArgs<0, 2>(Binary);
|
||||||
const char s[] = "Hello";
|
const char s[] = "Hello";
|
||||||
EXPECT_EQ(s + 2, a.Perform(std::make_tuple(CharPtr(s), 0.5, Short(2))));
|
EXPECT_EQ(s + 2, a.Perform(std::make_tuple(CharPtr(s), 0.5, Short(2))));
|
||||||
}
|
}
|
||||||
@ -1503,7 +1543,7 @@ struct ConcatAll {
|
|||||||
// Tests using WithArgs with an action that takes 10 arguments.
|
// Tests using WithArgs with an action that takes 10 arguments.
|
||||||
TEST(WithArgsTest, TenArgs) {
|
TEST(WithArgsTest, TenArgs) {
|
||||||
Action<std::string(const char*, const char*, const char*, const char*)> a =
|
Action<std::string(const char*, const char*, const char*, const char*)> a =
|
||||||
WithArgs<0, 1, 2, 3, 2, 1, 0, 1, 2, 3>(Invoke(ConcatAll{}));
|
WithArgs<0, 1, 2, 3, 2, 1, 0, 1, 2, 3>(ConcatAll{});
|
||||||
EXPECT_EQ("0123210123",
|
EXPECT_EQ("0123210123",
|
||||||
a.Perform(std::make_tuple(CharPtr("0"), CharPtr("1"), CharPtr("2"),
|
a.Perform(std::make_tuple(CharPtr("0"), CharPtr("1"), CharPtr("2"),
|
||||||
CharPtr("3"))));
|
CharPtr("3"))));
|
||||||
@ -1528,21 +1568,21 @@ TEST(WithArgsTest, NonInvokeAction) {
|
|||||||
// Tests using WithArgs to pass all original arguments in the original order.
|
// Tests using WithArgs to pass all original arguments in the original order.
|
||||||
TEST(WithArgsTest, Identity) {
|
TEST(WithArgsTest, Identity) {
|
||||||
Action<int(int x, char y, short z)> a = // NOLINT
|
Action<int(int x, char y, short z)> a = // NOLINT
|
||||||
WithArgs<0, 1, 2>(Invoke(Ternary));
|
WithArgs<0, 1, 2>(Ternary);
|
||||||
EXPECT_EQ(123, a.Perform(std::make_tuple(100, Char(20), Short(3))));
|
EXPECT_EQ(123, a.Perform(std::make_tuple(100, Char(20), Short(3))));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tests using WithArgs with repeated arguments.
|
// Tests using WithArgs with repeated arguments.
|
||||||
TEST(WithArgsTest, RepeatedArguments) {
|
TEST(WithArgsTest, RepeatedArguments) {
|
||||||
Action<int(bool, int m, int n)> a = // NOLINT
|
Action<int(bool, int m, int n)> a = // NOLINT
|
||||||
WithArgs<1, 1, 1, 1>(Invoke(SumOf4));
|
WithArgs<1, 1, 1, 1>(SumOf4);
|
||||||
EXPECT_EQ(4, a.Perform(std::make_tuple(false, 1, 10)));
|
EXPECT_EQ(4, a.Perform(std::make_tuple(false, 1, 10)));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tests using WithArgs with reversed argument order.
|
// Tests using WithArgs with reversed argument order.
|
||||||
TEST(WithArgsTest, ReversedArgumentOrder) {
|
TEST(WithArgsTest, ReversedArgumentOrder) {
|
||||||
Action<const char*(short n, const char* input)> a = // NOLINT
|
Action<const char*(short n, const char* input)> a = // NOLINT
|
||||||
WithArgs<1, 0>(Invoke(Binary));
|
WithArgs<1, 0>(Binary);
|
||||||
const char s[] = "Hello";
|
const char s[] = "Hello";
|
||||||
EXPECT_EQ(s + 2, a.Perform(std::make_tuple(Short(2), CharPtr(s))));
|
EXPECT_EQ(s + 2, a.Perform(std::make_tuple(Short(2), CharPtr(s))));
|
||||||
}
|
}
|
||||||
@ -1550,14 +1590,14 @@ TEST(WithArgsTest, ReversedArgumentOrder) {
|
|||||||
// Tests using WithArgs with compatible, but not identical, argument types.
|
// Tests using WithArgs with compatible, but not identical, argument types.
|
||||||
TEST(WithArgsTest, ArgsOfCompatibleTypes) {
|
TEST(WithArgsTest, ArgsOfCompatibleTypes) {
|
||||||
Action<long(short x, char y, double z, char c)> a = // NOLINT
|
Action<long(short x, char y, double z, char c)> a = // NOLINT
|
||||||
WithArgs<0, 1, 3>(Invoke(Ternary));
|
WithArgs<0, 1, 3>(Ternary);
|
||||||
EXPECT_EQ(123,
|
EXPECT_EQ(123,
|
||||||
a.Perform(std::make_tuple(Short(100), Char(20), 5.6, Char(3))));
|
a.Perform(std::make_tuple(Short(100), Char(20), 5.6, Char(3))));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tests using WithArgs with an action that returns void.
|
// Tests using WithArgs with an action that returns void.
|
||||||
TEST(WithArgsTest, VoidAction) {
|
TEST(WithArgsTest, VoidAction) {
|
||||||
Action<void(double x, char c, int n)> a = WithArgs<2, 1>(Invoke(VoidBinary));
|
Action<void(double x, char c, int n)> a = WithArgs<2, 1>(VoidBinary);
|
||||||
g_done = false;
|
g_done = false;
|
||||||
a.Perform(std::make_tuple(1.5, 'a', 3));
|
a.Perform(std::make_tuple(1.5, 'a', 3));
|
||||||
EXPECT_TRUE(g_done);
|
EXPECT_TRUE(g_done);
|
||||||
@ -1597,6 +1637,22 @@ TEST(WithArgsTest, RefQualifiedInnerAction) {
|
|||||||
EXPECT_EQ(19, mock.AsStdFunction()(0, 17));
|
EXPECT_EQ(19, mock.AsStdFunction()(0, 17));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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
|
#ifndef GTEST_OS_WINDOWS_MOBILE
|
||||||
|
|
||||||
class SetErrnoAndReturnTest : public testing::Test {
|
class SetErrnoAndReturnTest : public testing::Test {
|
||||||
@ -1766,7 +1822,7 @@ std::vector<std::unique_ptr<int>> VectorUniquePtrSource() {
|
|||||||
|
|
||||||
TEST(MockMethodTest, CanReturnMoveOnlyValue_Return) {
|
TEST(MockMethodTest, CanReturnMoveOnlyValue_Return) {
|
||||||
MockClass mock;
|
MockClass mock;
|
||||||
std::unique_ptr<int> i(new int(19));
|
std::unique_ptr<int> i = std::make_unique<int>(19);
|
||||||
EXPECT_CALL(mock, MakeUnique()).WillOnce(Return(ByMove(std::move(i))));
|
EXPECT_CALL(mock, MakeUnique()).WillOnce(Return(ByMove(std::move(i))));
|
||||||
EXPECT_CALL(mock, MakeVectorUnique())
|
EXPECT_CALL(mock, MakeVectorUnique())
|
||||||
.WillOnce(Return(ByMove(VectorUniquePtrSource())));
|
.WillOnce(Return(ByMove(VectorUniquePtrSource())));
|
||||||
@ -1789,7 +1845,7 @@ TEST(MockMethodTest, CanReturnMoveOnlyValue_Return) {
|
|||||||
TEST(MockMethodTest, CanReturnMoveOnlyValue_DoAllReturn) {
|
TEST(MockMethodTest, CanReturnMoveOnlyValue_DoAllReturn) {
|
||||||
testing::MockFunction<void()> mock_function;
|
testing::MockFunction<void()> mock_function;
|
||||||
MockClass mock;
|
MockClass mock;
|
||||||
std::unique_ptr<int> i(new int(19));
|
std::unique_ptr<int> i = std::make_unique<int>(19);
|
||||||
EXPECT_CALL(mock_function, Call());
|
EXPECT_CALL(mock_function, Call());
|
||||||
EXPECT_CALL(mock, MakeUnique())
|
EXPECT_CALL(mock, MakeUnique())
|
||||||
.WillOnce(DoAll(InvokeWithoutArgs(&mock_function,
|
.WillOnce(DoAll(InvokeWithoutArgs(&mock_function,
|
||||||
@ -1808,9 +1864,8 @@ TEST(MockMethodTest, CanReturnMoveOnlyValue_Invoke) {
|
|||||||
[] { return std::make_unique<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(UniquePtrSource);
|
||||||
EXPECT_CALL(mock, MakeVectorUnique())
|
EXPECT_CALL(mock, MakeVectorUnique()).WillRepeatedly(VectorUniquePtrSource);
|
||||||
.WillRepeatedly(Invoke(VectorUniquePtrSource));
|
|
||||||
std::unique_ptr<int> result1 = mock.MakeUnique();
|
std::unique_ptr<int> result1 = mock.MakeUnique();
|
||||||
EXPECT_EQ(19, *result1);
|
EXPECT_EQ(19, *result1);
|
||||||
std::unique_ptr<int> result2 = mock.MakeUnique();
|
std::unique_ptr<int> result2 = mock.MakeUnique();
|
||||||
@ -1832,7 +1887,7 @@ TEST(MockMethodTest, CanTakeMoveOnlyValue) {
|
|||||||
});
|
});
|
||||||
// DoAll() does not compile, since it would move from its arguments twice.
|
// DoAll() does not compile, since it would move from its arguments twice.
|
||||||
// EXPECT_CALL(mock, TakeUnique(_, _))
|
// EXPECT_CALL(mock, TakeUnique(_, _))
|
||||||
// .WillRepeatedly(DoAll(Invoke([](std::unique_ptr<int> j) {}),
|
// .WillRepeatedly(DoAll([](std::unique_ptr<int> j) {})),
|
||||||
// Return(1)));
|
// Return(1)));
|
||||||
EXPECT_CALL(mock, TakeUnique(testing::Pointee(7)))
|
EXPECT_CALL(mock, TakeUnique(testing::Pointee(7)))
|
||||||
.WillOnce(Return(-7))
|
.WillOnce(Return(-7))
|
||||||
|
|||||||
@ -91,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;
|
||||||
@ -325,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"));
|
||||||
|
|||||||
@ -545,7 +545,7 @@ TEST(ExpectCallTest, DoesNotLogWhenVerbosityIsError) {
|
|||||||
|
|
||||||
void OnCallLogger() {
|
void OnCallLogger() {
|
||||||
DummyMock mock;
|
DummyMock mock;
|
||||||
ON_CALL(mock, TestMethod());
|
(void)ON_CALL(mock, TestMethod());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Verifies that ON_CALL logs if the --gmock_verbose flag is set to "info".
|
// Verifies that ON_CALL logs if the --gmock_verbose flag is set to "info".
|
||||||
@ -568,7 +568,7 @@ TEST(OnCallTest, DoesNotLogWhenVerbosityIsError) {
|
|||||||
|
|
||||||
void OnCallAnyArgumentLogger() {
|
void OnCallAnyArgumentLogger() {
|
||||||
DummyMock mock;
|
DummyMock mock;
|
||||||
ON_CALL(mock, TestMethodArg(_));
|
(void)ON_CALL(mock, TestMethodArg(_));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Verifies that ON_CALL prints provided _ argument.
|
// Verifies that ON_CALL prints provided _ argument.
|
||||||
|
|||||||
@ -34,9 +34,12 @@
|
|||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <limits>
|
#include <limits>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <ostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
#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',
|
// 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
|
||||||
@ -396,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;
|
||||||
@ -559,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));
|
||||||
@ -572,8 +756,9 @@ 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(
|
||||||
Explain(m, 25));
|
"which is 15 more than 10, and is < 30, and which is 5 more than 20",
|
||||||
|
Explain(m, 25));
|
||||||
|
|
||||||
// Successful match. All matchers need to explain.
|
// Successful match. All matchers need to explain.
|
||||||
m = AllOf(GreaterThan(10), GreaterThan(20), GreaterThan(30));
|
m = AllOf(GreaterThan(10), GreaterThan(20), GreaterThan(30));
|
||||||
@ -585,13 +770,14 @@ TEST_P(AllOfTestP, ExplainsResult) {
|
|||||||
// Failed match. The first matcher, which failed, needs to
|
// Failed match. The first matcher, which failed, needs to
|
||||||
// explain.
|
// explain.
|
||||||
m = AllOf(GreaterThan(10), GreaterThan(20));
|
m = AllOf(GreaterThan(10), GreaterThan(20));
|
||||||
EXPECT_EQ("which is 5 less than 10", Explain(m, 5));
|
EXPECT_EQ("which is 5 less than 10, and which is 15 less than 20",
|
||||||
|
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.
|
||||||
@ -774,45 +960,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(
|
||||||
Explain(m, 5));
|
"which is 5 less than 10, and isn't > 20, and which is 25 less than 30",
|
||||||
|
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
|
||||||
@ -1442,7 +1626,7 @@ TEST_F(DoubleNearTest, NanSensitiveDoubleNearCanMatchNaN) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST(NotTest, WorksOnMoveOnlyType) {
|
TEST(NotTest, WorksOnMoveOnlyType) {
|
||||||
std::unique_ptr<int> p(new int(3));
|
std::unique_ptr<int> p = std::make_unique<int>(3);
|
||||||
EXPECT_THAT(p, Pointee(Eq(3)));
|
EXPECT_THAT(p, Pointee(Eq(3)));
|
||||||
EXPECT_THAT(p, Not(Pointee(Eq(2))));
|
EXPECT_THAT(p, Not(Pointee(Eq(2))));
|
||||||
}
|
}
|
||||||
@ -1498,13 +1682,13 @@ TEST(AnyOfTest, DoesNotCallAnyOfUnqualified) {
|
|||||||
} // namespace adl_test
|
} // namespace adl_test
|
||||||
|
|
||||||
TEST(AllOfTest, WorksOnMoveOnlyType) {
|
TEST(AllOfTest, WorksOnMoveOnlyType) {
|
||||||
std::unique_ptr<int> p(new int(3));
|
std::unique_ptr<int> p = std::make_unique<int>(3);
|
||||||
EXPECT_THAT(p, AllOf(Pointee(Eq(3)), Pointee(Gt(0)), Pointee(Lt(5))));
|
EXPECT_THAT(p, AllOf(Pointee(Eq(3)), Pointee(Gt(0)), Pointee(Lt(5))));
|
||||||
EXPECT_THAT(p, Not(AllOf(Pointee(Eq(3)), Pointee(Gt(0)), Pointee(Lt(3)))));
|
EXPECT_THAT(p, Not(AllOf(Pointee(Eq(3)), Pointee(Gt(0)), Pointee(Lt(3)))));
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(AnyOfTest, WorksOnMoveOnlyType) {
|
TEST(AnyOfTest, WorksOnMoveOnlyType) {
|
||||||
std::unique_ptr<int> p(new int(3));
|
std::unique_ptr<int> p = std::make_unique<int>(3);
|
||||||
EXPECT_THAT(p, AnyOf(Pointee(Eq(5)), Pointee(Lt(0)), Pointee(Lt(5))));
|
EXPECT_THAT(p, AnyOf(Pointee(Eq(5)), Pointee(Lt(0)), Pointee(Lt(5))));
|
||||||
EXPECT_THAT(p, Not(AnyOf(Pointee(Eq(5)), Pointee(Lt(0)), Pointee(Gt(5)))));
|
EXPECT_THAT(p, Not(AnyOf(Pointee(Eq(5)), Pointee(Lt(0)), Pointee(Gt(5)))));
|
||||||
}
|
}
|
||||||
|
|||||||
@ -33,17 +33,19 @@
|
|||||||
|
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <optional>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <tuple>
|
#include <tuple>
|
||||||
#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',
|
// 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
|
||||||
GTEST_DISABLE_MSC_WARNINGS_PUSH_(4244 4100)
|
GTEST_DISABLE_MSC_WARNINGS_PUSH_(4244 4100)
|
||||||
|
|
||||||
|
|
||||||
namespace testing {
|
namespace testing {
|
||||||
namespace gmock_matchers_test {
|
namespace gmock_matchers_test {
|
||||||
namespace {
|
namespace {
|
||||||
@ -410,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) {
|
||||||
@ -428,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&>.
|
||||||
@ -444,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>.
|
||||||
@ -576,15 +622,42 @@ 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) {
|
||||||
int n = 42;
|
{
|
||||||
Matcher<IntReferenceWrapper> m = MatcherCast<IntReferenceWrapper>(n);
|
// When an IntReferenceWrapper is passed.
|
||||||
// Verify that the matcher holds a reference to n, not to its temporary copy.
|
int n = 42;
|
||||||
EXPECT_TRUE(m.Matches(n));
|
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;
|
||||||
|
Matcher<IntReferenceWrapper> m = MatcherCast<IntReferenceWrapper>(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 {
|
||||||
@ -648,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;
|
||||||
@ -1769,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) {
|
||||||
@ -2297,22 +2389,19 @@ PolymorphicMatcher<DivisibleByImpl> DivisibleBy(int n) {
|
|||||||
return MakePolymorphicMatcher(DivisibleByImpl(n));
|
return MakePolymorphicMatcher(DivisibleByImpl(n));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tests that when AllOf() fails, only the first failing matcher is
|
// Tests that when AllOf() fails, all failing matchers are asked to explain why.
|
||||||
// asked to explain why.
|
|
||||||
TEST(ExplainMatchResultTest, AllOf_False_False) {
|
TEST(ExplainMatchResultTest, AllOf_False_False) {
|
||||||
const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3));
|
const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3));
|
||||||
EXPECT_EQ("which is 1 modulo 4", Explain(m, 5));
|
EXPECT_EQ("which is 1 modulo 4, and which is 2 modulo 3", Explain(m, 5));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tests that when AllOf() fails, only the first failing matcher is
|
// Tests that when AllOf() fails, all failing matchers are asked to explain why.
|
||||||
// asked to explain why.
|
|
||||||
TEST(ExplainMatchResultTest, AllOf_False_True) {
|
TEST(ExplainMatchResultTest, AllOf_False_True) {
|
||||||
const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3));
|
const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3));
|
||||||
EXPECT_EQ("which is 2 modulo 4", Explain(m, 6));
|
EXPECT_EQ("which is 2 modulo 4", Explain(m, 6));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tests that when AllOf() fails, only the first failing matcher is
|
// Tests that when AllOf() fails, all failing matchers are asked to explain why.
|
||||||
// asked to explain why.
|
|
||||||
TEST(ExplainMatchResultTest, AllOf_True_False) {
|
TEST(ExplainMatchResultTest, AllOf_True_False) {
|
||||||
const Matcher<int> m = AllOf(Ge(1), DivisibleBy(3));
|
const Matcher<int> m = AllOf(Ge(1), DivisibleBy(3));
|
||||||
EXPECT_EQ("which is 2 modulo 3", Explain(m, 5));
|
EXPECT_EQ("which is 2 modulo 3", Explain(m, 5));
|
||||||
@ -2325,9 +2414,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);
|
||||||
|
|||||||
@ -33,6 +33,7 @@
|
|||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <array>
|
#include <array>
|
||||||
|
#include <cstddef>
|
||||||
#include <deque>
|
#include <deque>
|
||||||
#include <forward_list>
|
#include <forward_list>
|
||||||
#include <iterator>
|
#include <iterator>
|
||||||
@ -43,14 +44,14 @@
|
|||||||
#include <tuple>
|
#include <tuple>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
#include "gmock/gmock.h"
|
||||||
|
#include "test/gmock-matchers_test.h"
|
||||||
#include "gtest/gtest.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
|
||||||
GTEST_DISABLE_MSC_WARNINGS_PUSH_(4244 4100)
|
GTEST_DISABLE_MSC_WARNINGS_PUSH_(4244 4100)
|
||||||
|
|
||||||
#include "test/gmock-matchers_test.h"
|
|
||||||
|
|
||||||
namespace testing {
|
namespace testing {
|
||||||
namespace gmock_matchers_test {
|
namespace gmock_matchers_test {
|
||||||
namespace {
|
namespace {
|
||||||
@ -216,7 +217,7 @@ TEST(PointeeTest, ReferenceToNonConstRawPointer) {
|
|||||||
TEST(PointeeTest, SmartPointer) {
|
TEST(PointeeTest, SmartPointer) {
|
||||||
const Matcher<std::unique_ptr<int>> m = Pointee(Ge(0));
|
const Matcher<std::unique_ptr<int>> m = Pointee(Ge(0));
|
||||||
|
|
||||||
std::unique_ptr<int> n(new int(1));
|
std::unique_ptr<int> n = std::make_unique<int>(1);
|
||||||
EXPECT_TRUE(m.Matches(n));
|
EXPECT_TRUE(m.Matches(n));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -253,7 +254,7 @@ TEST(PointerTest, RawPointerToConst) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST(PointerTest, SmartPointer) {
|
TEST(PointerTest, SmartPointer) {
|
||||||
std::unique_ptr<int> n(new int(10));
|
std::unique_ptr<int> n = std::make_unique<int>(10);
|
||||||
int* raw_n = n.get();
|
int* raw_n = n.get();
|
||||||
const Matcher<std::unique_ptr<int>> m = Pointer(Eq(raw_n));
|
const Matcher<std::unique_ptr<int>> m = Pointer(Eq(raw_n));
|
||||||
|
|
||||||
@ -1204,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));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1268,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
|
||||||
@ -1475,8 +1480,10 @@ 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(
|
||||||
Explain(m3, container));
|
"whose distance between begin() and end() 0 matches, which matches (is "
|
||||||
|
"equal to 0)",
|
||||||
|
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 "
|
||||||
"less than 1",
|
"less than 1",
|
||||||
@ -1487,8 +1494,10 @@ 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(
|
||||||
Explain(m3, container));
|
"whose distance between begin() and end() 2 doesn't match, isn't equal "
|
||||||
|
"to 0, and isn't equal to 3",
|
||||||
|
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 "
|
||||||
"than 1",
|
"than 1",
|
||||||
@ -1768,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".
|
||||||
|
|
||||||
@ -2014,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) {
|
||||||
@ -2022,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) {
|
||||||
@ -2137,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));
|
||||||
@ -2272,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.
|
||||||
@ -2400,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.
|
||||||
@ -2438,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};
|
||||||
@ -2487,7 +2796,7 @@ TEST(UnorderedPointwiseTest, WorksWithMoveOnly) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST(PointeeTest, WorksOnMoveOnlyType) {
|
TEST(PointeeTest, WorksOnMoveOnlyType) {
|
||||||
std::unique_ptr<int> p(new int(3));
|
std::unique_ptr<int> p = std::make_unique<int>(3);
|
||||||
EXPECT_THAT(p, Pointee(Eq(3)));
|
EXPECT_THAT(p, Pointee(Eq(3)));
|
||||||
EXPECT_THAT(p, Not(Pointee(Eq(2))));
|
EXPECT_THAT(p, Not(Pointee(Eq(2))));
|
||||||
}
|
}
|
||||||
@ -2651,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) {
|
||||||
@ -3055,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);
|
||||||
|
|||||||
@ -32,6 +32,7 @@
|
|||||||
// This file tests some commonly used argument matchers.
|
// This file tests some commonly used argument matchers.
|
||||||
|
|
||||||
#include <array>
|
#include <array>
|
||||||
|
#include <cstdint>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <ostream>
|
#include <ostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
@ -39,14 +40,14 @@
|
|||||||
#include <utility>
|
#include <utility>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
#include "gmock/gmock.h"
|
||||||
|
#include "test/gmock-matchers_test.h"
|
||||||
#include "gtest/gtest.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
|
||||||
GTEST_DISABLE_MSC_WARNINGS_PUSH_(4244 4100)
|
GTEST_DISABLE_MSC_WARNINGS_PUSH_(4244 4100)
|
||||||
|
|
||||||
#include "test/gmock-matchers_test.h"
|
|
||||||
|
|
||||||
namespace testing {
|
namespace testing {
|
||||||
namespace gmock_matchers_test {
|
namespace gmock_matchers_test {
|
||||||
namespace {
|
namespace {
|
||||||
@ -78,7 +79,7 @@ TEST(AddressTest, Const) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST(AddressTest, MatcherDoesntCopy) {
|
TEST(AddressTest, MatcherDoesntCopy) {
|
||||||
std::unique_ptr<int> n(new int(1));
|
std::unique_ptr<int> n = std::make_unique<int>(1);
|
||||||
const Matcher<std::unique_ptr<int>> m = Address(Eq(&n));
|
const Matcher<std::unique_ptr<int>> m = Address(Eq(&n));
|
||||||
|
|
||||||
EXPECT_TRUE(m.Matches(n));
|
EXPECT_TRUE(m.Matches(n));
|
||||||
@ -201,7 +202,7 @@ TEST(IsTrueTest, IsTrueIsFalse) {
|
|||||||
EXPECT_THAT(nullptr, Not(IsTrue()));
|
EXPECT_THAT(nullptr, Not(IsTrue()));
|
||||||
EXPECT_THAT(nullptr, IsFalse());
|
EXPECT_THAT(nullptr, IsFalse());
|
||||||
std::unique_ptr<int> null_unique;
|
std::unique_ptr<int> null_unique;
|
||||||
std::unique_ptr<int> nonnull_unique(new int(0));
|
std::unique_ptr<int> nonnull_unique = std::make_unique<int>(0);
|
||||||
EXPECT_THAT(null_unique, Not(IsTrue()));
|
EXPECT_THAT(null_unique, Not(IsTrue()));
|
||||||
EXPECT_THAT(null_unique, IsFalse());
|
EXPECT_THAT(null_unique, IsFalse());
|
||||||
EXPECT_THAT(nonnull_unique, IsTrue());
|
EXPECT_THAT(nonnull_unique, IsTrue());
|
||||||
@ -674,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>
|
||||||
@ -691,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) {}
|
||||||
@ -1576,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));
|
||||||
@ -1606,7 +1665,7 @@ MATCHER(IsNotNull, "") { return arg != nullptr; }
|
|||||||
// Verifies that a matcher defined using MATCHER() can work on
|
// Verifies that a matcher defined using MATCHER() can work on
|
||||||
// move-only types.
|
// move-only types.
|
||||||
TEST(MatcherMacroTest, WorksOnMoveOnlyType) {
|
TEST(MatcherMacroTest, WorksOnMoveOnlyType) {
|
||||||
std::unique_ptr<int> p(new int(3));
|
std::unique_ptr<int> p = std::make_unique<int>(3);
|
||||||
EXPECT_THAT(p, IsNotNull());
|
EXPECT_THAT(p, IsNotNull());
|
||||||
EXPECT_THAT(std::unique_ptr<int>(), Not(IsNotNull()));
|
EXPECT_THAT(std::unique_ptr<int>(), Not(IsNotNull()));
|
||||||
}
|
}
|
||||||
@ -1616,7 +1675,7 @@ MATCHER_P(UniquePointee, pointee, "") { return *arg == pointee; }
|
|||||||
// Verifies that a matcher defined using MATCHER_P*() can work on
|
// Verifies that a matcher defined using MATCHER_P*() can work on
|
||||||
// move-only types.
|
// move-only types.
|
||||||
TEST(MatcherPMacroTest, WorksOnMoveOnlyType) {
|
TEST(MatcherPMacroTest, WorksOnMoveOnlyType) {
|
||||||
std::unique_ptr<int> p(new int(3));
|
std::unique_ptr<int> p = std::make_unique<int>(3);
|
||||||
EXPECT_THAT(p, UniquePointee(3));
|
EXPECT_THAT(p, UniquePointee(3));
|
||||||
EXPECT_THAT(p, Not(UniquePointee(2)));
|
EXPECT_THAT(p, Not(UniquePointee(2)));
|
||||||
}
|
}
|
||||||
|
|||||||
@ -59,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;
|
||||||
@ -85,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
|
||||||
@ -191,45 +202,45 @@ class Foo {
|
|||||||
|
|
||||||
// Tests using Invoke() with a nullary function.
|
// Tests using Invoke() with a nullary function.
|
||||||
TEST(InvokeTest, Nullary) {
|
TEST(InvokeTest, Nullary) {
|
||||||
Action<int()> a = Invoke(Nullary); // NOLINT
|
Action<int()> a = &Nullary;
|
||||||
EXPECT_EQ(1, a.Perform(std::make_tuple()));
|
EXPECT_EQ(1, a.Perform(std::make_tuple()));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tests using Invoke() with a unary function.
|
// Tests using Invoke() with a unary function.
|
||||||
TEST(InvokeTest, Unary) {
|
TEST(InvokeTest, Unary) {
|
||||||
Action<bool(int)> a = Invoke(Unary); // NOLINT
|
Action<bool(int)> a = &Unary;
|
||||||
EXPECT_FALSE(a.Perform(std::make_tuple(1)));
|
EXPECT_FALSE(a.Perform(std::make_tuple(1)));
|
||||||
EXPECT_TRUE(a.Perform(std::make_tuple(-1)));
|
EXPECT_TRUE(a.Perform(std::make_tuple(-1)));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tests using Invoke() with a binary function.
|
// Tests using Invoke() with a binary function.
|
||||||
TEST(InvokeTest, Binary) {
|
TEST(InvokeTest, Binary) {
|
||||||
Action<const char*(const char*, short)> a = Invoke(Binary); // NOLINT
|
Action<const char*(const char*, short)> a = &Binary; // NOLINT
|
||||||
const char* p = "Hello";
|
const char* p = "Hello";
|
||||||
EXPECT_EQ(p + 2, a.Perform(std::make_tuple(p, Short(2))));
|
EXPECT_EQ(p + 2, a.Perform(std::make_tuple(p, Short(2))));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tests using Invoke() with a ternary function.
|
// Tests using Invoke() with a ternary function.
|
||||||
TEST(InvokeTest, Ternary) {
|
TEST(InvokeTest, Ternary) {
|
||||||
Action<int(int, char, short)> a = Invoke(Ternary); // NOLINT
|
Action<int(int, char, short)> a = &Ternary; // NOLINT
|
||||||
EXPECT_EQ(6, a.Perform(std::make_tuple(1, '\2', Short(3))));
|
EXPECT_EQ(6, a.Perform(std::make_tuple(1, '\2', Short(3))));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tests using Invoke() with a 4-argument function.
|
// Tests using Invoke() with a 4-argument function.
|
||||||
TEST(InvokeTest, FunctionThatTakes4Arguments) {
|
TEST(InvokeTest, FunctionThatTakes4Arguments) {
|
||||||
Action<int(int, int, int, int)> a = Invoke(SumOf4); // NOLINT
|
Action<int(int, int, int, int)> a = &SumOf4;
|
||||||
EXPECT_EQ(1234, a.Perform(std::make_tuple(1000, 200, 30, 4)));
|
EXPECT_EQ(1234, a.Perform(std::make_tuple(1000, 200, 30, 4)));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tests using Invoke() with a 5-argument function.
|
// Tests using Invoke() with a 5-argument function.
|
||||||
TEST(InvokeTest, FunctionThatTakes5Arguments) {
|
TEST(InvokeTest, FunctionThatTakes5Arguments) {
|
||||||
Action<int(int, int, int, int, int)> a = Invoke(SumOf5); // NOLINT
|
Action<int(int, int, int, int, int)> a = &SumOf5;
|
||||||
EXPECT_EQ(12345, a.Perform(std::make_tuple(10000, 2000, 300, 40, 5)));
|
EXPECT_EQ(12345, a.Perform(std::make_tuple(10000, 2000, 300, 40, 5)));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tests using Invoke() with a 6-argument function.
|
// Tests using Invoke() with a 6-argument function.
|
||||||
TEST(InvokeTest, FunctionThatTakes6Arguments) {
|
TEST(InvokeTest, FunctionThatTakes6Arguments) {
|
||||||
Action<int(int, int, int, int, int, int)> a = Invoke(SumOf6); // NOLINT
|
Action<int(int, int, int, int, int, int)> a = &SumOf6;
|
||||||
EXPECT_EQ(123456,
|
EXPECT_EQ(123456,
|
||||||
a.Perform(std::make_tuple(100000, 20000, 3000, 400, 50, 6)));
|
a.Perform(std::make_tuple(100000, 20000, 3000, 400, 50, 6)));
|
||||||
}
|
}
|
||||||
@ -242,7 +253,7 @@ inline const char* CharPtr(const char* s) { return s; }
|
|||||||
TEST(InvokeTest, FunctionThatTakes7Arguments) {
|
TEST(InvokeTest, FunctionThatTakes7Arguments) {
|
||||||
Action<std::string(const char*, const char*, const char*, const char*,
|
Action<std::string(const char*, const char*, const char*, const char*,
|
||||||
const char*, const char*, const char*)>
|
const char*, const char*, const char*)>
|
||||||
a = Invoke(Concat7);
|
a = &Concat7;
|
||||||
EXPECT_EQ("1234567",
|
EXPECT_EQ("1234567",
|
||||||
a.Perform(std::make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
|
a.Perform(std::make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
|
||||||
CharPtr("4"), CharPtr("5"), CharPtr("6"),
|
CharPtr("4"), CharPtr("5"), CharPtr("6"),
|
||||||
@ -253,7 +264,7 @@ TEST(InvokeTest, FunctionThatTakes7Arguments) {
|
|||||||
TEST(InvokeTest, FunctionThatTakes8Arguments) {
|
TEST(InvokeTest, FunctionThatTakes8Arguments) {
|
||||||
Action<std::string(const char*, const char*, const char*, const char*,
|
Action<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*)>
|
||||||
a = Invoke(Concat8);
|
a = &Concat8;
|
||||||
EXPECT_EQ("12345678",
|
EXPECT_EQ("12345678",
|
||||||
a.Perform(std::make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
|
a.Perform(std::make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
|
||||||
CharPtr("4"), CharPtr("5"), CharPtr("6"),
|
CharPtr("4"), CharPtr("5"), CharPtr("6"),
|
||||||
@ -265,7 +276,7 @@ TEST(InvokeTest, FunctionThatTakes9Arguments) {
|
|||||||
Action<std::string(const char*, const char*, const char*, const char*,
|
Action<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*)>
|
||||||
a = Invoke(Concat9);
|
a = &Concat9;
|
||||||
EXPECT_EQ("123456789", a.Perform(std::make_tuple(
|
EXPECT_EQ("123456789", a.Perform(std::make_tuple(
|
||||||
CharPtr("1"), CharPtr("2"), CharPtr("3"),
|
CharPtr("1"), CharPtr("2"), CharPtr("3"),
|
||||||
CharPtr("4"), CharPtr("5"), CharPtr("6"),
|
CharPtr("4"), CharPtr("5"), CharPtr("6"),
|
||||||
@ -277,7 +288,7 @@ TEST(InvokeTest, FunctionThatTakes10Arguments) {
|
|||||||
Action<std::string(const char*, const char*, const char*, const char*,
|
Action<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*)>
|
||||||
a = Invoke(Concat10);
|
a = &Concat10;
|
||||||
EXPECT_EQ("1234567890",
|
EXPECT_EQ("1234567890",
|
||||||
a.Perform(std::make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
|
a.Perform(std::make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
|
||||||
CharPtr("4"), CharPtr("5"), CharPtr("6"),
|
CharPtr("4"), CharPtr("5"), CharPtr("6"),
|
||||||
@ -287,12 +298,12 @@ TEST(InvokeTest, FunctionThatTakes10Arguments) {
|
|||||||
|
|
||||||
// Tests using Invoke() with functions with parameters declared as Unused.
|
// Tests using Invoke() with functions with parameters declared as Unused.
|
||||||
TEST(InvokeTest, FunctionWithUnusedParameters) {
|
TEST(InvokeTest, FunctionWithUnusedParameters) {
|
||||||
Action<int(int, int, double, const std::string&)> a1 = Invoke(SumOfFirst2);
|
Action<int(int, int, double, const std::string&)> a1 = &SumOfFirst2;
|
||||||
std::tuple<int, int, double, std::string> dummy =
|
std::tuple<int, int, double, std::string> dummy =
|
||||||
std::make_tuple(10, 2, 5.6, std::string("hi"));
|
std::make_tuple(10, 2, 5.6, std::string("hi"));
|
||||||
EXPECT_EQ(12, a1.Perform(dummy));
|
EXPECT_EQ(12, a1.Perform(dummy));
|
||||||
|
|
||||||
Action<int(int, int, bool, int*)> a2 = Invoke(SumOfFirst2);
|
Action<int(int, int, bool, int*)> a2 = &SumOfFirst2;
|
||||||
EXPECT_EQ(
|
EXPECT_EQ(
|
||||||
23, a2.Perform(std::make_tuple(20, 3, true, static_cast<int*>(nullptr))));
|
23, a2.Perform(std::make_tuple(20, 3, true, static_cast<int*>(nullptr))));
|
||||||
}
|
}
|
||||||
@ -309,13 +320,13 @@ TEST(InvokeTest, MethodWithUnusedParameters) {
|
|||||||
|
|
||||||
// Tests using Invoke() with a functor.
|
// Tests using Invoke() with a functor.
|
||||||
TEST(InvokeTest, Functor) {
|
TEST(InvokeTest, Functor) {
|
||||||
Action<long(long, int)> a = Invoke(plus<long>()); // NOLINT
|
Action<long(long, int)> a = plus<long>(); // NOLINT
|
||||||
EXPECT_EQ(3L, a.Perform(std::make_tuple(1, 2)));
|
EXPECT_EQ(3L, a.Perform(std::make_tuple(1, 2)));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tests using Invoke(f) as an action of a compatible type.
|
// Tests using Invoke(f) as an action of a compatible type.
|
||||||
TEST(InvokeTest, FunctionWithCompatibleType) {
|
TEST(InvokeTest, FunctionWithCompatibleType) {
|
||||||
Action<long(int, short, char, bool)> a = Invoke(SumOf4); // NOLINT
|
Action<long(int, short, char, bool)> a = &SumOf4; // NOLINT
|
||||||
EXPECT_EQ(4321, a.Perform(std::make_tuple(4000, Short(300), Char(20), true)));
|
EXPECT_EQ(4321, a.Perform(std::make_tuple(4000, Short(300), Char(20), true)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -436,13 +447,13 @@ TEST(InvokeMethodTest, MethodWithCompatibleType) {
|
|||||||
|
|
||||||
// Tests using WithoutArgs with an action that takes no argument.
|
// Tests using WithoutArgs with an action that takes no argument.
|
||||||
TEST(WithoutArgsTest, NoArg) {
|
TEST(WithoutArgsTest, NoArg) {
|
||||||
Action<int(int n)> a = WithoutArgs(Invoke(Nullary)); // NOLINT
|
Action<int(int n)> a = WithoutArgs(&Nullary); // NOLINT
|
||||||
EXPECT_EQ(1, a.Perform(std::make_tuple(2)));
|
EXPECT_EQ(1, a.Perform(std::make_tuple(2)));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tests using WithArg with an action that takes 1 argument.
|
// Tests using WithArg with an action that takes 1 argument.
|
||||||
TEST(WithArgTest, OneArg) {
|
TEST(WithArgTest, OneArg) {
|
||||||
Action<bool(double x, int n)> b = WithArg<1>(Invoke(Unary)); // NOLINT
|
Action<bool(double x, int n)> b = WithArg<1>(&Unary); // NOLINT
|
||||||
EXPECT_TRUE(b.Perform(std::make_tuple(1.5, -1)));
|
EXPECT_TRUE(b.Perform(std::make_tuple(1.5, -1)));
|
||||||
EXPECT_FALSE(b.Perform(std::make_tuple(1.5, 1)));
|
EXPECT_FALSE(b.Perform(std::make_tuple(1.5, 1)));
|
||||||
}
|
}
|
||||||
@ -482,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;
|
||||||
@ -698,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
|
||||||
@ -734,34 +785,34 @@ 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");
|
||||||
EXPECT_EQ("1234567", a.Perform(std::make_tuple(&Concat7)));
|
EXPECT_EQ("1234567", a.Perform(std::make_tuple(&Concat7)));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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");
|
||||||
EXPECT_EQ("12345678", a.Perform(std::make_tuple(&Concat8)));
|
EXPECT_EQ("12345678", a.Perform(std::make_tuple(&Concat8)));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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");
|
||||||
EXPECT_EQ("123456789", a.Perform(std::make_tuple(&Concat9)));
|
EXPECT_EQ("123456789", a.Perform(std::make_tuple(&Concat9)));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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");
|
||||||
@ -806,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;
|
||||||
|
|||||||
@ -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(), "");
|
||||||
|
|||||||
@ -160,7 +160,7 @@ class MockCC : public CC {
|
|||||||
// Tests that a method with expanded name compiles.
|
// Tests that a method with expanded name compiles.
|
||||||
TEST(OnCallSyntaxTest, CompilesWithMethodNameExpandedFromMacro) {
|
TEST(OnCallSyntaxTest, CompilesWithMethodNameExpandedFromMacro) {
|
||||||
MockCC cc;
|
MockCC cc;
|
||||||
ON_CALL(cc, Method());
|
(void)ON_CALL(cc, Method());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tests that the method with expanded name not only compiles but runs
|
// Tests that the method with expanded name not only compiles but runs
|
||||||
@ -193,7 +193,7 @@ TEST(OnCallSyntaxTest, EvaluatesFirstArgumentOnce) {
|
|||||||
MockA a;
|
MockA a;
|
||||||
MockA* pa = &a;
|
MockA* pa = &a;
|
||||||
|
|
||||||
ON_CALL(*pa++, DoA(_));
|
(void)ON_CALL(*pa++, DoA(_));
|
||||||
EXPECT_EQ(&a + 1, pa);
|
EXPECT_EQ(&a + 1, pa);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -201,7 +201,7 @@ TEST(OnCallSyntaxTest, EvaluatesSecondArgumentOnce) {
|
|||||||
MockA a;
|
MockA a;
|
||||||
int n = 0;
|
int n = 0;
|
||||||
|
|
||||||
ON_CALL(a, DoA(n++));
|
(void)ON_CALL(a, DoA(n++));
|
||||||
EXPECT_EQ(1, n);
|
EXPECT_EQ(1, n);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -232,7 +232,7 @@ TEST(OnCallSyntaxTest, WillByDefaultIsMandatory) {
|
|||||||
|
|
||||||
EXPECT_DEATH_IF_SUPPORTED(
|
EXPECT_DEATH_IF_SUPPORTED(
|
||||||
{
|
{
|
||||||
ON_CALL(a, DoA(5));
|
(void)ON_CALL(a, DoA(5));
|
||||||
a.DoA(5);
|
a.DoA(5);
|
||||||
},
|
},
|
||||||
"");
|
"");
|
||||||
@ -804,9 +804,8 @@ TEST(ExpectCallTest, InfersCardinality1WhenThereIsWillRepeatedly) {
|
|||||||
"to be called at least once");
|
"to be called at least once");
|
||||||
}
|
}
|
||||||
|
|
||||||
#if defined(GTEST_INTERNAL_CPLUSPLUS_LANG) && \
|
// TODO(b/396121064) - Fix this test under MSVC
|
||||||
GTEST_INTERNAL_CPLUSPLUS_LANG >= 201703L
|
#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.
|
||||||
@ -847,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.
|
||||||
@ -906,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));
|
||||||
@ -2046,7 +2045,7 @@ 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/"
|
||||||
|
|||||||
@ -186,6 +186,7 @@ using testing::SetErrnoAndReturn;
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if GTEST_HAS_EXCEPTIONS
|
#if GTEST_HAS_EXCEPTIONS
|
||||||
|
using testing::Rethrow;
|
||||||
using testing::Throw;
|
using testing::Throw;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@ -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
|
||||||
|
|||||||
@ -79,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-useoncall 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-useoncall 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
|
||||||
@ -283,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-useoncall 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-useoncall 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
|
||||||
|
|
||||||
|
|||||||
@ -5,7 +5,7 @@
|
|||||||
# CMake build script for Google Test.
|
# CMake build script for Google Test.
|
||||||
#
|
#
|
||||||
# To run the tests for Google Test itself on Linux, use 'make test' or
|
# To run the tests for Google Test itself on Linux, use 'make test' or
|
||||||
# ctest. You can select which tests to run using 'ctest -R regex'.
|
# ctest. You can select which tests to run using 'ctest -R regex'.
|
||||||
# For more options, run 'ctest --help'.
|
# For more options, run 'ctest --help'.
|
||||||
|
|
||||||
# When other libraries are using a shared version of runtime libraries,
|
# When other libraries are using a shared version of runtime libraries,
|
||||||
@ -35,7 +35,7 @@ endif()
|
|||||||
|
|
||||||
########################################################################
|
########################################################################
|
||||||
#
|
#
|
||||||
# Project-wide settings
|
# Project-wide settings.
|
||||||
|
|
||||||
# Name of the project.
|
# Name of the project.
|
||||||
#
|
#
|
||||||
@ -44,7 +44,7 @@ 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.13)
|
cmake_minimum_required(VERSION 3.13)
|
||||||
project(gtest VERSION ${GOOGLETEST_VERSION} LANGUAGES CXX C)
|
project(gtest VERSION ${GOOGLETEST_VERSION} LANGUAGES CXX C)
|
||||||
@ -53,7 +53,7 @@ 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
|
||||||
@ -83,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.
|
||||||
@ -114,10 +114,10 @@ include_directories(${gtest_build_include_dirs})
|
|||||||
|
|
||||||
########################################################################
|
########################################################################
|
||||||
#
|
#
|
||||||
# Defines the gtest & gtest_main libraries. User tests should link
|
# Defines the gtest & gtest_main libraries. User tests should link
|
||||||
# with one of them.
|
# with one of them.
|
||||||
|
|
||||||
# Google Test libraries. We build them using more strict warnings than what
|
# Google Test libraries. We build them using more strict warnings than what
|
||||||
# are used for other targets, to ensure that gtest can be compiled by a user
|
# are used for other targets, to ensure that gtest can be compiled by a user
|
||||||
# aggressive about warnings.
|
# aggressive about warnings.
|
||||||
cxx_library(gtest "${cxx_strict}" src/gtest-all.cc)
|
cxx_library(gtest "${cxx_strict}" src/gtest-all.cc)
|
||||||
@ -132,9 +132,6 @@ 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()
|
||||||
@ -147,22 +144,22 @@ target_include_directories(gtest SYSTEM INTERFACE
|
|||||||
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}>")
|
||||||
if(CMAKE_SYSTEM_NAME MATCHES "QNX")
|
if(CMAKE_SYSTEM_NAME MATCHES "QNX" AND CMAKE_SYSTEM_VERSION VERSION_GREATER_EQUAL 7.1)
|
||||||
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)
|
||||||
|
|
||||||
########################################################################
|
########################################################################
|
||||||
#
|
#
|
||||||
# Samples on how to link user tests with gtest or gtest_main.
|
# Samples on how to link user tests with gtest or gtest_main.
|
||||||
#
|
#
|
||||||
# They are not built by default. To build them, set the
|
# They are not built by default. To build them, set the
|
||||||
# gtest_build_samples option to ON. You can do it by running ccmake
|
# gtest_build_samples option to ON. You can do it by running ccmake
|
||||||
# or specifying the -Dgtest_build_samples=ON flag when running cmake.
|
# or specifying the -Dgtest_build_samples=ON flag when running cmake.
|
||||||
|
|
||||||
if (gtest_build_samples)
|
if (gtest_build_samples)
|
||||||
@ -185,8 +182,8 @@ endif()
|
|||||||
# You can skip this section if you aren't interested in testing
|
# You can skip this section if you aren't interested in testing
|
||||||
# Google Test itself.
|
# Google Test itself.
|
||||||
#
|
#
|
||||||
# The tests are not built by default. To build them, set the
|
# The tests are not built by default. To build them, set the
|
||||||
# gtest_build_tests option to ON. You can do it by running ccmake
|
# gtest_build_tests option to ON. You can do it by running ccmake
|
||||||
# or specifying the -Dgtest_build_tests=ON flag when running cmake.
|
# or specifying the -Dgtest_build_tests=ON flag when running cmake.
|
||||||
|
|
||||||
if (gtest_build_tests)
|
if (gtest_build_tests)
|
||||||
@ -268,7 +265,7 @@ if (gtest_build_tests)
|
|||||||
py_test(gtest_skip_environment_check_output_test)
|
py_test(gtest_skip_environment_check_output_test)
|
||||||
|
|
||||||
# Visual Studio .NET 2003 does not support STL with exceptions disabled.
|
# Visual Studio .NET 2003 does not support STL with exceptions disabled.
|
||||||
if (NOT MSVC OR MSVC_VERSION GREATER 1310) # 1310 is Visual Studio .NET 2003
|
if (NOT MSVC OR MSVC_VERSION GREATER 1310) # 1310 is Visual Studio .NET 2003
|
||||||
cxx_executable_with_flags(
|
cxx_executable_with_flags(
|
||||||
googletest-catch-exceptions-no-ex-test_
|
googletest-catch-exceptions-no-ex-test_
|
||||||
"${cxx_no_exception}"
|
"${cxx_no_exception}"
|
||||||
|
|||||||
@ -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 v1.13.0
|
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,9 +124,9 @@ match the project in which it is included.
|
|||||||
|
|
||||||
#### C++ Standard Version
|
#### C++ Standard Version
|
||||||
|
|
||||||
An environment that supports C++14 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 14)` command along
|
project, for example by using the `set(CMAKE_CXX_STANDARD 17)` command along
|
||||||
with `set(CMAKE_CXX_STANDARD_REQUIRED ON)`. If this is not feasible, for example
|
with `set(CMAKE_CXX_STANDARD_REQUIRED ON)`. If this is not feasible, for example
|
||||||
in a C project using GoogleTest for validation, then it can be specified by
|
in a C project using GoogleTest for validation, then it can be specified by
|
||||||
adding it to the options for cmake via the`-DCMAKE_CXX_FLAGS` option.
|
adding it to the options for cmake via the`-DCMAKE_CXX_FLAGS` option.
|
||||||
@ -145,9 +145,9 @@ 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
|
||||||
|
|||||||
@ -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@")
|
||||||
|
|||||||
@ -29,7 +29,7 @@ macro(fix_default_compiler_settings_)
|
|||||||
CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
|
CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
|
||||||
if (NOT BUILD_SHARED_LIBS AND NOT gtest_force_shared_crt)
|
if (NOT BUILD_SHARED_LIBS AND NOT gtest_force_shared_crt)
|
||||||
# When Google Test is built as a shared library, it should also use
|
# When Google Test is built as a shared library, it should also use
|
||||||
# shared runtime libraries. Otherwise, it may end up with multiple
|
# shared runtime libraries. Otherwise, it may end up with multiple
|
||||||
# copies of runtime library data in different modules, resulting in
|
# copies of runtime library data in different modules, resulting in
|
||||||
# hard-to-find crashes. When it is built as a static library, it is
|
# hard-to-find crashes. When it is built as a static library, it is
|
||||||
# preferable to use CRT as static libraries, as we don't have to rely
|
# preferable to use CRT as static libraries, as we don't have to rely
|
||||||
@ -55,11 +55,11 @@ macro(fix_default_compiler_settings_)
|
|||||||
endmacro()
|
endmacro()
|
||||||
|
|
||||||
# Defines the compiler/linker flags used to build Google Test and
|
# Defines the compiler/linker flags used to build Google Test and
|
||||||
# Google Mock. You can tweak these definitions to suit your need. A
|
# Google Mock. You can tweak these definitions to suit your need. A
|
||||||
# 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.
|
||||||
@ -79,8 +79,8 @@ 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")
|
||||||
@ -110,7 +110,7 @@ macro(config_compiler_and_linker)
|
|||||||
set(cxx_exception_flags "-fexceptions")
|
set(cxx_exception_flags "-fexceptions")
|
||||||
set(cxx_no_exception_flags "-fno-exceptions")
|
set(cxx_no_exception_flags "-fno-exceptions")
|
||||||
# Until version 4.3.2, GCC doesn't define a macro to indicate
|
# Until version 4.3.2, GCC doesn't define a macro to indicate
|
||||||
# whether RTTI is enabled. Therefore we define GTEST_HAS_RTTI
|
# whether RTTI is enabled. Therefore we define GTEST_HAS_RTTI
|
||||||
# explicitly.
|
# explicitly.
|
||||||
set(cxx_no_rtti_flags "-fno-rtti -DGTEST_HAS_RTTI=0")
|
set(cxx_no_rtti_flags "-fno-rtti -DGTEST_HAS_RTTI=0")
|
||||||
set(cxx_strict_flags
|
set(cxx_strict_flags
|
||||||
@ -127,7 +127,7 @@ macro(config_compiler_and_linker)
|
|||||||
set(cxx_exception_flags "-qeh")
|
set(cxx_exception_flags "-qeh")
|
||||||
set(cxx_no_exception_flags "-qnoeh")
|
set(cxx_no_exception_flags "-qnoeh")
|
||||||
# Until version 9.0, Visual Age doesn't define a macro to indicate
|
# Until version 9.0, Visual Age doesn't define a macro to indicate
|
||||||
# whether RTTI is enabled. Therefore we define GTEST_HAS_RTTI
|
# whether RTTI is enabled. Therefore we define GTEST_HAS_RTTI
|
||||||
# explicitly.
|
# explicitly.
|
||||||
set(cxx_no_rtti_flags "-qnortti -DGTEST_HAS_RTTI=0")
|
set(cxx_no_rtti_flags "-qnortti -DGTEST_HAS_RTTI=0")
|
||||||
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "HP")
|
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "HP")
|
||||||
@ -157,7 +157,7 @@ macro(config_compiler_and_linker)
|
|||||||
set(cxx_strict "${cxx_default} ${cxx_strict_flags}")
|
set(cxx_strict "${cxx_default} ${cxx_strict_flags}")
|
||||||
endmacro()
|
endmacro()
|
||||||
|
|
||||||
# Defines the gtest & gtest_main libraries. User tests should link
|
# Defines the gtest & gtest_main libraries. User tests should link
|
||||||
# with one of them.
|
# with one of them.
|
||||||
function(cxx_library_with_type name type cxx_flags)
|
function(cxx_library_with_type name type cxx_flags)
|
||||||
# type can be either STATIC or SHARED to denote a static or shared library.
|
# type can be either STATIC or SHARED to denote a static or shared library.
|
||||||
@ -167,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"
|
||||||
@ -175,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
|
||||||
@ -195,7 +195,7 @@ function(cxx_library_with_type name type cxx_flags)
|
|||||||
target_link_libraries(${name} PUBLIC Threads::Threads)
|
target_link_libraries(${name} PUBLIC Threads::Threads)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
target_compile_features(${name} PUBLIC cxx_std_14)
|
target_compile_features(${name} PUBLIC cxx_std_17)
|
||||||
endfunction()
|
endfunction()
|
||||||
|
|
||||||
########################################################################
|
########################################################################
|
||||||
@ -212,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})
|
||||||
@ -239,19 +239,21 @@ 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)
|
||||||
cxx_executable_with_flags(
|
cxx_executable_with_flags(
|
||||||
${name} "${cxx_default}" "${libs}" "${dir}/${name}.cc" ${ARGN})
|
${name} "${cxx_default}" "${libs}" "${dir}/${name}.cc" ${ARGN})
|
||||||
endfunction()
|
endfunction()
|
||||||
|
|
||||||
find_package(Python3)
|
if(gtest_build_tests)
|
||||||
|
find_package(Python3)
|
||||||
|
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})
|
||||||
@ -260,8 +262,8 @@ 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)
|
||||||
cxx_test_with_flags("${name}" "${cxx_default}" "${libs}"
|
cxx_test_with_flags("${name}" "${cxx_default}" "${libs}"
|
||||||
@ -270,8 +272,8 @@ 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 (NOT Python3_Interpreter_FOUND)
|
if (NOT Python3_Interpreter_FOUND)
|
||||||
return()
|
return()
|
||||||
@ -307,7 +309,7 @@ function(install_project)
|
|||||||
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)
|
||||||
|
|||||||
@ -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.
|
||||||
|
|||||||
@ -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 \
|
||||||
|
|||||||
@ -67,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.
|
||||||
@ -296,12 +296,12 @@ class MatcherBase : private MatcherDescriberInterface {
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
MatcherBase(MatcherBase&& other)
|
MatcherBase(MatcherBase&& other) noexcept
|
||||||
: vtable_(other.vtable_), buffer_(other.buffer_) {
|
: vtable_(other.vtable_), buffer_(other.buffer_) {
|
||||||
other.vtable_ = nullptr;
|
other.vtable_ = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
MatcherBase& operator=(MatcherBase&& other) {
|
MatcherBase& operator=(MatcherBase&& other) noexcept {
|
||||||
if (this == &other) return *this;
|
if (this == &other) return *this;
|
||||||
Destroy();
|
Destroy();
|
||||||
vtable_ = other.vtable_;
|
vtable_ = other.vtable_;
|
||||||
@ -773,6 +773,35 @@ class GeMatcher
|
|||||||
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;
|
||||||
|
|||||||
@ -59,7 +59,7 @@
|
|||||||
#ifdef GTEST_HAS_ABSL
|
#ifdef GTEST_HAS_ABSL
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
|
|
||||||
#include "absl/strings/internal/has_absl_stringify.h"
|
#include "absl/strings/has_absl_stringify.h"
|
||||||
#include "absl/strings/str_cat.h"
|
#include "absl/strings/str_cat.h"
|
||||||
#endif // GTEST_HAS_ABSL
|
#endif // GTEST_HAS_ABSL
|
||||||
|
|
||||||
@ -121,14 +121,14 @@ class GTEST_API_ Message {
|
|||||||
// Streams a non-pointer value to this object. If building a version of
|
// Streams a non-pointer value to this object. If building a version of
|
||||||
// GoogleTest with ABSL, this overload is only enabled if the value does not
|
// GoogleTest with ABSL, this overload is only enabled if the value does not
|
||||||
// have an AbslStringify definition.
|
// have an AbslStringify definition.
|
||||||
template <typename T
|
template <
|
||||||
|
typename T
|
||||||
#ifdef GTEST_HAS_ABSL
|
#ifdef GTEST_HAS_ABSL
|
||||||
,
|
,
|
||||||
typename std::enable_if<
|
typename std::enable_if<!absl::HasAbslStringify<T>::value, // NOLINT
|
||||||
!absl::strings_internal::HasAbslStringify<T>::value, // NOLINT
|
int>::type = 0
|
||||||
int>::type = 0
|
|
||||||
#endif // GTEST_HAS_ABSL
|
#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.
|
||||||
@ -153,9 +153,8 @@ class GTEST_API_ Message {
|
|||||||
// Streams a non-pointer value with an AbslStringify definition to this
|
// Streams a non-pointer value with an AbslStringify definition to this
|
||||||
// object.
|
// object.
|
||||||
template <typename T,
|
template <typename T,
|
||||||
typename std::enable_if<
|
typename std::enable_if<absl::HasAbslStringify<T>::value, // NOLINT
|
||||||
absl::strings_internal::HasAbslStringify<T>::value, // NOLINT
|
int>::type = 0>
|
||||||
int>::type = 0>
|
|
||||||
inline Message& operator<<(const T& val) {
|
inline Message& operator<<(const T& val) {
|
||||||
// ::operator<< is needed here for a similar reason as with the non-Abseil
|
// ::operator<< is needed here for a similar reason as with the non-Abseil
|
||||||
// version above
|
// version above
|
||||||
|
|||||||
@ -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,10 +443,65 @@ 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) \
|
||||||
@ -469,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_ = \
|
||||||
@ -493,39 +550,38 @@ internal::ParamConverterGenerator<T> ConvertGenerator(
|
|||||||
#define GTEST_GET_FIRST_(first, ...) first
|
#define GTEST_GET_FIRST_(first, ...) first
|
||||||
#define GTEST_GET_SECOND_(first, second, ...) second
|
#define GTEST_GET_SECOND_(first, second, ...) second
|
||||||
|
|
||||||
#define INSTANTIATE_TEST_SUITE_P(prefix, test_suite_name, ...) \
|
#define INSTANTIATE_TEST_SUITE_P(prefix, test_suite_name, ...) \
|
||||||
static ::testing::internal::ParamGenerator<test_suite_name::ParamType> \
|
static ::testing::internal::ParamGenerator<test_suite_name::ParamType> \
|
||||||
gtest_##prefix##test_suite_name##_EvalGenerator_() { \
|
gtest_##prefix##test_suite_name##_EvalGenerator_() { \
|
||||||
return GTEST_EXPAND_(GTEST_GET_FIRST_(__VA_ARGS__, DUMMY_PARAM_)); \
|
return GTEST_EXPAND_(GTEST_GET_FIRST_(__VA_ARGS__, DUMMY_PARAM_)); \
|
||||||
} \
|
} \
|
||||||
static ::std::string gtest_##prefix##test_suite_name##_EvalGenerateName_( \
|
static ::std::string gtest_##prefix##test_suite_name##_EvalGenerateName_( \
|
||||||
const ::testing::TestParamInfo<test_suite_name::ParamType>& info) { \
|
const ::testing::TestParamInfo<test_suite_name::ParamType>& info) { \
|
||||||
if (::testing::internal::AlwaysFalse()) { \
|
if (::testing::internal::AlwaysFalse()) { \
|
||||||
::testing::internal::TestNotEmpty(GTEST_EXPAND_(GTEST_GET_SECOND_( \
|
::testing::internal::TestNotEmpty(GTEST_EXPAND_(GTEST_GET_SECOND_( \
|
||||||
__VA_ARGS__, \
|
__VA_ARGS__, \
|
||||||
::testing::internal::DefaultParamName<test_suite_name::ParamType>, \
|
::testing::internal::DefaultParamName<test_suite_name::ParamType>, \
|
||||||
DUMMY_PARAM_))); \
|
DUMMY_PARAM_))); \
|
||||||
auto t = std::make_tuple(__VA_ARGS__); \
|
auto t = std::make_tuple(__VA_ARGS__); \
|
||||||
static_assert(std::tuple_size<decltype(t)>::value <= 2, \
|
static_assert(std::tuple_size<decltype(t)>::value <= 2, \
|
||||||
"Too Many Args!"); \
|
"Too Many Args!"); \
|
||||||
} \
|
} \
|
||||||
return ((GTEST_EXPAND_(GTEST_GET_SECOND_( \
|
return ((GTEST_EXPAND_(GTEST_GET_SECOND_( \
|
||||||
__VA_ARGS__, \
|
__VA_ARGS__, \
|
||||||
::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>( \
|
GTEST_STRINGIFY_(test_suite_name), \
|
||||||
GTEST_STRINGIFY_(test_suite_name), \
|
::testing::internal::CodeLocation(__FILE__, __LINE__)) \
|
||||||
::testing::internal::CodeLocation(__FILE__, __LINE__)) \
|
->AddTestSuiteInstantiation( \
|
||||||
->AddTestSuiteInstantiation( \
|
GTEST_STRINGIFY_(prefix), \
|
||||||
GTEST_STRINGIFY_(prefix), \
|
>est_##prefix##test_suite_name##_EvalGenerator_, \
|
||||||
>est_##prefix##test_suite_name##_EvalGenerator_, \
|
>est_##prefix##test_suite_name##_EvalGenerateName_, __FILE__, \
|
||||||
>est_##prefix##test_suite_name##_EvalGenerateName_, \
|
__LINE__)
|
||||||
__FILE__, __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) \
|
||||||
|
|||||||
@ -104,24 +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
|
#ifdef GTEST_HAS_ABSL
|
||||||
#include "absl/strings/internal/has_absl_stringify.h"
|
#include "absl/strings/has_absl_stringify.h"
|
||||||
#include "absl/strings/str_cat.h"
|
#include "absl/strings/str_cat.h"
|
||||||
#endif // GTEST_HAS_ABSL
|
#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.
|
||||||
@ -131,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 << '{';
|
||||||
@ -269,10 +300,9 @@ struct ConvertibleToStringViewPrinter {
|
|||||||
|
|
||||||
#ifdef GTEST_HAS_ABSL
|
#ifdef GTEST_HAS_ABSL
|
||||||
struct ConvertibleToAbslStringifyPrinter {
|
struct ConvertibleToAbslStringifyPrinter {
|
||||||
template <
|
template <typename T,
|
||||||
typename T,
|
typename = typename std::enable_if<
|
||||||
typename = typename std::enable_if<
|
absl::HasAbslStringify<T>::value>::type> // NOLINT
|
||||||
absl::strings_internal::HasAbslStringify<T>::value>::type> // NOLINT
|
|
||||||
static void PrintValue(const T& value, ::std::ostream* os) {
|
static void PrintValue(const T& value, ::std::ostream* os) {
|
||||||
*os << absl::StrCat(value);
|
*os << absl::StrCat(value);
|
||||||
}
|
}
|
||||||
@ -495,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_lib_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
|
||||||
|
|
||||||
@ -530,49 +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<FloatType>(static_cast<int32_t>(val * mulfor6 + 0.5)) /
|
if (static_cast<FloatType>(static_cast<int32_t>(
|
||||||
|
val * mulfor6 + (static_cast<FloatType>(0.5)))) /
|
||||||
mulfor6 ==
|
mulfor6 ==
|
||||||
val)
|
val)
|
||||||
return 6;
|
return 6;
|
||||||
} else if (val < 1e10) {
|
} else if (val < static_cast<FloatType>(1e10)) {
|
||||||
FloatType divfor6 = 1.0;
|
FloatType divfor6 = static_cast<FloatType>(1.0);
|
||||||
if (val >= 1e9) { // 1,000,000,000 to 9,999,999,999
|
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<FloatType>(static_cast<int32_t>(val / divfor6 + 0.5)) *
|
if (static_cast<FloatType>(static_cast<int32_t>(
|
||||||
|
val / divfor6 + (static_cast<FloatType>(0.5)))) *
|
||||||
divfor6 ==
|
divfor6 ==
|
||||||
val)
|
val)
|
||||||
return 6;
|
return 6;
|
||||||
}
|
}
|
||||||
|
#ifdef __GNUC__
|
||||||
|
#pragma GCC diagnostic pop
|
||||||
|
#endif
|
||||||
return full;
|
return full;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -655,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
|
||||||
|
|
||||||
@ -746,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>
|
||||||
@ -815,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 {
|
||||||
@ -831,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
|
||||||
@ -841,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";
|
||||||
@ -862,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 << '(';
|
||||||
#ifdef 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 << ')';
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -901,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>
|
||||||
|
|||||||
@ -48,15 +48,15 @@ template <typename T>
|
|||||||
class FooTest : public testing::Test {
|
class FooTest : public testing::Test {
|
||||||
public:
|
public:
|
||||||
...
|
...
|
||||||
typedef std::list<T> List;
|
using List = ::std::list<T>;
|
||||||
static T shared_;
|
static T shared_;
|
||||||
T value_;
|
T value_;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Next, associate a list of types with the test suite, which will be
|
// Next, associate a list of types with the test suite, which will be
|
||||||
// repeated for each type in the list. The typedef is necessary for
|
// repeated for each type in the list. The using-declaration is necessary for
|
||||||
// the macro to parse correctly.
|
// the macro to parse correctly.
|
||||||
typedef testing::Types<char, int, unsigned int> MyTypes;
|
using MyTypes = ::testing::Types<char, int, unsigned int>;
|
||||||
TYPED_TEST_SUITE(FooTest, MyTypes);
|
TYPED_TEST_SUITE(FooTest, MyTypes);
|
||||||
|
|
||||||
// If the type list contains only one type, you can write that type
|
// If the type list contains only one type, you can write that type
|
||||||
@ -157,7 +157,7 @@ REGISTER_TYPED_TEST_SUITE_P(FooTest,
|
|||||||
// argument to the INSTANTIATE_* macro is a prefix that will be added
|
// argument to the INSTANTIATE_* macro is a prefix that will be added
|
||||||
// to the actual test suite name. Remember to pick unique prefixes for
|
// to the actual test suite name. Remember to pick unique prefixes for
|
||||||
// different instances.
|
// different instances.
|
||||||
typedef testing::Types<char, int, unsigned int> MyTypes;
|
using MyTypes = ::testing::Types<char, int, unsigned int>;
|
||||||
INSTANTIATE_TYPED_TEST_SUITE_P(My, FooTest, MyTypes);
|
INSTANTIATE_TYPED_TEST_SUITE_P(My, FooTest, MyTypes);
|
||||||
|
|
||||||
// If the type list contains only one type, you can write that type
|
// If the type list contains only one type, you can write that type
|
||||||
@ -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)>, \
|
||||||
@ -267,22 +267,22 @@ INSTANTIATE_TYPED_TEST_SUITE_P(My, FooTest, MyTypes);
|
|||||||
TYPED_TEST_SUITE_P
|
TYPED_TEST_SUITE_P
|
||||||
#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_
|
#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_
|
||||||
|
|
||||||
#define TYPED_TEST_P(SuiteName, TestName) \
|
#define TYPED_TEST_P(SuiteName, TestName) \
|
||||||
namespace GTEST_SUITE_NAMESPACE_(SuiteName) { \
|
namespace GTEST_SUITE_NAMESPACE_(SuiteName) { \
|
||||||
template <typename gtest_TypeParam_> \
|
template <typename gtest_TypeParam_> \
|
||||||
class TestName : public SuiteName<gtest_TypeParam_> { \
|
class TestName : public SuiteName<gtest_TypeParam_> { \
|
||||||
private: \
|
private: \
|
||||||
typedef SuiteName<gtest_TypeParam_> TestFixture; \
|
typedef SuiteName<gtest_TypeParam_> TestFixture; \
|
||||||
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)); \
|
||||||
} \
|
} \
|
||||||
template <typename gtest_TypeParam_> \
|
template <typename gtest_TypeParam_> \
|
||||||
void GTEST_SUITE_NAMESPACE_( \
|
void GTEST_SUITE_NAMESPACE_( \
|
||||||
SuiteName)::TestName<gtest_TypeParam_>::TestBody()
|
SuiteName)::TestName<gtest_TypeParam_>::TestBody()
|
||||||
|
|
||||||
// Note: this won't work correctly if the trailing arguments are macros.
|
// Note: this won't work correctly if the trailing arguments are macros.
|
||||||
@ -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>:: \
|
||||||
|
|||||||
@ -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"
|
||||||
|
|
||||||
@ -608,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,
|
||||||
@ -616,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,
|
||||||
@ -684,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);
|
||||||
|
|
||||||
@ -1124,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.
|
||||||
@ -1263,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_; }
|
||||||
@ -1271,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;
|
||||||
@ -1574,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),
|
||||||
@ -1595,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 {
|
||||||
@ -1606,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
|
||||||
@ -1678,7 +1696,7 @@ class WithParamInterface {
|
|||||||
|
|
||||||
// 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?";
|
||||||
@ -1752,6 +1770,7 @@ class TestWithParam : public Test, public WithParamInterface<T> {};
|
|||||||
// generic name and clashes with some other libraries.
|
// generic name and clashes with some other libraries.
|
||||||
#if !(defined(GTEST_DONT_DEFINE_FAIL) && 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.
|
||||||
@ -2308,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(); }
|
||||||
|
|
||||||
|
|||||||
@ -46,17 +46,38 @@
|
|||||||
|
|
||||||
#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";
|
||||||
|
|
||||||
|
// 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
|
#ifdef GTEST_HAS_DEATH_TEST
|
||||||
|
|
||||||
GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \
|
GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \
|
||||||
@ -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:
|
||||||
@ -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
|
||||||
|
|||||||
@ -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_; }
|
||||||
|
|
||||||
|
|||||||
@ -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,17 +290,17 @@ 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.
|
||||||
@ -310,16 +309,16 @@ class FloatingPoint {
|
|||||||
// 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 {
|
||||||
@ -333,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
|
||||||
@ -363,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;
|
||||||
@ -377,14 +369,13 @@ 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.
|
||||||
};
|
};
|
||||||
|
|
||||||
// Typedefs the instances of the FloatingPoint template class that we
|
// Typedefs the instances of the FloatingPoint template class that we
|
||||||
@ -475,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;
|
||||||
@ -556,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
|
||||||
@ -565,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);
|
||||||
@ -596,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;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -701,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>()) {
|
||||||
@ -713,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.
|
||||||
@ -726,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);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -740,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>& =
|
||||||
@ -787,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);
|
||||||
@ -900,11 +886,6 @@ class HasDebugStringAndShortDebugString {
|
|||||||
HasDebugStringType::value && HasShortDebugStringType::value;
|
HasDebugStringType::value && HasShortDebugStringType::value;
|
||||||
};
|
};
|
||||||
|
|
||||||
#ifdef GTEST_INTERNAL_NEED_REDUNDANT_CONSTEXPR_DECL
|
|
||||||
template <typename T>
|
|
||||||
constexpr bool HasDebugStringAndShortDebugString<T>::value;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// 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
|
||||||
@ -1143,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
|
||||||
@ -1185,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.
|
||||||
@ -1196,9 +1143,8 @@ 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)...));
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct FlatTupleConstructTag {};
|
struct FlatTupleConstructTag {};
|
||||||
@ -1223,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)
|
||||||
@ -1260,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;
|
||||||
@ -1281,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
|
||||||
@ -1541,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, \
|
||||||
|
|||||||
@ -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;
|
||||||
@ -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>
|
||||||
@ -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();
|
std::string test_suite_name;
|
||||||
test_it != tests_.end(); ++test_it) {
|
std::string test_name;
|
||||||
std::shared_ptr<TestInfo> test_info = *test_it;
|
for (const std::shared_ptr<TestInfo>& test_info : tests_) {
|
||||||
for (typename InstantiationContainer::iterator gen_it =
|
for (const InstantiationInfo& instantiation : instantiations_) {
|
||||||
instantiations_.begin();
|
const std::string& instantiation_name = instantiation.name;
|
||||||
gen_it != instantiations_.end(); ++gen_it) {
|
ParamGenerator<ParamType> generator((*instantiation.generator)());
|
||||||
const std::string& instantiation_name = gen_it->name;
|
ParamNameGeneratorFunc* name_func = instantiation.name_func;
|
||||||
ParamGenerator<ParamType> generator((*gen_it->generator)());
|
const char* file = instantiation.file;
|
||||||
ParamNameGeneratorFunc* name_func = gen_it->name_func;
|
int line = instantiation.line;
|
||||||
const char* file = gen_it->file;
|
|
||||||
int line = gen_it->line;
|
|
||||||
|
|
||||||
std::string test_suite_name;
|
|
||||||
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),
|
||||||
@ -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 (test_suite_info->GetTestSuiteTypeId() != GetTypeId<TestSuite>()) {
|
if (item_it != suite_name_to_info_index_.end()) {
|
||||||
// Complain about incorrect usage of Google Test facilities
|
auto* test_suite_info = test_suite_infos_[item_it->second];
|
||||||
// and terminate the program since we cannot guaranty correct
|
if (test_suite_info->GetTestSuiteTypeId() != GetTypeId<TestSuite>()) {
|
||||||
// test suite setup and tear-down in this case.
|
// Complain about incorrect usage of Google Test facilities
|
||||||
ReportInvalidTestSuiteType(test_suite_name, code_location);
|
// and terminate the program since we cannot guaranty correct
|
||||||
posix::Abort();
|
// test suite setup and tear-down in this case.
|
||||||
} else {
|
ReportInvalidTestSuiteType(test_suite_name.c_str(), code_location);
|
||||||
// At this point we are sure that the object we found is of the same
|
posix::Abort();
|
||||||
// type we are looking for, so we downcast it to that type
|
} else {
|
||||||
// without further checks.
|
// At this point we are sure that the object we found is of the same
|
||||||
typed_test_info = CheckedDowncastToActualType<
|
// type we are looking for, so we downcast it to that type
|
||||||
ParameterizedTestSuiteInfo<TestSuite>>(test_suite_info);
|
// without further checks.
|
||||||
}
|
typed_test_info =
|
||||||
break;
|
CheckedDowncastToActualType<ParameterizedTestSuiteInfo<TestSuite>>(
|
||||||
|
test_suite_info);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
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;
|
||||||
@ -803,12 +807,12 @@ 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>())...};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -838,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,
|
||||||
@ -929,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_;
|
||||||
};
|
};
|
||||||
@ -948,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());
|
||||||
@ -961,13 +965,21 @@ 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 = default;
|
~Iterator() override = default;
|
||||||
|
|
||||||
@ -976,7 +988,9 @@ class ParamGeneratorConverter : public ParamGeneratorInterface<To> {
|
|||||||
}
|
}
|
||||||
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);
|
||||||
@ -996,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
|
||||||
|
|
||||||
|
|||||||
@ -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.
|
||||||
|
|||||||
@ -194,26 +194,12 @@
|
|||||||
//
|
//
|
||||||
// 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
|
||||||
// Always defined to 0 or 1.
|
|
||||||
// GTEST_INTERNAL_HAS_OPTIONAL - for enabling UniversalPrinter<std::optional>
|
|
||||||
// or
|
|
||||||
// UniversalPrinter<absl::optional>
|
|
||||||
// specializations. Always defined to 0 or 1.
|
|
||||||
// GTEST_INTERNAL_HAS_STRING_VIEW - for enabling Matcher<std::string_view> or
|
|
||||||
// Matcher<absl::string_view>
|
|
||||||
// specializations. Always defined to 0 or 1.
|
|
||||||
// GTEST_INTERNAL_HAS_VARIANT - for enabling UniversalPrinter<std::variant> or
|
|
||||||
// UniversalPrinter<absl::variant>
|
|
||||||
// specializations. Always defined to 0 or 1.
|
|
||||||
// GTEST_USE_OWN_FLAGFILE_FLAG_ - Always defined to 0 or 1.
|
// GTEST_USE_OWN_FLAGFILE_FLAG_ - Always defined to 0 or 1.
|
||||||
// GTEST_HAS_CXXABI_H_ - Always defined to 0 or 1.
|
// GTEST_HAS_CXXABI_H_ - Always defined to 0 or 1.
|
||||||
// GTEST_CAN_STREAM_RESULTS_ - Always defined to 0 or 1.
|
// GTEST_CAN_STREAM_RESULTS_ - Always defined to 0 or 1.
|
||||||
@ -260,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.
|
||||||
@ -275,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
|
||||||
@ -320,7 +321,8 @@
|
|||||||
#define GTEST_HAS_NOTIFICATION_ 0
|
#define GTEST_HAS_NOTIFICATION_ 0
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef GTEST_HAS_ABSL
|
#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"
|
||||||
@ -590,7 +592,8 @@ typedef struct _RTL_CRITICAL_SECTION GTEST_CRITICAL_SECTION;
|
|||||||
defined(GTEST_OS_NETBSD) || defined(GTEST_OS_FUCHSIA) || \
|
defined(GTEST_OS_NETBSD) || defined(GTEST_OS_FUCHSIA) || \
|
||||||
defined(GTEST_OS_DRAGONFLY) || defined(GTEST_OS_GNU_KFREEBSD) || \
|
defined(GTEST_OS_DRAGONFLY) || defined(GTEST_OS_GNU_KFREEBSD) || \
|
||||||
defined(GTEST_OS_OPENBSD) || defined(GTEST_OS_HAIKU) || \
|
defined(GTEST_OS_OPENBSD) || defined(GTEST_OS_HAIKU) || \
|
||||||
defined(GTEST_OS_GNU_HURD))
|
defined(GTEST_OS_GNU_HURD) || defined(GTEST_OS_SOLARIS) || \
|
||||||
|
defined(GTEST_OS_AIX) || defined(GTEST_OS_ZOS))
|
||||||
#define GTEST_HAS_PTHREAD 1
|
#define GTEST_HAS_PTHREAD 1
|
||||||
#else
|
#else
|
||||||
#define GTEST_HAS_PTHREAD 0
|
#define GTEST_HAS_PTHREAD 0
|
||||||
@ -609,7 +612,7 @@ 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.
|
||||||
|
|
||||||
@ -640,9 +643,9 @@ typedef struct _RTL_CRITICAL_SECTION GTEST_CRITICAL_SECTION;
|
|||||||
// 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 defined(GTEST_OS_WINDOWS_MOBILE) || defined(GTEST_OS_WINDOWS_PHONE) || \
|
#if defined(GTEST_OS_WINDOWS_MOBILE) || defined(GTEST_OS_WINDOWS_PHONE) || \
|
||||||
defined(GTEST_OS_WINDOWS_RT) || defined(GTEST_OS_ESP8266) || \
|
defined(GTEST_OS_WINDOWS_RT) || defined(GTEST_OS_WINDOWS_GAMES) || \
|
||||||
defined(GTEST_OS_XTENSA) || defined(GTEST_OS_QURT) || \
|
defined(GTEST_OS_ESP8266) || defined(GTEST_OS_XTENSA) || \
|
||||||
!GTEST_HAS_FILE_SYSTEM
|
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
|
||||||
@ -652,7 +655,7 @@ 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 (defined(GTEST_OS_LINUX) || defined(GTEST_OS_CYGWIN) || \
|
#if (defined(GTEST_OS_LINUX) || defined(GTEST_OS_CYGWIN) || \
|
||||||
defined(GTEST_OS_SOLARIS) || \
|
defined(GTEST_OS_SOLARIS) || defined(GTEST_OS_ZOS) || \
|
||||||
(defined(GTEST_OS_MAC) && !defined(GTEST_OS_IOS)) || \
|
(defined(GTEST_OS_MAC) && !defined(GTEST_OS_IOS)) || \
|
||||||
(defined(GTEST_OS_WINDOWS_DESKTOP) && _MSC_VER) || \
|
(defined(GTEST_OS_WINDOWS_DESKTOP) && _MSC_VER) || \
|
||||||
defined(GTEST_OS_WINDOWS_MINGW) || defined(GTEST_OS_AIX) || \
|
defined(GTEST_OS_WINDOWS_MINGW) || defined(GTEST_OS_AIX) || \
|
||||||
@ -730,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
|
||||||
@ -740,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
|
||||||
@ -765,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:
|
||||||
@ -827,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)
|
||||||
@ -1390,9 +1385,9 @@ class GTEST_API_ Mutex {
|
|||||||
Mutex();
|
Mutex();
|
||||||
~Mutex();
|
~Mutex();
|
||||||
|
|
||||||
void Lock();
|
void lock();
|
||||||
|
|
||||||
void Unlock();
|
void unlock();
|
||||||
|
|
||||||
// Does nothing if the current thread holds the mutex. Otherwise, crashes
|
// Does nothing if the current thread holds the mutex. Otherwise, crashes
|
||||||
// with high probability.
|
// with high probability.
|
||||||
@ -1429,12 +1424,11 @@ class GTEST_API_ Mutex {
|
|||||||
// "MutexLock l(&mu)". Hence the typedef trick below.
|
// "MutexLock l(&mu)". Hence the typedef trick below.
|
||||||
class GTestMutexLock {
|
class GTestMutexLock {
|
||||||
public:
|
public:
|
||||||
explicit GTestMutexLock(Mutex* mutex) : mutex_(mutex) { mutex_->Lock(); }
|
explicit GTestMutexLock(Mutex& mutex) : mutex_(mutex) { mutex_.lock(); }
|
||||||
|
~GTestMutexLock() { mutex_.unlock(); }
|
||||||
~GTestMutexLock() { mutex_->Unlock(); }
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Mutex* const mutex_;
|
Mutex& mutex_;
|
||||||
|
|
||||||
GTestMutexLock(const GTestMutexLock&) = delete;
|
GTestMutexLock(const GTestMutexLock&) = delete;
|
||||||
GTestMutexLock& operator=(const GTestMutexLock&) = delete;
|
GTestMutexLock& operator=(const GTestMutexLock&) = delete;
|
||||||
@ -1646,14 +1640,14 @@ class ThreadLocal : public ThreadLocalBase {
|
|||||||
class MutexBase {
|
class MutexBase {
|
||||||
public:
|
public:
|
||||||
// Acquires this mutex.
|
// Acquires this mutex.
|
||||||
void Lock() {
|
void lock() {
|
||||||
GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_lock(&mutex_));
|
GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_lock(&mutex_));
|
||||||
owner_ = pthread_self();
|
owner_ = pthread_self();
|
||||||
has_owner_ = true;
|
has_owner_ = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Releases this mutex.
|
// Releases this mutex.
|
||||||
void Unlock() {
|
void unlock() {
|
||||||
// Since the lock is being released the owner_ field should no longer be
|
// Since the lock is being released the owner_ field should no longer be
|
||||||
// considered valid. We don't protect writing to has_owner_ here, as it's
|
// considered valid. We don't protect writing to has_owner_ here, as it's
|
||||||
// the caller's responsibility to ensure that the current thread holds the
|
// the caller's responsibility to ensure that the current thread holds the
|
||||||
@ -1721,12 +1715,11 @@ class Mutex : public MutexBase {
|
|||||||
// "MutexLock l(&mu)". Hence the typedef trick below.
|
// "MutexLock l(&mu)". Hence the typedef trick below.
|
||||||
class GTestMutexLock {
|
class GTestMutexLock {
|
||||||
public:
|
public:
|
||||||
explicit GTestMutexLock(MutexBase* mutex) : mutex_(mutex) { mutex_->Lock(); }
|
explicit GTestMutexLock(MutexBase& mutex) : mutex_(mutex) { mutex_.lock(); }
|
||||||
|
~GTestMutexLock() { mutex_.unlock(); }
|
||||||
~GTestMutexLock() { mutex_->Unlock(); }
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
MutexBase* const mutex_;
|
MutexBase& mutex_;
|
||||||
|
|
||||||
GTestMutexLock(const GTestMutexLock&) = delete;
|
GTestMutexLock(const GTestMutexLock&) = delete;
|
||||||
GTestMutexLock& operator=(const GTestMutexLock&) = delete;
|
GTestMutexLock& operator=(const GTestMutexLock&) = delete;
|
||||||
@ -1869,8 +1862,8 @@ class GTEST_API_ ThreadLocal {
|
|||||||
class Mutex {
|
class Mutex {
|
||||||
public:
|
public:
|
||||||
Mutex() {}
|
Mutex() {}
|
||||||
void Lock() {}
|
void lock() {}
|
||||||
void Unlock() {}
|
void unlock() {}
|
||||||
void AssertHeld() const {}
|
void AssertHeld() const {}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -1886,7 +1879,7 @@ class Mutex {
|
|||||||
// "MutexLock l(&mu)". Hence the typedef trick below.
|
// "MutexLock l(&mu)". Hence the typedef trick below.
|
||||||
class GTestMutexLock {
|
class GTestMutexLock {
|
||||||
public:
|
public:
|
||||||
explicit GTestMutexLock(Mutex*) {} // NOLINT
|
explicit GTestMutexLock(Mutex&) {} // NOLINT
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef GTestMutexLock MutexLock;
|
typedef GTestMutexLock MutexLock;
|
||||||
@ -1987,7 +1980,9 @@ 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
|
||||||
|
// 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
|
#ifdef GTEST_OS_WINDOWS
|
||||||
|
|
||||||
typedef struct _stat StatStruct;
|
typedef struct _stat StatStruct;
|
||||||
@ -1998,27 +1993,32 @@ inline int FileNo(FILE* file) { return reinterpret_cast<int>(_fileno(file)); }
|
|||||||
// 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 defined(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); }
|
||||||
#ifdef 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
|
||||||
@ -2027,9 +2027,9 @@ inline int RmDir(const char*) { return 0; }
|
|||||||
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.
|
||||||
|
|
||||||
@ -2082,8 +2082,9 @@ GTEST_DISABLE_MSC_DEPRECATED_PUSH_()
|
|||||||
// defined there.
|
// defined there.
|
||||||
#if GTEST_HAS_FILE_SYSTEM
|
#if GTEST_HAS_FILE_SYSTEM
|
||||||
#if !defined(GTEST_OS_WINDOWS_MOBILE) && !defined(GTEST_OS_WINDOWS_PHONE) && \
|
#if !defined(GTEST_OS_WINDOWS_MOBILE) && !defined(GTEST_OS_WINDOWS_PHONE) && \
|
||||||
!defined(GTEST_OS_WINDOWS_RT) && !defined(GTEST_OS_ESP8266) && \
|
!defined(GTEST_OS_WINDOWS_RT) && !defined(GTEST_OS_WINDOWS_GAMES) && \
|
||||||
!defined(GTEST_OS_XTENSA) && !defined(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) {
|
||||||
@ -2227,7 +2228,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.
|
||||||
#ifdef 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) \
|
||||||
@ -2252,7 +2253,8 @@ using TimeInMillis = int64_t; // Represents time in milliseconds.
|
|||||||
(void)(::absl::SetFlag(>EST_FLAG(name), value))
|
(void)(::absl::SetFlag(>EST_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) \
|
||||||
@ -2294,7 +2296,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_)
|
||||||
@ -2318,93 +2320,20 @@ const char* StringFromGTestEnv(const char* flag, const char* default_val);
|
|||||||
} // namespace internal
|
} // namespace internal
|
||||||
} // namespace testing
|
} // namespace testing
|
||||||
|
|
||||||
#if !defined(GTEST_INTERNAL_DEPRECATED)
|
#if GTEST_INTERNAL_HAVE_CPP_ATTRIBUTE(clang::annotate)
|
||||||
|
#define GTEST_INTERNAL_DEPRECATE_AND_INLINE(msg) \
|
||||||
// Internal Macro to mark an API deprecated, for googletest usage only
|
[[deprecated(msg), clang::annotate("inline-me")]]
|
||||||
// Usage: class GTEST_INTERNAL_DEPRECATED(message) MyClass or
|
|
||||||
// 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
|
#else
|
||||||
#define GTEST_INTERNAL_DEPRECATED(message)
|
#define GTEST_INTERNAL_DEPRECATE_AND_INLINE(msg) [[deprecated(msg)]]
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#endif // !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
|
||||||
|
|
||||||
#ifdef GTEST_HAS_ABSL
|
#ifndef GTEST_INTERNAL_HAS_STD_SPAN
|
||||||
// Always use absl::any for UniversalPrinter<> specializations if googletest
|
#define GTEST_INTERNAL_HAS_STD_SPAN 0
|
||||||
// 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>) && GTEST_INTERNAL_CPLUSPLUS_LANG >= 201703L && \
|
|
||||||
(!defined(_MSC_VER) || GTEST_HAS_RTTI)
|
|
||||||
// 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>) && GTEST_INTERNAL_CPLUSPLUS_LANG >= 201703L
|
|
||||||
#endif // __has_include
|
|
||||||
#endif // GTEST_HAS_ABSL
|
|
||||||
|
|
||||||
#ifndef GTEST_INTERNAL_HAS_ANY
|
|
||||||
#define GTEST_INTERNAL_HAS_ANY 0
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef 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>) && GTEST_INTERNAL_CPLUSPLUS_LANG >= 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>) && GTEST_INTERNAL_CPLUSPLUS_LANG >= 201703L
|
|
||||||
#endif // __has_include
|
|
||||||
#endif // GTEST_HAS_ABSL
|
|
||||||
|
|
||||||
#ifndef GTEST_INTERNAL_HAS_OPTIONAL
|
|
||||||
#define GTEST_INTERNAL_HAS_OPTIONAL 0
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef GTEST_HAS_ABSL
|
#ifdef GTEST_HAS_ABSL
|
||||||
@ -2418,8 +2347,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>) && GTEST_INTERNAL_CPLUSPLUS_LANG >= 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
|
||||||
@ -2431,51 +2361,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>) && GTEST_INTERNAL_CPLUSPLUS_LANG >=
|
#endif // __cpp_lib_string_view
|
||||||
// 201703L
|
|
||||||
#endif // __has_include
|
|
||||||
#endif // GTEST_HAS_ABSL
|
#endif // GTEST_HAS_ABSL
|
||||||
|
|
||||||
#ifndef GTEST_INTERNAL_HAS_STRING_VIEW
|
#ifndef GTEST_INTERNAL_HAS_STRING_VIEW
|
||||||
#define GTEST_INTERNAL_HAS_STRING_VIEW 0
|
#define GTEST_INTERNAL_HAS_STRING_VIEW 0
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef GTEST_HAS_ABSL
|
#if (defined(__cpp_lib_three_way_comparison) || \
|
||||||
// Always use absl::variant for UniversalPrinter<> specializations if googletest
|
(GTEST_INTERNAL_HAS_INCLUDE(<compare>) && \
|
||||||
// is built with absl support.
|
GTEST_INTERNAL_CPLUSPLUS_LANG >= 201907L))
|
||||||
#define GTEST_INTERNAL_HAS_VARIANT 1
|
#define GTEST_INTERNAL_HAS_COMPARE_LIB 1
|
||||||
#include "absl/types/variant.h"
|
|
||||||
namespace testing {
|
|
||||||
namespace internal {
|
|
||||||
template <typename... T>
|
|
||||||
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>) && GTEST_INTERNAL_CPLUSPLUS_LANG >= 201703L
|
|
||||||
// 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>) && GTEST_INTERNAL_CPLUSPLUS_LANG >= 201703L
|
|
||||||
#endif // __has_include
|
|
||||||
#endif // GTEST_HAS_ABSL
|
|
||||||
|
|
||||||
#ifndef GTEST_INTERNAL_HAS_VARIANT
|
|
||||||
#define GTEST_INTERNAL_HAS_VARIANT 0
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(GTEST_INTERNAL_CPLUSPLUS_LANG) && \
|
|
||||||
GTEST_INTERNAL_CPLUSPLUS_LANG < 201703L
|
|
||||||
#define GTEST_INTERNAL_NEED_REDUNDANT_CONSTEXPR_DECL 1
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#endif // GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_
|
#endif // GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_
|
||||||
|
|||||||
@ -71,7 +71,7 @@ inline std::string CanonicalizeForStdLibVersioning(std::string s) {
|
|||||||
// Strip redundant spaces in typename to match MSVC
|
// Strip redundant spaces in typename to match MSVC
|
||||||
// For example, std::pair<int, bool> -> std::pair<int,bool>
|
// For example, std::pair<int, bool> -> std::pair<int,bool>
|
||||||
static const char to_search[] = ", ";
|
static const char to_search[] = ", ";
|
||||||
static const char replace_str[] = ",";
|
const char replace_char = ',';
|
||||||
size_t pos = 0;
|
size_t pos = 0;
|
||||||
while (true) {
|
while (true) {
|
||||||
// Get the next occurrence from the current position
|
// Get the next occurrence from the current position
|
||||||
@ -80,8 +80,8 @@ inline std::string CanonicalizeForStdLibVersioning(std::string s) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
// Replace this occurrence of substring
|
// Replace this occurrence of substring
|
||||||
s.replace(pos, strlen(to_search), replace_str);
|
s.replace(pos, strlen(to_search), 1, replace_char);
|
||||||
pos += strlen(replace_str);
|
++pos;
|
||||||
}
|
}
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -32,6 +32,8 @@
|
|||||||
|
|
||||||
#include "gtest/gtest-death-test.h"
|
#include "gtest/gtest-death-test.h"
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
@ -115,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 "
|
||||||
@ -299,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);
|
||||||
@ -511,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
|
||||||
@ -523,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.
|
||||||
@ -628,13 +630,13 @@ bool DeathTestImpl::Passed(bool status_ok) {
|
|||||||
#ifndef GTEST_OS_WINDOWS
|
#ifndef GTEST_OS_WINDOWS
|
||||||
// Note: The return value points into args, so the return value's lifetime is
|
// Note: The return value points into args, so the return value's lifetime is
|
||||||
// bound to that of args.
|
// bound to that of args.
|
||||||
static std::unique_ptr<char*[]> CreateArgvFromArgs(
|
static std::vector<char*> CreateArgvFromArgs(std::vector<std::string>& args) {
|
||||||
std::vector<std::string>& args) {
|
std::vector<char*> result;
|
||||||
auto result = std::make_unique<char*[]>(args.size() + 1);
|
result.reserve(args.size() + 1);
|
||||||
for (size_t i = 0; i < args.size(); ++i) {
|
for (auto& arg : args) {
|
||||||
result[i] = &args[i][0];
|
result.push_back(&arg[0]);
|
||||||
}
|
}
|
||||||
result[args.size()] = nullptr; // extra null terminator
|
result.push_back(nullptr); // Extra null terminator.
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
@ -783,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()));
|
||||||
|
|
||||||
@ -1034,8 +1036,8 @@ DeathTest::TestRole FuchsiaDeathTest::AssumeRole() {
|
|||||||
// "Fuchsia Test Component" which contains a "Fuchsia Component Manifest")
|
// "Fuchsia Test Component" which contains a "Fuchsia Component Manifest")
|
||||||
// Launching processes is a privileged operation in Fuchsia, and the
|
// Launching processes is a privileged operation in Fuchsia, and the
|
||||||
// declaration indicates that the ability is required for the component.
|
// declaration indicates that the ability is required for the component.
|
||||||
std::unique_ptr<char*[]> argv = CreateArgvFromArgs(args);
|
std::vector<char*> argv = CreateArgvFromArgs(args);
|
||||||
status = fdio_spawn_etc(child_job, FDIO_SPAWN_CLONE_ALL, argv[0], argv.get(),
|
status = fdio_spawn_etc(child_job, FDIO_SPAWN_CLONE_ALL, argv[0], argv.data(),
|
||||||
nullptr, 2, spawn_actions,
|
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);
|
||||||
@ -1333,7 +1335,7 @@ 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) {
|
||||||
_exit(ExecDeathTestChildMain(&args));
|
_Exit(ExecDeathTestChildMain(&args));
|
||||||
}
|
}
|
||||||
#endif // GTEST_OS_QNX
|
#endif // GTEST_OS_QNX
|
||||||
#ifdef GTEST_OS_LINUX
|
#ifdef GTEST_OS_LINUX
|
||||||
@ -1386,8 +1388,8 @@ DeathTest::TestRole ExecDeathTest::AssumeRole() {
|
|||||||
// is necessary.
|
// is necessary.
|
||||||
FlushInfoLog();
|
FlushInfoLog();
|
||||||
|
|
||||||
std::unique_ptr<char*[]> argv = CreateArgvFromArgs(args);
|
std::vector<char*> argv = CreateArgvFromArgs(args);
|
||||||
const pid_t child_pid = ExecDeathTestSpawnChild(argv.get(), pipe_fd[0]);
|
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]);
|
||||||
|
|||||||
@ -336,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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -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"
|
||||||
@ -312,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 =
|
||||||
@ -649,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);
|
||||||
@ -681,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_FILE_SYSTEM
|
#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);
|
||||||
}
|
}
|
||||||
@ -709,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
|
||||||
@ -835,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_;
|
||||||
|
|
||||||
@ -873,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
|
||||||
|
|||||||
@ -158,13 +158,13 @@ size_t GetThreadCount() {
|
|||||||
// we cannot detect it.
|
// we cannot detect it.
|
||||||
size_t GetThreadCount() {
|
size_t GetThreadCount() {
|
||||||
int mib[] = {
|
int mib[] = {
|
||||||
CTL_KERN,
|
CTL_KERN,
|
||||||
KERN_PROC,
|
KERN_PROC,
|
||||||
KERN_PROC_PID,
|
KERN_PROC_PID,
|
||||||
getpid(),
|
getpid(),
|
||||||
#ifdef GTEST_OS_NETBSD
|
#ifdef GTEST_OS_NETBSD
|
||||||
sizeof(struct kinfo_proc),
|
sizeof(struct kinfo_proc),
|
||||||
1,
|
1,
|
||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
u_int miblen = sizeof(mib) / sizeof(mib[0]);
|
u_int miblen = sizeof(mib) / sizeof(mib[0]);
|
||||||
@ -320,13 +320,13 @@ Mutex::~Mutex() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Mutex::Lock() {
|
void Mutex::lock() {
|
||||||
ThreadSafeLazyInit();
|
ThreadSafeLazyInit();
|
||||||
::EnterCriticalSection(critical_section_);
|
::EnterCriticalSection(critical_section_);
|
||||||
owner_thread_id_ = ::GetCurrentThreadId();
|
owner_thread_id_ = ::GetCurrentThreadId();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Mutex::Unlock() {
|
void Mutex::unlock() {
|
||||||
ThreadSafeLazyInit();
|
ThreadSafeLazyInit();
|
||||||
// We don't protect writing to owner_thread_id_ here, as it's the
|
// We don't protect writing to owner_thread_id_ here, as it's the
|
||||||
// caller's responsibility to ensure that the current thread holds the
|
// caller's responsibility to ensure that the current thread holds the
|
||||||
@ -499,7 +499,7 @@ class ThreadLocalRegistryImpl {
|
|||||||
MemoryIsNotDeallocated memory_is_not_deallocated;
|
MemoryIsNotDeallocated memory_is_not_deallocated;
|
||||||
#endif // _MSC_VER
|
#endif // _MSC_VER
|
||||||
DWORD current_thread = ::GetCurrentThreadId();
|
DWORD current_thread = ::GetCurrentThreadId();
|
||||||
MutexLock lock(&mutex_);
|
MutexLock lock(mutex_);
|
||||||
ThreadIdToThreadLocals* const thread_to_thread_locals =
|
ThreadIdToThreadLocals* const thread_to_thread_locals =
|
||||||
GetThreadLocalsMapLocked();
|
GetThreadLocalsMapLocked();
|
||||||
ThreadIdToThreadLocals::iterator thread_local_pos =
|
ThreadIdToThreadLocals::iterator thread_local_pos =
|
||||||
@ -532,7 +532,7 @@ class ThreadLocalRegistryImpl {
|
|||||||
// Clean up the ThreadLocalValues data structure while holding the lock, but
|
// Clean up the ThreadLocalValues data structure while holding the lock, but
|
||||||
// defer the destruction of the ThreadLocalValueHolderBases.
|
// defer the destruction of the ThreadLocalValueHolderBases.
|
||||||
{
|
{
|
||||||
MutexLock lock(&mutex_);
|
MutexLock lock(mutex_);
|
||||||
ThreadIdToThreadLocals* const thread_to_thread_locals =
|
ThreadIdToThreadLocals* const thread_to_thread_locals =
|
||||||
GetThreadLocalsMapLocked();
|
GetThreadLocalsMapLocked();
|
||||||
for (ThreadIdToThreadLocals::iterator it =
|
for (ThreadIdToThreadLocals::iterator it =
|
||||||
@ -559,7 +559,7 @@ class ThreadLocalRegistryImpl {
|
|||||||
// Clean up the ThreadIdToThreadLocals data structure while holding the
|
// Clean up the ThreadIdToThreadLocals data structure while holding the
|
||||||
// lock, but defer the destruction of the ThreadLocalValueHolderBases.
|
// lock, but defer the destruction of the ThreadLocalValueHolderBases.
|
||||||
{
|
{
|
||||||
MutexLock lock(&mutex_);
|
MutexLock lock(mutex_);
|
||||||
ThreadIdToThreadLocals* const thread_to_thread_locals =
|
ThreadIdToThreadLocals* const thread_to_thread_locals =
|
||||||
GetThreadLocalsMapLocked();
|
GetThreadLocalsMapLocked();
|
||||||
ThreadIdToThreadLocals::iterator thread_local_pos =
|
ThreadIdToThreadLocals::iterator thread_local_pos =
|
||||||
@ -587,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
|
||||||
@ -597,15 +599,20 @@ 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() << ".";
|
||||||
// Give the watcher thread the same priority as ours to avoid being
|
// Give the watcher thread the same priority as ours to avoid being
|
||||||
@ -614,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;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -697,13 +712,24 @@ bool RE::PartialMatch(const char* str, const RE& re) {
|
|||||||
void RE::Init(const char* regex) {
|
void RE::Init(const char* regex) {
|
||||||
pattern_ = 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.
|
||||||
const size_t full_regex_len = strlen(regex) + 10;
|
const size_t full_regex_len = strlen(regex) + 10;
|
||||||
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
|
||||||
@ -714,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
|
||||||
@ -1022,11 +1048,21 @@ 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 {
|
||||||
@ -1064,7 +1100,13 @@ 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/";
|
//
|
||||||
|
// 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)
|
#elif defined(GTEST_OS_IOS)
|
||||||
char user_temp_dir[PATH_MAX + 1];
|
char user_temp_dir[PATH_MAX + 1];
|
||||||
|
|
||||||
@ -1084,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/";
|
||||||
@ -1306,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;
|
||||||
|
|||||||
@ -50,7 +50,7 @@
|
|||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
#include <ios>
|
#include <ios>
|
||||||
#include <ostream> // NOLINT
|
#include <ostream> // NOLINT
|
||||||
#include <string>
|
#include <string_view>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
|
|
||||||
#include "gtest/internal/gtest-port.h"
|
#include "gtest/internal/gtest-port.h"
|
||||||
@ -333,14 +333,14 @@ void PrintTo(__int128_t v, ::std::ostream* os) {
|
|||||||
|
|
||||||
// Prints the given array of characters to the ostream. CharType must be either
|
// Prints the given array of characters to the ostream. CharType must be either
|
||||||
// char, char8_t, char16_t, char32_t, or wchar_t.
|
// char, char8_t, char16_t, char32_t, or wchar_t.
|
||||||
// The array starts at begin, the length is len, it may include '\0' characters
|
// The array starts at begin (which may be nullptr) and contains len characters.
|
||||||
// and may not be NUL-terminated.
|
// The array may include '\0' characters and may not be NUL-terminated.
|
||||||
template <typename CharType>
|
template <typename CharType>
|
||||||
GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_ GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_
|
GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_ GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_
|
||||||
GTEST_ATTRIBUTE_NO_SANITIZE_HWADDRESS_
|
GTEST_ATTRIBUTE_NO_SANITIZE_HWADDRESS_
|
||||||
GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_ static CharFormat
|
GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_ static CharFormat
|
||||||
PrintCharsAsStringTo(const CharType* begin, size_t len, ostream* os) {
|
PrintCharsAsStringTo(const CharType* begin, size_t len, ostream* os) {
|
||||||
const char* const quote_prefix = GetCharWidthPrefix(*begin);
|
const char* const quote_prefix = GetCharWidthPrefix(CharType());
|
||||||
*os << quote_prefix << "\"";
|
*os << quote_prefix << "\"";
|
||||||
bool is_previous_hex = false;
|
bool is_previous_hex = false;
|
||||||
CharFormat print_format = kAsIs;
|
CharFormat print_format = kAsIs;
|
||||||
@ -516,13 +516,13 @@ bool IsValidUTF8(const char* str, size_t length) {
|
|||||||
void ConditionalPrintAsText(const char* str, size_t length, ostream* os) {
|
void ConditionalPrintAsText(const char* str, size_t length, ostream* os) {
|
||||||
if (!ContainsUnprintableControlCodes(str, length) &&
|
if (!ContainsUnprintableControlCodes(str, length) &&
|
||||||
IsValidUTF8(str, length)) {
|
IsValidUTF8(str, length)) {
|
||||||
*os << "\n As Text: \"" << str << "\"";
|
*os << "\n As Text: \"" << ::std::string_view(str, length) << "\"";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} // anonymous namespace
|
} // anonymous namespace
|
||||||
|
|
||||||
void PrintStringTo(const ::std::string& s, ostream* os) {
|
void PrintStringTo(::std::string_view s, ostream* os) {
|
||||||
if (PrintCharsAsStringTo(s.data(), s.size(), os) == kHexEscape) {
|
if (PrintCharsAsStringTo(s.data(), s.size(), os) == kHexEscape) {
|
||||||
if (GTEST_FLAG_GET(print_utf8)) {
|
if (GTEST_FLAG_GET(print_utf8)) {
|
||||||
ConditionalPrintAsText(s.data(), s.size(), os);
|
ConditionalPrintAsText(s.data(), s.size(), os);
|
||||||
@ -531,21 +531,21 @@ void PrintStringTo(const ::std::string& s, ostream* os) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#ifdef __cpp_lib_char8_t
|
#ifdef __cpp_lib_char8_t
|
||||||
void PrintU8StringTo(const ::std::u8string& s, ostream* os) {
|
void PrintU8StringTo(::std::u8string_view s, ostream* os) {
|
||||||
PrintCharsAsStringTo(s.data(), s.size(), os);
|
PrintCharsAsStringTo(s.data(), s.size(), os);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void PrintU16StringTo(const ::std::u16string& s, ostream* os) {
|
void PrintU16StringTo(::std::u16string_view s, ostream* os) {
|
||||||
PrintCharsAsStringTo(s.data(), s.size(), os);
|
PrintCharsAsStringTo(s.data(), s.size(), os);
|
||||||
}
|
}
|
||||||
|
|
||||||
void PrintU32StringTo(const ::std::u32string& s, ostream* os) {
|
void PrintU32StringTo(::std::u32string_view s, ostream* os) {
|
||||||
PrintCharsAsStringTo(s.data(), s.size(), os);
|
PrintCharsAsStringTo(s.data(), s.size(), os);
|
||||||
}
|
}
|
||||||
|
|
||||||
#if GTEST_HAS_STD_WSTRING
|
#if GTEST_HAS_STD_WSTRING
|
||||||
void PrintWideStringTo(const ::std::wstring& s, ostream* os) {
|
void PrintWideStringTo(::std::wstring_view s, ostream* os) {
|
||||||
PrintCharsAsStringTo(s.data(), s.size(), os);
|
PrintCharsAsStringTo(s.data(), s.size(), os);
|
||||||
}
|
}
|
||||||
#endif // GTEST_HAS_STD_WSTRING
|
#endif // GTEST_HAS_STD_WSTRING
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@ -30,6 +30,7 @@
|
|||||||
#
|
#
|
||||||
# Bazel BUILD for The Google C++ Testing Framework (Google Test)
|
# Bazel BUILD for The Google C++ Testing Framework (Google Test)
|
||||||
|
|
||||||
|
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_test")
|
||||||
load("@rules_python//python:defs.bzl", "py_library", "py_test")
|
load("@rules_python//python:defs.bzl", "py_library", "py_test")
|
||||||
|
|
||||||
licenses(["notice"])
|
licenses(["notice"])
|
||||||
@ -45,36 +46,38 @@ cc_test(
|
|||||||
"gtest-*.cc",
|
"gtest-*.cc",
|
||||||
"googletest-*.cc",
|
"googletest-*.cc",
|
||||||
"*.h",
|
"*.h",
|
||||||
"googletest/include/gtest/**/*.h",
|
|
||||||
],
|
],
|
||||||
exclude = [
|
exclude = [
|
||||||
"gtest-unittest-api_test.cc",
|
# go/keep-sorted start
|
||||||
"googletest/src/gtest-all.cc",
|
"googletest-break-on-failure-unittest_.cc",
|
||||||
"gtest_all_test.cc",
|
|
||||||
"gtest-death-test_ex_test.cc",
|
|
||||||
"gtest-listener_test.cc",
|
|
||||||
"gtest-unittest-api_test.cc",
|
|
||||||
"googletest-param-test-test.cc",
|
|
||||||
"googletest-param-test2-test.cc",
|
|
||||||
"googletest-catch-exceptions-test_.cc",
|
"googletest-catch-exceptions-test_.cc",
|
||||||
"googletest-color-test_.cc",
|
"googletest-color-test_.cc",
|
||||||
|
"googletest-death-test_ex_test.cc",
|
||||||
"googletest-env-var-test_.cc",
|
"googletest-env-var-test_.cc",
|
||||||
|
"googletest-fail-if-no-test-linked-test-with-disabled-test_.cc",
|
||||||
|
"googletest-fail-if-no-test-linked-test-with-enabled-test_.cc",
|
||||||
"googletest-failfast-unittest_.cc",
|
"googletest-failfast-unittest_.cc",
|
||||||
"googletest-filter-unittest_.cc",
|
"googletest-filter-unittest_.cc",
|
||||||
"googletest-global-environment-unittest_.cc",
|
"googletest-global-environment-unittest_.cc",
|
||||||
"googletest-break-on-failure-unittest_.cc",
|
"googletest-list-tests-unittest_.cc",
|
||||||
"googletest-listener-test.cc",
|
"googletest-listener-test.cc",
|
||||||
"googletest-message-test.cc",
|
"googletest-message-test.cc",
|
||||||
"googletest-output-test_.cc",
|
"googletest-output-test_.cc",
|
||||||
"googletest-list-tests-unittest_.cc",
|
|
||||||
"googletest-shuffle-test_.cc",
|
|
||||||
"googletest-setuptestsuite-test_.cc",
|
|
||||||
"googletest-uninitialized-test_.cc",
|
|
||||||
"googletest-death-test_ex_test.cc",
|
|
||||||
"googletest-param-test-test",
|
|
||||||
"googletest-throw-on-failure-test_.cc",
|
|
||||||
"googletest-param-test-invalid-name1-test_.cc",
|
"googletest-param-test-invalid-name1-test_.cc",
|
||||||
"googletest-param-test-invalid-name2-test_.cc",
|
"googletest-param-test-invalid-name2-test_.cc",
|
||||||
|
"googletest-param-test-test",
|
||||||
|
"googletest-param-test-test.cc",
|
||||||
|
"googletest-param-test2-test.cc",
|
||||||
|
"googletest-setuptestsuite-test_.cc",
|
||||||
|
"googletest-shuffle-test_.cc",
|
||||||
|
"googletest-throw-on-failure-test_.cc",
|
||||||
|
"googletest-uninitialized-test_.cc",
|
||||||
|
"googletest/src/gtest-all.cc",
|
||||||
|
"gtest-death-test_ex_test.cc",
|
||||||
|
"gtest-listener_test.cc",
|
||||||
|
"gtest-unittest-api_test.cc",
|
||||||
|
"gtest_all_test.cc",
|
||||||
|
# go/keep-sorted end
|
||||||
],
|
],
|
||||||
) + select({
|
) + select({
|
||||||
"//:windows": [],
|
"//:windows": [],
|
||||||
@ -324,6 +327,26 @@ cc_binary(
|
|||||||
deps = ["//:gtest"],
|
deps = ["//:gtest"],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
cc_binary(
|
||||||
|
name = "googletest-fail-if-no-test-linked-test-without-test_",
|
||||||
|
testonly = 1,
|
||||||
|
deps = ["//:gtest_main"],
|
||||||
|
)
|
||||||
|
|
||||||
|
cc_binary(
|
||||||
|
name = "googletest-fail-if-no-test-linked-test-with-disabled-test_",
|
||||||
|
testonly = 1,
|
||||||
|
srcs = ["googletest-fail-if-no-test-linked-test-with-disabled-test_.cc"],
|
||||||
|
deps = ["//:gtest_main"],
|
||||||
|
)
|
||||||
|
|
||||||
|
cc_binary(
|
||||||
|
name = "googletest-fail-if-no-test-linked-test-with-enabled-test_",
|
||||||
|
testonly = 1,
|
||||||
|
srcs = ["googletest-fail-if-no-test-linked-test-with-enabled-test_.cc"],
|
||||||
|
deps = ["//:gtest_main"],
|
||||||
|
)
|
||||||
|
|
||||||
cc_test(
|
cc_test(
|
||||||
name = "gtest_skip_test",
|
name = "gtest_skip_test",
|
||||||
size = "small",
|
size = "small",
|
||||||
@ -364,6 +387,18 @@ py_test(
|
|||||||
deps = [":gtest_test_utils"],
|
deps = [":gtest_test_utils"],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
py_test(
|
||||||
|
name = "googletest-fail-if-no-test-linked-test",
|
||||||
|
size = "small",
|
||||||
|
srcs = ["googletest-fail-if-no-test-linked-test.py"],
|
||||||
|
data = [
|
||||||
|
":googletest-fail-if-no-test-linked-test-with-disabled-test_",
|
||||||
|
":googletest-fail-if-no-test-linked-test-with-enabled-test_",
|
||||||
|
":googletest-fail-if-no-test-linked-test-without-test_",
|
||||||
|
],
|
||||||
|
deps = [":gtest_test_utils"],
|
||||||
|
)
|
||||||
|
|
||||||
cc_binary(
|
cc_binary(
|
||||||
name = "googletest-shuffle-test_",
|
name = "googletest-shuffle-test_",
|
||||||
srcs = ["googletest-shuffle-test_.cc"],
|
srcs = ["googletest-shuffle-test_.cc"],
|
||||||
|
|||||||
@ -79,7 +79,9 @@ class GTestColorTest(gtest_test_utils.TestCase):
|
|||||||
self.assertTrue(UsesColor('cygwin', None, None))
|
self.assertTrue(UsesColor('cygwin', None, None))
|
||||||
self.assertTrue(UsesColor('xterm', None, None))
|
self.assertTrue(UsesColor('xterm', None, None))
|
||||||
self.assertTrue(UsesColor('xterm-color', None, None))
|
self.assertTrue(UsesColor('xterm-color', None, None))
|
||||||
|
self.assertTrue(UsesColor('xterm-ghostty', None, None))
|
||||||
self.assertTrue(UsesColor('xterm-kitty', None, None))
|
self.assertTrue(UsesColor('xterm-kitty', None, None))
|
||||||
|
self.assertTrue(UsesColor('alacritty', None, None))
|
||||||
self.assertTrue(UsesColor('xterm-256color', None, None))
|
self.assertTrue(UsesColor('xterm-256color', None, None))
|
||||||
|
|
||||||
def testFlagOnly(self):
|
def testFlagOnly(self):
|
||||||
|
|||||||
@ -30,6 +30,8 @@
|
|||||||
//
|
//
|
||||||
// Tests for death tests.
|
// Tests for death tests.
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
#include "gtest/gtest-death-test.h"
|
#include "gtest/gtest-death-test.h"
|
||||||
#include "gtest/gtest.h"
|
#include "gtest/gtest.h"
|
||||||
#include "gtest/internal/gtest-filepath.h"
|
#include "gtest/internal/gtest-filepath.h"
|
||||||
@ -111,15 +113,15 @@ void DieWithMessage(const ::std::string& message) {
|
|||||||
fprintf(stderr, "%s", message.c_str());
|
fprintf(stderr, "%s", message.c_str());
|
||||||
fflush(stderr); // Make sure the text is printed before the process exits.
|
fflush(stderr); // Make sure the text is printed before the process exits.
|
||||||
|
|
||||||
// We call _exit() instead of exit(), as the former is a direct
|
// We call _Exit() instead of exit(), as the former is a direct
|
||||||
// system call and thus safer in the presence of threads. exit()
|
// system call and thus safer in the presence of threads. exit()
|
||||||
// will invoke user-defined exit-hooks, which may do dangerous
|
// will invoke user-defined exit-hooks, which may do dangerous
|
||||||
// things that conflict with death tests.
|
// things that conflict with death tests.
|
||||||
//
|
//
|
||||||
// Some compilers can recognize that _exit() never returns and issue the
|
// Some compilers can recognize that _Exit() never returns and issue the
|
||||||
// 'unreachable code' warning for code following this function, unless
|
// 'unreachable code' warning for code following this function, unless
|
||||||
// fooled by a fake condition.
|
// fooled by a fake condition.
|
||||||
if (AlwaysTrue()) _exit(1);
|
if (AlwaysTrue()) _Exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
void DieInside(const ::std::string& function) {
|
void DieInside(const ::std::string& function) {
|
||||||
@ -238,13 +240,13 @@ TEST(ExitStatusPredicateTest, ExitedWithCode) {
|
|||||||
|
|
||||||
#else
|
#else
|
||||||
|
|
||||||
// Returns the exit status of a process that calls _exit(2) with a
|
// Returns the exit status of a process that calls _Exit(2) with a
|
||||||
// given exit code. This is a helper function for the
|
// given exit code. This is a helper function for the
|
||||||
// ExitStatusPredicateTest test suite.
|
// ExitStatusPredicateTest test suite.
|
||||||
static int NormalExitStatus(int exit_code) {
|
static int NormalExitStatus(int exit_code) {
|
||||||
pid_t child_pid = fork();
|
pid_t child_pid = fork();
|
||||||
if (child_pid == 0) {
|
if (child_pid == 0) {
|
||||||
_exit(exit_code);
|
_Exit(exit_code);
|
||||||
}
|
}
|
||||||
int status;
|
int status;
|
||||||
waitpid(child_pid, &status, 0);
|
waitpid(child_pid, &status, 0);
|
||||||
@ -260,7 +262,7 @@ static int KilledExitStatus(int signum) {
|
|||||||
pid_t child_pid = fork();
|
pid_t child_pid = fork();
|
||||||
if (child_pid == 0) {
|
if (child_pid == 0) {
|
||||||
raise(signum);
|
raise(signum);
|
||||||
_exit(1);
|
_Exit(1);
|
||||||
}
|
}
|
||||||
int status;
|
int status;
|
||||||
waitpid(child_pid, &status, 0);
|
waitpid(child_pid, &status, 0);
|
||||||
@ -289,7 +291,9 @@ TEST(ExitStatusPredicateTest, KilledBySignal) {
|
|||||||
const int status_kill = KilledExitStatus(SIGKILL);
|
const int status_kill = KilledExitStatus(SIGKILL);
|
||||||
const testing::KilledBySignal pred_segv(SIGSEGV);
|
const testing::KilledBySignal pred_segv(SIGSEGV);
|
||||||
const testing::KilledBySignal pred_kill(SIGKILL);
|
const testing::KilledBySignal pred_kill(SIGKILL);
|
||||||
|
#if !(defined(GTEST_OS_LINUX_ANDROID) && __ANDROID_API__ <= 23)
|
||||||
EXPECT_PRED1(pred_segv, status_segv);
|
EXPECT_PRED1(pred_segv, status_segv);
|
||||||
|
#endif
|
||||||
EXPECT_PRED1(pred_kill, status_kill);
|
EXPECT_PRED1(pred_kill, status_kill);
|
||||||
EXPECT_FALSE(pred_segv(status_kill));
|
EXPECT_FALSE(pred_segv(status_kill));
|
||||||
EXPECT_FALSE(pred_kill(status_segv));
|
EXPECT_FALSE(pred_kill(status_segv));
|
||||||
@ -313,7 +317,7 @@ TEST_F(TestForDeathTest, SingleStatement) {
|
|||||||
ASSERT_DEATH(return, "");
|
ASSERT_DEATH(return, "");
|
||||||
|
|
||||||
if (AlwaysTrue())
|
if (AlwaysTrue())
|
||||||
EXPECT_DEATH(_exit(1), "");
|
EXPECT_DEATH(_Exit(1), "");
|
||||||
else
|
else
|
||||||
// This empty "else" branch is meant to ensure that EXPECT_DEATH
|
// This empty "else" branch is meant to ensure that EXPECT_DEATH
|
||||||
// doesn't expand into an "if" statement without an "else"
|
// doesn't expand into an "if" statement without an "else"
|
||||||
@ -324,7 +328,7 @@ TEST_F(TestForDeathTest, SingleStatement) {
|
|||||||
if (AlwaysFalse())
|
if (AlwaysFalse())
|
||||||
;
|
;
|
||||||
else
|
else
|
||||||
EXPECT_DEATH(_exit(1), "") << 1 << 2 << 3;
|
EXPECT_DEATH(_Exit(1), "") << 1 << 2 << 3;
|
||||||
}
|
}
|
||||||
#ifdef __GNUC__
|
#ifdef __GNUC__
|
||||||
#pragma GCC diagnostic pop
|
#pragma GCC diagnostic pop
|
||||||
@ -339,11 +343,11 @@ TEST_F(TestForDeathTest, SwitchStatement) {
|
|||||||
|
|
||||||
switch (0)
|
switch (0)
|
||||||
default:
|
default:
|
||||||
ASSERT_DEATH(_exit(1), "") << "exit in default switch handler";
|
ASSERT_DEATH(_Exit(1), "") << "exit in default switch handler";
|
||||||
|
|
||||||
switch (0)
|
switch (0)
|
||||||
case 0:
|
case 0:
|
||||||
EXPECT_DEATH(_exit(1), "") << "exit in switch case";
|
EXPECT_DEATH(_Exit(1), "") << "exit in switch case";
|
||||||
|
|
||||||
GTEST_DISABLE_MSC_WARNINGS_POP_()
|
GTEST_DISABLE_MSC_WARNINGS_POP_()
|
||||||
}
|
}
|
||||||
@ -371,10 +375,10 @@ TEST_F(TestForDeathTest, FastDeathTestInChangedDir) {
|
|||||||
GTEST_FLAG_SET(death_test_style, "fast");
|
GTEST_FLAG_SET(death_test_style, "fast");
|
||||||
|
|
||||||
ChangeToRootDir();
|
ChangeToRootDir();
|
||||||
EXPECT_EXIT(_exit(1), testing::ExitedWithCode(1), "");
|
EXPECT_EXIT(_Exit(1), testing::ExitedWithCode(1), "");
|
||||||
|
|
||||||
ChangeToRootDir();
|
ChangeToRootDir();
|
||||||
ASSERT_DEATH(_exit(1), "");
|
ASSERT_DEATH(_Exit(1), "");
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef GTEST_OS_LINUX
|
#ifdef GTEST_OS_LINUX
|
||||||
@ -416,7 +420,7 @@ void DisableSigprofActionAndTimer(struct sigaction* old_signal_action) {
|
|||||||
TEST_F(TestForDeathTest, FastSigprofActionSet) {
|
TEST_F(TestForDeathTest, FastSigprofActionSet) {
|
||||||
GTEST_FLAG_SET(death_test_style, "fast");
|
GTEST_FLAG_SET(death_test_style, "fast");
|
||||||
SetSigprofActionAndTimer();
|
SetSigprofActionAndTimer();
|
||||||
EXPECT_DEATH(_exit(1), "");
|
EXPECT_DEATH(_Exit(1), "");
|
||||||
struct sigaction old_signal_action;
|
struct sigaction old_signal_action;
|
||||||
DisableSigprofActionAndTimer(&old_signal_action);
|
DisableSigprofActionAndTimer(&old_signal_action);
|
||||||
EXPECT_TRUE(old_signal_action.sa_sigaction == SigprofAction);
|
EXPECT_TRUE(old_signal_action.sa_sigaction == SigprofAction);
|
||||||
@ -425,7 +429,7 @@ TEST_F(TestForDeathTest, FastSigprofActionSet) {
|
|||||||
TEST_F(TestForDeathTest, ThreadSafeSigprofActionSet) {
|
TEST_F(TestForDeathTest, ThreadSafeSigprofActionSet) {
|
||||||
GTEST_FLAG_SET(death_test_style, "threadsafe");
|
GTEST_FLAG_SET(death_test_style, "threadsafe");
|
||||||
SetSigprofActionAndTimer();
|
SetSigprofActionAndTimer();
|
||||||
EXPECT_DEATH(_exit(1), "");
|
EXPECT_DEATH(_Exit(1), "");
|
||||||
struct sigaction old_signal_action;
|
struct sigaction old_signal_action;
|
||||||
DisableSigprofActionAndTimer(&old_signal_action);
|
DisableSigprofActionAndTimer(&old_signal_action);
|
||||||
EXPECT_TRUE(old_signal_action.sa_sigaction == SigprofAction);
|
EXPECT_TRUE(old_signal_action.sa_sigaction == SigprofAction);
|
||||||
@ -449,24 +453,24 @@ TEST_F(TestForDeathTest, ThreadsafeDeathTestInLoop) {
|
|||||||
GTEST_FLAG_SET(death_test_style, "threadsafe");
|
GTEST_FLAG_SET(death_test_style, "threadsafe");
|
||||||
|
|
||||||
for (int i = 0; i < 3; ++i)
|
for (int i = 0; i < 3; ++i)
|
||||||
EXPECT_EXIT(_exit(i), testing::ExitedWithCode(i), "") << ": i = " << i;
|
EXPECT_EXIT(_Exit(i), testing::ExitedWithCode(i), "") << ": i = " << i;
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(TestForDeathTest, ThreadsafeDeathTestInChangedDir) {
|
TEST_F(TestForDeathTest, ThreadsafeDeathTestInChangedDir) {
|
||||||
GTEST_FLAG_SET(death_test_style, "threadsafe");
|
GTEST_FLAG_SET(death_test_style, "threadsafe");
|
||||||
|
|
||||||
ChangeToRootDir();
|
ChangeToRootDir();
|
||||||
EXPECT_EXIT(_exit(1), testing::ExitedWithCode(1), "");
|
EXPECT_EXIT(_Exit(1), testing::ExitedWithCode(1), "");
|
||||||
|
|
||||||
ChangeToRootDir();
|
ChangeToRootDir();
|
||||||
ASSERT_DEATH(_exit(1), "");
|
ASSERT_DEATH(_Exit(1), "");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(TestForDeathTest, MixedStyles) {
|
TEST_F(TestForDeathTest, MixedStyles) {
|
||||||
GTEST_FLAG_SET(death_test_style, "threadsafe");
|
GTEST_FLAG_SET(death_test_style, "threadsafe");
|
||||||
EXPECT_DEATH(_exit(1), "");
|
EXPECT_DEATH(_Exit(1), "");
|
||||||
GTEST_FLAG_SET(death_test_style, "fast");
|
GTEST_FLAG_SET(death_test_style, "fast");
|
||||||
EXPECT_DEATH(_exit(1), "");
|
EXPECT_DEATH(_Exit(1), "");
|
||||||
}
|
}
|
||||||
|
|
||||||
#if GTEST_HAS_CLONE && GTEST_HAS_PTHREAD
|
#if GTEST_HAS_CLONE && GTEST_HAS_PTHREAD
|
||||||
@ -480,7 +484,7 @@ TEST_F(TestForDeathTest, DoesNotExecuteAtforkHooks) {
|
|||||||
GTEST_FLAG_SET(death_test_style, "threadsafe");
|
GTEST_FLAG_SET(death_test_style, "threadsafe");
|
||||||
pthread_flag = false;
|
pthread_flag = false;
|
||||||
ASSERT_EQ(0, pthread_atfork(&SetPthreadFlag, nullptr, nullptr));
|
ASSERT_EQ(0, pthread_atfork(&SetPthreadFlag, nullptr, nullptr));
|
||||||
ASSERT_DEATH(_exit(1), "");
|
ASSERT_DEATH(_Exit(1), "");
|
||||||
ASSERT_FALSE(pthread_flag);
|
ASSERT_FALSE(pthread_flag);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -805,8 +809,8 @@ TEST_F(TestForDeathTest, AssertDebugDeathAborts10) {
|
|||||||
|
|
||||||
// Tests the *_EXIT family of macros, using a variety of predicates.
|
// Tests the *_EXIT family of macros, using a variety of predicates.
|
||||||
static void TestExitMacros() {
|
static void TestExitMacros() {
|
||||||
EXPECT_EXIT(_exit(1), testing::ExitedWithCode(1), "");
|
EXPECT_EXIT(_Exit(1), testing::ExitedWithCode(1), "");
|
||||||
ASSERT_EXIT(_exit(42), testing::ExitedWithCode(42), "");
|
ASSERT_EXIT(_Exit(42), testing::ExitedWithCode(42), "");
|
||||||
|
|
||||||
#ifdef GTEST_OS_WINDOWS
|
#ifdef GTEST_OS_WINDOWS
|
||||||
|
|
||||||
@ -823,7 +827,7 @@ static void TestExitMacros() {
|
|||||||
|
|
||||||
EXPECT_FATAL_FAILURE(
|
EXPECT_FATAL_FAILURE(
|
||||||
{ // NOLINT
|
{ // NOLINT
|
||||||
ASSERT_EXIT(_exit(0), testing::KilledBySignal(SIGSEGV), "")
|
ASSERT_EXIT(_Exit(0), testing::KilledBySignal(SIGSEGV), "")
|
||||||
<< "This failure is expected, too.";
|
<< "This failure is expected, too.";
|
||||||
},
|
},
|
||||||
"This failure is expected, too.");
|
"This failure is expected, too.");
|
||||||
@ -849,7 +853,7 @@ TEST_F(TestForDeathTest, InvalidStyle) {
|
|||||||
GTEST_FLAG_SET(death_test_style, "rococo");
|
GTEST_FLAG_SET(death_test_style, "rococo");
|
||||||
EXPECT_NONFATAL_FAILURE(
|
EXPECT_NONFATAL_FAILURE(
|
||||||
{ // NOLINT
|
{ // NOLINT
|
||||||
EXPECT_DEATH(_exit(0), "") << "This failure is expected.";
|
EXPECT_DEATH(_Exit(0), "") << "This failure is expected.";
|
||||||
},
|
},
|
||||||
"This failure is expected.");
|
"This failure is expected.");
|
||||||
}
|
}
|
||||||
@ -1140,7 +1144,7 @@ TEST_F(MacroLogicDeathTest, ChildDoesNotDie) {
|
|||||||
// This time there are two calls to Abort: one since the test didn't
|
// This time there are two calls to Abort: one since the test didn't
|
||||||
// die, and another from the ReturnSentinel when it's destroyed. The
|
// die, and another from the ReturnSentinel when it's destroyed. The
|
||||||
// sentinel normally isn't destroyed if a test doesn't die, since
|
// sentinel normally isn't destroyed if a test doesn't die, since
|
||||||
// _exit(2) is called in that case by ForkingDeathTest, but not by
|
// _Exit(2) is called in that case by ForkingDeathTest, but not by
|
||||||
// our MockDeathTest.
|
// our MockDeathTest.
|
||||||
ASSERT_EQ(2U, factory_->AbortCalls());
|
ASSERT_EQ(2U, factory_->AbortCalls());
|
||||||
EXPECT_EQ(DeathTest::TEST_DID_NOT_DIE, factory_->AbortArgument(0));
|
EXPECT_EQ(DeathTest::TEST_DID_NOT_DIE, factory_->AbortArgument(0));
|
||||||
@ -1152,21 +1156,21 @@ TEST_F(MacroLogicDeathTest, ChildDoesNotDie) {
|
|||||||
// Tests that a successful death test does not register a successful
|
// Tests that a successful death test does not register a successful
|
||||||
// test part.
|
// test part.
|
||||||
TEST(SuccessRegistrationDeathTest, NoSuccessPart) {
|
TEST(SuccessRegistrationDeathTest, NoSuccessPart) {
|
||||||
EXPECT_DEATH(_exit(1), "");
|
EXPECT_DEATH(_Exit(1), "");
|
||||||
EXPECT_EQ(0, GetUnitTestImpl()->current_test_result()->total_part_count());
|
EXPECT_EQ(0, GetUnitTestImpl()->current_test_result()->total_part_count());
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(StreamingAssertionsDeathTest, DeathTest) {
|
TEST(StreamingAssertionsDeathTest, DeathTest) {
|
||||||
EXPECT_DEATH(_exit(1), "") << "unexpected failure";
|
EXPECT_DEATH(_Exit(1), "") << "unexpected failure";
|
||||||
ASSERT_DEATH(_exit(1), "") << "unexpected failure";
|
ASSERT_DEATH(_Exit(1), "") << "unexpected failure";
|
||||||
EXPECT_NONFATAL_FAILURE(
|
EXPECT_NONFATAL_FAILURE(
|
||||||
{ // NOLINT
|
{ // NOLINT
|
||||||
EXPECT_DEATH(_exit(0), "") << "expected failure";
|
EXPECT_DEATH(_Exit(0), "") << "expected failure";
|
||||||
},
|
},
|
||||||
"expected failure");
|
"expected failure");
|
||||||
EXPECT_FATAL_FAILURE(
|
EXPECT_FATAL_FAILURE(
|
||||||
{ // NOLINT
|
{ // NOLINT
|
||||||
ASSERT_DEATH(_exit(0), "") << "expected failure";
|
ASSERT_DEATH(_Exit(0), "") << "expected failure";
|
||||||
},
|
},
|
||||||
"expected failure");
|
"expected failure");
|
||||||
}
|
}
|
||||||
@ -1330,7 +1334,7 @@ TEST(InDeathTestChildDeathTest, ReportsDeathTestCorrectlyInFastStyle) {
|
|||||||
{
|
{
|
||||||
fprintf(stderr, InDeathTestChild() ? "Inside" : "Outside");
|
fprintf(stderr, InDeathTestChild() ? "Inside" : "Outside");
|
||||||
fflush(stderr);
|
fflush(stderr);
|
||||||
_exit(1);
|
_Exit(1);
|
||||||
},
|
},
|
||||||
"Inside");
|
"Inside");
|
||||||
}
|
}
|
||||||
@ -1342,7 +1346,7 @@ TEST(InDeathTestChildDeathTest, ReportsDeathTestCorrectlyInThreadSafeStyle) {
|
|||||||
{
|
{
|
||||||
fprintf(stderr, InDeathTestChild() ? "Inside" : "Outside");
|
fprintf(stderr, InDeathTestChild() ? "Inside" : "Outside");
|
||||||
fflush(stderr);
|
fflush(stderr);
|
||||||
_exit(1);
|
_Exit(1);
|
||||||
},
|
},
|
||||||
"Inside");
|
"Inside");
|
||||||
}
|
}
|
||||||
@ -1350,7 +1354,7 @@ TEST(InDeathTestChildDeathTest, ReportsDeathTestCorrectlyInThreadSafeStyle) {
|
|||||||
void DieWithMessage(const char* message) {
|
void DieWithMessage(const char* message) {
|
||||||
fputs(message, stderr);
|
fputs(message, stderr);
|
||||||
fflush(stderr); // Make sure the text is printed before the process exits.
|
fflush(stderr); // Make sure the text is printed before the process exits.
|
||||||
_exit(1);
|
_Exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(MatcherDeathTest, DoesNotBreakBareRegexMatching) {
|
TEST(MatcherDeathTest, DoesNotBreakBareRegexMatching) {
|
||||||
@ -1466,7 +1470,7 @@ TEST(ConditionalDeathMacrosSyntaxDeathTest, SingleStatement) {
|
|||||||
ASSERT_DEATH_IF_SUPPORTED(return, "");
|
ASSERT_DEATH_IF_SUPPORTED(return, "");
|
||||||
|
|
||||||
if (AlwaysTrue())
|
if (AlwaysTrue())
|
||||||
EXPECT_DEATH_IF_SUPPORTED(_exit(1), "");
|
EXPECT_DEATH_IF_SUPPORTED(_Exit(1), "");
|
||||||
else
|
else
|
||||||
// This empty "else" branch is meant to ensure that EXPECT_DEATH
|
// This empty "else" branch is meant to ensure that EXPECT_DEATH
|
||||||
// doesn't expand into an "if" statement without an "else"
|
// doesn't expand into an "if" statement without an "else"
|
||||||
@ -1477,7 +1481,7 @@ TEST(ConditionalDeathMacrosSyntaxDeathTest, SingleStatement) {
|
|||||||
if (AlwaysFalse())
|
if (AlwaysFalse())
|
||||||
; // NOLINT
|
; // NOLINT
|
||||||
else
|
else
|
||||||
EXPECT_DEATH_IF_SUPPORTED(_exit(1), "") << 1 << 2 << 3;
|
EXPECT_DEATH_IF_SUPPORTED(_Exit(1), "") << 1 << 2 << 3;
|
||||||
}
|
}
|
||||||
#ifdef __GNUC__
|
#ifdef __GNUC__
|
||||||
#pragma GCC diagnostic pop
|
#pragma GCC diagnostic pop
|
||||||
@ -1492,11 +1496,11 @@ TEST(ConditionalDeathMacrosSyntaxDeathTest, SwitchStatement) {
|
|||||||
|
|
||||||
switch (0)
|
switch (0)
|
||||||
default:
|
default:
|
||||||
ASSERT_DEATH_IF_SUPPORTED(_exit(1), "") << "exit in default switch handler";
|
ASSERT_DEATH_IF_SUPPORTED(_Exit(1), "") << "exit in default switch handler";
|
||||||
|
|
||||||
switch (0)
|
switch (0)
|
||||||
case 0:
|
case 0:
|
||||||
EXPECT_DEATH_IF_SUPPORTED(_exit(1), "") << "exit in switch case";
|
EXPECT_DEATH_IF_SUPPORTED(_Exit(1), "") << "exit in switch case";
|
||||||
|
|
||||||
GTEST_DISABLE_MSC_WARNINGS_POP_()
|
GTEST_DISABLE_MSC_WARNINGS_POP_()
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,38 @@
|
|||||||
|
// Copyright 2025, 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.
|
||||||
|
|
||||||
|
// Unit test for Google Test's --gtest_fail_if_no_test_linked flag.
|
||||||
|
//
|
||||||
|
// This program will be invoked from a Python test.
|
||||||
|
// Don't run it directly.
|
||||||
|
|
||||||
|
#include "gtest/gtest.h"
|
||||||
|
|
||||||
|
// A dummy test that is disabled.
|
||||||
|
TEST(SomeTest, DISABLED_Test1) {}
|
||||||
@ -0,0 +1,38 @@
|
|||||||
|
// Copyright 2025, 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.
|
||||||
|
|
||||||
|
// Unit test for Google Test's --gtest_fail_if_no_test_linked flag.
|
||||||
|
//
|
||||||
|
// This program will be invoked from a Python test.
|
||||||
|
// Don't run it directly.
|
||||||
|
|
||||||
|
#include "gtest/gtest.h"
|
||||||
|
|
||||||
|
// A dummy test that is enabled.
|
||||||
|
TEST(SomeTest, Test1) {}
|
||||||
165
googletest/test/googletest-fail-if-no-test-linked-test.py
Executable file
165
googletest/test/googletest-fail-if-no-test-linked-test.py
Executable file
@ -0,0 +1,165 @@
|
|||||||
|
#!/usr/bin/env python3 # pylint: disable=g-interpreter-mismatch
|
||||||
|
#
|
||||||
|
# Copyright 2025, 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.
|
||||||
|
|
||||||
|
"""Tests for Google Test's --gtest_fail_if_no_test_linked flag."""
|
||||||
|
|
||||||
|
import os
|
||||||
|
from googletest.test import gtest_test_utils
|
||||||
|
|
||||||
|
# The command line flag for enabling the fail-if-no-test-linked behavior.
|
||||||
|
FAIL_IF_NO_TEST_LINKED_FLAG = "gtest_fail_if_no_test_linked"
|
||||||
|
|
||||||
|
# The environment variable for the test output warnings file.
|
||||||
|
TEST_WARNINGS_OUTPUT_FILE = "TEST_WARNINGS_OUTPUT_FILE"
|
||||||
|
|
||||||
|
|
||||||
|
class GTestFailIfNoTestLinkedTest(gtest_test_utils.TestCase):
|
||||||
|
"""Tests the --gtest_fail_if_no_test_linked flag."""
|
||||||
|
|
||||||
|
def Run(self, program_name, flag=None, env=None):
|
||||||
|
"""Run the given program with the given flag.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
program_name: Name of the program to run.
|
||||||
|
flag: The command line flag to pass to the program, or None.
|
||||||
|
env: Dictionary with environment to pass to the subprocess.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
True if the program exits with code 0, false otherwise.
|
||||||
|
"""
|
||||||
|
|
||||||
|
exe_path = gtest_test_utils.GetTestExecutablePath(program_name)
|
||||||
|
args = [exe_path]
|
||||||
|
if flag is not None:
|
||||||
|
args += [flag]
|
||||||
|
process = gtest_test_utils.Subprocess(args, capture_stderr=False, env=env)
|
||||||
|
return process.exited and process.exit_code == 0
|
||||||
|
|
||||||
|
def testSucceedsIfNoTestLinkedAndFlagNotSpecified(self):
|
||||||
|
"""Tests the behavior of no test linked and flag not specified."""
|
||||||
|
self.assertTrue(
|
||||||
|
self.Run("googletest-fail-if-no-test-linked-test-without-test_")
|
||||||
|
)
|
||||||
|
|
||||||
|
def testSucceedsIfNoTestLinkedAndFlagNotSpecifiedWithWarningFile(self):
|
||||||
|
"""Tests that no test linked results in warning file output."""
|
||||||
|
|
||||||
|
warning_file = os.path.join(gtest_test_utils.GetTempDir(), "NO_TEST_LINKED")
|
||||||
|
self.assertTrue(
|
||||||
|
self.Run(
|
||||||
|
"googletest-fail-if-no-test-linked-test-without-test_",
|
||||||
|
env={TEST_WARNINGS_OUTPUT_FILE: warning_file},
|
||||||
|
)
|
||||||
|
)
|
||||||
|
warning_file_contents = open(warning_file, "r").read()
|
||||||
|
self.assertIn("does NOT link", warning_file_contents)
|
||||||
|
|
||||||
|
def testFailsIfNoTestLinkedAndFlagSpecified(self):
|
||||||
|
"""Tests the behavior of no test linked and flag specified."""
|
||||||
|
|
||||||
|
warning_file = os.path.join(
|
||||||
|
gtest_test_utils.GetTempDir(), "SHOULD_NOT_EXIST"
|
||||||
|
)
|
||||||
|
self.assertFalse(
|
||||||
|
self.Run(
|
||||||
|
"googletest-fail-if-no-test-linked-test-without-test_",
|
||||||
|
f"--{FAIL_IF_NO_TEST_LINKED_FLAG}",
|
||||||
|
env={TEST_WARNINGS_OUTPUT_FILE: warning_file},
|
||||||
|
)
|
||||||
|
)
|
||||||
|
with self.assertRaises(FileNotFoundError):
|
||||||
|
open(warning_file, "r")
|
||||||
|
|
||||||
|
def testSucceedsIfEnabledTestLinkedAndFlagNotSpecified(self):
|
||||||
|
"""Tests the behavior of enabled test linked and flag not specified."""
|
||||||
|
|
||||||
|
warning_file = os.path.join(
|
||||||
|
gtest_test_utils.GetTempDir(), "SHOULD_NOT_EXIST"
|
||||||
|
)
|
||||||
|
self.assertTrue(
|
||||||
|
self.Run(
|
||||||
|
"googletest-fail-if-no-test-linked-test-with-enabled-test_",
|
||||||
|
env={TEST_WARNINGS_OUTPUT_FILE: warning_file},
|
||||||
|
)
|
||||||
|
)
|
||||||
|
with self.assertRaises(FileNotFoundError):
|
||||||
|
open(warning_file, "r")
|
||||||
|
|
||||||
|
def testSucceedsIfEnabledTestLinkedAndFlagSpecified(self):
|
||||||
|
"""Tests the behavior of enabled test linked and flag specified."""
|
||||||
|
|
||||||
|
warning_file = os.path.join(
|
||||||
|
gtest_test_utils.GetTempDir(), "SHOULD_NOT_EXIST"
|
||||||
|
)
|
||||||
|
self.assertTrue(
|
||||||
|
self.Run(
|
||||||
|
"googletest-fail-if-no-test-linked-test-with-enabled-test_",
|
||||||
|
f"--{FAIL_IF_NO_TEST_LINKED_FLAG}",
|
||||||
|
env={TEST_WARNINGS_OUTPUT_FILE: warning_file},
|
||||||
|
)
|
||||||
|
)
|
||||||
|
with self.assertRaises(FileNotFoundError):
|
||||||
|
open(warning_file, "r")
|
||||||
|
|
||||||
|
def testSucceedsIfDisabledTestLinkedAndFlagNotSpecified(self):
|
||||||
|
"""Tests the behavior of disabled test linked and flag not specified."""
|
||||||
|
|
||||||
|
warning_file = os.path.join(
|
||||||
|
gtest_test_utils.GetTempDir(), "SHOULD_NOT_EXIST"
|
||||||
|
)
|
||||||
|
self.assertTrue(
|
||||||
|
self.Run(
|
||||||
|
"googletest-fail-if-no-test-linked-test-with-disabled-test_",
|
||||||
|
env={TEST_WARNINGS_OUTPUT_FILE: warning_file},
|
||||||
|
)
|
||||||
|
)
|
||||||
|
with self.assertRaises(FileNotFoundError):
|
||||||
|
open(warning_file, "r")
|
||||||
|
|
||||||
|
def testSucceedsIfDisabledTestLinkedAndFlagSpecified(self):
|
||||||
|
"""Tests the behavior of disabled test linked and flag specified."""
|
||||||
|
|
||||||
|
warning_file = os.path.join(
|
||||||
|
gtest_test_utils.GetTempDir(), "SHOULD_NOT_EXIST"
|
||||||
|
)
|
||||||
|
self.assertTrue(
|
||||||
|
self.Run(
|
||||||
|
"googletest-fail-if-no-test-linked-test-with-disabled-test_",
|
||||||
|
f"--{FAIL_IF_NO_TEST_LINKED_FLAG}",
|
||||||
|
env={TEST_WARNINGS_OUTPUT_FILE: warning_file},
|
||||||
|
)
|
||||||
|
)
|
||||||
|
with self.assertRaises(FileNotFoundError):
|
||||||
|
open(warning_file, "r")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
gtest_test_utils.Main()
|
||||||
91
googletest/test/googletest-fail-if-no-test-selected-test.py
Normal file
91
googletest/test/googletest-fail-if-no-test-selected-test.py
Normal file
@ -0,0 +1,91 @@
|
|||||||
|
#!/usr/bin/env python3 # pylint: disable=g-interpreter-mismatch
|
||||||
|
#
|
||||||
|
# Copyright 2025, 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.
|
||||||
|
"""Tests for Google Test's --gtest_fail_if_no_test_selected flag."""
|
||||||
|
|
||||||
|
from googletest.test import gtest_test_utils
|
||||||
|
|
||||||
|
|
||||||
|
# The command line flag for enabling the fail-if-no-test-selected behavior.
|
||||||
|
FAIL_IF_NO_TEST_SELECTED_FLAG = "gtest_fail_if_no_test_selected"
|
||||||
|
|
||||||
|
# The environment variable for the test output warnings file.
|
||||||
|
TEST_WARNINGS_OUTPUT_FILE = "TEST_WARNINGS_OUTPUT_FILE"
|
||||||
|
|
||||||
|
|
||||||
|
class GTestFailIfNoTestSelectedTest(gtest_test_utils.TestCase):
|
||||||
|
"""Tests the --gtest_fail_if_no_test_selected flag."""
|
||||||
|
|
||||||
|
def Run(self, program_name, flags=None, env=None):
|
||||||
|
"""Run the given program with the given flag.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
program_name: Name of the program to run.
|
||||||
|
flags: The command line flags to pass to the program, or None.
|
||||||
|
env: Dictionary with environment to pass to the subprocess.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
True if the program exits with code 0, false otherwise.
|
||||||
|
"""
|
||||||
|
|
||||||
|
exe_path = gtest_test_utils.GetTestExecutablePath(program_name)
|
||||||
|
args = [exe_path]
|
||||||
|
if flags is not None:
|
||||||
|
args.extend(flags)
|
||||||
|
process = gtest_test_utils.Subprocess(args, capture_stderr=False, env=env)
|
||||||
|
return process.exited and process.exit_code == 0
|
||||||
|
|
||||||
|
def testSucceedsWhenFlagIsNotSetAndOnlyDisabledTestsPresent(self):
|
||||||
|
"""Tests that no test selected results in success without the flag set."""
|
||||||
|
self.assertTrue(
|
||||||
|
self.Run("googletest-fail-if-no-test-linked-test-with-disabled-test_"),
|
||||||
|
)
|
||||||
|
|
||||||
|
def testFailsWhenFlagIsSetAndOnlyDisabledTestsPresent(self):
|
||||||
|
"""Tests that no test selected results in failure with the flag set."""
|
||||||
|
self.assertFalse(
|
||||||
|
self.Run(
|
||||||
|
"googletest-fail-if-no-test-linked-test-with-disabled-test_",
|
||||||
|
flags=[f"--{FAIL_IF_NO_TEST_SELECTED_FLAG}"],
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
def testSucceedsWhenFlagIsSetAndEnabledTestsPresent(self):
|
||||||
|
"""Tests that a test running still succeeds when the flag is set."""
|
||||||
|
self.assertTrue(
|
||||||
|
self.Run(
|
||||||
|
"googletest-fail-if-no-test-linked-test-with-enabled-test_",
|
||||||
|
flags=[f"--{FAIL_IF_NO_TEST_SELECTED_FLAG}"],
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
gtest_test_utils.Main()
|
||||||
@ -97,6 +97,9 @@ TOTAL_SHARDS_ENV_VAR = 'GTEST_TOTAL_SHARDS'
|
|||||||
SHARD_INDEX_ENV_VAR = 'GTEST_SHARD_INDEX'
|
SHARD_INDEX_ENV_VAR = 'GTEST_SHARD_INDEX'
|
||||||
SHARD_STATUS_FILE_ENV_VAR = 'GTEST_SHARD_STATUS_FILE'
|
SHARD_STATUS_FILE_ENV_VAR = 'GTEST_SHARD_STATUS_FILE'
|
||||||
|
|
||||||
|
# The environment variable for the test warnings output file.
|
||||||
|
TEST_WARNINGS_OUTPUT_FILE = 'TEST_WARNINGS_OUTPUT_FILE'
|
||||||
|
|
||||||
# The command line flag for specifying the test filters.
|
# The command line flag for specifying the test filters.
|
||||||
FILTER_FLAG = 'gtest_filter'
|
FILTER_FLAG = 'gtest_filter'
|
||||||
|
|
||||||
@ -419,6 +422,22 @@ class GTestFilterUnitTest(gtest_test_utils.TestCase):
|
|||||||
self.RunAndVerify('BadFilter', [])
|
self.RunAndVerify('BadFilter', [])
|
||||||
self.RunAndVerifyAllowingDisabled('BadFilter', [])
|
self.RunAndVerifyAllowingDisabled('BadFilter', [])
|
||||||
|
|
||||||
|
def testBadFilterWithWarningFile(self):
|
||||||
|
"""Tests the warning file when a filter that matches nothing."""
|
||||||
|
|
||||||
|
warning_file = os.path.join(
|
||||||
|
gtest_test_utils.GetTempDir(), 'testBadFilterWithWarningFile'
|
||||||
|
)
|
||||||
|
extra_env = {TEST_WARNINGS_OUTPUT_FILE: warning_file}
|
||||||
|
args = ['--%s=%s' % (FILTER_FLAG, 'BadFilter')]
|
||||||
|
InvokeWithModifiedEnv(extra_env, RunAndReturnOutput, args)
|
||||||
|
with open(warning_file, 'r') as f:
|
||||||
|
warning_file_contents = f.read()
|
||||||
|
self.assertEqual(
|
||||||
|
warning_file_contents,
|
||||||
|
'filter "BadFilter" did not match any test; no tests were run\n',
|
||||||
|
)
|
||||||
|
|
||||||
def testFullName(self):
|
def testFullName(self):
|
||||||
"""Tests filtering by full name."""
|
"""Tests filtering by full name."""
|
||||||
|
|
||||||
|
|||||||
@ -57,7 +57,7 @@ else:
|
|||||||
STACK_TRACE_TEMPLATE = '\n'
|
STACK_TRACE_TEMPLATE = '\n'
|
||||||
|
|
||||||
EXPECTED_NON_EMPTY = {
|
EXPECTED_NON_EMPTY = {
|
||||||
'tests': 26,
|
'tests': 28,
|
||||||
'failures': 5,
|
'failures': 5,
|
||||||
'disabled': 2,
|
'disabled': 2,
|
||||||
'errors': 0,
|
'errors': 0,
|
||||||
@ -150,6 +150,9 @@ EXPECTED_NON_EMPTY = {
|
|||||||
'time': '*',
|
'time': '*',
|
||||||
'timestamp': '*',
|
'timestamp': '*',
|
||||||
'classname': 'SkippedTest',
|
'classname': 'SkippedTest',
|
||||||
|
'skipped': [
|
||||||
|
{'message': 'gtest_xml_output_unittest_.cc:*\n\n'}
|
||||||
|
],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
'name': 'SkippedWithMessage',
|
'name': 'SkippedWithMessage',
|
||||||
@ -160,6 +163,12 @@ EXPECTED_NON_EMPTY = {
|
|||||||
'time': '*',
|
'time': '*',
|
||||||
'timestamp': '*',
|
'timestamp': '*',
|
||||||
'classname': 'SkippedTest',
|
'classname': 'SkippedTest',
|
||||||
|
'skipped': [{
|
||||||
|
'message': (
|
||||||
|
'gtest_xml_output_unittest_.cc:*\n'
|
||||||
|
'It is good practice to tell why you skip a test.\n'
|
||||||
|
)
|
||||||
|
}],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
'name': 'SkippedAfterFailure',
|
'name': 'SkippedAfterFailure',
|
||||||
@ -179,6 +188,12 @@ EXPECTED_NON_EMPTY = {
|
|||||||
),
|
),
|
||||||
'type': '',
|
'type': '',
|
||||||
}],
|
}],
|
||||||
|
'skipped': [{
|
||||||
|
'message': (
|
||||||
|
'gtest_xml_output_unittest_.cc:*\n'
|
||||||
|
'It is good practice to tell why you skip a test.\n'
|
||||||
|
)
|
||||||
|
}],
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
@ -308,12 +323,14 @@ EXPECTED_NON_EMPTY = {
|
|||||||
'time': '*',
|
'time': '*',
|
||||||
'timestamp': '*',
|
'timestamp': '*',
|
||||||
'SetUpTestSuite': 'yes',
|
'SetUpTestSuite': 'yes',
|
||||||
|
'SetUpTestSuite (with whitespace)': 'yes and yes',
|
||||||
'TearDownTestSuite': 'aye',
|
'TearDownTestSuite': 'aye',
|
||||||
|
'TearDownTestSuite (with whitespace)': 'aye and aye',
|
||||||
'testsuite': [
|
'testsuite': [
|
||||||
{
|
{
|
||||||
'name': 'OneProperty',
|
'name': 'OneProperty',
|
||||||
'file': 'gtest_xml_output_unittest_.cc',
|
'file': 'gtest_xml_output_unittest_.cc',
|
||||||
'line': 121,
|
'line': 125,
|
||||||
'status': 'RUN',
|
'status': 'RUN',
|
||||||
'result': 'COMPLETED',
|
'result': 'COMPLETED',
|
||||||
'time': '*',
|
'time': '*',
|
||||||
@ -324,7 +341,7 @@ EXPECTED_NON_EMPTY = {
|
|||||||
{
|
{
|
||||||
'name': 'IntValuedProperty',
|
'name': 'IntValuedProperty',
|
||||||
'file': 'gtest_xml_output_unittest_.cc',
|
'file': 'gtest_xml_output_unittest_.cc',
|
||||||
'line': 125,
|
'line': 129,
|
||||||
'status': 'RUN',
|
'status': 'RUN',
|
||||||
'result': 'COMPLETED',
|
'result': 'COMPLETED',
|
||||||
'time': '*',
|
'time': '*',
|
||||||
@ -335,7 +352,7 @@ EXPECTED_NON_EMPTY = {
|
|||||||
{
|
{
|
||||||
'name': 'ThreeProperties',
|
'name': 'ThreeProperties',
|
||||||
'file': 'gtest_xml_output_unittest_.cc',
|
'file': 'gtest_xml_output_unittest_.cc',
|
||||||
'line': 129,
|
'line': 133,
|
||||||
'status': 'RUN',
|
'status': 'RUN',
|
||||||
'result': 'COMPLETED',
|
'result': 'COMPLETED',
|
||||||
'time': '*',
|
'time': '*',
|
||||||
@ -348,7 +365,7 @@ EXPECTED_NON_EMPTY = {
|
|||||||
{
|
{
|
||||||
'name': 'TwoValuesForOneKeyUsesLastValue',
|
'name': 'TwoValuesForOneKeyUsesLastValue',
|
||||||
'file': 'gtest_xml_output_unittest_.cc',
|
'file': 'gtest_xml_output_unittest_.cc',
|
||||||
'line': 135,
|
'line': 139,
|
||||||
'status': 'RUN',
|
'status': 'RUN',
|
||||||
'result': 'COMPLETED',
|
'result': 'COMPLETED',
|
||||||
'time': '*',
|
'time': '*',
|
||||||
@ -370,7 +387,7 @@ EXPECTED_NON_EMPTY = {
|
|||||||
{
|
{
|
||||||
'name': 'RecordProperty',
|
'name': 'RecordProperty',
|
||||||
'file': 'gtest_xml_output_unittest_.cc',
|
'file': 'gtest_xml_output_unittest_.cc',
|
||||||
'line': 140,
|
'line': 144,
|
||||||
'status': 'RUN',
|
'status': 'RUN',
|
||||||
'result': 'COMPLETED',
|
'result': 'COMPLETED',
|
||||||
'time': '*',
|
'time': '*',
|
||||||
@ -381,7 +398,7 @@ EXPECTED_NON_EMPTY = {
|
|||||||
{
|
{
|
||||||
'name': 'ExternalUtilityThatCallsRecordIntValuedProperty',
|
'name': 'ExternalUtilityThatCallsRecordIntValuedProperty',
|
||||||
'file': 'gtest_xml_output_unittest_.cc',
|
'file': 'gtest_xml_output_unittest_.cc',
|
||||||
'line': 153,
|
'line': 157,
|
||||||
'status': 'RUN',
|
'status': 'RUN',
|
||||||
'result': 'COMPLETED',
|
'result': 'COMPLETED',
|
||||||
'time': '*',
|
'time': '*',
|
||||||
@ -394,7 +411,7 @@ EXPECTED_NON_EMPTY = {
|
|||||||
'ExternalUtilityThatCallsRecordStringValuedProperty'
|
'ExternalUtilityThatCallsRecordStringValuedProperty'
|
||||||
),
|
),
|
||||||
'file': 'gtest_xml_output_unittest_.cc',
|
'file': 'gtest_xml_output_unittest_.cc',
|
||||||
'line': 157,
|
'line': 161,
|
||||||
'status': 'RUN',
|
'status': 'RUN',
|
||||||
'result': 'COMPLETED',
|
'result': 'COMPLETED',
|
||||||
'time': '*',
|
'time': '*',
|
||||||
@ -404,6 +421,83 @@ EXPECTED_NON_EMPTY = {
|
|||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
'name': 'SetupFailTest',
|
||||||
|
'tests': 1,
|
||||||
|
'failures': 0,
|
||||||
|
'disabled': 0,
|
||||||
|
'errors': 0,
|
||||||
|
'time': '*',
|
||||||
|
'timestamp': '*',
|
||||||
|
'testsuite': [
|
||||||
|
{
|
||||||
|
'name': 'NoopPassingTest',
|
||||||
|
'file': 'gtest_xml_output_unittest_.cc',
|
||||||
|
'line': 172,
|
||||||
|
'status': 'RUN',
|
||||||
|
'result': 'SKIPPED',
|
||||||
|
'timestamp': '*',
|
||||||
|
'time': '*',
|
||||||
|
'classname': 'SetupFailTest',
|
||||||
|
'skipped': [
|
||||||
|
{'message': 'gtest_xml_output_unittest_.cc:*\n'}
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
'name': '',
|
||||||
|
'status': 'RUN',
|
||||||
|
'result': 'COMPLETED',
|
||||||
|
'timestamp': '*',
|
||||||
|
'time': '*',
|
||||||
|
'classname': '',
|
||||||
|
'failures': [{
|
||||||
|
'failure': (
|
||||||
|
'gtest_xml_output_unittest_.cc:*\nExpected equality'
|
||||||
|
' of these values:\n 1\n 2'
|
||||||
|
+ STACK_TRACE_TEMPLATE
|
||||||
|
),
|
||||||
|
'type': '',
|
||||||
|
}],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
'name': 'TearDownFailTest',
|
||||||
|
'tests': 1,
|
||||||
|
'failures': 0,
|
||||||
|
'disabled': 0,
|
||||||
|
'errors': 0,
|
||||||
|
'timestamp': '*',
|
||||||
|
'time': '*',
|
||||||
|
'testsuite': [
|
||||||
|
{
|
||||||
|
'name': 'NoopPassingTest',
|
||||||
|
'file': 'gtest_xml_output_unittest_.cc',
|
||||||
|
'line': 179,
|
||||||
|
'status': 'RUN',
|
||||||
|
'result': 'COMPLETED',
|
||||||
|
'timestamp': '*',
|
||||||
|
'time': '*',
|
||||||
|
'classname': 'TearDownFailTest',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
'name': '',
|
||||||
|
'status': 'RUN',
|
||||||
|
'result': 'COMPLETED',
|
||||||
|
'timestamp': '*',
|
||||||
|
'time': '*',
|
||||||
|
'classname': '',
|
||||||
|
'failures': [{
|
||||||
|
'failure': (
|
||||||
|
'gtest_xml_output_unittest_.cc:*\nExpected equality'
|
||||||
|
' of these values:\n 1\n 2'
|
||||||
|
+ STACK_TRACE_TEMPLATE
|
||||||
|
),
|
||||||
|
'type': '',
|
||||||
|
}],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
{
|
{
|
||||||
'name': 'TypedTest/0',
|
'name': 'TypedTest/0',
|
||||||
'tests': 1,
|
'tests': 1,
|
||||||
@ -416,7 +510,7 @@ EXPECTED_NON_EMPTY = {
|
|||||||
'name': 'HasTypeParamAttribute',
|
'name': 'HasTypeParamAttribute',
|
||||||
'type_param': 'int',
|
'type_param': 'int',
|
||||||
'file': 'gtest_xml_output_unittest_.cc',
|
'file': 'gtest_xml_output_unittest_.cc',
|
||||||
'line': 173,
|
'line': 193,
|
||||||
'status': 'RUN',
|
'status': 'RUN',
|
||||||
'result': 'COMPLETED',
|
'result': 'COMPLETED',
|
||||||
'time': '*',
|
'time': '*',
|
||||||
@ -436,7 +530,7 @@ EXPECTED_NON_EMPTY = {
|
|||||||
'name': 'HasTypeParamAttribute',
|
'name': 'HasTypeParamAttribute',
|
||||||
'type_param': 'long',
|
'type_param': 'long',
|
||||||
'file': 'gtest_xml_output_unittest_.cc',
|
'file': 'gtest_xml_output_unittest_.cc',
|
||||||
'line': 173,
|
'line': 193,
|
||||||
'status': 'RUN',
|
'status': 'RUN',
|
||||||
'result': 'COMPLETED',
|
'result': 'COMPLETED',
|
||||||
'time': '*',
|
'time': '*',
|
||||||
@ -456,7 +550,7 @@ EXPECTED_NON_EMPTY = {
|
|||||||
'name': 'HasTypeParamAttribute',
|
'name': 'HasTypeParamAttribute',
|
||||||
'type_param': 'int',
|
'type_param': 'int',
|
||||||
'file': 'gtest_xml_output_unittest_.cc',
|
'file': 'gtest_xml_output_unittest_.cc',
|
||||||
'line': 180,
|
'line': 200,
|
||||||
'status': 'RUN',
|
'status': 'RUN',
|
||||||
'result': 'COMPLETED',
|
'result': 'COMPLETED',
|
||||||
'time': '*',
|
'time': '*',
|
||||||
@ -476,7 +570,7 @@ EXPECTED_NON_EMPTY = {
|
|||||||
'name': 'HasTypeParamAttribute',
|
'name': 'HasTypeParamAttribute',
|
||||||
'type_param': 'long',
|
'type_param': 'long',
|
||||||
'file': 'gtest_xml_output_unittest_.cc',
|
'file': 'gtest_xml_output_unittest_.cc',
|
||||||
'line': 180,
|
'line': 200,
|
||||||
'status': 'RUN',
|
'status': 'RUN',
|
||||||
'result': 'COMPLETED',
|
'result': 'COMPLETED',
|
||||||
'time': '*',
|
'time': '*',
|
||||||
@ -497,7 +591,7 @@ EXPECTED_NON_EMPTY = {
|
|||||||
'name': 'HasValueParamAttribute/0',
|
'name': 'HasValueParamAttribute/0',
|
||||||
'value_param': '33',
|
'value_param': '33',
|
||||||
'file': 'gtest_xml_output_unittest_.cc',
|
'file': 'gtest_xml_output_unittest_.cc',
|
||||||
'line': 164,
|
'line': 184,
|
||||||
'status': 'RUN',
|
'status': 'RUN',
|
||||||
'result': 'COMPLETED',
|
'result': 'COMPLETED',
|
||||||
'time': '*',
|
'time': '*',
|
||||||
@ -508,7 +602,7 @@ EXPECTED_NON_EMPTY = {
|
|||||||
'name': 'HasValueParamAttribute/1',
|
'name': 'HasValueParamAttribute/1',
|
||||||
'value_param': '42',
|
'value_param': '42',
|
||||||
'file': 'gtest_xml_output_unittest_.cc',
|
'file': 'gtest_xml_output_unittest_.cc',
|
||||||
'line': 164,
|
'line': 184,
|
||||||
'status': 'RUN',
|
'status': 'RUN',
|
||||||
'result': 'COMPLETED',
|
'result': 'COMPLETED',
|
||||||
'time': '*',
|
'time': '*',
|
||||||
@ -519,7 +613,7 @@ EXPECTED_NON_EMPTY = {
|
|||||||
'name': 'AnotherTestThatHasValueParamAttribute/0',
|
'name': 'AnotherTestThatHasValueParamAttribute/0',
|
||||||
'value_param': '33',
|
'value_param': '33',
|
||||||
'file': 'gtest_xml_output_unittest_.cc',
|
'file': 'gtest_xml_output_unittest_.cc',
|
||||||
'line': 165,
|
'line': 185,
|
||||||
'status': 'RUN',
|
'status': 'RUN',
|
||||||
'result': 'COMPLETED',
|
'result': 'COMPLETED',
|
||||||
'time': '*',
|
'time': '*',
|
||||||
@ -530,7 +624,7 @@ EXPECTED_NON_EMPTY = {
|
|||||||
'name': 'AnotherTestThatHasValueParamAttribute/1',
|
'name': 'AnotherTestThatHasValueParamAttribute/1',
|
||||||
'value_param': '42',
|
'value_param': '42',
|
||||||
'file': 'gtest_xml_output_unittest_.cc',
|
'file': 'gtest_xml_output_unittest_.cc',
|
||||||
'line': 165,
|
'line': 185,
|
||||||
'status': 'RUN',
|
'status': 'RUN',
|
||||||
'result': 'COMPLETED',
|
'result': 'COMPLETED',
|
||||||
'time': '*',
|
'time': '*',
|
||||||
|
|||||||
@ -115,11 +115,14 @@ TEST(OutputFileHelpersTest, GetCurrentExecutableName) {
|
|||||||
strcasecmp("gtest_dll_test", exe_str.c_str()) == 0;
|
strcasecmp("gtest_dll_test", exe_str.c_str()) == 0;
|
||||||
#elif defined(GTEST_OS_FUCHSIA)
|
#elif defined(GTEST_OS_FUCHSIA)
|
||||||
const bool success = exe_str == "app";
|
const bool success = exe_str == "app";
|
||||||
|
#elif defined(__EMSCRIPTEN__)
|
||||||
|
const bool success = exe_str == "patched_googletest-options-test.js";
|
||||||
#else
|
#else
|
||||||
const bool success =
|
const bool success =
|
||||||
exe_str == "googletest-options-test" || exe_str == "gtest_all_test" ||
|
exe_str == "googletest-options-test" || exe_str == "gtest_all_test" ||
|
||||||
exe_str == "lt-gtest_all_test" || exe_str == "gtest_dll_test";
|
exe_str == "lt-gtest_all_test" || exe_str == "gtest_dll_test";
|
||||||
#endif // GTEST_OS_WINDOWS
|
#endif // platform ifdefs
|
||||||
|
|
||||||
if (!success) FAIL() << "GetCurrentExecutableName() returns " << exe_str;
|
if (!success) FAIL() << "GetCurrentExecutableName() returns " << exe_str;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -62,7 +62,7 @@ Expected equality of these values:
|
|||||||
Which is: "\"Line\0 1\"\nLine 2"
|
Which is: "\"Line\0 1\"\nLine 2"
|
||||||
"Line 2"
|
"Line 2"
|
||||||
With diff:
|
With diff:
|
||||||
@@ -1,2 @@
|
@@ -1,2 +1 @@
|
||||||
-\"Line\0 1\"
|
-\"Line\0 1\"
|
||||||
Line 2
|
Line 2
|
||||||
|
|
||||||
@ -696,7 +696,6 @@ Expected: 1 fatal failure
|
|||||||
Actual:
|
Actual:
|
||||||
googletest-output-test_.cc:#: Success:
|
googletest-output-test_.cc:#: Success:
|
||||||
Succeeded
|
Succeeded
|
||||||
Stack trace: (omitted)
|
|
||||||
|
|
||||||
|
|
||||||
Stack trace: (omitted)
|
Stack trace: (omitted)
|
||||||
@ -733,7 +732,6 @@ Expected: 1 non-fatal failure
|
|||||||
Actual:
|
Actual:
|
||||||
googletest-output-test_.cc:#: Success:
|
googletest-output-test_.cc:#: Success:
|
||||||
Succeeded
|
Succeeded
|
||||||
Stack trace: (omitted)
|
|
||||||
|
|
||||||
|
|
||||||
Stack trace: (omitted)
|
Stack trace: (omitted)
|
||||||
@ -770,7 +768,6 @@ Expected: 1 fatal failure
|
|||||||
Actual:
|
Actual:
|
||||||
googletest-output-test_.cc:#: Success:
|
googletest-output-test_.cc:#: Success:
|
||||||
Succeeded
|
Succeeded
|
||||||
Stack trace: (omitted)
|
|
||||||
|
|
||||||
|
|
||||||
Stack trace: (omitted)
|
Stack trace: (omitted)
|
||||||
@ -807,7 +804,6 @@ Expected: 1 non-fatal failure
|
|||||||
Actual:
|
Actual:
|
||||||
googletest-output-test_.cc:#: Success:
|
googletest-output-test_.cc:#: Success:
|
||||||
Succeeded
|
Succeeded
|
||||||
Stack trace: (omitted)
|
|
||||||
|
|
||||||
|
|
||||||
Stack trace: (omitted)
|
Stack trace: (omitted)
|
||||||
@ -970,7 +966,6 @@ googletest-output-test_.cc:#: Skipped
|
|||||||
[0;32m[----------] [m1 test from TestSuiteThatSkipsInSetUp
|
[0;32m[----------] [m1 test from TestSuiteThatSkipsInSetUp
|
||||||
googletest-output-test_.cc:#: Skipped
|
googletest-output-test_.cc:#: Skipped
|
||||||
Skip entire test suite
|
Skip entire test suite
|
||||||
Stack trace: (omitted)
|
|
||||||
|
|
||||||
[0;32m[ RUN ] [mTestSuiteThatSkipsInSetUp.ShouldNotRun
|
[0;32m[ RUN ] [mTestSuiteThatSkipsInSetUp.ShouldNotRun
|
||||||
googletest-output-test_.cc:#: Skipped
|
googletest-output-test_.cc:#: Skipped
|
||||||
|
|||||||
@ -35,12 +35,17 @@
|
|||||||
#include "test/googletest-param-test-test.h"
|
#include "test/googletest-param-test-test.h"
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <cstddef>
|
||||||
|
#include <cstdint>
|
||||||
|
#include <functional>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <list>
|
#include <list>
|
||||||
#include <set>
|
#include <set>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <string_view>
|
||||||
#include <tuple>
|
#include <tuple>
|
||||||
|
#include <type_traits>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "gtest/gtest.h"
|
#include "gtest/gtest.h"
|
||||||
@ -583,6 +588,71 @@ TEST(ConvertTest, NonDefaultConstructAssign) {
|
|||||||
EXPECT_TRUE(it == gen.end());
|
EXPECT_TRUE(it == gen.end());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(ConvertTest, WithConverterLambdaAndDeducedType) {
|
||||||
|
const ParamGenerator<ConstructFromT<int8_t>> gen =
|
||||||
|
ConvertGenerator(Values("0", std::string("1")), [](const std::string& s) {
|
||||||
|
size_t pos;
|
||||||
|
int64_t value = std::stoll(s, &pos);
|
||||||
|
EXPECT_EQ(pos, s.size());
|
||||||
|
return value;
|
||||||
|
});
|
||||||
|
|
||||||
|
ConstructFromT<int8_t> expected_values[] = {ConstructFromT<int8_t>(0),
|
||||||
|
ConstructFromT<int8_t>(1)};
|
||||||
|
VerifyGenerator(gen, expected_values);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(ConvertTest, WithConverterLambdaAndExplicitType) {
|
||||||
|
auto convert_generator = ConvertGenerator<std::string>(
|
||||||
|
Values("0", std::string("1")), [](std::string_view s) {
|
||||||
|
size_t pos;
|
||||||
|
int64_t value = std::stoll(std::string(s), &pos);
|
||||||
|
EXPECT_EQ(pos, s.size());
|
||||||
|
return value;
|
||||||
|
});
|
||||||
|
constexpr bool is_correct_type = std::is_same_v<
|
||||||
|
decltype(convert_generator),
|
||||||
|
testing::internal::ParamConverterGenerator<
|
||||||
|
std::string, std::function<int64_t(std::string_view)>>>;
|
||||||
|
EXPECT_TRUE(is_correct_type);
|
||||||
|
const ParamGenerator<ConstructFromT<int8_t>> gen = convert_generator;
|
||||||
|
|
||||||
|
ConstructFromT<int8_t> expected_values[] = {ConstructFromT<int8_t>(0),
|
||||||
|
ConstructFromT<int8_t>(1)};
|
||||||
|
VerifyGenerator(gen, expected_values);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(ConvertTest, WithConverterFunctionPointer) {
|
||||||
|
int64_t (*func_ptr)(const std::string&) = [](const std::string& s) {
|
||||||
|
size_t pos;
|
||||||
|
int64_t value = std::stoll(s, &pos);
|
||||||
|
EXPECT_EQ(pos, s.size());
|
||||||
|
return value;
|
||||||
|
};
|
||||||
|
const ParamGenerator<ConstructFromT<int8_t>> gen =
|
||||||
|
ConvertGenerator(Values("0", std::string("1")), func_ptr);
|
||||||
|
|
||||||
|
ConstructFromT<int8_t> expected_values[] = {ConstructFromT<int8_t>(0),
|
||||||
|
ConstructFromT<int8_t>(1)};
|
||||||
|
VerifyGenerator(gen, expected_values);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(ConvertTest, WithConverterFunctionReference) {
|
||||||
|
int64_t (*func_ptr)(const std::string&) = [](const std::string& s) {
|
||||||
|
size_t pos;
|
||||||
|
int64_t value = std::stoll(s, &pos);
|
||||||
|
EXPECT_EQ(pos, s.size());
|
||||||
|
return value;
|
||||||
|
};
|
||||||
|
int64_t (&func_ref)(const std::string&) = *func_ptr;
|
||||||
|
const ParamGenerator<ConstructFromT<int8_t>> gen =
|
||||||
|
ConvertGenerator(Values("0", std::string("1")), func_ref);
|
||||||
|
|
||||||
|
ConstructFromT<int8_t> expected_values[] = {ConstructFromT<int8_t>(0),
|
||||||
|
ConstructFromT<int8_t>(1)};
|
||||||
|
VerifyGenerator(gen, expected_values);
|
||||||
|
}
|
||||||
|
|
||||||
// Tests that an generator produces correct sequence after being
|
// Tests that an generator produces correct sequence after being
|
||||||
// assigned from another generator.
|
// assigned from another generator.
|
||||||
TEST(ParamGeneratorTest, AssignmentWorks) {
|
TEST(ParamGeneratorTest, AssignmentWorks) {
|
||||||
@ -626,7 +696,7 @@ class TestGenerationEnvironment : public ::testing::Environment {
|
|||||||
msg << "TestsExpandedAndRun/" << i;
|
msg << "TestsExpandedAndRun/" << i;
|
||||||
if (UnitTestOptions::FilterMatchesTest(
|
if (UnitTestOptions::FilterMatchesTest(
|
||||||
"TestExpansionModule/MultipleTestGenerationTest",
|
"TestExpansionModule/MultipleTestGenerationTest",
|
||||||
msg.GetString().c_str())) {
|
msg.GetString())) {
|
||||||
perform_check = true;
|
perform_check = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1104,7 +1174,7 @@ TEST_P(ParameterizedDerivedTest, SeesSequence) {
|
|||||||
class ParameterizedDeathTest : public ::testing::TestWithParam<int> {};
|
class ParameterizedDeathTest : public ::testing::TestWithParam<int> {};
|
||||||
|
|
||||||
TEST_F(ParameterizedDeathTest, GetParamDiesFromTestF) {
|
TEST_F(ParameterizedDeathTest, GetParamDiesFromTestF) {
|
||||||
EXPECT_DEATH_IF_SUPPORTED(GetParam(), ".* value-parameterized test .*");
|
EXPECT_DEATH_IF_SUPPORTED((void)GetParam(), ".* value-parameterized test .*");
|
||||||
}
|
}
|
||||||
|
|
||||||
INSTANTIATE_TEST_SUITE_P(RangeZeroToFive, ParameterizedDerivedTest,
|
INSTANTIATE_TEST_SUITE_P(RangeZeroToFive, ParameterizedDerivedTest,
|
||||||
|
|||||||
@ -288,15 +288,15 @@ TEST(FormatCompilerIndependentFileLocationTest, FormatsUknownFileAndLine) {
|
|||||||
defined(GTEST_OS_OPENBSD) || defined(GTEST_OS_GNU_HURD)
|
defined(GTEST_OS_OPENBSD) || defined(GTEST_OS_GNU_HURD)
|
||||||
void* ThreadFunc(void* data) {
|
void* ThreadFunc(void* data) {
|
||||||
internal::Mutex* mutex = static_cast<internal::Mutex*>(data);
|
internal::Mutex* mutex = static_cast<internal::Mutex*>(data);
|
||||||
mutex->Lock();
|
mutex->lock();
|
||||||
mutex->Unlock();
|
mutex->unlock();
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(GetThreadCountTest, ReturnsCorrectValue) {
|
TEST(GetThreadCountTest, ReturnsCorrectValue) {
|
||||||
size_t starting_count;
|
size_t starting_count;
|
||||||
size_t thread_count_after_create;
|
size_t thread_count_after_create;
|
||||||
size_t thread_count_after_join;
|
size_t thread_count_after_join = 0;
|
||||||
|
|
||||||
// We can't guarantee that no other thread was created or destroyed between
|
// We can't guarantee that no other thread was created or destroyed between
|
||||||
// any two calls to GetThreadCount(). We make multiple attempts, hoping that
|
// any two calls to GetThreadCount(). We make multiple attempts, hoping that
|
||||||
@ -308,7 +308,7 @@ TEST(GetThreadCountTest, ReturnsCorrectValue) {
|
|||||||
|
|
||||||
internal::Mutex mutex;
|
internal::Mutex mutex;
|
||||||
{
|
{
|
||||||
internal::MutexLock lock(&mutex);
|
internal::MutexLock lock(mutex);
|
||||||
pthread_attr_t attr;
|
pthread_attr_t attr;
|
||||||
ASSERT_EQ(0, pthread_attr_init(&attr));
|
ASSERT_EQ(0, pthread_attr_init(&attr));
|
||||||
ASSERT_EQ(0, pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE));
|
ASSERT_EQ(0, pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE));
|
||||||
@ -316,9 +316,9 @@ TEST(GetThreadCountTest, ReturnsCorrectValue) {
|
|||||||
const int status = pthread_create(&thread_id, &attr, &ThreadFunc, &mutex);
|
const int status = pthread_create(&thread_id, &attr, &ThreadFunc, &mutex);
|
||||||
ASSERT_EQ(0, pthread_attr_destroy(&attr));
|
ASSERT_EQ(0, pthread_attr_destroy(&attr));
|
||||||
ASSERT_EQ(0, status);
|
ASSERT_EQ(0, status);
|
||||||
}
|
|
||||||
|
|
||||||
thread_count_after_create = GetThreadCount();
|
thread_count_after_create = GetThreadCount();
|
||||||
|
}
|
||||||
|
|
||||||
void* dummy;
|
void* dummy;
|
||||||
ASSERT_EQ(0, pthread_join(thread_id, &dummy));
|
ASSERT_EQ(0, pthread_join(thread_id, &dummy));
|
||||||
@ -1028,7 +1028,9 @@ TEST(MutexDeathTest, AssertHeldShouldAssertWhenNotLocked) {
|
|||||||
EXPECT_DEATH_IF_SUPPORTED(
|
EXPECT_DEATH_IF_SUPPORTED(
|
||||||
{
|
{
|
||||||
Mutex m;
|
Mutex m;
|
||||||
{ MutexLock lock(&m); }
|
{
|
||||||
|
MutexLock lock(m);
|
||||||
|
}
|
||||||
m.AssertHeld();
|
m.AssertHeld();
|
||||||
},
|
},
|
||||||
"thread .*hold");
|
"thread .*hold");
|
||||||
@ -1036,13 +1038,13 @@ TEST(MutexDeathTest, AssertHeldShouldAssertWhenNotLocked) {
|
|||||||
|
|
||||||
TEST(MutexTest, AssertHeldShouldNotAssertWhenLocked) {
|
TEST(MutexTest, AssertHeldShouldNotAssertWhenLocked) {
|
||||||
Mutex m;
|
Mutex m;
|
||||||
MutexLock lock(&m);
|
MutexLock lock(m);
|
||||||
m.AssertHeld();
|
m.AssertHeld();
|
||||||
}
|
}
|
||||||
|
|
||||||
class AtomicCounterWithMutex {
|
class AtomicCounterWithMutex {
|
||||||
public:
|
public:
|
||||||
explicit AtomicCounterWithMutex(Mutex* mutex)
|
explicit AtomicCounterWithMutex(Mutex& mutex)
|
||||||
: value_(0), mutex_(mutex), random_(42) {}
|
: value_(0), mutex_(mutex), random_(42) {}
|
||||||
|
|
||||||
void Increment() {
|
void Increment() {
|
||||||
@ -1083,7 +1085,7 @@ class AtomicCounterWithMutex {
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
volatile int value_;
|
volatile int value_;
|
||||||
Mutex* const mutex_; // Protects value_.
|
Mutex& mutex_; // Protects value_.
|
||||||
Random random_;
|
Random random_;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -1094,7 +1096,7 @@ void CountingThreadFunc(pair<AtomicCounterWithMutex*, int> param) {
|
|||||||
// Tests that the mutex only lets one thread at a time to lock it.
|
// Tests that the mutex only lets one thread at a time to lock it.
|
||||||
TEST(MutexTest, OnlyOneThreadCanLockAtATime) {
|
TEST(MutexTest, OnlyOneThreadCanLockAtATime) {
|
||||||
Mutex mutex;
|
Mutex mutex;
|
||||||
AtomicCounterWithMutex locked_counter(&mutex);
|
AtomicCounterWithMutex locked_counter(mutex);
|
||||||
|
|
||||||
typedef ThreadWithParam<pair<AtomicCounterWithMutex*, int> > ThreadType;
|
typedef ThreadWithParam<pair<AtomicCounterWithMutex*, int> > ThreadType;
|
||||||
const int kCycleCount = 20;
|
const int kCycleCount = 20;
|
||||||
|
|||||||
@ -32,6 +32,7 @@
|
|||||||
// This file tests the universal value printer.
|
// This file tests the universal value printer.
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <any>
|
||||||
#include <cctype>
|
#include <cctype>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
@ -42,23 +43,35 @@
|
|||||||
#include <list>
|
#include <list>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <optional>
|
||||||
#include <ostream>
|
#include <ostream>
|
||||||
#include <set>
|
#include <set>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <string_view>
|
||||||
#include <tuple>
|
#include <tuple>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <unordered_set>
|
#include <unordered_set>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
#include <variant>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "gtest/gtest-printers.h"
|
#include "gtest/gtest-printers.h"
|
||||||
#include "gtest/gtest.h"
|
#include "gtest/gtest.h"
|
||||||
|
#include "gtest/internal/gtest-port.h"
|
||||||
|
|
||||||
#ifdef GTEST_HAS_ABSL
|
#ifdef GTEST_HAS_ABSL
|
||||||
#include "absl/strings/str_format.h"
|
#include "absl/strings/str_format.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#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
|
||||||
|
|
||||||
// Some user-defined types for testing the universal value printer.
|
// Some user-defined types for testing the universal value printer.
|
||||||
|
|
||||||
// An anonymous enum type.
|
// An anonymous enum type.
|
||||||
@ -112,6 +125,9 @@ class UnprintableTemplateInGlobal {
|
|||||||
// A user-defined streamable type in the global namespace.
|
// A user-defined streamable type in the global namespace.
|
||||||
class StreamableInGlobal {
|
class StreamableInGlobal {
|
||||||
public:
|
public:
|
||||||
|
StreamableInGlobal() = default;
|
||||||
|
StreamableInGlobal(const StreamableInGlobal&) = default;
|
||||||
|
StreamableInGlobal& operator=(const StreamableInGlobal&) = default;
|
||||||
virtual ~StreamableInGlobal() = default;
|
virtual ~StreamableInGlobal() = default;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -563,6 +579,8 @@ TEST(PrintU8StringTest, Null) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Tests that u8 strings are escaped properly.
|
// Tests that u8 strings are escaped properly.
|
||||||
|
// TODO(b/396121064) - Fix this test under MSVC
|
||||||
|
#ifndef _MSC_VER
|
||||||
TEST(PrintU8StringTest, EscapesProperly) {
|
TEST(PrintU8StringTest, EscapesProperly) {
|
||||||
const char8_t* p = u8"'\"?\\\a\b\f\n\r\t\v\x7F\xFF hello 世界";
|
const char8_t* p = u8"'\"?\\\a\b\f\n\r\t\v\x7F\xFF hello 世界";
|
||||||
EXPECT_EQ(PrintPointer(p) +
|
EXPECT_EQ(PrintPointer(p) +
|
||||||
@ -570,7 +588,8 @@ TEST(PrintU8StringTest, EscapesProperly) {
|
|||||||
"hello \\xE4\\xB8\\x96\\xE7\\x95\\x8C\"",
|
"hello \\xE4\\xB8\\x96\\xE7\\x95\\x8C\"",
|
||||||
Print(p));
|
Print(p));
|
||||||
}
|
}
|
||||||
#endif
|
#endif // _MSC_VER
|
||||||
|
#endif // __cpp_lib_char8_t
|
||||||
|
|
||||||
// const char16_t*.
|
// const char16_t*.
|
||||||
TEST(PrintU16StringTest, Const) {
|
TEST(PrintU16StringTest, Const) {
|
||||||
@ -781,7 +800,7 @@ struct Foo {
|
|||||||
TEST(PrintPointerTest, MemberVariablePointer) {
|
TEST(PrintPointerTest, MemberVariablePointer) {
|
||||||
EXPECT_TRUE(HasPrefix(Print(&Foo::value),
|
EXPECT_TRUE(HasPrefix(Print(&Foo::value),
|
||||||
Print(sizeof(&Foo::value)) + "-byte object "));
|
Print(sizeof(&Foo::value)) + "-byte object "));
|
||||||
int Foo::*p = NULL; // NOLINT
|
int Foo::* p = NULL; // NOLINT
|
||||||
EXPECT_TRUE(HasPrefix(Print(p), Print(sizeof(p)) + "-byte object "));
|
EXPECT_TRUE(HasPrefix(Print(p), Print(sizeof(p)) + "-byte object "));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -908,7 +927,7 @@ TEST(PrintArrayTest, BigArray) {
|
|||||||
PrintArrayHelper(a));
|
PrintArrayHelper(a));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tests printing ::string and ::std::string.
|
// Tests printing ::std::string and ::string_view.
|
||||||
|
|
||||||
// ::std::string.
|
// ::std::string.
|
||||||
TEST(PrintStringTest, StringInStdNamespace) {
|
TEST(PrintStringTest, StringInStdNamespace) {
|
||||||
@ -918,6 +937,13 @@ TEST(PrintStringTest, StringInStdNamespace) {
|
|||||||
Print(str));
|
Print(str));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(PrintStringTest, StringViewInStdNamespace) {
|
||||||
|
const char s[] = "'\"?\\\a\b\f\n\0\r\t\v\x7F\xFF a";
|
||||||
|
const ::std::string_view str(s, sizeof(s));
|
||||||
|
EXPECT_EQ("\"'\\\"?\\\\\\a\\b\\f\\n\\0\\r\\t\\v\\x7F\\xFF a\\0\"",
|
||||||
|
Print(str));
|
||||||
|
}
|
||||||
|
|
||||||
TEST(PrintStringTest, StringAmbiguousHex) {
|
TEST(PrintStringTest, StringAmbiguousHex) {
|
||||||
// "\x6BANANA" is ambiguous, it can be interpreted as starting with either of:
|
// "\x6BANANA" is ambiguous, it can be interpreted as starting with either of:
|
||||||
// '\x6', '\x6B', or '\x6BA'.
|
// '\x6', '\x6B', or '\x6BA'.
|
||||||
@ -935,7 +961,7 @@ TEST(PrintStringTest, StringAmbiguousHex) {
|
|||||||
EXPECT_EQ("\"!\\x5-!\"", Print(::std::string("!\x5-!")));
|
EXPECT_EQ("\"!\\x5-!\"", Print(::std::string("!\x5-!")));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tests printing ::std::wstring.
|
// Tests printing ::std::wstring and ::std::wstring_view.
|
||||||
#if GTEST_HAS_STD_WSTRING
|
#if GTEST_HAS_STD_WSTRING
|
||||||
// ::std::wstring.
|
// ::std::wstring.
|
||||||
TEST(PrintWideStringTest, StringInStdNamespace) {
|
TEST(PrintWideStringTest, StringInStdNamespace) {
|
||||||
@ -947,6 +973,15 @@ TEST(PrintWideStringTest, StringInStdNamespace) {
|
|||||||
Print(str));
|
Print(str));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(PrintWideStringTest, StringViewInStdNamespace) {
|
||||||
|
const wchar_t s[] = L"'\"?\\\a\b\f\n\0\r\t\v\xD3\x576\x8D3\xC74D a";
|
||||||
|
const ::std::wstring_view str(s, sizeof(s) / sizeof(wchar_t));
|
||||||
|
EXPECT_EQ(
|
||||||
|
"L\"'\\\"?\\\\\\a\\b\\f\\n\\0\\r\\t\\v"
|
||||||
|
"\\xD3\\x576\\x8D3\\xC74D a\\0\"",
|
||||||
|
Print(str));
|
||||||
|
}
|
||||||
|
|
||||||
TEST(PrintWideStringTest, StringAmbiguousHex) {
|
TEST(PrintWideStringTest, StringAmbiguousHex) {
|
||||||
// same for wide strings.
|
// same for wide strings.
|
||||||
EXPECT_EQ("L\"0\\x12\" L\"3\"", Print(::std::wstring(L"0\x12"
|
EXPECT_EQ("L\"0\\x12\" L\"3\"", Print(::std::wstring(L"0\x12"
|
||||||
@ -965,6 +1000,12 @@ TEST(PrintStringTest, U8String) {
|
|||||||
EXPECT_EQ(str, str); // Verify EXPECT_EQ compiles with this type.
|
EXPECT_EQ(str, str); // Verify EXPECT_EQ compiles with this type.
|
||||||
EXPECT_EQ("u8\"Hello, \\xE4\\xB8\\x96\\xE7\\x95\\x8C\"", Print(str));
|
EXPECT_EQ("u8\"Hello, \\xE4\\xB8\\x96\\xE7\\x95\\x8C\"", Print(str));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(PrintStringTest, U8StringView) {
|
||||||
|
std::u8string_view str = u8"Hello, 世界";
|
||||||
|
EXPECT_EQ(str, str); // Verify EXPECT_EQ compiles with this type.
|
||||||
|
EXPECT_EQ("u8\"Hello, \\xE4\\xB8\\x96\\xE7\\x95\\x8C\"", Print(str));
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
TEST(PrintStringTest, U16String) {
|
TEST(PrintStringTest, U16String) {
|
||||||
@ -973,12 +1014,24 @@ TEST(PrintStringTest, U16String) {
|
|||||||
EXPECT_EQ("u\"Hello, \\x4E16\\x754C\"", Print(str));
|
EXPECT_EQ("u\"Hello, \\x4E16\\x754C\"", Print(str));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(PrintStringTest, U16StringView) {
|
||||||
|
std::u16string_view str = u"Hello, 世界";
|
||||||
|
EXPECT_EQ(str, str); // Verify EXPECT_EQ compiles with this type.
|
||||||
|
EXPECT_EQ("u\"Hello, \\x4E16\\x754C\"", Print(str));
|
||||||
|
}
|
||||||
|
|
||||||
TEST(PrintStringTest, U32String) {
|
TEST(PrintStringTest, U32String) {
|
||||||
std::u32string str = U"Hello, 🗺️";
|
std::u32string str = U"Hello, 🗺️";
|
||||||
EXPECT_EQ(str, str); // Verify EXPECT_EQ compiles with this type
|
EXPECT_EQ(str, str); // Verify EXPECT_EQ compiles with this type
|
||||||
EXPECT_EQ("U\"Hello, \\x1F5FA\\xFE0F\"", Print(str));
|
EXPECT_EQ("U\"Hello, \\x1F5FA\\xFE0F\"", Print(str));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(PrintStringTest, U32StringView) {
|
||||||
|
std::u32string_view str = U"Hello, 🗺️";
|
||||||
|
EXPECT_EQ(str, str); // Verify EXPECT_EQ compiles with this type
|
||||||
|
EXPECT_EQ("U\"Hello, \\x1F5FA\\xFE0F\"", Print(str));
|
||||||
|
}
|
||||||
|
|
||||||
// Tests printing types that support generic streaming (i.e. streaming
|
// Tests printing types that support generic streaming (i.e. streaming
|
||||||
// to std::basic_ostream<Char, CharTraits> for any valid Char and
|
// to std::basic_ostream<Char, CharTraits> for any valid Char and
|
||||||
// CharTraits types).
|
// CharTraits types).
|
||||||
@ -1179,6 +1232,17 @@ TEST(PrintStlContainerTest, Vector) {
|
|||||||
EXPECT_EQ("{ 1, 2 }", Print(v));
|
EXPECT_EQ("{ 1, 2 }", Print(v));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(PrintStlContainerTest, StdSpan) {
|
||||||
|
#if GTEST_INTERNAL_HAS_STD_SPAN
|
||||||
|
int a[] = {3, 6, 5};
|
||||||
|
std::span<int> s = a;
|
||||||
|
|
||||||
|
EXPECT_EQ("{ 3, 6, 5 }", Print(s));
|
||||||
|
#else
|
||||||
|
GTEST_SKIP() << "Does not have std::span.";
|
||||||
|
#endif // GTEST_INTERNAL_HAS_STD_SPAN
|
||||||
|
}
|
||||||
|
|
||||||
TEST(PrintStlContainerTest, LongSequence) {
|
TEST(PrintStlContainerTest, LongSequence) {
|
||||||
const int a[100] = {1, 2, 3};
|
const int a[100] = {1, 2, 3};
|
||||||
const vector<int> v(a, a + 100);
|
const vector<int> v(a, a + 100);
|
||||||
@ -1427,7 +1491,7 @@ TEST(PrintReferenceTest, HandlesMemberFunctionPointer) {
|
|||||||
// Tests that the universal printer prints a member variable pointer
|
// Tests that the universal printer prints a member variable pointer
|
||||||
// passed by reference.
|
// passed by reference.
|
||||||
TEST(PrintReferenceTest, HandlesMemberVariablePointer) {
|
TEST(PrintReferenceTest, HandlesMemberVariablePointer) {
|
||||||
int Foo::*p = &Foo::value; // NOLINT
|
int Foo::* p = &Foo::value; // NOLINT
|
||||||
EXPECT_TRUE(HasPrefix(PrintByRef(p), "@" + PrintPointer(&p) + " " +
|
EXPECT_TRUE(HasPrefix(PrintByRef(p), "@" + PrintPointer(&p) + " " +
|
||||||
Print(sizeof(p)) + "-byte object "));
|
Print(sizeof(p)) + "-byte object "));
|
||||||
}
|
}
|
||||||
@ -1635,7 +1699,7 @@ TEST(PrintToStringTest, ContainsNonLatin) {
|
|||||||
EXPECT_PRINT_TO_STRING_(non_ascii_str,
|
EXPECT_PRINT_TO_STRING_(non_ascii_str,
|
||||||
"\"\\xEC\\x98\\xA4\\xEC\\xA0\\x84 4:30\"\n"
|
"\"\\xEC\\x98\\xA4\\xEC\\xA0\\x84 4:30\"\n"
|
||||||
" As Text: \"오전 4:30\"");
|
" As Text: \"오전 4:30\"");
|
||||||
non_ascii_str = ::std::string("From ä — ẑ");
|
non_ascii_str = "From ä — ẑ";
|
||||||
EXPECT_PRINT_TO_STRING_(non_ascii_str,
|
EXPECT_PRINT_TO_STRING_(non_ascii_str,
|
||||||
"\"From \\xC3\\xA4 \\xE2\\x80\\x94 \\xE1\\xBA\\x91\""
|
"\"From \\xC3\\xA4 \\xE2\\x80\\x94 \\xE1\\xBA\\x91\""
|
||||||
"\n As Text: \"From ä — ẑ\"");
|
"\n As Text: \"From ä — ẑ\"");
|
||||||
@ -1865,6 +1929,22 @@ TEST(UniversalPrintTest, SmartPointers) {
|
|||||||
PrintToString(std::shared_ptr<void>(p.get(), [](void*) {})));
|
PrintToString(std::shared_ptr<void>(p.get(), [](void*) {})));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(UniversalPrintTest, StringViewNonZeroTerminated) {
|
||||||
|
// Craft a non-ASCII UTF-8 input (to trigger a special path in
|
||||||
|
// `ConditionalPrintAsText`). Use array notation instead of the string
|
||||||
|
// literal syntax, to avoid placing a terminating 0 at the end of the input.
|
||||||
|
const char s[] = {'\357', '\243', '\242', 'X'};
|
||||||
|
// Only include the first 3 bytes in the `string_view` and leave the last one
|
||||||
|
// ('X') outside. This way, if the code tries to use `str.data()` with
|
||||||
|
// `strlen` instead of `str.size()`, it will include 'X' and cause a visible
|
||||||
|
// difference (in addition to ASAN tests detecting a buffer overflow due to
|
||||||
|
// the missing 0 at the end).
|
||||||
|
const ::std::string_view str(s, 3);
|
||||||
|
::std::stringstream ss;
|
||||||
|
UniversalPrint(str, &ss);
|
||||||
|
EXPECT_EQ("\"\\xEF\\xA3\\xA2\"\n As Text: \"\xEF\xA3\xA2\"", ss.str());
|
||||||
|
}
|
||||||
|
|
||||||
TEST(UniversalTersePrintTupleFieldsToStringsTestWithStd, PrintsEmptyTuple) {
|
TEST(UniversalTersePrintTupleFieldsToStringsTestWithStd, PrintsEmptyTuple) {
|
||||||
Strings result = UniversalTersePrintTupleFieldsToStrings(::std::make_tuple());
|
Strings result = UniversalTersePrintTupleFieldsToStrings(::std::make_tuple());
|
||||||
EXPECT_EQ(0u, result.size());
|
EXPECT_EQ(0u, result.size());
|
||||||
@ -1894,7 +1974,6 @@ TEST(UniversalTersePrintTupleFieldsToStringsTestWithStd, PrintsTersely) {
|
|||||||
EXPECT_EQ("\"a\"", result[1]);
|
EXPECT_EQ("\"a\"", result[1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
#if GTEST_INTERNAL_HAS_ANY
|
|
||||||
class PrintAnyTest : public ::testing::Test {
|
class PrintAnyTest : public ::testing::Test {
|
||||||
protected:
|
protected:
|
||||||
template <typename T>
|
template <typename T>
|
||||||
@ -1908,12 +1987,12 @@ class PrintAnyTest : public ::testing::Test {
|
|||||||
};
|
};
|
||||||
|
|
||||||
TEST_F(PrintAnyTest, Empty) {
|
TEST_F(PrintAnyTest, Empty) {
|
||||||
internal::Any any;
|
std::any any;
|
||||||
EXPECT_EQ("no value", PrintToString(any));
|
EXPECT_EQ("no value", PrintToString(any));
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(PrintAnyTest, NonEmpty) {
|
TEST_F(PrintAnyTest, NonEmpty) {
|
||||||
internal::Any any;
|
std::any any;
|
||||||
constexpr int val1 = 10;
|
constexpr int val1 = 10;
|
||||||
const std::string val2 = "content";
|
const std::string val2 = "content";
|
||||||
|
|
||||||
@ -1924,27 +2003,23 @@ TEST_F(PrintAnyTest, NonEmpty) {
|
|||||||
EXPECT_EQ("value of type " + ExpectedTypeName<std::string>(),
|
EXPECT_EQ("value of type " + ExpectedTypeName<std::string>(),
|
||||||
PrintToString(any));
|
PrintToString(any));
|
||||||
}
|
}
|
||||||
#endif // GTEST_INTERNAL_HAS_ANY
|
|
||||||
|
|
||||||
#if GTEST_INTERNAL_HAS_OPTIONAL
|
|
||||||
TEST(PrintOptionalTest, Basic) {
|
TEST(PrintOptionalTest, Basic) {
|
||||||
EXPECT_EQ("(nullopt)", PrintToString(internal::Nullopt()));
|
EXPECT_EQ("(nullopt)", PrintToString(std::nullopt));
|
||||||
internal::Optional<int> value;
|
std::optional<int> value;
|
||||||
EXPECT_EQ("(nullopt)", PrintToString(value));
|
EXPECT_EQ("(nullopt)", PrintToString(value));
|
||||||
value = {7};
|
value = {7};
|
||||||
EXPECT_EQ("(7)", PrintToString(value));
|
EXPECT_EQ("(7)", PrintToString(value));
|
||||||
EXPECT_EQ("(1.1)", PrintToString(internal::Optional<double>{1.1}));
|
EXPECT_EQ("(1.1)", PrintToString(std::optional<double>{1.1}));
|
||||||
EXPECT_EQ("(\"A\")", PrintToString(internal::Optional<std::string>{"A"}));
|
EXPECT_EQ("(\"A\")", PrintToString(std::optional<std::string>{"A"}));
|
||||||
}
|
}
|
||||||
#endif // GTEST_INTERNAL_HAS_OPTIONAL
|
|
||||||
|
|
||||||
#if GTEST_INTERNAL_HAS_VARIANT
|
|
||||||
struct NonPrintable {
|
struct NonPrintable {
|
||||||
unsigned char contents = 17;
|
unsigned char contents = 17;
|
||||||
};
|
};
|
||||||
|
|
||||||
TEST(PrintOneofTest, Basic) {
|
TEST(PrintOneofTest, Basic) {
|
||||||
using Type = internal::Variant<int, StreamableInGlobal, NonPrintable>;
|
using Type = std::variant<int, StreamableInGlobal, NonPrintable>;
|
||||||
EXPECT_EQ("('int(index = 0)' with value 7)", PrintToString(Type(7)));
|
EXPECT_EQ("('int(index = 0)' with value 7)", PrintToString(Type(7)));
|
||||||
EXPECT_EQ("('StreamableInGlobal(index = 1)' with value StreamableInGlobal)",
|
EXPECT_EQ("('StreamableInGlobal(index = 1)' with value StreamableInGlobal)",
|
||||||
PrintToString(Type(StreamableInGlobal{})));
|
PrintToString(Type(StreamableInGlobal{})));
|
||||||
@ -1953,7 +2028,26 @@ TEST(PrintOneofTest, Basic) {
|
|||||||
"1-byte object <11>)",
|
"1-byte object <11>)",
|
||||||
PrintToString(Type(NonPrintable{})));
|
PrintToString(Type(NonPrintable{})));
|
||||||
}
|
}
|
||||||
#endif // GTEST_INTERNAL_HAS_VARIANT
|
|
||||||
|
#if GTEST_INTERNAL_HAS_COMPARE_LIB
|
||||||
|
TEST(PrintOrderingTest, Basic) {
|
||||||
|
EXPECT_EQ("(less)", PrintToString(std::strong_ordering::less));
|
||||||
|
EXPECT_EQ("(greater)", PrintToString(std::strong_ordering::greater));
|
||||||
|
// equal == equivalent for strong_ordering.
|
||||||
|
EXPECT_EQ("(equal)", PrintToString(std::strong_ordering::equivalent));
|
||||||
|
EXPECT_EQ("(equal)", PrintToString(std::strong_ordering::equal));
|
||||||
|
|
||||||
|
EXPECT_EQ("(less)", PrintToString(std::weak_ordering::less));
|
||||||
|
EXPECT_EQ("(greater)", PrintToString(std::weak_ordering::greater));
|
||||||
|
EXPECT_EQ("(equivalent)", PrintToString(std::weak_ordering::equivalent));
|
||||||
|
|
||||||
|
EXPECT_EQ("(less)", PrintToString(std::partial_ordering::less));
|
||||||
|
EXPECT_EQ("(greater)", PrintToString(std::partial_ordering::greater));
|
||||||
|
EXPECT_EQ("(equivalent)", PrintToString(std::partial_ordering::equivalent));
|
||||||
|
EXPECT_EQ("(unordered)", PrintToString(std::partial_ordering::unordered));
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
class string_ref;
|
class string_ref;
|
||||||
|
|
||||||
|
|||||||
@ -31,14 +31,14 @@
|
|||||||
|
|
||||||
class SetupFailTest : public ::testing::Test {
|
class SetupFailTest : public ::testing::Test {
|
||||||
protected:
|
protected:
|
||||||
static void SetUpTestSuite() { ASSERT_EQ("", "SET_UP_FAIL"); }
|
static void SetUpTestSuite() { ASSERT_STREQ("", "SET_UP_FAIL"); }
|
||||||
};
|
};
|
||||||
|
|
||||||
TEST_F(SetupFailTest, NoopPassingTest) {}
|
TEST_F(SetupFailTest, NoopPassingTest) {}
|
||||||
|
|
||||||
class TearDownFailTest : public ::testing::Test {
|
class TearDownFailTest : public ::testing::Test {
|
||||||
protected:
|
protected:
|
||||||
static void TearDownTestSuite() { ASSERT_EQ("", "TEAR_DOWN_FAIL"); }
|
static void TearDownTestSuite() { ASSERT_STREQ("", "TEAR_DOWN_FAIL"); }
|
||||||
};
|
};
|
||||||
|
|
||||||
TEST_F(TearDownFailTest, NoopPassingTest) {}
|
TEST_F(TearDownFailTest, NoopPassingTest) {}
|
||||||
|
|||||||
@ -40,16 +40,21 @@ namespace {
|
|||||||
|
|
||||||
enum FailureType { NO_FAILURE, NON_FATAL_FAILURE, FATAL_FAILURE };
|
enum FailureType { NO_FAILURE, NON_FATAL_FAILURE, FATAL_FAILURE };
|
||||||
|
|
||||||
|
// Was SetUp run?
|
||||||
|
bool set_up_was_run;
|
||||||
|
// Was TearDown run?
|
||||||
|
bool tear_down_was_run;
|
||||||
|
// Was the TEST run?
|
||||||
|
bool test_was_run;
|
||||||
|
|
||||||
// For testing using global test environments.
|
// For testing using global test environments.
|
||||||
class MyEnvironment : public testing::Environment {
|
class MyEnvironment : public testing::Environment {
|
||||||
public:
|
public:
|
||||||
MyEnvironment() { Reset(); }
|
|
||||||
|
|
||||||
// Depending on the value of failure_in_set_up_, SetUp() will
|
// Depending on the value of failure_in_set_up_, SetUp() will
|
||||||
// generate a non-fatal failure, generate a fatal failure, or
|
// generate a non-fatal failure, generate a fatal failure, or
|
||||||
// succeed.
|
// succeed.
|
||||||
void SetUp() override {
|
void SetUp() override {
|
||||||
set_up_was_run_ = true;
|
set_up_was_run = true;
|
||||||
|
|
||||||
switch (failure_in_set_up_) {
|
switch (failure_in_set_up_) {
|
||||||
case NON_FATAL_FAILURE:
|
case NON_FATAL_FAILURE:
|
||||||
@ -65,36 +70,18 @@ class MyEnvironment : public testing::Environment {
|
|||||||
|
|
||||||
// Generates a non-fatal failure.
|
// Generates a non-fatal failure.
|
||||||
void TearDown() override {
|
void TearDown() override {
|
||||||
tear_down_was_run_ = true;
|
tear_down_was_run = true;
|
||||||
ADD_FAILURE() << "Expected non-fatal failure in global tear-down.";
|
ADD_FAILURE() << "Expected non-fatal failure in global tear-down.";
|
||||||
}
|
}
|
||||||
|
|
||||||
// Resets the state of the environment s.t. it can be reused.
|
|
||||||
void Reset() {
|
|
||||||
failure_in_set_up_ = NO_FAILURE;
|
|
||||||
set_up_was_run_ = false;
|
|
||||||
tear_down_was_run_ = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// We call this function to set the type of failure SetUp() should
|
// We call this function to set the type of failure SetUp() should
|
||||||
// generate.
|
// generate.
|
||||||
void set_failure_in_set_up(FailureType type) { failure_in_set_up_ = type; }
|
void set_failure_in_set_up(FailureType type) { failure_in_set_up_ = type; }
|
||||||
|
|
||||||
// Was SetUp() run?
|
|
||||||
bool set_up_was_run() const { return set_up_was_run_; }
|
|
||||||
|
|
||||||
// Was TearDown() run?
|
|
||||||
bool tear_down_was_run() const { return tear_down_was_run_; }
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
FailureType failure_in_set_up_;
|
FailureType failure_in_set_up_;
|
||||||
bool set_up_was_run_;
|
|
||||||
bool tear_down_was_run_;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Was the TEST run?
|
|
||||||
bool test_was_run;
|
|
||||||
|
|
||||||
// The sole purpose of this TEST is to enable us to check whether it
|
// The sole purpose of this TEST is to enable us to check whether it
|
||||||
// was run.
|
// was run.
|
||||||
TEST(FooTest, Bar) { test_was_run = true; }
|
TEST(FooTest, Bar) { test_was_run = true; }
|
||||||
@ -112,67 +99,88 @@ void Check(bool condition, const char* msg) {
|
|||||||
// The 'failure' parameter specifies the type of failure that should
|
// The 'failure' parameter specifies the type of failure that should
|
||||||
// be generated by the global set-up.
|
// be generated by the global set-up.
|
||||||
int RunAllTests(MyEnvironment* env, FailureType failure) {
|
int RunAllTests(MyEnvironment* env, FailureType failure) {
|
||||||
env->Reset();
|
set_up_was_run = false;
|
||||||
env->set_failure_in_set_up(failure);
|
tear_down_was_run = false;
|
||||||
test_was_run = false;
|
test_was_run = false;
|
||||||
|
env->set_failure_in_set_up(failure);
|
||||||
testing::internal::GetUnitTestImpl()->ClearAdHocTestResult();
|
testing::internal::GetUnitTestImpl()->ClearAdHocTestResult();
|
||||||
return RUN_ALL_TESTS();
|
return RUN_ALL_TESTS();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Registers a global test environment, and verifies that the
|
||||||
|
// registration function returns its argument.
|
||||||
|
MyEnvironment* RegisterTestEnv() {
|
||||||
|
MyEnvironment* const env = new MyEnvironment;
|
||||||
|
Check(testing::AddGlobalTestEnvironment(env) == env,
|
||||||
|
"AddGlobalTestEnvironment() should return its argument.");
|
||||||
|
return env;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verifies that RUN_ALL_TESTS() runs the tests when the global
|
||||||
|
// set-up is successful.
|
||||||
|
void TestGlobalSetUp() {
|
||||||
|
MyEnvironment* const env = RegisterTestEnv();
|
||||||
|
Check(RunAllTests(env, NO_FAILURE) != 0,
|
||||||
|
"RUN_ALL_TESTS() should return non-zero, as the global tear-down "
|
||||||
|
"should generate a failure.");
|
||||||
|
Check(test_was_run,
|
||||||
|
"The tests should run, as the global set-up should generate no "
|
||||||
|
"failure");
|
||||||
|
Check(tear_down_was_run,
|
||||||
|
"The global tear-down should run, as the global set-up was run.");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verifies that RUN_ALL_TESTS() runs the tests when the global
|
||||||
|
// set-up generates no fatal failure.
|
||||||
|
void TestTestsRun() {
|
||||||
|
MyEnvironment* const env = RegisterTestEnv();
|
||||||
|
Check(RunAllTests(env, NON_FATAL_FAILURE) != 0,
|
||||||
|
"RUN_ALL_TESTS() should return non-zero, as both the global set-up "
|
||||||
|
"and the global tear-down should generate a non-fatal failure.");
|
||||||
|
Check(test_was_run,
|
||||||
|
"The tests should run, as the global set-up should generate no "
|
||||||
|
"fatal failure.");
|
||||||
|
Check(tear_down_was_run,
|
||||||
|
"The global tear-down should run, as the global set-up was run.");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verifies that RUN_ALL_TESTS() runs no test when the global set-up
|
||||||
|
// generates a fatal failure.
|
||||||
|
void TestNoTestsRunSetUpFailure() {
|
||||||
|
MyEnvironment* const env = RegisterTestEnv();
|
||||||
|
Check(RunAllTests(env, FATAL_FAILURE) != 0,
|
||||||
|
"RUN_ALL_TESTS() should return non-zero, as the global set-up "
|
||||||
|
"should generate a fatal failure.");
|
||||||
|
Check(!test_was_run,
|
||||||
|
"The tests should not run, as the global set-up should generate "
|
||||||
|
"a fatal failure.");
|
||||||
|
Check(tear_down_was_run,
|
||||||
|
"The global tear-down should run, as the global set-up was run.");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verifies that RUN_ALL_TESTS() doesn't do global set-up or
|
||||||
|
// tear-down when there is no test to run.
|
||||||
|
void TestNoTestsSkipsSetUp() {
|
||||||
|
MyEnvironment* const env = RegisterTestEnv();
|
||||||
|
GTEST_FLAG_SET(filter, "-*");
|
||||||
|
Check(RunAllTests(env, NO_FAILURE) == 0,
|
||||||
|
"RUN_ALL_TESTS() should return zero, as there is no test to run.");
|
||||||
|
Check(!set_up_was_run,
|
||||||
|
"The global set-up should not run, as there is no test to run.");
|
||||||
|
Check(!tear_down_was_run,
|
||||||
|
"The global tear-down should not run, "
|
||||||
|
"as the global set-up was not run.");
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
testing::InitGoogleTest(&argc, argv);
|
testing::InitGoogleTest(&argc, argv);
|
||||||
|
|
||||||
// Registers a global test environment, and verifies that the
|
TestGlobalSetUp();
|
||||||
// registration function returns its argument.
|
TestTestsRun();
|
||||||
MyEnvironment* const env = new MyEnvironment;
|
TestNoTestsRunSetUpFailure();
|
||||||
Check(testing::AddGlobalTestEnvironment(env) == env,
|
TestNoTestsSkipsSetUp();
|
||||||
"AddGlobalTestEnvironment() should return its argument.");
|
|
||||||
|
|
||||||
// Verifies that RUN_ALL_TESTS() runs the tests when the global
|
|
||||||
// set-up is successful.
|
|
||||||
Check(RunAllTests(env, NO_FAILURE) != 0,
|
|
||||||
"RUN_ALL_TESTS() should return non-zero, as the global tear-down "
|
|
||||||
"should generate a failure.");
|
|
||||||
Check(test_was_run,
|
|
||||||
"The tests should run, as the global set-up should generate no "
|
|
||||||
"failure");
|
|
||||||
Check(env->tear_down_was_run(),
|
|
||||||
"The global tear-down should run, as the global set-up was run.");
|
|
||||||
|
|
||||||
// Verifies that RUN_ALL_TESTS() runs the tests when the global
|
|
||||||
// set-up generates no fatal failure.
|
|
||||||
Check(RunAllTests(env, NON_FATAL_FAILURE) != 0,
|
|
||||||
"RUN_ALL_TESTS() should return non-zero, as both the global set-up "
|
|
||||||
"and the global tear-down should generate a non-fatal failure.");
|
|
||||||
Check(test_was_run,
|
|
||||||
"The tests should run, as the global set-up should generate no "
|
|
||||||
"fatal failure.");
|
|
||||||
Check(env->tear_down_was_run(),
|
|
||||||
"The global tear-down should run, as the global set-up was run.");
|
|
||||||
|
|
||||||
// Verifies that RUN_ALL_TESTS() runs no test when the global set-up
|
|
||||||
// generates a fatal failure.
|
|
||||||
Check(RunAllTests(env, FATAL_FAILURE) != 0,
|
|
||||||
"RUN_ALL_TESTS() should return non-zero, as the global set-up "
|
|
||||||
"should generate a fatal failure.");
|
|
||||||
Check(!test_was_run,
|
|
||||||
"The tests should not run, as the global set-up should generate "
|
|
||||||
"a fatal failure.");
|
|
||||||
Check(env->tear_down_was_run(),
|
|
||||||
"The global tear-down should run, as the global set-up was run.");
|
|
||||||
|
|
||||||
// Verifies that RUN_ALL_TESTS() doesn't do global set-up or
|
|
||||||
// tear-down when there is no test to run.
|
|
||||||
GTEST_FLAG_SET(filter, "-*");
|
|
||||||
Check(RunAllTests(env, NO_FAILURE) == 0,
|
|
||||||
"RUN_ALL_TESTS() should return zero, as there is no test to run.");
|
|
||||||
Check(!env->set_up_was_run(),
|
|
||||||
"The global set-up should not run, as there is no test to run.");
|
|
||||||
Check(!env->tear_down_was_run(),
|
|
||||||
"The global tear-down should not run, "
|
|
||||||
"as the global set-up was not run.");
|
|
||||||
|
|
||||||
printf("PASS\n");
|
printf("PASS\n");
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
@ -43,11 +43,22 @@ import sys
|
|||||||
from googletest.test import gtest_test_utils
|
from googletest.test import gtest_test_utils
|
||||||
|
|
||||||
|
|
||||||
|
FREEBSD = ('FreeBSD', 'GNU/kFreeBSD')
|
||||||
|
NETBSD = ('NetBSD',)
|
||||||
|
OPENBSD = ('OpenBSD',)
|
||||||
|
|
||||||
|
|
||||||
|
def is_bsd_based_os() -> bool:
|
||||||
|
"""Determine whether or not the OS is BSD-based."""
|
||||||
|
if os.name != 'posix':
|
||||||
|
return False
|
||||||
|
|
||||||
|
return os.uname()[0] in (FREEBSD + NETBSD + OPENBSD)
|
||||||
|
|
||||||
|
|
||||||
IS_DARWIN = os.name == 'posix' and os.uname()[0] == 'Darwin'
|
IS_DARWIN = os.name == 'posix' and os.uname()[0] == 'Darwin'
|
||||||
IS_LINUX = os.name == 'posix' and os.uname()[0] == 'Linux'
|
IS_LINUX = os.name == 'posix' and os.uname()[0] == 'Linux'
|
||||||
IS_GNUHURD = os.name == 'posix' and os.uname()[0] == 'GNU'
|
IS_GNUHURD = os.name == 'posix' and os.uname()[0] == 'GNU'
|
||||||
IS_GNUKFREEBSD = os.name == 'posix' and os.uname()[0] == 'GNU/kFreeBSD'
|
|
||||||
IS_OPENBSD = os.name == 'posix' and os.uname()[0] == 'OpenBSD'
|
|
||||||
IS_WINDOWS = os.name == 'nt'
|
IS_WINDOWS = os.name == 'nt'
|
||||||
|
|
||||||
PROGRAM_PATH = gtest_test_utils.GetTestExecutablePath('gtest_help_test_')
|
PROGRAM_PATH = gtest_test_utils.GetTestExecutablePath('gtest_help_test_')
|
||||||
@ -96,7 +107,7 @@ HELP_REGEX = re.compile(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def RunWithFlag(flag):
|
def run_with_flag(flag):
|
||||||
"""Runs gtest_help_test_ with the given flag.
|
"""Runs gtest_help_test_ with the given flag.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
@ -116,17 +127,14 @@ def RunWithFlag(flag):
|
|||||||
class GTestHelpTest(gtest_test_utils.TestCase):
|
class GTestHelpTest(gtest_test_utils.TestCase):
|
||||||
"""Tests the --help flag and its equivalent forms."""
|
"""Tests the --help flag and its equivalent forms."""
|
||||||
|
|
||||||
def TestHelpFlag(self, flag):
|
def test_prints_help_with_full_flag(self):
|
||||||
"""Verifies correct behavior when help flag is specified.
|
"""Verifies correct behavior when help flag is specified.
|
||||||
|
|
||||||
The right message must be printed and the tests must
|
The right message must be printed and the tests must
|
||||||
skipped when the given flag is specified.
|
skipped when the given flag is specified.
|
||||||
|
|
||||||
Args:
|
|
||||||
flag: A flag to pass to the binary or None.
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
exit_code, output = RunWithFlag(flag)
|
exit_code, output = run_with_flag('--help')
|
||||||
if HAS_ABSL_FLAGS:
|
if HAS_ABSL_FLAGS:
|
||||||
# The Abseil flags library prints the ProgramUsageMessage() with
|
# The Abseil flags library prints the ProgramUsageMessage() with
|
||||||
# --help and returns 1.
|
# --help and returns 1.
|
||||||
@ -136,7 +144,7 @@ class GTestHelpTest(gtest_test_utils.TestCase):
|
|||||||
|
|
||||||
self.assertTrue(HELP_REGEX.search(output), output)
|
self.assertTrue(HELP_REGEX.search(output), output)
|
||||||
|
|
||||||
if IS_DARWIN or IS_LINUX or IS_GNUHURD or IS_GNUKFREEBSD or IS_OPENBSD:
|
if IS_DARWIN or IS_LINUX or IS_GNUHURD or is_bsd_based_os():
|
||||||
self.assertIn(STREAM_RESULT_TO_FLAG, output)
|
self.assertIn(STREAM_RESULT_TO_FLAG, output)
|
||||||
else:
|
else:
|
||||||
self.assertNotIn(STREAM_RESULT_TO_FLAG, output)
|
self.assertNotIn(STREAM_RESULT_TO_FLAG, output)
|
||||||
@ -146,53 +154,27 @@ class GTestHelpTest(gtest_test_utils.TestCase):
|
|||||||
else:
|
else:
|
||||||
self.assertNotIn(DEATH_TEST_STYLE_FLAG, output)
|
self.assertNotIn(DEATH_TEST_STYLE_FLAG, output)
|
||||||
|
|
||||||
def TestUnknownFlagWithAbseil(self, flag):
|
def test_runs_tests_without_help_flag(self):
|
||||||
"""Verifies correct behavior when an unknown flag is specified.
|
|
||||||
|
|
||||||
The right message must be printed and the tests must
|
|
||||||
skipped when the given flag is specified.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
flag: A flag to pass to the binary or None.
|
|
||||||
"""
|
|
||||||
exit_code, output = RunWithFlag(flag)
|
|
||||||
self.assertEqual(1, exit_code)
|
|
||||||
self.assertIn('ERROR: Unknown command line flag', output)
|
|
||||||
|
|
||||||
def TestNonHelpFlag(self, flag):
|
|
||||||
"""Verifies correct behavior when no help flag is specified.
|
"""Verifies correct behavior when no help flag is specified.
|
||||||
|
|
||||||
Verifies that when no help flag is specified, the tests are run
|
Verifies that when no help flag is specified, the tests are run
|
||||||
and the help message is not printed.
|
and the help message is not printed.
|
||||||
|
|
||||||
Args:
|
|
||||||
flag: A flag to pass to the binary or None.
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
exit_code, output = RunWithFlag(flag)
|
exit_code, output = run_with_flag(None)
|
||||||
self.assertNotEqual(exit_code, 0)
|
self.assertNotEqual(exit_code, 0)
|
||||||
self.assertFalse(HELP_REGEX.search(output), output)
|
self.assertFalse(HELP_REGEX.search(output), output)
|
||||||
|
|
||||||
def testPrintsHelpWithFullFlag(self):
|
def test_runs_tests_with_gtest_internal_flag(self):
|
||||||
self.TestHelpFlag('--help')
|
|
||||||
|
|
||||||
def testRunsTestsWithoutHelpFlag(self):
|
|
||||||
"""Verifies correct behavior when no help flag is specified.
|
|
||||||
|
|
||||||
Verifies that when no help flag is specified, the tests are run
|
|
||||||
and the help message is not printed.
|
|
||||||
"""
|
|
||||||
|
|
||||||
self.TestNonHelpFlag(None)
|
|
||||||
|
|
||||||
def testRunsTestsWithGtestInternalFlag(self):
|
|
||||||
"""Verifies correct behavior when internal testing flag is specified.
|
"""Verifies correct behavior when internal testing flag is specified.
|
||||||
|
|
||||||
Verifies that the tests are run and no help message is printed when
|
Verifies that the tests are run and no help message is printed when
|
||||||
a flag starting with Google Test prefix and 'internal_' is supplied.
|
a flag starting with Google Test prefix and 'internal_' is supplied.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
self.TestNonHelpFlag(INTERNAL_FLAG_FOR_TESTING)
|
exit_code, output = run_with_flag(INTERNAL_FLAG_FOR_TESTING)
|
||||||
|
self.assertNotEqual(exit_code, 0)
|
||||||
|
self.assertFalse(HELP_REGEX.search(output), output)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|||||||
@ -51,6 +51,9 @@ def normalize(obj):
|
|||||||
elif key == 'failure':
|
elif key == 'failure':
|
||||||
value = re.sub(r'^.*[/\\](.*:)\d+\n', '\\1*\n', value)
|
value = re.sub(r'^.*[/\\](.*:)\d+\n', '\\1*\n', value)
|
||||||
return re.sub(r'Stack trace:\n(.|\n)*', 'Stack trace:\n*', value)
|
return re.sub(r'Stack trace:\n(.|\n)*', 'Stack trace:\n*', value)
|
||||||
|
elif key == 'message':
|
||||||
|
value = re.sub(r'^.*[/\\](.*:)\d+\n', '\\1*\n', value)
|
||||||
|
return re.sub(r'Stack trace:\n(.|\n)*', 'Stack trace:\n*', value)
|
||||||
elif key == 'file':
|
elif key == 'file':
|
||||||
return re.sub(r'^.*[/\\](.*)', '\\1', value)
|
return re.sub(r'^.*[/\\](.*)', '\\1', value)
|
||||||
else:
|
else:
|
||||||
|
|||||||
@ -61,7 +61,6 @@ int g_environment_tear_down_count = 0;
|
|||||||
|
|
||||||
class MyEnvironment : public testing::Environment {
|
class MyEnvironment : public testing::Environment {
|
||||||
public:
|
public:
|
||||||
MyEnvironment() = default;
|
|
||||||
void SetUp() override { g_environment_set_up_count++; }
|
void SetUp() override { g_environment_set_up_count++; }
|
||||||
void TearDown() override { g_environment_tear_down_count++; }
|
void TearDown() override { g_environment_tear_down_count++; }
|
||||||
};
|
};
|
||||||
@ -117,6 +116,7 @@ void ResetCounts() {
|
|||||||
g_should_pass_count = 0;
|
g_should_pass_count = 0;
|
||||||
g_death_test_count = 0;
|
g_death_test_count = 0;
|
||||||
g_param_test_count = 0;
|
g_param_test_count = 0;
|
||||||
|
testing::AddGlobalTestEnvironment(new MyEnvironment);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Checks that the count for each test is expected.
|
// Checks that the count for each test is expected.
|
||||||
@ -197,8 +197,6 @@ void TestRepeatWithFilterForFailedTests(int repeat) {
|
|||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
testing::InitGoogleTest(&argc, argv);
|
testing::InitGoogleTest(&argc, argv);
|
||||||
|
|
||||||
testing::AddGlobalTestEnvironment(new MyEnvironment);
|
|
||||||
|
|
||||||
TestRepeatUnspecified();
|
TestRepeatUnspecified();
|
||||||
TestRepeat(0);
|
TestRepeat(0);
|
||||||
TestRepeat(1);
|
TestRepeat(1);
|
||||||
|
|||||||
@ -95,9 +95,9 @@ void ManyAsserts(int id) {
|
|||||||
|
|
||||||
// RecordProperty() should interact safely with other threads as well.
|
// RecordProperty() should interact safely with other threads as well.
|
||||||
// The shared_key forces property updates.
|
// The shared_key forces property updates.
|
||||||
Test::RecordProperty(IdToKey(id, "string").c_str(), IdToString(id).c_str());
|
Test::RecordProperty(IdToKey(id, "string"), IdToString(id));
|
||||||
Test::RecordProperty(IdToKey(id, "int").c_str(), id);
|
Test::RecordProperty(IdToKey(id, "int").c_str(), id);
|
||||||
Test::RecordProperty("shared_key", IdToString(id).c_str());
|
Test::RecordProperty("shared_key", IdToString(id));
|
||||||
|
|
||||||
// This assertion should fail kThreadCount times per thread. It
|
// This assertion should fail kThreadCount times per thread. It
|
||||||
// is for testing whether Google Test can handle failed assertions in a
|
// is for testing whether Google Test can handle failed assertions in a
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user