gtest_util.h 4.1 KB

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