blob: 25610c6b4098a3df1d8bc447f443dfdab189e007 [file] [log] [blame]
Andrew Top0d1858f2019-05-15 22:01:47 -07001// Copyright 2014 The Chromium Authors. All rights reserved.
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/compiler_specific.h"
13#include "base/logging.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// Death tests misbehave on Android.
24#if DCHECK_IS_ON() && defined(GTEST_HAS_DEATH_TEST) && !defined(OS_ANDROID)
25
26// EXPECT/ASSERT_DCHECK_DEATH tests verify that a DCHECK is hit ("Check failed"
27// is part of the error message), but intentionally do not expose the gtest
28// death test's full |regex| parameter to avoid users having to verify the exact
29// syntax of the error message produced by the DCHECK.
30#define EXPECT_DCHECK_DEATH(statement) EXPECT_DEATH(statement, "Check failed")
31#define ASSERT_DCHECK_DEATH(statement) ASSERT_DEATH(statement, "Check failed")
32
33#else
34// DCHECK_IS_ON() && defined(GTEST_HAS_DEATH_TEST) && !defined(OS_ANDROID)
35
36// Macro copied from gtest-death-test-internal.h as it's (1) internal for now
37// and (2) only defined if !GTEST_HAS_DEATH_TEST which is only a subset of the
38// conditions in which it's needed here.
39// TODO(gab): Expose macro in upstream gtest repo for consumers like us that
40// want more specific death tests and remove this hack.
41#define GTEST_UNSUPPORTED_DEATH_TEST(statement, regex, terminator) \
42 GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
43 if (::testing::internal::AlwaysTrue()) { \
44 GTEST_LOG_(WARNING) \
45 << "Death tests are not supported in this configuration.\n" \
46 << "Statement '" #statement "' cannot be verified."; \
47 } else if (::testing::internal::AlwaysFalse()) { \
48 ::testing::internal::RE::PartialMatch(".*", (regex)); \
49 GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \
50 terminator; \
51 } else \
52 ::testing::Message()
53
54#define EXPECT_DCHECK_DEATH(statement) \
55 GTEST_UNSUPPORTED_DEATH_TEST(statement, "Check failed", )
56#define ASSERT_DCHECK_DEATH(statement) \
57 GTEST_UNSUPPORTED_DEATH_TEST(statement, "Check failed", return)
58
59#endif
60// DCHECK_IS_ON() && defined(GTEST_HAS_DEATH_TEST) && !defined(OS_ANDROID)
61
62// As above, but for CHECK().
63#if defined(GTEST_HAS_DEATH_TEST) && !defined(OS_ANDROID)
64
65// Official builds will CHECK, but also eat stream parameters. So match "".
66#if defined(OFFICIAL_BUILD) && defined(NDEBUG)
67#define EXPECT_CHECK_DEATH(statement) EXPECT_DEATH(statement, "")
68#define ASSERT_CHECK_DEATH(statement) ASSERT_DEATH(statement, "")
69#else
70#define EXPECT_CHECK_DEATH(statement) EXPECT_DEATH(statement, "Check failed")
71#define ASSERT_CHECK_DEATH(statement) ASSERT_DEATH(statement, "Check failed")
72#endif // defined(OFFICIAL_BUILD) && defined(NDEBUG)
73
74#else // defined(GTEST_HAS_DEATH_TEST) && !defined(OS_ANDROID)
75
76// Note GTEST_UNSUPPORTED_DEATH_TEST takes a |regex| only to see whether it is a
77// valid regex. It is never evaluated.
78#define EXPECT_CHECK_DEATH(statement) \
79 GTEST_UNSUPPORTED_DEATH_TEST(statement, "", )
80#define ASSERT_CHECK_DEATH(statement) \
81 GTEST_UNSUPPORTED_DEATH_TEST(statement, "", return )
82
83#endif // defined(GTEST_HAS_DEATH_TEST) && !defined(OS_ANDROID)
84
85namespace base {
86
87class FilePath;
88
89struct TestIdentifier {
90 TestIdentifier();
91 TestIdentifier(const TestIdentifier& other);
92
93 std::string test_case_name;
94 std::string test_name;
95 std::string file;
96 int line;
97};
98
99// Constructs a full test name given a test case name and a test name,
100// e.g. for test case "A" and test name "B" returns "A.B".
101std::string FormatFullTestName(const std::string& test_case_name,
102 const std::string& test_name);
103
104// Returns the full test name with the "DISABLED_" prefix stripped out.
105// e.g. for the full test names "A.DISABLED_B", "DISABLED_A.B", and
106// "DISABLED_A.DISABLED_B", returns "A.B".
107std::string TestNameWithoutDisabledPrefix(const std::string& full_test_name);
108
109// Returns a vector of gtest-based tests compiled into
110// current executable.
111std::vector<TestIdentifier> GetCompiledInTests();
112
113// Writes the list of gtest-based tests compiled into
114// current executable as a JSON file. Returns true on success.
115bool WriteCompiledInTestsToFile(const FilePath& path) WARN_UNUSED_RESULT;
116
117// Reads the list of gtest-based tests from |path| into |output|.
118// Returns true on success.
119bool ReadTestNamesFromFile(
120 const FilePath& path,
121 std::vector<TestIdentifier>* output) WARN_UNUSED_RESULT;
122
123} // namespace base
124
125#endif // BASE_TEST_GTEST_UTIL_H_