gtest_util.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. #ifndef BASE_TEST_GTEST_UTIL_H_
  5. #define BASE_TEST_GTEST_UTIL_H_
  6. #include <string>
  7. #include <utility>
  8. #include <vector>
  9. #include "base/check.h"
  10. #include "build/build_config.h"
  11. #include "testing/gtest/include/gtest/gtest.h"
  12. // EXPECT/ASSERT_DCHECK_DEATH is intended to replace EXPECT/ASSERT_DEBUG_DEATH
  13. // when the death is expected to be caused by a DCHECK. Contrary to
  14. // EXPECT/ASSERT_DEBUG_DEATH however, it doesn't execute the statement in non-
  15. // dcheck builds as DCHECKs are intended to catch things that should never
  16. // happen and as such executing the statement results in undefined behavior
  17. // (|statement| is compiled in unsupported configurations nonetheless).
  18. // Death tests misbehave on Android.
  19. #if DCHECK_IS_ON() && defined(GTEST_HAS_DEATH_TEST) && !BUILDFLAG(IS_ANDROID)
  20. // EXPECT/ASSERT_DCHECK_DEATH tests verify that a DCHECK is hit ("Check failed"
  21. // is part of the error message). Optionally you may specify part of the message
  22. // to verify which DCHECK (or LOG(DFATAL)) is being hit.
  23. #define EXPECT_DCHECK_DEATH(statement) EXPECT_DEATH(statement, "Check failed")
  24. #define EXPECT_DCHECK_DEATH_WITH(statement, msg) EXPECT_DEATH(statement, msg)
  25. #define ASSERT_DCHECK_DEATH(statement) ASSERT_DEATH(statement, "Check failed")
  26. #define ASSERT_DCHECK_DEATH_WITH(statement, msg) ASSERT_DEATH(statement, msg)
  27. #else
  28. // DCHECK_IS_ON() && defined(GTEST_HAS_DEATH_TEST) && !BUILDFLAG(IS_ANDROID)
  29. #define EXPECT_DCHECK_DEATH(statement) \
  30. GTEST_UNSUPPORTED_DEATH_TEST(statement, "Check failed", )
  31. #define EXPECT_DCHECK_DEATH_WITH(statement, msg) \
  32. GTEST_UNSUPPORTED_DEATH_TEST(statement, msg, )
  33. #define ASSERT_DCHECK_DEATH(statement) \
  34. GTEST_UNSUPPORTED_DEATH_TEST(statement, "Check failed", return )
  35. #define ASSERT_DCHECK_DEATH_WITH(statement, msg) \
  36. GTEST_UNSUPPORTED_DEATH_TEST(statement, msg, return )
  37. #endif
  38. // DCHECK_IS_ON() && defined(GTEST_HAS_DEATH_TEST) && !BUILDFLAG(IS_ANDROID)
  39. // As above, but for CHECK().
  40. #if defined(GTEST_HAS_DEATH_TEST) && !BUILDFLAG(IS_ANDROID)
  41. #if CHECK_WILL_STREAM()
  42. #define EXPECT_CHECK_DEATH(statement) EXPECT_DEATH(statement, "Check failed")
  43. #define ASSERT_CHECK_DEATH(statement) ASSERT_DEATH(statement, "Check failed")
  44. #else
  45. #define EXPECT_CHECK_DEATH(statement) EXPECT_DEATH(statement, "")
  46. #define ASSERT_CHECK_DEATH(statement) ASSERT_DEATH(statement, "")
  47. #endif // CHECK_WILL_STREAM()
  48. #else // defined(GTEST_HAS_DEATH_TEST) && !BUILDFLAG(IS_ANDROID)
  49. // Note GTEST_UNSUPPORTED_DEATH_TEST takes a |regex| only to see whether it is a
  50. // valid regex. It is never evaluated.
  51. #define EXPECT_CHECK_DEATH(statement) \
  52. GTEST_UNSUPPORTED_DEATH_TEST(statement, "", )
  53. #define ASSERT_CHECK_DEATH(statement) \
  54. GTEST_UNSUPPORTED_DEATH_TEST(statement, "", return )
  55. #endif // defined(GTEST_HAS_DEATH_TEST) && !BUILDFLAG(IS_ANDROID)
  56. namespace base {
  57. class FilePath;
  58. struct TestIdentifier {
  59. TestIdentifier();
  60. TestIdentifier(const TestIdentifier& other);
  61. TestIdentifier& operator=(const TestIdentifier& other);
  62. std::string test_case_name;
  63. std::string test_name;
  64. std::string file;
  65. int line;
  66. };
  67. // Constructs a full test name given a test case name and a test name,
  68. // e.g. for test case "A" and test name "B" returns "A.B".
  69. std::string FormatFullTestName(const std::string& test_case_name,
  70. const std::string& test_name);
  71. // Returns the full test name with the "DISABLED_" prefix stripped out.
  72. // e.g. for the full test names "A.DISABLED_B", "DISABLED_A.B", and
  73. // "DISABLED_A.DISABLED_B", returns "A.B".
  74. std::string TestNameWithoutDisabledPrefix(const std::string& full_test_name);
  75. // Returns a vector of gtest-based tests compiled into
  76. // current executable.
  77. std::vector<TestIdentifier> GetCompiledInTests();
  78. // Writes the list of gtest-based tests compiled into
  79. // current executable as a JSON file. Returns true on success.
  80. [[nodiscard]] bool WriteCompiledInTestsToFile(const FilePath& path);
  81. // Reads the list of gtest-based tests from |path| into |output|.
  82. // Returns true on success.
  83. [[nodiscard]] bool ReadTestNamesFromFile(const FilePath& path,
  84. std::vector<TestIdentifier>* output);
  85. } // namespace base
  86. #endif // BASE_TEST_GTEST_UTIL_H_