• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef BASE_TEST_GTEST_UTIL_H_
6 #define BASE_TEST_GTEST_UTIL_H_
7 
8 #include <string>
9 #include <utility>
10 #include <vector>
11 
12 #include "base/check.h"
13 #include "base/debug/debugging_buildflags.h"
14 #include "build/build_config.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 
17 // EXPECT/ASSERT_DCHECK_DEATH is intended to replace EXPECT/ASSERT_DEBUG_DEATH
18 // when the death is expected to be caused by a DCHECK. Contrary to
19 // EXPECT/ASSERT_DEBUG_DEATH however, it doesn't execute the statement in non-
20 // dcheck builds as DCHECKs are intended to catch things that should never
21 // happen and as such executing the statement results in undefined behavior
22 // (|statement| is compiled in unsupported configurations nonetheless).
23 // DCHECK_IS_CONFIGURABLE is excluded from DCHECK_DEATH because it's non-FATAL
24 // by default and there are no known tests that configure a FATAL level. If this
25 // gets used from FATAL contexts under DCHECK_IS_CONFIGURABLE this may need to
26 // be updated to look at LOGGING_DCHECK's current severity level.
27 // Death tests misbehave on Android.
28 #if DCHECK_IS_ON() && defined(GTEST_HAS_DEATH_TEST) && \
29     !BUILDFLAG(DCHECK_IS_CONFIGURABLE) && !BUILDFLAG(IS_ANDROID)
30 
31 // EXPECT/ASSERT_DCHECK_DEATH tests verify that a DCHECK is hit ("Check failed"
32 // is part of the error message). Optionally you may specify part of the message
33 // to verify which DCHECK (or LOG(DFATAL)) is being hit.
34 #define EXPECT_DCHECK_DEATH(statement) EXPECT_DEATH(statement, "Check failed")
35 #define EXPECT_DCHECK_DEATH_WITH(statement, msg) EXPECT_DEATH(statement, msg)
36 #define ASSERT_DCHECK_DEATH(statement) ASSERT_DEATH(statement, "Check failed")
37 #define ASSERT_DCHECK_DEATH_WITH(statement, msg) ASSERT_DEATH(statement, msg)
38 
39 #else
40 
41 #define EXPECT_DCHECK_DEATH(statement) \
42   GTEST_UNSUPPORTED_DEATH_TEST(statement, "Check failed", )
43 #define EXPECT_DCHECK_DEATH_WITH(statement, msg) \
44   GTEST_UNSUPPORTED_DEATH_TEST(statement, msg, )
45 #define ASSERT_DCHECK_DEATH(statement) \
46   GTEST_UNSUPPORTED_DEATH_TEST(statement, "Check failed", return )
47 #define ASSERT_DCHECK_DEATH_WITH(statement, msg) \
48   GTEST_UNSUPPORTED_DEATH_TEST(statement, msg, return )
49 
50 #endif  // DCHECK_IS_ON() && defined(GTEST_HAS_DEATH_TEST) &&
51         // !BUILDFLAG(DCHECK_IS_CONFIGURABLE) && !BUILDFLAG(IS_ANDROID)
52 
53 // As above, but for CHECK().
54 #if defined(GTEST_HAS_DEATH_TEST) && !BUILDFLAG(IS_ANDROID)
55 
56 #if CHECK_WILL_STREAM()
57 #define EXPECT_CHECK_DEATH(statement) EXPECT_DEATH(statement, "Check failed")
58 #define EXPECT_CHECK_DEATH_WITH(statement, msg) EXPECT_DEATH(statement, msg)
59 #define ASSERT_CHECK_DEATH(statement) ASSERT_DEATH(statement, "Check failed")
60 #define EXPECT_NOTREACHED_DEATH(statement) \
61   EXPECT_DEATH(statement, "NOTREACHED hit")
62 #define ASSERT_NOTREACHED_DEATH(statement) \
63   ASSERT_DEATH(statement, "NOTREACHED hit")
64 #else
65 #define EXPECT_CHECK_DEATH(statement) EXPECT_DEATH(statement, "")
66 #define EXPECT_CHECK_DEATH_WITH(statement, msg) EXPECT_DEATH(statement, "")
67 #define ASSERT_CHECK_DEATH(statement) ASSERT_DEATH(statement, "")
68 #define EXPECT_NOTREACHED_DEATH(statement) EXPECT_DEATH(statement, "")
69 #define ASSERT_NOTREACHED_DEATH(statement) ASSERT_DEATH(statement, "")
70 #endif  // CHECK_WILL_STREAM()
71 
72 #else  // defined(GTEST_HAS_DEATH_TEST) && !BUILDFLAG(IS_ANDROID)
73 
74 // Note GTEST_UNSUPPORTED_DEATH_TEST takes a |regex| only to see whether it is a
75 // valid regex. It is never evaluated.
76 #define EXPECT_CHECK_DEATH(statement) \
77   GTEST_UNSUPPORTED_DEATH_TEST(statement, "", )
78 #define EXPECT_CHECK_DEATH_WITH(statement, msg) \
79   GTEST_UNSUPPORTED_DEATH_TEST(statement, "", )
80 #define ASSERT_CHECK_DEATH(statement) \
81   GTEST_UNSUPPORTED_DEATH_TEST(statement, "", return )
82 #define EXPECT_NOTREACHED_DEATH(statement) \
83   GTEST_UNSUPPORTED_DEATH_TEST(statement, "", )
84 #define ASSERT_NOTREACHED_DEATH(statement) \
85   GTEST_UNSUPPORTED_DEATH_TEST(statement, "", return)
86 
87 #endif  // defined(GTEST_HAS_DEATH_TEST) && !BUILDFLAG(IS_ANDROID)
88 
89 // `BASE_EXPECT_DEATH` is similar to gtest's `EXPECT_DEATH_IF_SUPPORTED`. It
90 // takes into account that Android does not support them.
91 #if defined(GTEST_HAS_DEATH_TEST) && !BUILDFLAG(IS_ANDROID)
92 
93 #define BASE_EXPECT_DEATH EXPECT_DEATH
94 
95 #else  // defined(GTEST_HAS_DEATH_TEST) && !BUILDFLAG(IS_ANDROID)
96 
97 #define BASE_EXPECT_DEATH(statement, matcher) \
98   GTEST_UNSUPPORTED_DEATH_TEST(statement, "", )
99 
100 #endif  // defined(GTEST_HAS_DEATH_TEST) && !BUILDFLAG(IS_ANDROID)
101 
102 namespace base {
103 
104 class FilePath;
105 
106 struct TestIdentifier {
107   TestIdentifier();
108   TestIdentifier(const TestIdentifier& other);
109   TestIdentifier& operator=(const TestIdentifier& other);
110 
111   std::string test_case_name;
112   std::string test_name;
113   std::string file;
114   int line;
115 };
116 
117 // Constructs a full test name given a test case name and a test name,
118 // e.g. for test case "A" and test name "B" returns "A.B".
119 std::string FormatFullTestName(const std::string& test_case_name,
120                                const std::string& test_name);
121 
122 // Returns the full test name with the "DISABLED_" prefix stripped out.
123 // e.g. for the full test names "A.DISABLED_B", "DISABLED_A.B", and
124 // "DISABLED_A.DISABLED_B", returns "A.B".
125 std::string TestNameWithoutDisabledPrefix(const std::string& full_test_name);
126 
127 // Returns a vector of gtest-based tests compiled into
128 // current executable.
129 std::vector<TestIdentifier> GetCompiledInTests();
130 
131 // Writes the list of gtest-based tests compiled into
132 // current executable as a JSON file. Returns true on success.
133 [[nodiscard]] bool WriteCompiledInTestsToFile(const FilePath& path);
134 
135 // Reads the list of gtest-based tests from |path| into |output|.
136 // Returns true on success.
137 [[nodiscard]] bool ReadTestNamesFromFile(const FilePath& path,
138                                          std::vector<TestIdentifier>* output);
139 
140 }  // namespace base
141 
142 #endif  // BASE_TEST_GTEST_UTIL_H_
143