Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: test/plugin/ExceptionTest.cpp

Issue 5137721374801920: Issue #1173 - Default behavior for catch-all blocks
Left Patch Set: Add ExceptionDefault() Created March 5, 2015, 2:56 p.m.
Right Patch Set: Changed entry point defaults in CPluginClass to match rebase Created March 20, 2015, 9:50 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« src/plugin/Exception.h ('K') | « src/plugin/PluginStdAfx.h ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2015 Eyeo GmbH 3 * Copyright (C) 2006-2015 Eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 #include <gtest/gtest.h> 18 #include <gtest/gtest.h>
19
20 #include "../../src/plugin/Exception.h" 19 #include "../../src/plugin/Exception.h"
21
sergei 2015/03/06 13:51:12 These lines between #include are not necessary.
Eric 2015/03/06 17:29:59 Done.
22 #include <stdexcept> 20 #include <stdexcept>
23 21
24 void AssignCurrentException(std::exception_ptr& e) 22 void AssignCurrentException(std::exception_ptr& e)
sergei 2015/03/06 13:51:12 I don't think that we need this function
Eric 2015/03/06 17:29:59 It's needed in order for the test 'CurrentExceptio
25 { 23 {
26 e = std::current_exception(); 24 e = std::current_exception();
27 } 25 }
28 26
29 /* 27 /*
30 * This test verifies that std::current_exception works during exception handlin g generally, 28 * This test verifies that std::current_exception works during exception handlin g generally,
31 * not just in the immediate context of a 'catch' clause. 29 * not just in the immediate context of a 'catch' clause.
32 */ 30 */
33 TEST(Exception, CurrentExceptionWorksOutsideCatchHandler) 31 TEST(Exception, CurrentExceptionWorksOutsideCatchHandler)
sergei 2015/03/06 13:51:12 What does it test? Standard C++ library? Even more
Eric 2015/03/06 17:29:59 It checks that the implementation of 'current_exce
34 { 32 {
35 std::exception_ptr ep; 33 std::exception_ptr ep;
36 const auto e = std::runtime_error("BJTCiRhkVAmvMg"); 34 const auto e = std::runtime_error("BJTCiRhkVAmvMg");
37 try 35 try
38 { 36 {
39 throw e; 37 throw e;
40 } 38 }
41 catch (...) 39 catch (...)
42 { 40 {
43 AssignCurrentException(ep); 41 AssignCurrentException(ep);
44 ASSERT_TRUE(ep); 42 ASSERT_TRUE(ep);
45 /* 43 /*
46 * You might think that the next set would be EXPECT_EQ betwwen the thrown e xception and the original one. 44 * You might think that the next set would be EXPECT_EQ betwwen the thrown e xception and the original one.
sergei 2015/03/06 13:51:12 This comment is not necessary.
Eric 2015/03/06 17:29:59 I'm leaving it in because I got caught up with deb
47 * Such a test does not pass, nor is in necessary. 45 * Such a test does not pass, nor is in necessary.
48 * The throw statement above is throwing by value, so it makes a copy. 46 * The throw statement above is throwing by value, so it makes a copy.
49 */ 47 */
50 } 48 }
51 try 49 try
52 { 50 {
53 rethrow_exception(ep); 51 rethrow_exception(ep);
54 FAIL() << "Statement after 'rethrow_exception' executed."; 52 FAIL() << "Statement after 'rethrow_exception' executed.";
55 } 53 }
56 catch (std::runtime_error& ex) 54 catch (std::runtime_error& ex)
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
124 TEST(Exception, TrivialVoidRuntimeError) 122 TEST(Exception, TrivialVoidRuntimeError)
125 { 123 {
126 TrivialVoid(std::runtime_error("")); 124 TrivialVoid(std::runtime_error(""));
127 } 125 }
128 126
129 TEST(Exception, TrivialVoidSystemError) 127 TEST(Exception, TrivialVoidSystemError)
130 { 128 {
131 TrivialVoid(std::system_error(std::error_code())); 129 TrivialVoid(std::system_error(std::error_code()));
132 } 130 }
133 131
134 enum ExceptionCode 132 enum
sergei 2015/03/06 13:51:12 It's not necessary here but I think it would be be
Eric 2015/03/06 17:29:59 I specifically want an unscoped enumeration here.
sergei 2015/03/31 14:30:51 It's not an extra syntax, it helps to understand t
Eric 2015/05/14 14:42:50 I don't agree with you. It provides nothing extra.
sergei 2015/05/15 13:13:24 If you does see it now it does not mean that it pr
135 { 133 {
136 InvalidCode = -1, 134 InvalidCode = -1,
137 UnknownCode = 1, 135 UnknownCode = 1,
138 ExceptionCode, 136 ExceptionCode,
139 LogicErrorCode, 137 LogicErrorCode,
140 RuntimeErrorCode, 138 RuntimeErrorCode,
141 SystemErrorCode 139 SystemErrorCode
142 }; 140 };
143 141
144 struct NullHandlersReturn 142 struct NullHandlersReturn
145 { 143 {
146 typedef int return_t; 144 typedef int ReturnType;
sergei 2015/03/31 14:30:51 It's not related whether it's scoped or not (enume
Eric 2015/05/14 14:42:50 Defining a typedef for the symbol 'return_t' refle
sergei 2015/05/15 13:13:24 I really don't understand attempts to create less
147 static return_t Unknown(int) { return UnknownCode; } 145 static ReturnType Unknown(int) { return UnknownCode; }
148 static return_t Exception(std::exception& ex, int) { return ExceptionCode; } 146 static ReturnType Exception(std::exception& ex, int) { return ExceptionCode; }
149 static return_t LogicError(std::logic_error& ex, int) { return LogicErrorCode; } 147 static ReturnType LogicError(std::logic_error& ex, int) { return LogicErrorCod e; }
150 static return_t RuntimeError(std::runtime_error& ex, int) { return RuntimeErro rCode; } 148 static ReturnType RuntimeError(std::runtime_error& ex, int) { return RuntimeEr rorCode; }
151 static return_t SystemError(std::system_error& ex, int) { return SystemErrorCo de; } 149 static ReturnType SystemError(std::system_error& ex, int) { return SystemError Code; }
152 }; 150 };
153 151
154 template<class X> 152 template<class X>
155 void TrivialReturn(int n, X x) 153 void TrivialReturn(int n, X x)
156 { 154 {
157 ASSERT_NO_THROW( 155 ASSERT_NO_THROW(
158 { 156 {
159 try 157 try
160 { 158 {
161 throw x; 159 throw x;
(...skipping 21 matching lines...) Expand all
183 } 181 }
184 182
185 TEST(Exception, TrivialReturnRuntimeError) 183 TEST(Exception, TrivialReturnRuntimeError)
186 { 184 {
187 TrivialReturn(RuntimeErrorCode, std::runtime_error("")); 185 TrivialReturn(RuntimeErrorCode, std::runtime_error(""));
188 } 186 }
189 187
190 TEST(Exception, TrivialReturnSystemError) 188 TEST(Exception, TrivialReturnSystemError)
191 { 189 {
192 TrivialReturn(SystemErrorCode, std::system_error(std::error_code())); 190 TrivialReturn(SystemErrorCode, std::system_error(std::error_code()));
193 }
194
195 /*
196 * The simple tests ensure that the flow of control arrives in the correct subha ndler.
197 */
198 template<class X, class H>
199 void SimpleVoid(X x, H h)
sergei 2015/03/06 13:51:12 we don't need argument `H h` here as well as we do
Eric 2015/03/06 17:29:59 We need to have 'H' present in order to instantiat
sergei 2015/03/31 14:30:51 How does it simplify? `SimpleVoid(std::runtime_err
Eric 2015/05/14 14:42:50 I picked a different syntax that the one you would
sergei 2015/05/15 13:13:24 So, how does it simplify?
200 {
201 SimpleResult = 0;
Eric 2015/03/06 17:29:59 Good suggestion. Done.
202 SimpleExpected = rand();
sergei 2015/03/06 13:51:12 `std::rand` and seeding is missed.
Eric 2015/03/06 17:29:59 Added namespace scope. If I were to provide a see
203 if (SimpleExpected == 0) { SimpleExpected = 1; }
sergei 2015/03/06 13:51:12 Why do we need random numbers, I would say enum wo
sergei 2015/03/31 14:30:51 Sorry, I don't understand what kind of references
Eric 2015/05/14 14:42:50 The word 'reference' can mean either its generic E
sergei 2015/05/15 13:13:24 Such randomness here does not add any integrity ch
204 ASSERT_NO_THROW(
205 {
206 try
207 {
208 throw x;
209 }
210 catch (...)
211 {
212 CatchAllVoid<H>::Handler(0);
213 }
214 });
215 EXPECT_EQ(SimpleExpected, SimpleResult);
216 } 191 }
217 192
218 /* 193 /*
219 * VS 2012 supports thread_local semantics for POD only, not arbitrary types. 194 * VS 2012 supports thread_local semantics for POD only, not arbitrary types.
220 * That's good enough for now. 195 * That's good enough for now.
221 * Remove the definition when VS matures. 196 * Remove the definition when VS matures.
222 */ 197 */
223 #define thread_local __declspec(thread) 198 #define thread_local __declspec(thread)
224 199
225 /* 200 /*
226 * The sub-handlers are purely static functions, 201 * The sub-handlers are purely static functions,
227 * so getting them to return something unique for testing has possible race co nditions. 202 * so getting them to return something unique for testing has possible race co nditions.
228 * We're using a thread-local variable as a return code from the simple handlers , 203 * We're using a thread-local variable as a return code from the simple handlers ,
229 * which ensures that even a multi-threaded test runner will work here. 204 * which ensures that even a multi-threaded test runner will work here.
230 */ 205 */
231 thread_local int SimpleResult; 206 thread_local int SimpleResult;
232 thread_local int SimpleExpected; 207 thread_local int SimpleExpected;
sergei 2015/03/06 13:51:12 I don't like the idea to use global variables, and
Eric 2015/03/06 17:29:59 Sorry, they're needed here. I tried to get rid of
208
209 /*
210 * The simple tests ensure that the flow of control arrives in the correct subha ndler.
211 */
212 template<class X, class H>
213 void SimpleVoid(X x, H h)
214 {
215 SimpleResult = 0;
216 SimpleExpected = std::rand();
217 if (SimpleExpected == 0) { SimpleExpected = 1; }
218 ASSERT_NO_THROW(
219 {
220 try
221 {
222 throw x;
223 }
224 catch (...)
225 {
226 CatchAllVoid<H>::Handler(0);
227 }
228 });
229 EXPECT_EQ(SimpleExpected, SimpleResult);
230 }
233 231
234 /* 232 /*
235 * The base handler class for the simple tests fails every execution path. 233 * The base handler class for the simple tests fails every execution path.
236 * Each specific test redefines a single one of the handler functions that it us es. 234 * Each specific test redefines a single one of the handler functions that it us es.
237 */ 235 */
238 class SimpleHandlersBase 236 class SimpleHandlersBase
239 { 237 {
240 public: 238 public:
241 static void Unknown(int) { FAIL() << "Unexpected exception of unknown type"; } 239 static void Unknown(int) { FAIL() << "Unexpected exception of unknown type"; }
242 static void Exception(std::exception& ex, int) { FAIL() << "Unexpected std::ex ception"; } 240 static void Exception(std::exception& ex, int) { FAIL() << "Unexpected std::ex ception"; }
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
316 }); 314 });
317 EXPECT_EQ(SimpleExpected, SimpleResult); 315 EXPECT_EQ(SimpleExpected, SimpleResult);
318 } 316 }
319 317
320 /* 318 /*
321 * The base handlers class fails every execution path. 319 * The base handlers class fails every execution path.
322 */ 320 */
323 class SimpleHandlersReturnBase 321 class SimpleHandlersReturnBase
324 { 322 {
325 public: 323 public:
326 typedef int return_t; 324 typedef int ReturnType;
327 static return_t Unknown(int) { ADD_FAILURE() << "Unexpected exception of unkno wn type"; return InvalidCode; } 325 static ReturnType Unknown(int) { ADD_FAILURE() << "Unexpected exception of unk nown type"; return InvalidCode; }
328 static return_t Exception(std::exception& ex, int) { ADD_FAILURE() << "Unexpec ted std::exception"; return InvalidCode; } 326 static ReturnType Exception(std::exception& ex, int) { ADD_FAILURE() << "Unexp ected std::exception"; return InvalidCode; }
329 static return_t LogicError(std::logic_error& ex, int) { ADD_FAILURE() << "Unex pected std::logic_error"; return InvalidCode; } 327 static ReturnType LogicError(std::logic_error& ex, int) { ADD_FAILURE() << "Un expected std::logic_error"; return InvalidCode; }
330 static return_t RuntimeError(std::runtime_error& ex, int) { ADD_FAILURE() << " Unexpected std::runtime_error"; return InvalidCode; } 328 static ReturnType RuntimeError(std::runtime_error& ex, int) { ADD_FAILURE() << "Unexpected std::runtime_error"; return InvalidCode; }
331 static return_t SystemError(std::system_error& ex, int) { ADD_FAILURE() << "Un expected std::system_error"; return InvalidCode; } 329 static ReturnType SystemError(std::system_error& ex, int) { ADD_FAILURE() << " Unexpected std::system_error"; return InvalidCode; }
332 protected: 330 protected:
333 static void Ping() { SimpleResult = SimpleExpected; } 331 static void Ping() { SimpleResult = SimpleExpected; }
334 }; 332 };
335 333
336 TEST(Exception, SimpleReturnUnknown) 334 TEST(Exception, SimpleReturnUnknown)
337 { 335 {
338 struct Handler : 336 struct Handler :
339 public SimpleHandlersReturnBase 337 public SimpleHandlersReturnBase
340 { 338 {
341 static int Unknown(int) { Ping(); return UnknownCode; } 339 static int Unknown(int) { Ping(); return UnknownCode; }
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
376 TEST(Exception, SimpleReturnSystemError) 374 TEST(Exception, SimpleReturnSystemError)
377 { 375 {
378 struct Handler : 376 struct Handler :
379 public SimpleHandlersReturnBase 377 public SimpleHandlersReturnBase
380 { 378 {
381 static int SystemError(std::system_error& ex, int) { Ping(); return SystemEr rorCode; } 379 static int SystemError(std::system_error& ex, int) { Ping(); return SystemEr rorCode; }
382 }; 380 };
383 SimpleReturn(SystemErrorCode, std::system_error(std::error_code()), Handler()) ; 381 SimpleReturn(SystemErrorCode, std::system_error(std::error_code()), Handler()) ;
384 } 382 }
385 383
LEFTRIGHT

Powered by Google App Engine
This is Rietveld