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

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: 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 GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
43 if (true) { \ 43 if (true) { \
44 ::base::test::MockLog gtest_log; \ 44 ::base::test::MockLog gtest_log; \
45 ::net::test::ScopedDisableExitOnDFatal gtest_disable_exit; \ 45 ::net::test::ScopedDisableExitOnDFatal gtest_disable_exit; \
46 using ::testing::_; \ 46 using ::testing::_; \
47 EXPECT_CALL(gtest_log, Log(_, _, _, _, _)) \ 47 EXPECT_CALL(gtest_log, Log(_, _, _, _, _)) \
48 .WillRepeatedly(::testing::Return(false)); \ 48 .WillRepeatedly(::testing::Return(false)); \
49 EXPECT_CALL(gtest_log, Log(logging::LOG_DFATAL, _, _, _, matcher)) \ 49 EXPECT_CALL(gtest_log, Log(::logging::LOG_##severity, _, _, _, matcher)) \
50 .Times(::testing::AtLeast(1)) \ 50 .Times(::testing::AtLeast(1)) \
51 .WillOnce(::testing::Return(false)); \ 51 .WillOnce(::testing::Return(false)); \
52 gtest_log.StartCapturingLogs(); \ 52 gtest_log.StartCapturingLogs(); \
53 { statement; } \ 53 { statement; } \
54 gtest_log.StopCapturingLogs(); \ 54 gtest_log.StopCapturingLogs(); \
55 if (!testing::Mock::VerifyAndClear(&gtest_log)) { \ 55 if (!testing::Mock::VerifyAndClear(&gtest_log)) { \
56 goto GTEST_CONCAT_TOKEN_(gtest_label_dfatal_, __LINE__); \ 56 goto GTEST_CONCAT_TOKEN_(gtest_label_dfatal_, __LINE__); \
57 } \ 57 } \
58 } else \ 58 } else \
59 GTEST_CONCAT_TOKEN_(gtest_label_dfatal_, __LINE__) : fail("") 59 GTEST_CONCAT_TOKEN_(gtest_label_dfatal_, __LINE__) : fail("")
60 60
61 // The EXPECT_DFATAL and ASSERT_DFATAL macros are lightweight 61 // The EXPECT_DFATAL and ASSERT_DFATAL macros are lightweight
62 // alternatives to EXPECT_DEBUG_DEATH and ASSERT_DEBUG_DEATH. They 62 // alternatives to EXPECT_DEBUG_DEATH and ASSERT_DEBUG_DEATH. They
63 // are appropriate for testing that your code logs a message at the 63 // are appropriate for testing that your code logs a message at the
64 // DFATAL level. 64 // DFATAL level.
65 // 65 //
66 // Unlike EXPECT_DEBUG_DEATH and ASSERT_DEBUG_DEATH, these macros 66 // Unlike EXPECT_DEBUG_DEATH and ASSERT_DEBUG_DEATH, these macros
67 // execute the given statement in the current process, not a forked 67 // execute the given statement in the current process, not a forked
68 // one. This works because we disable exiting the program for 68 // one. This works because we disable exiting the program for
69 // LOG(DFATAL). This makes the tests run more quickly. 69 // LOG(DFATAL). This makes the tests run more quickly.
70 // 70 //
71 // The _WITH() variants allow one to specify any matcher for the 71 // The _WITH() variants allow one to specify any matcher for the
72 // DFATAL log message, whereas the other variants assume a regex. 72 // DFATAL log message, whereas the other variants assume a regex.
73 73
74 #define EXPECT_DFATAL_WITH(statement, matcher) \ 74 #define EXPECT_DFATAL_WITH(statement, matcher) \
75 GTEST_DFATAL_(statement, matcher, GTEST_NONFATAL_FAILURE_) 75 GTEST_DFATAL_(statement, DFATAL, matcher, GTEST_NONFATAL_FAILURE_)
76 76
77 #define ASSERT_DFATAL_WITH(statement, matcher) \ 77 #define ASSERT_DFATAL_WITH(statement, matcher) \
78 GTEST_DFATAL_(statement, matcher, GTEST_FATAL_FAILURE_) 78 GTEST_DFATAL_(statement, DFATAL, matcher, GTEST_FATAL_FAILURE_)
79 79
80 #define EXPECT_DFATAL(statement, regex) \ 80 #define EXPECT_DFATAL(statement, regex) \
81 EXPECT_DFATAL_WITH(statement, ::testing::ContainsRegex(regex)) 81 EXPECT_DFATAL_WITH(statement, ::testing::ContainsRegex(regex))
82 82
83 #define ASSERT_DFATAL(statement, regex) \ 83 #define ASSERT_DFATAL(statement, regex) \
84 ASSERT_DFATAL_WITH(statement, ::testing::ContainsRegex(regex)) 84 ASSERT_DFATAL_WITH(statement, ::testing::ContainsRegex(regex))
85 85
86 // The EXPECT_DEBUG_DFATAL and ASSERT_DEBUG_DFATAL macros are similar to 86 // 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) 87 // 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. 88 // or similar macros that produce no-op in opt build and DFATAL in dbg build.
(...skipping 17 matching lines...) Expand all
106 #define ASSERT_DEBUG_DFATAL(statement, regex) \ 106 #define ASSERT_DEBUG_DFATAL(statement, regex) \
107 GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ 107 GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
108 if (true) { \ 108 if (true) { \
109 (void)(regex); \ 109 (void)(regex); \
110 statement; \ 110 statement; \
111 } else \ 111 } else \
112 GTEST_NONFATAL_FAILURE_("") 112 GTEST_NONFATAL_FAILURE_("")
113 113
114 #endif // NDEBUG 114 #endif // NDEBUG
115 115
116 // The EXPECT_DCHECK and ASSERT_DCHECK macros are similar to EXPECT_DFATAL and
117 // ASSERT_DFATAL. Use them in conjunction with DCHECK that produces no-op in opt
118 // build and LOG_DCHECK (FATAL) if DCHECK_IS_ON().
119
120 #if DCHECK_IS_ON()
121
122 #define EXPECT_DCHECK(statement, regex) \
123 GTEST_DFATAL_(statement, DCHECK, ::testing::ContainsRegex(regex), \
124 GTEST_NONFATAL_FAILURE_)
125 #define ASSERT_DCHECK(statement, regex) \
126 GTEST_DFATAL_(statement, DCHECK, ::testing::ContainsRegex(regex), \
127 GTEST_FATAL_FAILURE_)
128
129 #else // DCHECK_IS_ON()
130
131 #define EXPECT_DCHECK(statement, regex) \
132 GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
133 if (true) { \
Paweł Hajdan Jr. 2016/08/05 14:35:07 Could you explain more this weird "if (true)" code
johnme 2016/08/05 16:57:37 I copy-pasted from EXPECT_DEBUG_DFATAL and ASSERT_
134 (void)(regex); \
135 statement; \
136 } else \
137 GTEST_NONFATAL_FAILURE_("")
138 #define ASSERT_DCHECK(statement, regex) \
139 GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
140 if (true) { \
141 (void)(regex); \
142 statement; \
143 } else \
144 GTEST_NONFATAL_FAILURE_("")
145
146 #endif // DCHECK_IS_ON()
147
116 } // namespace test 148 } // namespace test
117 } // namespace net 149 } // namespace net
118 150
119 #endif // NET_TEST_GTEST_UTIL_H_ 151 #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