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_TEST_GTEST_UTIL_H_
8 #define NET_TEST_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"
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 ::base::test::MockLog 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__) : 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
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.
71 #define EXPECT_DEBUG_DFATAL(statement, regex) \
72 EXPECT_DFATAL(statement, regex)
73 #define ASSERT_DEBUG_DFATAL(statement, regex) \
74 ASSERT_DFATAL(statement, regex)
78 #define EXPECT_DEBUG_DFATAL(statement, regex) \
79 GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
84 GTEST_NONFATAL_FAILURE_("")
85 #define ASSERT_DEBUG_DFATAL(statement, regex) \
86 GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
91 GTEST_NONFATAL_FAILURE_("")
98 #endif // NET_TEST_GTEST_UTIL_H_