diff options
author | Menno Fraters <menno.fraters@tutanota.com> | 2022-03-06 02:14:35 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-06 02:14:35 +0300 |
commit | 8de4cf7e759c55a89761c25900d80e01ae7ac3fd (patch) | |
tree | eda913a29bc57e9d3ed018e45eb7ffd0aac6ffdf /examples/all_features | |
parent | bef19659da2b7ca49feec44fd0756072470836a0 (diff) |
Add contains option to checks. (#620)
* Add contains otion to checks.
* Add test for teh contains option of CHECK_THROWS_WITH.
* Adress comments Saalvage: move compare function to Contains, add operator== and make m_exception_string a union."
* Add new tests.
* Attempt to fix windows specific warning about unions.
* Attempt 2 to fix windows specific warning about unions.
* Attempt 3 to fix windows specific warning about unions.
* Attempt 4 to fix windows specific warning about unions.
* Return union to a struct.
* Fixing and refactoring
* Docs
Co-authored-by: Salvage <29021710+Saalvage@users.noreply.github.com>
Diffstat (limited to 'examples/all_features')
8 files changed, 79 insertions, 10 deletions
diff --git a/examples/all_features/assertion_macros.cpp b/examples/all_features/assertion_macros.cpp index f0fbe9b0..959a9c3d 100644 --- a/examples/all_features/assertion_macros.cpp +++ b/examples/all_features/assertion_macros.cpp @@ -38,8 +38,11 @@ TEST_CASE("exceptions-related macros") { CHECK_THROWS_AS(throw_if(false, 0), int); // fails CHECK_THROWS_WITH(throw_if(true, "whops!"), "whops! no match!"); // fails + CHECK_THROWS_WITH(throw_if(true, "whops! does it match?"), doctest::Contains("whops!")); + CHECK_THROWS_WITH(throw_if(true, "whops! does it match?"), doctest::Contains("whops! no match!")); // fails CHECK_THROWS_WITH_AS(throw_if(true, "whops!"), "whops! no match!", bool); // fails CHECK_THROWS_WITH_AS(throw_if(true, "whops!"), "whops!", int); // fails + CHECK_THROWS_WITH_AS(throw_if(true, "whops! does it match?"), doctest::Contains("whops! no match!"), int); // fails CHECK_NOTHROW(throw_if(true, 0)); // fails CHECK_NOTHROW(throw_if(false, 0)); @@ -73,6 +76,10 @@ TEST_CASE("WARN level of asserts don't fail the test case") { WARN_NOTHROW(throw_if(true, 0)); WARN_EQ(1, 0); + doctest::String myStr = doctest::String("Hello world, how are you doing? Well, nice to meet you, Goodbye!"); + WARN_EQ(myStr, doctest::Contains("Hello")); + WARN(myStr == doctest::Contains("Goodbye")); + WARN(myStr != doctest::Contains("goodbye")); WARN_UNARY(0); WARN_UNARY_FALSE(1); } @@ -88,6 +95,10 @@ TEST_CASE("CHECK level of asserts fail the test case but don't abort it") { CHECK_NOTHROW(throw_if(true, 0)); CHECK_EQ(1, 0); + doctest::String myStr = doctest::String("Hello world, how are you doing? Well, nice to meet you, Goodbye!"); + CHECK_EQ(myStr, doctest::Contains("Hello")); + CHECK(myStr == doctest::Contains("Goodbye")); + CHECK(myStr != doctest::Contains("goodbye")); CHECK_UNARY(0); CHECK_UNARY_FALSE(1); diff --git a/examples/all_features/stringification.cpp b/examples/all_features/stringification.cpp index adca1e7f..2add560d 100644 --- a/examples/all_features/stringification.cpp +++ b/examples/all_features/stringification.cpp @@ -151,6 +151,8 @@ TEST_CASE("all asserts should fail and show how the objects get stringified") { CHECK(doctest::IsNaN<float>(std::numeric_limits<float>::infinity())); // can't test actual nan because it's implementation defined + CHECK("a" == doctest::Contains("aaa")); + // lets see if this exception gets translated throw_if(true, bla1); } diff --git a/examples/all_features/test_output/assertion_macros.cpp.txt b/examples/all_features/test_output/assertion_macros.cpp.txt index 513939d4..fc4cc946 100644 --- a/examples/all_features/test_output/assertion_macros.cpp.txt +++ b/examples/all_features/test_output/assertion_macros.cpp.txt @@ -20,10 +20,14 @@ assertion_macros.cpp(0): ERROR: CHECK_THROWS_AS( throw_if(false, 0), int ) did N assertion_macros.cpp(0): ERROR: CHECK_THROWS_WITH( throw_if(true, "whops!"), "whops! no match!" ) threw a DIFFERENT exception: "whops!" +assertion_macros.cpp(0): ERROR: CHECK_THROWS_WITH( throw_if(true, "whops! does it match?"), "whops! no match!" ) threw a DIFFERENT exception: "whops! does it match?" + assertion_macros.cpp(0): ERROR: CHECK_THROWS_WITH_AS( throw_if(true, "whops!"), "whops! no match!", bool ) threw a DIFFERENT exception! (contents: "whops!") assertion_macros.cpp(0): ERROR: CHECK_THROWS_WITH_AS( throw_if(true, "whops!"), "whops!", int ) threw a DIFFERENT exception! (contents: "whops!") +assertion_macros.cpp(0): ERROR: CHECK_THROWS_WITH_AS( throw_if(true, "whops! does it match?"), "whops! no match!", int ) threw a DIFFERENT exception! (contents: "whops! does it match?") + assertion_macros.cpp(0): ERROR: CHECK_NOTHROW( throw_if(true, 0) ) THREW exception: "0" =============================================================================== @@ -248,6 +252,6 @@ assertion_macros.cpp(0): ERROR: CHECK_THROWS_WITH( throw_if(true, 2), "1" ) thre =============================================================================== [doctest] test cases: 23 | 4 passed | 19 failed | -[doctest] assertions: 92 | 44 passed | 48 failed | +[doctest] assertions: 98 | 48 passed | 50 failed | [doctest] Status: FAILURE! Program code. diff --git a/examples/all_features/test_output/assertion_macros.cpp_junit.txt b/examples/all_features/test_output/assertion_macros.cpp_junit.txt index bcc119c3..3802a4e6 100644 --- a/examples/all_features/test_output/assertion_macros.cpp_junit.txt +++ b/examples/all_features/test_output/assertion_macros.cpp_junit.txt @@ -1,6 +1,6 @@ <?xml version="1.0" encoding="UTF-8"?> <testsuites> - <testsuite name="all_features" errors="0" failures="61" tests="92"> + <testsuite name="all_features" errors="0" failures="63" tests="98"> <testcase classname="assertion_macros.cpp" name="normal macros" status="run"> <failure type="CHECK"> assertion_macros.cpp(0): @@ -36,6 +36,11 @@ assertion_macros.cpp(0): CHECK_THROWS_WITH( throw_if(true, "whops!"), "whops! no match!" ) threw a DIFFERENT exception: "whops!" </failure> + <failure type="CHECK_THROWS_WITH"> +assertion_macros.cpp(0): +CHECK_THROWS_WITH( throw_if(true, "whops! does it match?"), "whops! no match!" ) threw a DIFFERENT exception: "whops! does it match?" + + </failure> <failure type="CHECK_THROWS_WITH_AS"> assertion_macros.cpp(0): CHECK_THROWS_WITH_AS( throw_if(true, "whops!"), "whops! no match!", bool ) threw a DIFFERENT exception! (contents: "whops!") @@ -46,6 +51,11 @@ assertion_macros.cpp(0): CHECK_THROWS_WITH_AS( throw_if(true, "whops!"), "whops!", int ) threw a DIFFERENT exception! (contents: "whops!") </failure> + <failure type="CHECK_THROWS_WITH_AS"> +assertion_macros.cpp(0): +CHECK_THROWS_WITH_AS( throw_if(true, "whops! does it match?"), "whops! no match!", int ) threw a DIFFERENT exception! (contents: "whops! does it match?") + + </failure> <failure type="CHECK_NOTHROW"> assertion_macros.cpp(0): CHECK_NOTHROW( throw_if(true, 0) ) THREW exception: "0" diff --git a/examples/all_features/test_output/assertion_macros.cpp_xml.txt b/examples/all_features/test_output/assertion_macros.cpp_xml.txt index 28e10330..b67c33aa 100644 --- a/examples/all_features/test_output/assertion_macros.cpp_xml.txt +++ b/examples/all_features/test_output/assertion_macros.cpp_xml.txt @@ -60,6 +60,17 @@ whops! no match! </ExpectedExceptionString> </Expression> + <Expression success="false" type="CHECK_THROWS_WITH" filename="assertion_macros.cpp" line="0"> + <Original> + throw_if(true, "whops! does it match?") + </Original> + <Exception> + "whops! does it match?" + </Exception> + <ExpectedExceptionString> + whops! no match! + </ExpectedExceptionString> + </Expression> <Expression success="false" type="CHECK_THROWS_WITH_AS" filename="assertion_macros.cpp" line="0"> <Original> throw_if(true, "whops!") @@ -88,6 +99,20 @@ whops! </ExpectedExceptionString> </Expression> + <Expression success="false" type="CHECK_THROWS_WITH_AS" filename="assertion_macros.cpp" line="0"> + <Original> + throw_if(true, "whops! does it match?") + </Original> + <Exception> + "whops! does it match?" + </Exception> + <ExpectedException> + int + </ExpectedException> + <ExpectedExceptionString> + whops! no match! + </ExpectedExceptionString> + </Expression> <Expression success="false" type="CHECK_NOTHROW" filename="assertion_macros.cpp" line="0"> <Original> throw_if(true, 0) @@ -96,7 +121,7 @@ "0" </Exception> </Expression> - <OverallResultsAsserts successes="3" failures="7" test_case_success="false"/> + <OverallResultsAsserts successes="4" failures="9" test_case_success="false"/> </TestCase> <TestCase name="exceptions-related macros for std::exception" filename="assertion_macros.cpp" line="0"> <Expression success="false" type="CHECK_THROWS" filename="assertion_macros.cpp" line="0"> @@ -359,7 +384,7 @@ reached! </Text> </Message> - <OverallResultsAsserts successes="0" failures="11" test_case_success="false"/> + <OverallResultsAsserts successes="3" failures="11" test_case_success="false"/> </TestCase> <TestCase name="REQUIRE level of asserts fail and abort the test case - 1" filename="assertion_macros.cpp" line="0"> <Expression success="false" type="REQUIRE" filename="assertion_macros.cpp" line="0"> @@ -659,7 +684,7 @@ <OverallResultsAsserts successes="0" failures="9" test_case_success="false"/> </TestCase> </TestSuite> - <OverallResultsAsserts successes="44" failures="48"/> + <OverallResultsAsserts successes="48" failures="50"/> <OverallResultsTestCases successes="4" failures="19"/> </doctest> Program code. diff --git a/examples/all_features/test_output/stringification.cpp.txt b/examples/all_features/test_output/stringification.cpp.txt index 234e806a..650063ec 100644 --- a/examples/all_features/test_output/stringification.cpp.txt +++ b/examples/all_features/test_output/stringification.cpp.txt @@ -33,6 +33,9 @@ stringification.cpp(0): ERROR: CHECK( doctest::IsNaN<double>(0.5) ) is NOT corre stringification.cpp(0): ERROR: CHECK( doctest::IsNaN<float>(std::numeric_limits<float>::infinity()) ) is NOT correct! values: CHECK( inf ) +stringification.cpp(0): ERROR: CHECK( "a" == doctest::Contains("aaa") ) is NOT correct! + values: CHECK( a == Contains( aaa ) ) + stringification.cpp(0): ERROR: test case THREW exception: MyTypeInherited<int>(5, 4) =============================================================================== @@ -42,7 +45,7 @@ TEST CASE: a test case that registers an exception translator for int and then stringification.cpp(0): ERROR: test case THREW exception: 5 =============================================================================== -[doctest] test cases: 2 | 0 passed | 2 failed | -[doctest] assertions: 9 | 0 passed | 9 failed | +[doctest] test cases: 2 | 0 passed | 2 failed | +[doctest] assertions: 10 | 0 passed | 10 failed | [doctest] Status: FAILURE! Program code. diff --git a/examples/all_features/test_output/stringification.cpp_junit.txt b/examples/all_features/test_output/stringification.cpp_junit.txt index a5e39dc2..eac29e12 100644 --- a/examples/all_features/test_output/stringification.cpp_junit.txt +++ b/examples/all_features/test_output/stringification.cpp_junit.txt @@ -1,6 +1,6 @@ <?xml version="1.0" encoding="UTF-8"?> <testsuites> - <testsuite name="all_features" errors="2" failures="9" tests="9"> + <testsuite name="all_features" errors="2" failures="10" tests="10"> <testcase classname="stringification.cpp" name="all asserts should fail and show how the objects get stringified" status="run"> <failure message="Foo{} == Foo{}" type="CHECK"> stringification.cpp(0): @@ -59,6 +59,12 @@ CHECK( doctest::IsNaN<float>(std::numeric_limits<float>::infinity()) ) is values: CHECK( inf ) </failure> + <failure message="a == Contains( aaa )" type="CHECK"> +stringification.cpp(0): +CHECK( "a" == doctest::Contains("aaa") ) is NOT correct! + values: CHECK( a == Contains( aaa ) ) + + </failure> <error message="exception"> MyTypeInherited<int>(5, 4) </error> diff --git a/examples/all_features/test_output/stringification.cpp_xml.txt b/examples/all_features/test_output/stringification.cpp_xml.txt index 67f6c96c..066df99f 100644 --- a/examples/all_features/test_output/stringification.cpp_xml.txt +++ b/examples/all_features/test_output/stringification.cpp_xml.txt @@ -84,10 +84,18 @@ inf </Expanded> </Expression> + <Expression success="false" type="CHECK" filename="stringification.cpp" line="0"> + <Original> + "a" == doctest::Contains("aaa") + </Original> + <Expanded> + a == Contains( aaa ) + </Expanded> + </Expression> <Exception crash="false"> MyTypeInherited<int>(5, 4) </Exception> - <OverallResultsAsserts successes="0" failures="9" test_case_success="false"/> + <OverallResultsAsserts successes="0" failures="10" test_case_success="false"/> </TestCase> <TestCase name="a test case that registers an exception translator for int and then throws one" filename="stringification.cpp" line="0"> <Exception crash="false"> @@ -96,7 +104,7 @@ <OverallResultsAsserts successes="0" failures="0" test_case_success="false"/> </TestCase> </TestSuite> - <OverallResultsAsserts successes="0" failures="9"/> + <OverallResultsAsserts successes="0" failures="10"/> <OverallResultsTestCases successes="0" failures="2"/> </doctest> Program code. |