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. 6 7 #ifndef NET_QUIC_TEST_TOOLS_GTEST_UTIL_H_ 8 #define NET_QUIC_TEST_TOOLS_GTEST_UTIL_H_ 9 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" 14 15 namespace net { 16 namespace test { 17 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 ::net::test::ScopedMockLog 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(>est_log)) { \ 35 goto GTEST_CONCAT_TOKEN_(gtest_label_dfatal_, __LINE__); \ 36 } \ 37 } else \ 38 GTEST_CONCAT_TOKEN_(gtest_label_dfatal_, __LINE__): \ 39 fail("") 40 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 44 // DFATAL level. 45 // 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. 50 // 51 // The _WITH() variants allow one to specify any matcher for the 52 // DFATAL log message, whereas the other variants assume a regex. 53 54 #define EXPECT_DFATAL_WITH(statement, matcher) \ 55 GTEST_DFATAL_(statement, matcher, GTEST_NONFATAL_FAILURE_) 56 57 #define ASSERT_DFATAL_WITH(statement, matcher) \ 58 GTEST_DFATAL_(statement, matcher, GTEST_FATAL_FAILURE_) 59 60 #define EXPECT_DFATAL(statement, regex) \ 61 EXPECT_DFATAL_WITH(statement, ::testing::ContainsRegex(regex)) 62 63 #define ASSERT_DFATAL(statement, regex) \ 64 ASSERT_DFATAL_WITH(statement, ::testing::ContainsRegex(regex)) 65 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. 69 70 #ifndef NDEBUG 71 72 #define EXPECT_DEBUG_DFATAL(statement, regex) \ 73 EXPECT_DFATAL(statement, regex) 74 #define ASSERT_DEBUG_DFATAL(statement, regex) \ 75 ASSERT_DFATAL(statement, regex) 76 77 #else // NDEBUG 78 79 #define EXPECT_DEBUG_DFATAL(statement, regex) \ 80 GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ 81 if (true) { \ 82 (void)(regex); \ 83 statement; \ 84 } else \ 85 GTEST_NONFATAL_FAILURE_("") 86 #define ASSERT_DEBUG_DFATAL(statement, regex) \ 87 GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ 88 if (true) { \ 89 (void)(regex); \ 90 statement; \ 91 } else \ 92 GTEST_NONFATAL_FAILURE_("") 93 94 #endif // NDEBUG 95 96 } // namespace test 97 } // namespace net 98 99 #endif // NET_QUIC_TEST_TOOLS_GTEST_UTIL_H_ 100