• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // 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 #define EXPECT_DCHECK_DEATH(statement) \
37     GTEST_UNSUPPORTED_DEATH_TEST(statement, "Check failed", )
38 #define ASSERT_DCHECK_DEATH(statement) \
39     GTEST_UNSUPPORTED_DEATH_TEST(statement, "Check failed", return)
40 
41 #endif
42 // DCHECK_IS_ON() && defined(GTEST_HAS_DEATH_TEST) && !defined(OS_ANDROID)
43 
44 namespace base {
45 
46 class FilePath;
47 
48 struct TestIdentifier {
49   TestIdentifier();
50   TestIdentifier(const TestIdentifier& other);
51 
52   std::string test_case_name;
53   std::string test_name;
54   std::string file;
55   int line;
56 };
57 
58 // Constructs a full test name given a test case name and a test name,
59 // e.g. for test case "A" and test name "B" returns "A.B".
60 std::string FormatFullTestName(const std::string& test_case_name,
61                                const std::string& test_name);
62 
63 // Returns the full test name with the "DISABLED_" prefix stripped out.
64 // e.g. for the full test names "A.DISABLED_B", "DISABLED_A.B", and
65 // "DISABLED_A.DISABLED_B", returns "A.B".
66 std::string TestNameWithoutDisabledPrefix(const std::string& full_test_name);
67 
68 // Returns a vector of gtest-based tests compiled into
69 // current executable.
70 std::vector<TestIdentifier> GetCompiledInTests();
71 
72 // Writes the list of gtest-based tests compiled into
73 // current executable as a JSON file. Returns true on success.
74 bool WriteCompiledInTestsToFile(const FilePath& path) WARN_UNUSED_RESULT;
75 
76 // Reads the list of gtest-based tests from |path| into |output|.
77 // Returns true on success.
78 bool ReadTestNamesFromFile(
79     const FilePath& path,
80     std::vector<TestIdentifier>* output) WARN_UNUSED_RESULT;
81 
82 }  // namespace base
83 
84 #endif  // BASE_TEST_GTEST_UTIL_H_
85