gtest_prod_util.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Copyright (c) 2012 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_GTEST_PROD_UTIL_H_
  5. #define BASE_GTEST_PROD_UTIL_H_
  6. #include "testing/gtest/include/gtest/gtest_prod.h" // nogncheck
  7. // This is a wrapper for gtest's FRIEND_TEST macro that friends
  8. // test with all possible prefixes. This is very helpful when changing the test
  9. // prefix, because the friend declarations don't need to be updated.
  10. //
  11. // Example usage:
  12. //
  13. // class MyClass {
  14. // private:
  15. // void MyMethod();
  16. // FRIEND_TEST_ALL_PREFIXES(MyClassTest, MyMethod);
  17. // };
  18. #define FRIEND_TEST_ALL_PREFIXES(test_case_name, test_name) \
  19. FRIEND_TEST(test_case_name, test_name); \
  20. FRIEND_TEST(test_case_name, DISABLED_##test_name); \
  21. FRIEND_TEST(test_case_name, FLAKY_##test_name)
  22. // C++ compilers will refuse to compile the following code:
  23. //
  24. // namespace foo {
  25. // class MyClass {
  26. // private:
  27. // FRIEND_TEST_ALL_PREFIXES(MyClassTest, TestMethod);
  28. // bool private_var;
  29. // };
  30. // } // namespace foo
  31. //
  32. // class MyClassTest::TestMethod() {
  33. // foo::MyClass foo_class;
  34. // foo_class.private_var = true;
  35. // }
  36. //
  37. // Unless you forward declare MyClassTest::TestMethod outside of namespace foo.
  38. // Use FORWARD_DECLARE_TEST to do so for all possible prefixes.
  39. //
  40. // Example usage:
  41. //
  42. // FORWARD_DECLARE_TEST(MyClassTest, TestMethod);
  43. //
  44. // namespace foo {
  45. // class MyClass {
  46. // private:
  47. // FRIEND_TEST_ALL_PREFIXES(::MyClassTest, TestMethod); // NOTE use of ::
  48. // bool private_var;
  49. // };
  50. // } // namespace foo
  51. //
  52. // class MyClassTest::TestMethod() {
  53. // foo::MyClass foo_class;
  54. // foo_class.private_var = true;
  55. // }
  56. #define FORWARD_DECLARE_TEST(test_case_name, test_name) \
  57. class test_case_name##_##test_name##_Test; \
  58. class test_case_name##_##DISABLED_##test_name##_Test; \
  59. class test_case_name##_##FLAKY_##test_name##_Test
  60. #endif // BASE_GTEST_PROD_UTIL_H_