FCF 2.0 development in progress...
> > > >
[News] [C++ Libraries API] [C++ Downloads] [Donate to the project] [Contacts]

FCF_TEST_THROW_CHECK macro

bool FCF_TEST_THROW_CHECK (CODE_BLOCK am_code, TYPE am_exception, [mixed am_observedVariable...])

Package: fcfTest

File: test.hpp

Available from version: 1.1.13

A non-throwing assertion macro. It checks if a code block throws a specific exception. If no exception is thrown, it logs an error and returns false without interrupting the test execution.

The FCF_TEST_THROW_CHECK macro is a non-interrupting assertion designed for exception testing. It is particularly useful in multithreaded environments or when you want to perform multiple checks within a single test case and inspect all failures at once.

Arguments

CODE_BLOCK am_code
- The code block or expression that is expected to throw an exception.

TYPE am_exception
- The type of exception that is expected to be thrown (e.g., std::out_of_range). To catch any exception, use std::exception.

mixed am_observedVariable
- [OPTIONAL] A list of variables whose values will be included in the error report if the assertion fails.
Detailed description

Behavior:

  1. Executes the provided code block within a try-catch structure.
  2. If the expected exception is caught:

    * The macro returns true and execution continues.

  3. If no exception is thrown, or an exception of a different type is caught:

      Generates a detailed error message containing:

        A text representation of the code block being tested.
      • The name of the expected exception.
      • The path to the file and line number where the failure occurred.
      • A list of the names and current values of all passed variable arguments.

    • Adds the error information to the current test state.
    • Returns false. The test execution is NOT interrupted.

Example: Non-throwing exception check

A complete example showing how to use FCF_TEST_THROW_CHECK to verify exception handling without stopping the test execution upon failure.

#define FCF_TEST_IMPLEMENTATION #include <fcfTest/test.hpp> #include <stdexcept> void functionThatThrows() { throw std::runtime_error("Error occurred"); } void functionThatDoesNotThrow() { // Do nothing } FCF_TEST_DECLARE("ExceptionTests", "NonBlocking", "CheckExceptions") { int contextValue = 42; // 1. This check should pass because the function throws the expected exception. // The macro returns true. FCF_TEST_THROW_CHECK(functionThatThrows(), std::runtime_error, contextValue); // 2. This check will fail because no exception is thrown. // The macro will log an error with context, but the test will continue. if (!FCF_TEST_THROW_CHECK(functionThatDoesNotThrow(), std::runtime_error, contextValue)) { fcf::NTest::log() << "The second check failed as expected, but we are still running!" << std::endl; } // 3. This check will fail because the exception type is wrong. // The macro will log an error and return false. FCF_TEST_THROW_CHECK(functionThatThrows(), std::out_of_range, contextValue); fcf::NTest::log() << "Test execution reached the end." << std::endl; } int main(int a_argc, char* a_argv[]) { bool error = false; fcf::NTest::cmdRun(a_argc, a_argv, fcf::NTest::CRM_RUN, &error); return error ? 1 : 0; }

Output:

[100%] Built target app Performing the test: "ExceptionTests" -> "NonBlocking" -> "CheckExceptions" ... > The second check failed as expected, but we are still running! > Test execution reached the end. Test error: 'functionThatDoesNotThrow()' did not throw [FILE: PATH/main.cpp:22] Values: contextValue: 42 Test error: 'functionThatThrows()' threw an exception that does not match 'std::out_of_range' [FILE: PATH/main.cpp:28] Values: contextValue: 42 [FAILED] Test failed (0.000`063`921 sec) [FAILED] Testing completed with failures. Tests: 0 passed, 1 failed, 0 skipped, 1 total Duration: 0.000`063`921 sec