onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 1 | #define DOCTEST_CONFIG_DISABLE |
| 2 | |
| 3 | #include "doctest.h" |
| 4 | |
| 5 | #ifdef _MSC_VER |
| 6 | #pragma warning(disable : 4505) // unreferenced local functions being removed |
| 7 | #endif // _MSC_VER |
| 8 | |
| 9 | #ifdef __clang__ |
| 10 | #pragma clang diagnostic ignored "-Wunneeded-internal-declaration" |
| 11 | #endif // __clang__ |
| 12 | |
| 13 | using doctest::Approx; |
| 14 | |
| 15 | static int throws(bool in) { |
| 16 | if(in) |
onqtam | 4b68df3 | 2017-03-17 20:45:54 +0200 | [diff] [blame] | 17 | #ifndef DOCTEST_CONFIG_NO_EXCEPTIONS |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 18 | throw "whops!"; |
onqtam | 4b68df3 | 2017-03-17 20:45:54 +0200 | [diff] [blame] | 19 | #else // DOCTEST_CONFIG_NO_EXCEPTIONS |
| 20 | return 0; |
| 21 | #endif // DOCTEST_CONFIG_NO_EXCEPTIONS |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 22 | return 42; |
| 23 | } |
| 24 | |
onqtam | 658c870 | 2017-03-14 14:08:22 +0200 | [diff] [blame] | 25 | TEST_SUITE_BEGIN("the testsuite!"); |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 26 | |
| 27 | #define DO_STUFF() \ |
| 28 | CHECK(1 == 0); \ |
| 29 | CHECK_FALSE(1 == 0); \ |
| 30 | CHECK(Approx(0.502) == 0.501); \ |
| 31 | CHECK(1 == 1); \ |
| 32 | REQUIRE(1 == 1); \ |
| 33 | CHECK_FALSE(0); \ |
| 34 | REQUIRE_FALSE(0); \ |
onqtam | cc9e865 | 2016-08-02 14:23:38 +0300 | [diff] [blame] | 35 | CHECK_THROWS(throws(true)); \ |
| 36 | REQUIRE_THROWS(throws(true)); \ |
| 37 | CHECK_THROWS_AS(throws(true), int); \ |
| 38 | REQUIRE_THROWS_AS(throws(true), char); \ |
| 39 | CHECK_NOTHROW(throws(false)); \ |
| 40 | REQUIRE_NOTHROW(throws(false)); \ |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 41 | SUBCASE("") {} |
| 42 | |
| 43 | // in a separate function because the TEST_CASE() macro will expand to an uninstantiated template |
| 44 | // and we want to ensure this code is parsed (MSVC will not if it is in an uninstantiated template) |
| 45 | static void f() { DO_STUFF(); } |
| 46 | |
| 47 | TEST_CASE("ops") { |
| 48 | f(); |
| 49 | |
| 50 | throws(false); |
| 51 | |
| 52 | DO_STUFF(); |
| 53 | } |
| 54 | |
| 55 | TEST_SUITE_END(); |
| 56 | |
| 57 | // to silence GCC warnings when inheriting from the class TheFixture which has no virtual destructor |
| 58 | #if defined(__GNUC__) && !defined(__clang__) |
| 59 | #pragma GCC diagnostic ignored "-Weffc++" |
| 60 | #endif // __GNUC__ |
| 61 | |
| 62 | struct TheFixture |
| 63 | { |
| 64 | int data; |
| 65 | TheFixture() |
| 66 | : data(42) { |
| 67 | // setup here |
| 68 | } |
| 69 | |
| 70 | ~TheFixture() { |
| 71 | // teardown here |
| 72 | } |
| 73 | }; |
| 74 | |
onqtam | 658c870 | 2017-03-14 14:08:22 +0200 | [diff] [blame] | 75 | TEST_SUITE("the testsuite!") { |
| 76 | TEST_CASE_FIXTURE(TheFixture, "test with a fixture - 1") { |
| 77 | data /= 2; |
| 78 | CHECK(data == 21); |
| 79 | } |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 80 | } |