1
2 //
3 // This source file is part of appleseed.
4 // Visit https://appleseedhq.net/ for additional information and resources.
5 //
6 // This software is released under the MIT license.
7 //
8 // Copyright (c) 2010-2013 Francois Beaune, Jupiter Jazz Limited
9 // Copyright (c) 2014-2018 Francois Beaune, The appleseedhq Organization
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining a copy
12 // of this software and associated documentation files (the "Software"), to deal
13 // in the Software without restriction, including without limitation the rights
14 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 // copies of the Software, and to permit persons to whom the Software is
16 // furnished to do so, subject to the following conditions:
17 //
18 // The above copyright notice and this permission notice shall be included in
19 // all copies or substantial portions of the Software.
20 //
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 // THE SOFTWARE.
28 //
29
30 // appleseed.foundation headers.
31 #include "foundation/utility/filter.h"
32 #include "foundation/utility/test.h"
33
34 // Standard headers.
35 #include <cstddef>
36
37 #undef FOUNDATION_ENABLE_FALSE_ASSERTIONS_CHECKS
38
39 using namespace foundation;
40
41 namespace
42 {
43 struct FakeTestListener
44 : public TestListenerBase
45 {
release__anon2745589a0111::FakeTestListener46 void release() override
47 {
48 delete this;
49 }
50 };
51
52 struct FakeTestSuite
53 : public TestSuite
54 {
FakeTestSuite__anon2745589a0111::FakeTestSuite55 FakeTestSuite()
56 : TestSuite("FakeTestSuite") {}
57 };
58
59 struct FakeTestCase
60 : public ITestCase
61 {
get_name__anon2745589a0111::FakeTestCase62 const char* get_name() const override
63 {
64 return "FakeTestCase";
65 }
66
run__anon2745589a0111::FakeTestCase67 void run(ITestListener& test_listener, TestResult& case_result) override {}
68 };
69
70 struct FakeTestCaseFactory
71 : public ITestCaseFactory
72 {
get_name__anon2745589a0111::FakeTestCaseFactory73 const char* get_name() const override
74 {
75 return "FakeTestCase";
76 }
77
create__anon2745589a0111::FakeTestCaseFactory78 FakeTestCase* create() override
79 {
80 return new FakeTestCase();
81 }
82 };
83 }
84
TEST_SUITE(Foundation_Utility_Test_TestResult)85 TEST_SUITE(Foundation_Utility_Test_TestResult)
86 {
87 TEST_CASE(TestResultInitializesTestSuiteMetricsCorrectly)
88 {
89 TestResult result;
90
91 EXPECT_EQ(0, result.get_suite_execution_count());
92 EXPECT_EQ(0, result.get_suite_failure_count());
93 EXPECT_EQ(0, result.get_case_execution_count());
94 EXPECT_EQ(0, result.get_case_failure_count());
95 EXPECT_EQ(0, result.get_assertion_execution_count());
96 EXPECT_EQ(0, result.get_assertion_failure_count());
97 }
98
99 TEST_CASE(TestResultTracksTestSuiteMetricsCorrectly)
100 {
101 TestResult result;
102
103 result.signal_suite_execution();
104 result.signal_suite_execution();
105 result.signal_suite_failure();
106
107 EXPECT_EQ(2, result.get_suite_execution_count());
108 EXPECT_EQ(1, result.get_suite_failure_count());
109 }
110
111 TEST_CASE(TestResultTracksTestCaseMetricsCorrectly)
112 {
113 TestResult result;
114
115 result.signal_case_execution();
116 result.signal_case_execution();
117 result.signal_case_failure();
118
119 EXPECT_EQ(2, result.get_case_execution_count());
120 EXPECT_EQ(1, result.get_case_failure_count());
121 }
122
123 TEST_CASE(TestResultTracksAssertionMetricsCorrectly)
124 {
125 TestResult result;
126
127 result.signal_assertion_execution();
128 result.signal_assertion_execution();
129 result.signal_assertion_failure();
130
131 EXPECT_EQ(2, result.get_assertion_execution_count());
132 EXPECT_EQ(1, result.get_assertion_failure_count());
133 }
134
135 TEST_CASE(TestResultsGetMergedCorrectly)
136 {
137 TestResult result1;
138 result1.signal_suite_execution();
139 result1.signal_suite_execution();
140 result1.signal_suite_failure();
141 result1.signal_case_execution();
142 result1.signal_case_execution();
143 result1.signal_case_failure();
144
145 TestResult result2;
146 result2.signal_assertion_execution();
147 result2.signal_assertion_execution();
148 result2.signal_assertion_failure();
149
150 result2.merge(result1);
151
152 EXPECT_EQ(2, result2.get_suite_execution_count());
153 EXPECT_EQ(1, result2.get_suite_failure_count());
154 EXPECT_EQ(2, result2.get_case_execution_count());
155 EXPECT_EQ(1, result2.get_case_failure_count());
156 EXPECT_EQ(2, result2.get_assertion_execution_count());
157 EXPECT_EQ(1, result2.get_assertion_failure_count());
158 }
159 }
160
TEST_SUITE(Foundation_Utility_Test_TestSuiteRepository)161 TEST_SUITE(Foundation_Utility_Test_TestSuiteRepository)
162 {
163 TEST_CASE(GetSuiteCount_GivenTestSuiteRepositoryWithOneSuite_ReturnsOne)
164 {
165 TestSuiteRepository repository;
166 FakeTestSuite suite;
167 repository.register_suite(&suite);
168
169 const size_t suite_count = repository.get_suite_count();
170
171 EXPECT_EQ(1, suite_count);
172 }
173
174 TEST_CASE(GetSuite_GivenTestSuiteRepositoryWithOneSuite_ReturnsTestSuite)
175 {
176 TestSuiteRepository repository;
177 FakeTestSuite expected_suite;
178 repository.register_suite(&expected_suite);
179
180 const TestSuite* suite = repository.get_suite(0);
181
182 EXPECT_EQ(&expected_suite, suite);
183 }
184
185 TEST_CASE(Run_GivenTestSuiteRepositoryWithOneEmptyTestSuite_DoesNotReportTestSuiteExecution)
186 {
187 FakeTestSuite suite;
188
189 TestSuiteRepository repository;
190 repository.register_suite(&suite);
191
192 FakeTestListener listener;
193 TestResult result;
194 repository.run(listener, result);
195
196 EXPECT_EQ(0, result.get_suite_execution_count());
197 }
198
199 TEST_CASE(Run_GivenTestSuiteRepositoryWithOneNonEmptyTestSuite_ReportsTestSuiteExecution)
200 {
201 FakeTestSuite suite;
202
203 FakeTestCaseFactory test_case_factory;
204 suite.register_case(&test_case_factory);
205
206 TestSuiteRepository repository;
207 repository.register_suite(&suite);
208
209 FakeTestListener listener;
210 TestResult result;
211 repository.run(listener, result);
212
213 EXPECT_EQ(1, result.get_suite_execution_count());
214 }
215 }
216
TEST_SUITE(Foundation_Utility_Test_TestSuite)217 TEST_SUITE(Foundation_Utility_Test_TestSuite)
218 {
219 struct PassingTestCase
220 : public ITestCase
221 {
222 size_t& m_run_count;
223
224 explicit PassingTestCase(size_t& run_count)
225 : m_run_count(run_count) {}
226
227 const char* get_name() const override
228 {
229 return "PassingTestCase";
230 }
231
232 void run(ITestListener& test_listener, TestResult& case_result) override
233 {
234 ++m_run_count;
235 }
236 };
237
238 struct PassingTestCaseFactory
239 : public ITestCaseFactory
240 {
241 size_t& m_run_count;
242
243 explicit PassingTestCaseFactory(size_t& run_count)
244 : m_run_count(run_count) {}
245
246 const char* get_name() const override
247 {
248 return "PassingTestCase";
249 }
250
251 PassingTestCase* create() override
252 {
253 return new PassingTestCase(m_run_count);
254 }
255 };
256
257 TEST_CASE(Run_GivenTestSuiteWithOneCase_RunsCase)
258 {
259 size_t run_count = 0;
260 PassingTestCaseFactory test_case_factory(run_count);
261
262 FakeTestSuite test_suite;
263 test_suite.register_case(&test_case_factory);
264
265 FakeTestListener listener;
266 TestResult result;
267 test_suite.run(listener, result);
268
269 EXPECT_EQ(1, run_count);
270 }
271
272 struct FailingTestCase
273 : public ITestCase
274 {
275 const char* get_name() const override
276 {
277 return "FailingTestCase";
278 }
279
280 void run(ITestListener& test_listener, TestResult& case_result) override
281 {
282 case_result.signal_assertion_failure();
283 }
284 };
285
286 struct FailingTestCaseFactory
287 : public ITestCaseFactory
288 {
289 const char* get_name() const override
290 {
291 return "FailingTestCase";
292 }
293
294 FailingTestCase* create() override
295 {
296 return new FailingTestCase();
297 }
298 };
299
300 struct TestListenerCapturingTestSuiteResults
301 : public TestListenerBase
302 {
303 TestResult m_test_suite_result;
304
305 void release() override
306 {
307 delete this;
308 }
309
310 void end_suite(
311 const TestSuite& test_suite,
312 const TestResult& test_suite_result,
313 const TestResult& cumulated_result) override
314 {
315 m_test_suite_result = test_suite_result;
316 }
317 };
318
319 TEST_CASE(Run_GivenFailingTestCase_ReportsTestSuiteFailure)
320 {
321 FailingTestCaseFactory test_case_factory;
322
323 FakeTestSuite test_suite;
324 test_suite.register_case(&test_case_factory);
325
326 TestListenerCapturingTestSuiteResults listener;
327 TestResult result;
328 test_suite.run(listener, result);
329
330 EXPECT_EQ(1, listener.m_test_suite_result.get_suite_failure_count());
331 }
332
333 TEST_CASE(Run_GivenTestSuiteRejectedByFilter_DoesNotReportTestSuiteExecution)
334 {
335 FakeTestSuite test_suite;
336 RejectAllFilter filter;
337 FakeTestListener listener;
338 TestResult result;
339
340 test_suite.run(filter, listener, result);
341
342 EXPECT_EQ(0, result.get_suite_execution_count());
343 }
344 }
345
TEST_SUITE(Foundation_Utility_Test_Assertions)346 TEST_SUITE(Foundation_Utility_Test_Assertions)
347 {
348 TEST_CASE(ExpectTrue_GivenTrue_Succeeds)
349 {
350 EXPECT_TRUE(true);
351 }
352
353 #ifdef FOUNDATION_ENABLE_FALSE_ASSERTIONS_CHECKS
354 TEST_CASE(ExpectTrue_GivenFalse_Fails)
355 {
356 EXPECT_TRUE(false);
357 }
358 #endif
359
360 TEST_CASE(ExpectFalse_GivenFalse_Succeeds)
361 {
362 EXPECT_FALSE(false);
363 }
364
365 #ifdef FOUNDATION_ENABLE_FALSE_ASSERTIONS_CHECKS
366 TEST_CASE(ExpectFalse_GivenTrue_Fails)
367 {
368 EXPECT_FALSE(true);
369 }
370 #endif
371
372 TEST_CASE(ExpectEq_GivenEqualValues_Succeeds)
373 {
374 EXPECT_EQ(42, 42);
375 }
376
377 #ifdef FOUNDATION_ENABLE_FALSE_ASSERTIONS_CHECKS
378 TEST_CASE(ExpectEq_GivenDifferentValues_Fails)
379 {
380 EXPECT_EQ(42, 0);
381 }
382 #endif
383
384 #ifdef FOUNDATION_ENABLE_FALSE_ASSERTIONS_CHECKS
385 TEST_CASE(ExpectEq_ExpectingNonNullCStringButGivenNullCString_Fails)
386 {
387 const char* Expected = "bunny";
388 const char* Obtained = 0;
389
390 EXPECT_EQ(Expected, Obtained);
391 }
392
393 TEST_CASE(ExpectEq_ExpectingNullCStringButGivenNonNullCString_Fails)
394 {
395 const char* Expected = 0;
396 const char* Obtained = "bunny";
397
398 EXPECT_EQ(Expected, Obtained);
399 }
400 #endif
401
402 TEST_CASE(ExpectArrayEq_GivenEqualArrays_Succeeds)
403 {
404 const int Expected[] = { 1, 2, 3 };
405 const int Array[] = { 1, 2, 3 };
406
407 EXPECT_ARRAY_EQ(Expected, Array);
408 }
409
410 #ifdef FOUNDATION_ENABLE_FALSE_ASSERTIONS_CHECKS
411 TEST_CASE(ExpectArrayEq_GivenArraysOfSameSizeButDifferentContent_Fails)
412 {
413 const int Expected[] = { 1, 2, 3 };
414 const int Array[] = { 1, 2, 0 };
415
416 EXPECT_ARRAY_EQ(Expected, Array);
417 }
418
419 TEST_CASE(ExpectArrayEq_GivenArraysOfDifferentSizes_Fails)
420 {
421 const int Expected[] = { 1, 2, 3 };
422 const int Array[] = { 1, 2 };
423
424 EXPECT_ARRAY_EQ(Expected, Array);
425 }
426 #endif
427
428 TEST_CASE(ExpectSequenceEq_GivenEqualArrays_Succeeds)
429 {
430 const int Expected[] = { 1, 2, 3 };
431 const int Array[] = { 1, 2, 3 };
432
433 EXPECT_SEQUENCE_EQ(3, Expected, Array);
434 }
435
436 #ifdef FOUNDATION_ENABLE_FALSE_ASSERTIONS_CHECKS
437 TEST_CASE(ExpectSequenceEq_GivenDifferentArrays_Fails)
438 {
439 const int Expected[] = { 1, 2, 3 };
440 const int Array[] = { 1, 2, 0 };
441
442 EXPECT_SEQUENCE_EQ(3, Expected, Array);
443 }
444 #endif
445
446 TEST_CASE(ExpectNeq_GivenDifferentValues_Succeeds)
447 {
448 EXPECT_NEQ(42, 0);
449 }
450
451 #ifdef FOUNDATION_ENABLE_FALSE_ASSERTIONS_CHECKS
452 TEST_CASE(ExpectNeq_GivenEqualValues_Fails)
453 {
454 EXPECT_NEQ(42, 42);
455 }
456 #endif
457
458 TEST_CASE(ExpectFeq_GivenEqualValues_Succeeds)
459 {
460 EXPECT_FEQ(42.0, 42.0);
461 }
462
463 #ifdef FOUNDATION_ENABLE_FALSE_ASSERTIONS_CHECKS
464 TEST_CASE(ExpectFeq_GivenDifferentValues_Fails)
465 {
466 EXPECT_FEQ(42.0, 0.0);
467 }
468 #endif
469
470 TEST_CASE(ExpectArrayFeq_GivenEqualArrays_Succeeds)
471 {
472 const double Expected[] = { 1.0, 2.0, 3.0 };
473 const double Array[] = { 1.0, 2.0, 3.0 };
474
475 EXPECT_ARRAY_FEQ(Expected, Array);
476 }
477
478 #ifdef FOUNDATION_ENABLE_FALSE_ASSERTIONS_CHECKS
479 TEST_CASE(ExpectArrayFeq_GivenArraysOfSameSizeButDifferentContent_Fails)
480 {
481 const double Expected[] = { 1.0, 2.0, 3.0 };
482 const double Array[] = { 1.0, 2.0, 0.0 };
483
484 EXPECT_ARRAY_FEQ(Expected, Array);
485 }
486
487 TEST_CASE(ExpectArrayFeq_GivenArraysOfDifferentSizes_Fails)
488 {
489 const double Expected[] = { 1.0, 2.0, 3.0 };
490 const double Array[] = { 1.0, 2.0 };
491
492 EXPECT_ARRAY_FEQ(Expected, Array);
493 }
494 #endif
495
496 TEST_CASE(ExpectSequenceFeq_GivenEqualArrays_Succeeds)
497 {
498 const double Expected[] = { 1.0, 2.0, 3.0 };
499 const double Array[] = { 1.0, 2.0, 3.0 };
500
501 EXPECT_SEQUENCE_FEQ(3, Expected, Array);
502 }
503
504 #ifdef FOUNDATION_ENABLE_FALSE_ASSERTIONS_CHECKS
505 TEST_CASE(ExpectSequenceFeq_GivenDifferentArrays_Fails)
506 {
507 const double Expected[] = { 1.0, 2.0, 3.0 };
508 const double Array[] = { 1.0, 2.0, 0.0 };
509
510 EXPECT_SEQUENCE_FEQ(3, Expected, Array);
511 }
512 #endif
513
514 TEST_CASE(ExpectFneq_GivenDifferentValues_Succeeds)
515 {
516 EXPECT_FNEQ(42.0, 0.0);
517 }
518
519 #ifdef FOUNDATION_ENABLE_FALSE_ASSERTIONS_CHECKS
520 TEST_CASE(ExpectFneq_GivenEqualValues_Fails)
521 {
522 EXPECT_FNEQ(42.0, 42.0);
523 }
524 #endif
525
526 struct Exception {};
527
528 TEST_CASE(ExpectException_GivenCodeThatThrowsExpectedException_Succeeds)
529 {
530 EXPECT_EXCEPTION(Exception,
531 {
532 throw Exception();
533 });
534 }
535
536 #ifdef FOUNDATION_ENABLE_FALSE_ASSERTIONS_CHECKS
537 TEST_CASE(ExpectException_GivenCodeThatDoesNotThrowExpectedException_Fails)
538 {
539 EXPECT_EXCEPTION(Exception,
540 {
541 });
542 }
543
544 TEST_CASE(Test_GivenCodeThatThrowsUnexpectedException_Fails)
545 {
546 throw Exception();
547 }
548 #endif
549 }
550