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