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.
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 "net/test/scoped_disable_exit_on_dfatal.h"
11 #include "net/test/scoped_mock_log.h"
12 #include "testing/gmock/include/gmock/gmock.h"
13 #include "testing/gtest/include/gtest/gtest.h"
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_ \
23 ::net::test::ScopedMockLog gtest_log; \
24 ::net::test::ScopedDisableExitOnDFatal gtest_disable_exit; \
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(); \
33 gtest_log.StopCapturingLogs(); \
34 if (!testing::Mock::VerifyAndClear(>est_log)) { \
35 goto GTEST_CONCAT_TOKEN_(gtest_label_dfatal_, __LINE__); \
38 GTEST_CONCAT_TOKEN_(gtest_label_dfatal_, __LINE__): \
41 // The EXPECT_DFATAL and ASSERT_DFATAL macros are lightweight
42 // alternatives to EXPECT_DEBUG_DEATH and ASSERT_DEBUG_DEATH. They
43 // are appropriate for testing that your code logs a message at the
46 // Unlike EXPECT_DEBUG_DEATH and ASSERT_DEBUG_DEATH, these macros
47 // execute the given statement in the current process, not a forked
48 // one. This works because we disable exiting the program for
49 // LOG(DFATAL). This makes the tests run more quickly.
51 // The _WITH() variants allow one to specify any matcher for the
52 // DFATAL log message, whereas the other variants assume a regex.
54 #define EXPECT_DFATAL_WITH(statement, matcher) \
55 GTEST_DFATAL_(statement, matcher, GTEST_NONFATAL_FAILURE_)
57 #define ASSERT_DFATAL_WITH(statement, matcher) \
58 GTEST_DFATAL_(statement, matcher, GTEST_FATAL_FAILURE_)
60 #define EXPECT_DFATAL(statement, regex) \
61 EXPECT_DFATAL_WITH(statement, ::testing::ContainsRegex(regex))
63 #define ASSERT_DFATAL(statement, regex) \
64 ASSERT_DFATAL_WITH(statement, ::testing::ContainsRegex(regex))
66 // The EXPECT_DEBUG_DFATAL and ASSERT_DEBUG_DFATAL macros are similar to
67 // EXPECT_DFATAL and ASSERT_DFATAL. Use them in conjunction with DLOG(DFATAL)
68 // or similar macros that produce no-op in opt build and DFATAL in dbg build.
72 #define EXPECT_DEBUG_DFATAL(statement, regex) \
73 EXPECT_DFATAL(statement, regex)
74 #define ASSERT_DEBUG_DFATAL(statement, regex) \
75 ASSERT_DFATAL(statement, regex)
79 #define EXPECT_DEBUG_DFATAL(statement, regex) \
80 GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
85 GTEST_NONFATAL_FAILURE_("")
86 #define ASSERT_DEBUG_DFATAL(statement, regex) \
87 GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
92 GTEST_NONFATAL_FAILURE_("")
99 #endif // NET_QUIC_TEST_TOOLS_GTEST_UTIL_H_