ncihnegn | c5458f2 | 2019-01-28 05:08:18 -0800 | [diff] [blame] | 1 | #include <doctest/doctest.h> |
onqtam | 3ac4c3f | 2018-06-01 15:31:06 +0300 | [diff] [blame] | 2 | |
Stefan | db758e0 | 2022-06-06 03:42:02 +0200 | [diff] [blame] | 3 | #include "header.h" |
onqtam | 3ac4c3f | 2018-06-01 15:31:06 +0300 | [diff] [blame] | 4 | |
| 5 | DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_BEGIN |
| 6 | #include <ostream> |
| 7 | #include <sstream> |
Stefan | 8acee4f | 2022-01-11 19:35:14 +0100 | [diff] [blame] | 8 | #include <stdexcept> |
onqtam | 3ac4c3f | 2018-06-01 15:31:06 +0300 | [diff] [blame] | 9 | DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_END |
| 10 | |
| 11 | #ifndef DOCTEST_CONFIG_DISABLE |
| 12 | |
| 13 | // ================================================================================================= |
| 14 | // !!! THESE ARE NOT PROPER EXAMPLES OF LIBRARY USAGE !!! THESE ARE MEANT FOR CODE COVERAGE ONLY !!! |
| 15 | // ================================================================================================= |
| 16 | |
| 17 | TEST_CASE("exercising tricky code paths of doctest") { |
| 18 | using namespace doctest; |
| 19 | |
| 20 | // trigger code path for comparing the file in "operator<" of SubcaseSignature |
onqtam | dca8b66 | 2019-09-22 17:34:50 +0300 | [diff] [blame] | 21 | CHECK(SubcaseSignature{"", "a.cpp", 0} < SubcaseSignature{"", "b.cpp", 0}); |
onqtam | 3ac4c3f | 2018-06-01 15:31:06 +0300 | [diff] [blame] | 22 | // same for String |
| 23 | CHECK(String("a.cpp") < String("b.cpp")); |
| 24 | |
| 25 | // trigger code path for string with nullptr |
| 26 | String str; |
| 27 | const String const_str("omgomgomg"); |
| 28 | str = const_str.c_str(); |
| 29 | CHECK(const_str[0] == 'o'); |
| 30 | CHECK(str.capacity() == 24); |
| 31 | CHECK(str.size() == const_str.size()); |
| 32 | CHECK_MESSAGE(str.compare(const_str, true) != 0, "should fail"); |
| 33 | CHECK_MESSAGE(str.compare("omgomgomg", false) != 0, "should fail"); |
| 34 | |
| 35 | String heap_str("012345678901234567890123456789"); |
| 36 | CHECK(heap_str.capacity() == heap_str.size() + 1); // on heap with maxed capacity |
| 37 | heap_str += "0123456789"; |
| 38 | CHECK(heap_str.capacity() > heap_str.size() + 1); |
| 39 | heap_str += "0123456789"; // triggers path in += |
| 40 | CHECK(heap_str[heap_str.size() - 1] == '9'); |
onqtam | 8d5eafa | 2019-03-21 12:19:25 +0200 | [diff] [blame] | 41 | heap_str = ""; |
onqtam | 3ac4c3f | 2018-06-01 15:31:06 +0300 | [diff] [blame] | 42 | |
| 43 | CHECK(String("abc") == "abc"); |
| 44 | CHECK(String("abc") > "aaa"); |
| 45 | CHECK(String("abc") >= "aaa"); |
| 46 | CHECK(String("abc") < "bbb"); |
| 47 | CHECK(String("abc") <= "bbb"); |
| 48 | CHECK(String("abc")[0] == 'a'); |
| 49 | |
| 50 | // toString |
| 51 | str += toString("aaa") // |
| 52 | + toString(nullptr) // |
| 53 | + toString(true) // |
Stefan | e8ba771 | 2022-04-28 02:47:24 +0200 | [diff] [blame] | 54 | + toString(0u) // |
onqtam | 3ac4c3f | 2018-06-01 15:31:06 +0300 | [diff] [blame] | 55 | + toString('c') // |
| 56 | + toString(static_cast<signed char>('c')) // |
| 57 | + toString(static_cast<unsigned char>(1)) // |
| 58 | + toString(static_cast<short>(1)) // |
Stefan | e8ba771 | 2022-04-28 02:47:24 +0200 | [diff] [blame] | 59 | + toString(1L) // |
| 60 | + toString(1UL) // |
onqtam | 3ac4c3f | 2018-06-01 15:31:06 +0300 | [diff] [blame] | 61 | + toString(static_cast<unsigned short>(1)) // |
Stefan | e8ba771 | 2022-04-28 02:47:24 +0200 | [diff] [blame] | 62 | + toString(1LL) // |
| 63 | + toString(1ULL); |
onqtam | 3ac4c3f | 2018-06-01 15:31:06 +0300 | [diff] [blame] | 64 | |
| 65 | std::ostringstream oss; |
| 66 | |
onqtam | 3ac4c3f | 2018-06-01 15:31:06 +0300 | [diff] [blame] | 67 | // trigger code path for String to ostream through operator<< |
| 68 | oss << str; |
| 69 | // trigger code path for assert string of a non-existent assert type |
Stefan | 8acee4f | 2022-01-11 19:35:14 +0100 | [diff] [blame] | 70 | #ifndef DOCTEST_CONFIG_NO_EXCEPTIONS |
| 71 | try { |
| 72 | assertString(static_cast<assertType::Enum>(3)); |
| 73 | } catch (const std::logic_error&) { } |
| 74 | #endif |
onqtam | 3ac4c3f | 2018-06-01 15:31:06 +0300 | [diff] [blame] | 75 | str += oss.str().c_str(); |
onqtam | acbcd12 | 2019-03-02 21:24:18 +0200 | [diff] [blame] | 76 | str += failureString(assertType::is_normal); |
Stefan | e8ba771 | 2022-04-28 02:47:24 +0200 | [diff] [blame] | 77 | CHECK(str == "omgomgomgaaanullptrtrue099991111111" |
| 78 | "omgomgomgaaanullptrtrue099991111111"); |
onqtam | 3ac4c3f | 2018-06-01 15:31:06 +0300 | [diff] [blame] | 79 | // trigger code path for rawMemoryToString |
| 80 | bool isThereAnything = str.size() > 0u; |
onqtam | 3ac4c3f | 2018-06-01 15:31:06 +0300 | [diff] [blame] | 81 | String unknown = toString(skip()); // trigger code path for "{?}" |
| 82 | str = unknown; // trigger code path for deleting memory in operator= |
Stefan | e8ba771 | 2022-04-28 02:47:24 +0200 | [diff] [blame] | 83 | CHECK_FALSE_MESSAGE(isThereAnything, "should fail"); |
onqtam | 3ac4c3f | 2018-06-01 15:31:06 +0300 | [diff] [blame] | 84 | |
| 85 | Approx a(5); |
| 86 | a.scale(4); |
| 87 | Approx b = a(7); |
| 88 | |
Stefan | e8ba771 | 2022-04-28 02:47:24 +0200 | [diff] [blame] | 89 | CHECK(b == 7); |
| 90 | CHECK(b != 6); |
| 91 | CHECK(b > 6); |
| 92 | CHECK(b < 8); |
| 93 | CHECK(b >= 7); |
| 94 | CHECK(b <= 7); |
onqtam | 3ac4c3f | 2018-06-01 15:31:06 +0300 | [diff] [blame] | 95 | |
Stefan | e8ba771 | 2022-04-28 02:47:24 +0200 | [diff] [blame] | 96 | CHECK(5 == a); |
onqtam | 3ac4c3f | 2018-06-01 15:31:06 +0300 | [diff] [blame] | 97 | CHECK(6 != a); |
| 98 | CHECK(6 > a); |
Stefan | e8ba771 | 2022-04-28 02:47:24 +0200 | [diff] [blame] | 99 | CHECK(4 < a); |
| 100 | CHECK(5 >= a); |
| 101 | CHECK(5 <= a); |
onqtam | 3ac4c3f | 2018-06-01 15:31:06 +0300 | [diff] [blame] | 102 | |
onqtam | acbcd12 | 2019-03-02 21:24:18 +0200 | [diff] [blame] | 103 | // trigger another single line of code... lol |
Stefan | db758e0 | 2022-06-06 03:42:02 +0200 | [diff] [blame] | 104 | // NOLINTBEGIN(cppcoreguidelines-pro-type-const-cast) |
onqtam | acbcd12 | 2019-03-02 21:24:18 +0200 | [diff] [blame] | 105 | auto oldVal = const_cast<ContextOptions*>(getContextOptions())->no_path_in_filenames; |
| 106 | const_cast<ContextOptions*>(getContextOptions())->no_path_in_filenames = false; |
onqtam | 15ce266 | 2019-03-15 18:43:17 +0200 | [diff] [blame] | 107 | CHECK(String(skipPathFromFilename("")) == ""); |
onqtam | acbcd12 | 2019-03-02 21:24:18 +0200 | [diff] [blame] | 108 | const_cast<ContextOptions*>(getContextOptions())->no_path_in_filenames = oldVal; |
Stefan | db758e0 | 2022-06-06 03:42:02 +0200 | [diff] [blame] | 109 | // NOLINTEND(cppcoreguidelines-pro-type-const-cast) |
onqtam | acbcd12 | 2019-03-02 21:24:18 +0200 | [diff] [blame] | 110 | |
onqtam | 3ac4c3f | 2018-06-01 15:31:06 +0300 | [diff] [blame] | 111 | // a hack to trigger a bug in doctest: currently a 0 cannot be successfully parsed for an int option! |
| 112 | Context().setOption("last", 0); |
| 113 | } |
| 114 | |
| 115 | TEST_SUITE("will be overridden by a decorator" * doctest::test_suite("exception related")) { |
| 116 | TEST_CASE("will end from a std::string exception") { |
| 117 | throw_if(true, std::string("std::string!")); |
| 118 | } |
| 119 | |
| 120 | TEST_CASE("will end from a const char* exception") { throw_if(true, "const char*!"); } |
| 121 | |
| 122 | TEST_CASE("will end from an unknown exception") { |
| 123 | throw_if(true, doctest::String("unknown :(")); |
| 124 | } |
| 125 | } |
| 126 | |
onqtam | 3ac4c3f | 2018-06-01 15:31:06 +0300 | [diff] [blame] | 127 | #endif // DOCTEST_CONFIG_DISABLE |