Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / net / test / gtest_util.h
blob1d6708774ffd48627952a8a0a218684992f345e5
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 // Testing utilities that extend gtest.
7 #ifndef NET_QUIC_TEST_TOOLS_GTEST_UTIL_H_
8 #define NET_QUIC_TEST_TOOLS_GTEST_UTIL_H_
10 #include "base/test/mock_log.h"
11 #include "net/test/scoped_disable_exit_on_dfatal.h"
12 #include "testing/gmock/include/gmock/gmock.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 namespace net {
16 namespace test {
18 // Internal implementation for the EXPECT_DFATAL and ASSERT_DFATAL
19 // macros. Do not use this directly.
20 #define GTEST_DFATAL_(statement, matcher, fail) \
21 GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
22 if (true) { \
23 ::base::test::MockLog gtest_log; \
24 ::net::test::ScopedDisableExitOnDFatal gtest_disable_exit; \
25 using ::testing::_; \
26 EXPECT_CALL(gtest_log, Log(_, _, _, _, _)) \
27 .WillRepeatedly(::testing::Return(false)); \
28 EXPECT_CALL(gtest_log, Log(logging::LOG_DFATAL, _, _, _, matcher)) \
29 .Times(::testing::AtLeast(1)) \
30 .WillOnce(::testing::Return(false)); \
31 gtest_log.StartCapturingLogs(); \
32 { statement; } \
33 gtest_log.StopCapturingLogs(); \
34 if (!testing::Mock::VerifyAndClear(&gtest_log)) { \
35 goto GTEST_CONCAT_TOKEN_(gtest_label_dfatal_, __LINE__); \
36 } \
37 } else \
38 GTEST_CONCAT_TOKEN_(gtest_label_dfatal_, __LINE__) : fail("")
40 // The EXPECT_DFATAL and ASSERT_DFATAL macros are lightweight
41 // alternatives to EXPECT_DEBUG_DEATH and ASSERT_DEBUG_DEATH. They
42 // are appropriate for testing that your code logs a message at the
43 // DFATAL level.
45 // Unlike EXPECT_DEBUG_DEATH and ASSERT_DEBUG_DEATH, these macros
46 // execute the given statement in the current process, not a forked
47 // one. This works because we disable exiting the program for
48 // LOG(DFATAL). This makes the tests run more quickly.
50 // The _WITH() variants allow one to specify any matcher for the
51 // DFATAL log message, whereas the other variants assume a regex.
53 #define EXPECT_DFATAL_WITH(statement, matcher) \
54 GTEST_DFATAL_(statement, matcher, GTEST_NONFATAL_FAILURE_)
56 #define ASSERT_DFATAL_WITH(statement, matcher) \
57 GTEST_DFATAL_(statement, matcher, GTEST_FATAL_FAILURE_)
59 #define EXPECT_DFATAL(statement, regex) \
60 EXPECT_DFATAL_WITH(statement, ::testing::ContainsRegex(regex))
62 #define ASSERT_DFATAL(statement, regex) \
63 ASSERT_DFATAL_WITH(statement, ::testing::ContainsRegex(regex))
65 // The EXPECT_DEBUG_DFATAL and ASSERT_DEBUG_DFATAL macros are similar to
66 // EXPECT_DFATAL and ASSERT_DFATAL. Use them in conjunction with DLOG(DFATAL)
67 // or similar macros that produce no-op in opt build and DFATAL in dbg build.
69 #ifndef NDEBUG
71 #define EXPECT_DEBUG_DFATAL(statement, regex) \
72 EXPECT_DFATAL(statement, regex)
73 #define ASSERT_DEBUG_DFATAL(statement, regex) \
74 ASSERT_DFATAL(statement, regex)
76 #else // NDEBUG
78 #define EXPECT_DEBUG_DFATAL(statement, regex) \
79 GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
80 if (true) { \
81 (void)(regex); \
82 statement; \
83 } else \
84 GTEST_NONFATAL_FAILURE_("")
85 #define ASSERT_DEBUG_DFATAL(statement, regex) \
86 GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
87 if (true) { \
88 (void)(regex); \
89 statement; \
90 } else \
91 GTEST_NONFATAL_FAILURE_("")
93 #endif // NDEBUG
95 } // namespace test
96 } // namespace net
98 #endif // NET_QUIC_TEST_TOOLS_GTEST_UTIL_H_