1 #include "UnitTest++/Config.h"
2 #ifndef UNITTEST_NO_EXCEPTIONS
3 
4 #include "UnitTest++/UnitTestPP.h"
5 #include "UnitTest++/CurrentTest.h"
6 #include "RecordingReporter.h"
7 #include "ScopedCurrentTest.h"
8 
9 #include <stdexcept>
10 
11 using namespace std;
12 
13 namespace {
14 
15 int ThrowingFunction()
16 {
17     throw "Doh";
18 }
19 
20 int ThrowingStdExceptionFunction()
21 {
22     throw std::logic_error("Doh");
23 }
24 
25 SUITE(CheckExceptionTests)
26 {
27     struct CheckFixture
28     {
29         CheckFixture()
30           : reporter()
31           , testResults(&reporter)
32         {
33         }
34 
35         void PerformCheckWithNonStdThrow()
36         {
37             ScopedCurrentTest scopedResults(testResults);
38             CHECK(ThrowingFunction() == 1);
39         }
40 
41         void PerformCheckWithStdThrow()
42         {
43             ScopedCurrentTest scopedResults(testResults);
44             CHECK(ThrowingStdExceptionFunction() == 1);
45         }
46 
47         RecordingReporter reporter;
48         UnitTest::TestResults testResults;
49     };
50 
51     TEST_FIXTURE(CheckFixture, CheckFailsOnException)
52     {
53         PerformCheckWithNonStdThrow();
54         CHECK(testResults.GetFailureCount() > 0);
55     }
56 
57     TEST_FIXTURE(CheckFixture, CheckFailsOnStdException)
58     {
59         PerformCheckWithStdThrow();
60         CHECK(testResults.GetFailureCount() > 0);
61     }
62 
63     TEST_FIXTURE(CheckFixture, CheckFailureBecauseOfExceptionIncludesCheckContents)
64     {
65         PerformCheckWithNonStdThrow();
66         CHECK(strstr(reporter.lastFailedMessage, "ThrowingFunction() == 1"));
67     }
68 
69     TEST_FIXTURE(CheckFixture, CheckFailureBecauseOfStdExceptionIncludesCheckContents)
70     {
71         PerformCheckWithStdThrow();
72         CHECK(strstr(reporter.lastFailedMessage, "ThrowingStdExceptionFunction() == 1"));
73     }
74 
75     TEST_FIXTURE(CheckFixture, CheckFailureBecauseOfStandardExceptionIncludesWhat)
76     {
77         PerformCheckWithStdThrow();
78         CHECK(strstr(reporter.lastFailedMessage, "exception (Doh)"));
79     }
80 }
81 
82 SUITE(CheckEqualExceptionTests)
83 {
84     struct CheckEqualFixture
85     {
86         CheckEqualFixture()
87           : reporter()
88           , testResults(&reporter)
89           , line(-1)
90         {
91         }
92 
93         void PerformCheckWithNonStdThrow()
94         {
95             UnitTest::TestDetails const testDetails("testName", "suiteName", "filename", -1);
96             ScopedCurrentTest scopedResults(testResults, &testDetails);
97             CHECK_EQUAL(ThrowingFunction(), 123); line = __LINE__;
98         }
99 
100         void PerformCheckWithStdThrow()
101         {
102             UnitTest::TestDetails const testDetails("testName", "suiteName", "filename", -1);
103             ScopedCurrentTest scopedResults(testResults, &testDetails);
104             CHECK_EQUAL(ThrowingStdExceptionFunction(), 123); line = __LINE__;
105         }
106 
107         RecordingReporter reporter;
108         UnitTest::TestResults testResults;
109         int line;
110     };
111 
112     TEST_FIXTURE(CheckEqualFixture, CheckEqualFailsOnException)
113     {
114         PerformCheckWithNonStdThrow();
115         CHECK(testResults.GetFailureCount() > 0);
116     }
117 
118     TEST_FIXTURE(CheckEqualFixture, CheckEqualFailsOnStdException)
119     {
120         PerformCheckWithStdThrow();
121         CHECK(testResults.GetFailureCount() > 0);
122     }
123 
124     TEST_FIXTURE(CheckEqualFixture, CheckEqualFailureBecauseOfExceptionContainsCorrectDetails)
125     {
126         PerformCheckWithNonStdThrow();
127 
128         CHECK_EQUAL("testName", reporter.lastFailedTest);
129         CHECK_EQUAL("suiteName", reporter.lastFailedSuite);
130         CHECK_EQUAL("filename", reporter.lastFailedFile);
131         CHECK_EQUAL(line, reporter.lastFailedLine);
132     }
133 
134     TEST_FIXTURE(CheckEqualFixture, CheckEqualFailureBecauseOfStdExceptionContainsCorrectDetails)
135     {
136         PerformCheckWithStdThrow();
137 
138         CHECK_EQUAL("testName", reporter.lastFailedTest);
139         CHECK_EQUAL("suiteName", reporter.lastFailedSuite);
140         CHECK_EQUAL("filename", reporter.lastFailedFile);
141         CHECK_EQUAL(line, reporter.lastFailedLine);
142     }
143 
144     TEST_FIXTURE(CheckEqualFixture, CheckEqualFailureBecauseOfExceptionIncludesCheckContents)
145     {
146         PerformCheckWithNonStdThrow();
147 
148         CHECK(strstr(reporter.lastFailedMessage, "ThrowingFunction()"));
149         CHECK(strstr(reporter.lastFailedMessage, "123"));
150     }
151 
152     TEST_FIXTURE(CheckEqualFixture, CheckEqualFailureBecauseOfStdExceptionIncludesCheckContents)
153     {
154         PerformCheckWithStdThrow();
155 
156         CHECK(strstr(reporter.lastFailedMessage, "ThrowingStdExceptionFunction()"));
157         CHECK(strstr(reporter.lastFailedMessage, "123"));
158     }
159 
160     TEST_FIXTURE(CheckEqualFixture, CheckEqualFailureBecauseOfStandardExceptionIncludesWhat)
161     {
162         PerformCheckWithStdThrow();
163 
164         CHECK(strstr(reporter.lastFailedMessage, "exception (Doh)"));
165     }
166 }
167 
168 SUITE(CheckCloseExceptionTests)
169 {
170     struct CheckCloseFixture
171     {
172         CheckCloseFixture()
173           : reporter()
174           , testResults(&reporter)
175           , line(-1)
176         {
177         }
178 
179         void PerformCheckWithNonStdThrow()
180         {
181             UnitTest::TestDetails const testDetails("closeTest", "closeSuite", "filename", -1);
182             ScopedCurrentTest scopedResults(testResults, &testDetails);
183             CHECK_CLOSE(static_cast<float>(ThrowingFunction()), 1.0001f, 0.1f); line = __LINE__;
184         }
185 
186         void PerformCheckWithStdThrow()
187         {
188             UnitTest::TestDetails const testDetails("closeTest", "closeSuite", "filename", -1);
189             ScopedCurrentTest scopedResults(testResults, &testDetails);
190             CHECK_CLOSE(static_cast<float>(ThrowingStdExceptionFunction()), 1.0001f, 0.1f); line = __LINE__;
191         }
192 
193         RecordingReporter reporter;
194         UnitTest::TestResults testResults;
195         int line;
196     };
197 
198     TEST_FIXTURE(CheckCloseFixture, CheckCloseFailsOnException)
199     {
200         PerformCheckWithNonStdThrow();
201 
202         CHECK(testResults.GetFailureCount() > 0);
203     }
204 
205     TEST_FIXTURE(CheckCloseFixture, CheckCloseFailsOnStdException)
206     {
207         PerformCheckWithStdThrow();
208 
209         CHECK(testResults.GetFailureCount() > 0);
210     }
211 
212     TEST_FIXTURE(CheckCloseFixture, CheckCloseFailureBecauseOfExceptionContainsCorrectDetails)
213     {
214         PerformCheckWithNonStdThrow();
215 
216         CHECK_EQUAL("closeTest", reporter.lastFailedTest);
217         CHECK_EQUAL("closeSuite", reporter.lastFailedSuite);
218         CHECK_EQUAL("filename", reporter.lastFailedFile);
219         CHECK_EQUAL(line, reporter.lastFailedLine);
220     }
221 
222     TEST_FIXTURE(CheckCloseFixture, CheckCloseFailureBecauseOfStdExceptionContainsCorrectDetails)
223     {
224         PerformCheckWithStdThrow();
225 
226         CHECK_EQUAL("closeTest", reporter.lastFailedTest);
227         CHECK_EQUAL("closeSuite", reporter.lastFailedSuite);
228         CHECK_EQUAL("filename", reporter.lastFailedFile);
229         CHECK_EQUAL(line, reporter.lastFailedLine);
230     }
231 
232     TEST_FIXTURE(CheckCloseFixture, CheckCloseFailureBecauseOfExceptionIncludesCheckContents)
233     {
234         PerformCheckWithNonStdThrow();
235 
236         CHECK(strstr(reporter.lastFailedMessage, "static_cast<float>(ThrowingFunction())"));
237         CHECK(strstr(reporter.lastFailedMessage, "1.0001f"));
238     }
239 
240     TEST_FIXTURE(CheckCloseFixture, CheckCloseFailureBecauseOfStdExceptionIncludesCheckContents)
241     {
242         PerformCheckWithStdThrow();
243 
244         CHECK(strstr(reporter.lastFailedMessage, "static_cast<float>(ThrowingStdExceptionFunction())"));
245         CHECK(strstr(reporter.lastFailedMessage, "1.0001f"));
246     }
247 
248     TEST_FIXTURE(CheckCloseFixture, CheckCloseFailureBecauseOfStandardExceptionIncludesWhat)
249     {
250         PerformCheckWithStdThrow();
251 
252         CHECK(strstr(reporter.lastFailedMessage, "exception (Doh)"));
253     }
254 }
255 
256 class ThrowingObject
257 {
258 public:
259     float operator[](int) const
260     {
261         throw "Test throw";
262     }
263 };
264 
265 class StdThrowingObject
266 {
267 public:
268     float operator[](int) const
269     {
270         throw std::runtime_error("Test throw");
271     }
272 };
273 
274 SUITE(CheckArrayCloseExceptionTests)
275 {
276     struct CheckArrayCloseFixture
277     {
278         CheckArrayCloseFixture()
279           : reporter()
280           , testResults(&reporter)
281           , line(-1)
282         {
283         }
284 
285         void PerformCheckWithNonStdThrow()
286         {
287             UnitTest::TestDetails const testDetails("arrayCloseTest", "arrayCloseSuite", "filename", -1);
288             ScopedCurrentTest scopedResults(testResults, &testDetails);
289             int const data[4] = { 0, 1, 2, 3 };
290             CHECK_ARRAY_CLOSE(data, ThrowingObject(), 4, 0.01f); line = __LINE__;
291         }
292 
293         void PerformCheckWithStdThrow()
294         {
295             UnitTest::TestDetails const testDetails("arrayCloseTest", "arrayCloseSuite", "filename", -1);
296             ScopedCurrentTest scopedResults(testResults, &testDetails);
297             int const data[4] = { 0, 1, 2, 3 };
298             CHECK_ARRAY_CLOSE(data, StdThrowingObject(), 4, 0.01f); line = __LINE__;
299         }
300 
301         RecordingReporter reporter;
302         UnitTest::TestResults testResults;
303         int line;
304     };
305 
306     TEST_FIXTURE(CheckArrayCloseFixture, CheckFailureBecauseOfExceptionContainsCorrectDetails)
307     {
308         PerformCheckWithNonStdThrow();
309 
310         CHECK_EQUAL("arrayCloseTest", reporter.lastFailedTest);
311         CHECK_EQUAL("arrayCloseSuite", reporter.lastFailedSuite);
312         CHECK_EQUAL("filename", reporter.lastFailedFile);
313         CHECK_EQUAL(line, reporter.lastFailedLine);
314     }
315 
316     TEST_FIXTURE(CheckArrayCloseFixture, CheckFailureBecauseOfStdExceptionContainsCorrectDetails)
317     {
318         PerformCheckWithStdThrow();
319 
320         CHECK_EQUAL("arrayCloseTest", reporter.lastFailedTest);
321         CHECK_EQUAL("arrayCloseSuite", reporter.lastFailedSuite);
322         CHECK_EQUAL("filename", reporter.lastFailedFile);
323         CHECK_EQUAL(line, reporter.lastFailedLine);
324     }
325 
326     TEST_FIXTURE(CheckArrayCloseFixture, CheckFailsOnException)
327     {
328         PerformCheckWithNonStdThrow();
329 
330         CHECK(testResults.GetFailureCount() > 0);
331     }
332 
333     TEST_FIXTURE(CheckArrayCloseFixture, CheckFailsOnStdException)
334     {
335         PerformCheckWithStdThrow();
336 
337         CHECK(testResults.GetFailureCount() > 0);
338     }
339 
340     TEST_FIXTURE(CheckArrayCloseFixture, CheckFailureOnExceptionIncludesCheckContents)
341     {
342         PerformCheckWithNonStdThrow();
343 
344         CHECK(strstr(reporter.lastFailedMessage, "data"));
345         CHECK(strstr(reporter.lastFailedMessage, "ThrowingObject()"));
346     }
347 
348     TEST_FIXTURE(CheckArrayCloseFixture, CheckFailureOnStdExceptionIncludesCheckContents)
349     {
350         PerformCheckWithStdThrow();
351 
352         CHECK(strstr(reporter.lastFailedMessage, "data"));
353         CHECK(strstr(reporter.lastFailedMessage, "StdThrowingObject()"));
354     }
355 
356     TEST_FIXTURE(CheckArrayCloseFixture, CheckFailureOnStdExceptionIncludesWhat)
357     {
358         PerformCheckWithStdThrow();
359 
360         CHECK(strstr(reporter.lastFailedMessage, "exception (Test throw)"));
361     }
362 }
363 
364 SUITE(CheckArrayEqualExceptionTests)
365 {
366     struct CheckArrayEqualFixture
367     {
368         CheckArrayEqualFixture()
369         : reporter()
370         , testResults(&reporter)
371         , line(-1)
372         {
373         }
374 
375         void PerformCheckWithNonStdThrow()
376         {
377             UnitTest::TestDetails const testDetails("arrayEqualTest", "arrayEqualSuite", "filename", -1);
378             ScopedCurrentTest scopedResults(testResults, &testDetails);
379             int const data[4] = { 0, 1, 2, 3 };
380             CHECK_ARRAY_EQUAL(data, ThrowingObject(), 4); line = __LINE__;
381         }
382 
383         void PerformCheckWithStdThrow()
384         {
385             UnitTest::TestDetails const testDetails("arrayEqualTest", "arrayEqualSuite", "filename", -1);
386             ScopedCurrentTest scopedResults(testResults, &testDetails);
387             int const data[4] = { 0, 1, 2, 3 };
388             CHECK_ARRAY_EQUAL(data, StdThrowingObject(), 4); line = __LINE__;
389         }
390 
391         RecordingReporter reporter;
392         UnitTest::TestResults testResults;
393         int line;
394     };
395 
396     TEST_FIXTURE(CheckArrayEqualFixture, CheckFailureBecauseOfExceptionContainsCorrectDetails)
397     {
398         PerformCheckWithNonStdThrow();
399 
400         CHECK_EQUAL("arrayEqualTest", reporter.lastFailedTest);
401         CHECK_EQUAL("arrayEqualSuite", reporter.lastFailedSuite);
402         CHECK_EQUAL("filename", reporter.lastFailedFile);
403         CHECK_EQUAL(line, reporter.lastFailedLine);
404     }
405 
406     TEST_FIXTURE(CheckArrayEqualFixture, CheckFailureBecauseOfStdExceptionContainsCorrectDetails)
407     {
408         PerformCheckWithStdThrow();
409 
410         CHECK_EQUAL("arrayEqualTest", reporter.lastFailedTest);
411         CHECK_EQUAL("arrayEqualSuite", reporter.lastFailedSuite);
412         CHECK_EQUAL("filename", reporter.lastFailedFile);
413         CHECK_EQUAL(line, reporter.lastFailedLine);
414     }
415 
416     TEST_FIXTURE(CheckArrayEqualFixture, CheckFailsOnException)
417     {
418         PerformCheckWithNonStdThrow();
419 
420         CHECK(testResults.GetFailureCount() > 0);
421     }
422 
423     TEST_FIXTURE(CheckArrayEqualFixture, CheckFailsOnStdException)
424     {
425         PerformCheckWithStdThrow();
426 
427         CHECK(testResults.GetFailureCount() > 0);
428     }
429 
430     TEST_FIXTURE(CheckArrayEqualFixture, CheckFailureOnExceptionIncludesCheckContents)
431     {
432         PerformCheckWithNonStdThrow();
433 
434         CHECK(strstr(reporter.lastFailedMessage, "data"));
435         CHECK(strstr(reporter.lastFailedMessage, "ThrowingObject()"));
436     }
437 
438     TEST_FIXTURE(CheckArrayEqualFixture, CheckFailureOnStdExceptionIncludesCheckContents)
439     {
440         PerformCheckWithStdThrow();
441 
442         CHECK(strstr(reporter.lastFailedMessage, "data"));
443         CHECK(strstr(reporter.lastFailedMessage, "StdThrowingObject()"));
444     }
445 
446     TEST_FIXTURE(CheckArrayEqualFixture, CheckFailureOnStdExceptionIncludesWhat)
447     {
448         PerformCheckWithStdThrow();
449 
450         CHECK(strstr(reporter.lastFailedMessage, "exception (Test throw)"));
451     }
452 }
453 
454 SUITE(CheckArray2DExceptionTests)
455 {
456     class ThrowingObject2D
457     {
458     public:
459         float* operator[](int) const
460         {
461             throw "Test throw";
462         }
463     };
464 
465     class StdThrowingObject2D
466     {
467     public:
468         float* operator[](int) const
469         {
470             throw std::runtime_error("Test throw");
471         }
472     };
473 
474     struct CheckArray2DCloseFixture
475     {
476         CheckArray2DCloseFixture()
477           : reporter()
478           , testResults(&reporter)
479           , line(-1)
480         {
481         }
482 
483         void PerformCheckWithNonStdThrow()
484         {
485             UnitTest::TestDetails const testDetails("array2DCloseTest", "array2DCloseSuite", "filename", -1);
486             ScopedCurrentTest scopedResults(testResults, &testDetails);
487             const float data[2][2] = { {0, 1}, {2, 3} };
488             CHECK_ARRAY2D_CLOSE(data, ThrowingObject2D(), 2, 2, 0.01f); line = __LINE__;
489         }
490 
491         void PerformCheckWithStdThrow()
492         {
493             UnitTest::TestDetails const testDetails("array2DCloseTest", "array2DCloseSuite", "filename", -1);
494             ScopedCurrentTest scopedResults(testResults, &testDetails);
495             const float data[2][2] = { {0, 1}, {2, 3} };
496             CHECK_ARRAY2D_CLOSE(data, StdThrowingObject2D(), 2, 2, 0.01f); line = __LINE__;
497         }
498 
499         RecordingReporter reporter;
500         UnitTest::TestResults testResults;
501         int line;
502     };
503 
504     TEST_FIXTURE(CheckArray2DCloseFixture, CheckFailureBecauseOfExceptionContainsCorrectDetails)
505     {
506         PerformCheckWithNonStdThrow();
507 
508         CHECK_EQUAL("array2DCloseTest", reporter.lastFailedTest);
509         CHECK_EQUAL("array2DCloseSuite", reporter.lastFailedSuite);
510         CHECK_EQUAL("filename", reporter.lastFailedFile);
511         CHECK_EQUAL(line, reporter.lastFailedLine);
512     }
513 
514     TEST_FIXTURE(CheckArray2DCloseFixture, CheckFailureBecauseOfStdExceptionContainsCorrectDetails)
515     {
516         PerformCheckWithStdThrow();
517 
518         CHECK_EQUAL("array2DCloseTest", reporter.lastFailedTest);
519         CHECK_EQUAL("array2DCloseSuite", reporter.lastFailedSuite);
520         CHECK_EQUAL("filename", reporter.lastFailedFile);
521         CHECK_EQUAL(line, reporter.lastFailedLine);
522     }
523 
524     TEST_FIXTURE(CheckArray2DCloseFixture, CheckFailsOnException)
525     {
526         PerformCheckWithNonStdThrow();
527 
528         CHECK(testResults.GetFailureCount() > 0);
529     }
530 
531     TEST_FIXTURE(CheckArray2DCloseFixture, CheckFailsOnStdException)
532     {
533         PerformCheckWithStdThrow();
534 
535         CHECK(testResults.GetFailureCount() > 0);
536     }
537 
538     TEST_FIXTURE(CheckArray2DCloseFixture, CheckFailureOnExceptionIncludesCheckContents)
539     {
540         PerformCheckWithNonStdThrow();
541 
542         CHECK(strstr(reporter.lastFailedMessage, "data"));
543         CHECK(strstr(reporter.lastFailedMessage, "ThrowingObject2D()"));
544     }
545 
546     TEST_FIXTURE(CheckArray2DCloseFixture, CheckFailureOnStdExceptionIncludesCheckContents)
547     {
548         PerformCheckWithStdThrow();
549 
550         CHECK(strstr(reporter.lastFailedMessage, "data"));
551         CHECK(strstr(reporter.lastFailedMessage, "StdThrowingObject2D()"));
552     }
553 
554     TEST_FIXTURE(CheckArray2DCloseFixture, CheckFailureOnStdExceptionIncludesWhat)
555     {
556         PerformCheckWithStdThrow();
557 
558         CHECK(strstr(reporter.lastFailedMessage, "exception (Test throw)"));
559     }
560 }
561 }
562 
563 #endif
564