Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(284)

Side by Side Diff: net/test/gtest_util.h

Issue 2217803003: Add {ASSERT,EXPECT}_DCHECK macros for tests (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@iid8hookupcrypto_empty
Patch Set: Switch to do { ... } while (false) Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 // 4 //
5 // Testing utilities that extend gtest. 5 // Testing utilities that extend gtest.
6 6
7 #ifndef NET_TEST_GTEST_UTIL_H_ 7 #ifndef NET_TEST_GTEST_UTIL_H_
8 #define NET_TEST_GTEST_UTIL_H_ 8 #define NET_TEST_GTEST_UTIL_H_
9 9
10 #include "base/test/mock_log.h" 10 #include "base/test/mock_log.h"
(...skipping 20 matching lines...) Expand all
31 // Usage: EXPECT_THAT(foo(), IsOk()); 31 // Usage: EXPECT_THAT(foo(), IsOk());
32 MATCHER(IsOk, 32 MATCHER(IsOk,
33 std::string(negation ? "not " : "") + net::ErrorToString(net::OK)) { 33 std::string(negation ? "not " : "") + net::ErrorToString(net::OK)) {
34 if (arg <= 0) 34 if (arg <= 0)
35 *result_listener << net::ErrorToString(arg); 35 *result_listener << net::ErrorToString(arg);
36 return arg == net::OK; 36 return arg == net::OK;
37 } 37 }
38 38
39 // Internal implementation for the EXPECT_DFATAL and ASSERT_DFATAL 39 // Internal implementation for the EXPECT_DFATAL and ASSERT_DFATAL
40 // macros. Do not use this directly. 40 // macros. Do not use this directly.
41 #define GTEST_DFATAL_(statement, matcher, fail) \ 41 #define GTEST_DFATAL_(statement, severity, matcher, fail) \
42 GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ 42 do { \
43 if (true) { \ 43 ::base::test::MockLog gtest_log; \
44 ::base::test::MockLog gtest_log; \ 44 ::net::test::ScopedDisableExitOnDFatal gtest_disable_exit; \
45 ::net::test::ScopedDisableExitOnDFatal gtest_disable_exit; \ 45 using ::testing::_; \
46 using ::testing::_; \ 46 EXPECT_CALL(gtest_log, Log(_, _, _, _, _)) \
47 EXPECT_CALL(gtest_log, Log(_, _, _, _, _)) \ 47 .WillRepeatedly(::testing::Return(false)); \
48 .WillRepeatedly(::testing::Return(false)); \ 48 EXPECT_CALL(gtest_log, Log(::logging::LOG_##severity, _, _, _, matcher)) \
49 EXPECT_CALL(gtest_log, Log(logging::LOG_DFATAL, _, _, _, matcher)) \ 49 .Times(::testing::AtLeast(1)) \
50 .Times(::testing::AtLeast(1)) \ 50 .WillOnce(::testing::Return(false)); \
51 .WillOnce(::testing::Return(false)); \ 51 gtest_log.StartCapturingLogs(); \
52 gtest_log.StartCapturingLogs(); \ 52 { statement; } \
53 { statement; } \ 53 gtest_log.StopCapturingLogs(); \
54 gtest_log.StopCapturingLogs(); \ 54 if (!testing::Mock::VerifyAndClear(&gtest_log)) \
55 if (!testing::Mock::VerifyAndClear(&gtest_log)) { \ 55 fail(""); \
56 goto GTEST_CONCAT_TOKEN_(gtest_label_dfatal_, __LINE__); \ 56 } while (false)
57 } \
58 } else \
59 GTEST_CONCAT_TOKEN_(gtest_label_dfatal_, __LINE__) : fail("")
60 57
61 // The EXPECT_DFATAL and ASSERT_DFATAL macros are lightweight 58 // The EXPECT_DFATAL and ASSERT_DFATAL macros are lightweight
62 // alternatives to EXPECT_DEBUG_DEATH and ASSERT_DEBUG_DEATH. They 59 // alternatives to EXPECT_DEBUG_DEATH and ASSERT_DEBUG_DEATH. They
63 // are appropriate for testing that your code logs a message at the 60 // are appropriate for testing that your code logs a message at the
64 // DFATAL level. 61 // DFATAL level.
65 // 62 //
66 // Unlike EXPECT_DEBUG_DEATH and ASSERT_DEBUG_DEATH, these macros 63 // Unlike EXPECT_DEBUG_DEATH and ASSERT_DEBUG_DEATH, these macros
67 // execute the given statement in the current process, not a forked 64 // execute the given statement in the current process, not a forked
68 // one. This works because we disable exiting the program for 65 // one. This works because we disable exiting the program for
69 // LOG(DFATAL). This makes the tests run more quickly. 66 // LOG(DFATAL). This makes the tests run more quickly.
70 // 67 //
71 // The _WITH() variants allow one to specify any matcher for the 68 // The _WITH() variants allow one to specify any matcher for the
72 // DFATAL log message, whereas the other variants assume a regex. 69 // DFATAL log message, whereas the other variants assume a regex.
73 70
74 #define EXPECT_DFATAL_WITH(statement, matcher) \ 71 #define EXPECT_DFATAL_WITH(statement, matcher) \
75 GTEST_DFATAL_(statement, matcher, GTEST_NONFATAL_FAILURE_) 72 GTEST_DFATAL_(statement, DFATAL, matcher, GTEST_NONFATAL_FAILURE_)
76 73
77 #define ASSERT_DFATAL_WITH(statement, matcher) \ 74 #define ASSERT_DFATAL_WITH(statement, matcher) \
78 GTEST_DFATAL_(statement, matcher, GTEST_FATAL_FAILURE_) 75 GTEST_DFATAL_(statement, DFATAL, matcher, GTEST_FATAL_FAILURE_)
79 76
80 #define EXPECT_DFATAL(statement, regex) \ 77 #define EXPECT_DFATAL(statement, regex) \
81 EXPECT_DFATAL_WITH(statement, ::testing::ContainsRegex(regex)) 78 EXPECT_DFATAL_WITH(statement, ::testing::ContainsRegex(regex))
82 79
83 #define ASSERT_DFATAL(statement, regex) \ 80 #define ASSERT_DFATAL(statement, regex) \
84 ASSERT_DFATAL_WITH(statement, ::testing::ContainsRegex(regex)) 81 ASSERT_DFATAL_WITH(statement, ::testing::ContainsRegex(regex))
85 82
86 // The EXPECT_DEBUG_DFATAL and ASSERT_DEBUG_DFATAL macros are similar to 83 // The EXPECT_DEBUG_DFATAL and ASSERT_DEBUG_DFATAL macros are similar to
87 // EXPECT_DFATAL and ASSERT_DFATAL. Use them in conjunction with DLOG(DFATAL) 84 // EXPECT_DFATAL and ASSERT_DFATAL. Use them in conjunction with DLOG(DFATAL)
88 // or similar macros that produce no-op in opt build and DFATAL in dbg build. 85 // or similar macros that produce no-op in opt build and DFATAL in dbg build.
89 86
90 #ifndef NDEBUG 87 #ifndef NDEBUG
91 88
92 #define EXPECT_DEBUG_DFATAL(statement, regex) \ 89 #define EXPECT_DEBUG_DFATAL(statement, regex) \
93 EXPECT_DFATAL(statement, regex) 90 EXPECT_DFATAL(statement, regex)
94 #define ASSERT_DEBUG_DFATAL(statement, regex) \ 91 #define ASSERT_DEBUG_DFATAL(statement, regex) \
95 ASSERT_DFATAL(statement, regex) 92 ASSERT_DFATAL(statement, regex)
96 93
97 #else // NDEBUG 94 #else // NDEBUG
98 95
99 #define EXPECT_DEBUG_DFATAL(statement, regex) \ 96 #define EXPECT_DEBUG_DFATAL(statement, regex) \
100 GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ 97 do { \
101 if (true) { \
102 (void)(regex); \ 98 (void)(regex); \
103 statement; \ 99 statement; \
104 } else \ 100 } while (false)
105 GTEST_NONFATAL_FAILURE_("")
106 #define ASSERT_DEBUG_DFATAL(statement, regex) \ 101 #define ASSERT_DEBUG_DFATAL(statement, regex) \
107 GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ 102 do { \
108 if (true) { \
109 (void)(regex); \ 103 (void)(regex); \
110 statement; \ 104 statement; \
111 } else \ 105 } while (false)
112 GTEST_NONFATAL_FAILURE_("")
113 106
114 #endif // NDEBUG 107 #endif // NDEBUG
115 108
109 // The EXPECT_DCHECK and ASSERT_DCHECK macros are similar to EXPECT_DFATAL and
110 // ASSERT_DFATAL. Use them in conjunction with DCHECK that produces no-op in opt
111 // build and LOG_DCHECK (FATAL) if DCHECK_IS_ON().
112
113 #if DCHECK_IS_ON()
114
115 #define EXPECT_DCHECK(statement, regex) \
116 GTEST_DFATAL_(statement, DCHECK, ::testing::ContainsRegex(regex), \
117 GTEST_NONFATAL_FAILURE_)
118 #define ASSERT_DCHECK(statement, regex) \
119 GTEST_DFATAL_(statement, DCHECK, ::testing::ContainsRegex(regex), \
120 GTEST_FATAL_FAILURE_)
121
122 #else // DCHECK_IS_ON()
123
124 #define EXPECT_DCHECK(statement, regex) \
125 do { \
126 (void)(regex); \
127 statement; \
128 } while (false)
129 #define ASSERT_DCHECK(statement, regex) \
130 do { \
131 (void)(regex); \
132 statement; \
133 } while (false)
134
135 #endif // DCHECK_IS_ON()
136
116 } // namespace test 137 } // namespace test
117 } // namespace net 138 } // namespace net
118 139
119 #endif // NET_TEST_GTEST_UTIL_H_ 140 #endif // NET_TEST_GTEST_UTIL_H_
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698