LEFT | RIGHT |
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 | |
22 #include <stdexcept> | 20 #include <stdexcept> |
23 | 21 |
24 void AssignCurrentException(std::exception_ptr& e) | 22 void AssignCurrentException(std::exception_ptr& e) |
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. |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 |
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; |
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 Loading... |
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) | |
200 { | |
201 SimpleResult = 0; | |
202 SimpleExpected = rand(); | |
203 if (SimpleExpected == 0) { SimpleExpected = 1; } | |
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; |
| 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 Loading... |
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 Loading... |
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 |
LEFT | RIGHT |