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 #else 61 #define EXPECT_CHECK_DEATH(statement) EXPECT_DEATH(statement, "") 62 #define EXPECT_CHECK_DEATH_WITH(statement, msg) EXPECT_DEATH(statement, "") 63 #define ASSERT_CHECK_DEATH(statement) ASSERT_DEATH(statement, "") 64 #endif // CHECK_WILL_STREAM() 65 66 #else // defined(GTEST_HAS_DEATH_TEST) && !BUILDFLAG(IS_ANDROID) 67 68 // Note GTEST_UNSUPPORTED_DEATH_TEST takes a |regex| only to see whether it is a 69 // valid regex. It is never evaluated. 70 #define EXPECT_CHECK_DEATH(statement) \ 71 GTEST_UNSUPPORTED_DEATH_TEST(statement, "", ) 72 #define EXPECT_CHECK_DEATH_WITH(statement, msg) \ 73 GTEST_UNSUPPORTED_DEATH_TEST(statement, "", ) 74 #define ASSERT_CHECK_DEATH(statement) \ 75 GTEST_UNSUPPORTED_DEATH_TEST(statement, "", return ) 76 77 #endif // defined(GTEST_HAS_DEATH_TEST) && !BUILDFLAG(IS_ANDROID) 78 79 // `BASE_EXPECT_DEATH` is similar to gtest's `EXPECT_DEATH_IF_SUPPORTED`. It 80 // takes into account that Android does not support them. 81 #if defined(GTEST_HAS_DEATH_TEST) && !BUILDFLAG(IS_ANDROID) 82 83 #define BASE_EXPECT_DEATH EXPECT_DEATH 84 85 #else // defined(GTEST_HAS_DEATH_TEST) && !BUILDFLAG(IS_ANDROID) 86 87 #define BASE_EXPECT_DEATH(statement, matcher) \ 88 GTEST_UNSUPPORTED_DEATH_TEST(statement, "", ) 89 90 #endif // defined(GTEST_HAS_DEATH_TEST) && !BUILDFLAG(IS_ANDROID) 91 92 namespace base { 93 94 class FilePath; 95 96 struct TestIdentifier { 97 TestIdentifier(); 98 TestIdentifier(const TestIdentifier& other); 99 TestIdentifier& operator=(const TestIdentifier& other); 100 101 std::string test_case_name; 102 std::string test_name; 103 std::string file; 104 int line; 105 }; 106 107 // Constructs a full test name given a test case name and a test name, 108 // e.g. for test case "A" and test name "B" returns "A.B". 109 std::string FormatFullTestName(const std::string& test_case_name, 110 const std::string& test_name); 111 112 // Returns the full test name with the "DISABLED_" prefix stripped out. 113 // e.g. for the full test names "A.DISABLED_B", "DISABLED_A.B", and 114 // "DISABLED_A.DISABLED_B", returns "A.B". 115 std::string TestNameWithoutDisabledPrefix(const std::string& full_test_name); 116 117 // Returns a vector of gtest-based tests compiled into 118 // current executable. 119 std::vector<TestIdentifier> GetCompiledInTests(); 120 121 // Writes the list of gtest-based tests compiled into 122 // current executable as a JSON file. Returns true on success. 123 [[nodiscard]] bool WriteCompiledInTestsToFile(const FilePath& path); 124 125 // Reads the list of gtest-based tests from |path| into |output|. 126 // Returns true on success. 127 [[nodiscard]] bool ReadTestNamesFromFile(const FilePath& path, 128 std::vector<TestIdentifier>* output); 129 130 } // namespace base 131 132 #endif // BASE_TEST_GTEST_UTIL_H_ 133