FCF_TEST_THROW
(CODE_BLOCK am_code, TYPE am_exception, [mixed am_observedVariable...])
Package: fcfTest
File: test.hpp
Available from version: 1.1.13
Asserts that a code block throws a specific exception. If no exception is thrown, or an unexpected exception type is caught, it generates a detailed error report and throws a std::runtime_error.
The FCF_TEST_THROW macro is used to verify the exception-handling logic of your code. It ensures that a specific block of code behaves correctly by throwing the expected exception type.
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::runtime_error). 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:
- Executes the provided code block within a try-catch structure.
-
If the expected exception is caught:
* The test continues normally.
-
If no exception is thrown, or an exception of a different type is caught:
- Adds error information to the current test state.
- Throws a std::runtime_error to interrupt the test.
Example: Basic exception testing
A complete example demonstrating how to test for a specific exception and how to provide context variables.
#define FCF_TEST_IMPLEMENTATION
#include <fcfTest/test.hpp>
#include <stdexcept>
void riskyFunction(int a_value) {
if (a_value < 0) {
throw std::out_of_range("Value is negative");
}
}
FCF_TEST_DECLARE("ExceptionTests", "Basic", "ThrowCheck ok") {
int val = -5;
// We expect riskyFunction to throw std::out_of_range when val is -5.
// We also pass 'val' to the macro so its value is printed if the test fails.
FCF_TEST_THROW(riskyFunction(val), std::out_of_range, val);
}
FCF_TEST_DECLARE("ExceptionTests", "Basic", "ThrowCheck fail") {
int val = 5;
// We expect riskyFunction to throw std::out_of_range when val is -5.
// We also pass 'val' to the macro so its value is printed if the test fails.
FCF_TEST_THROW(riskyFunction(val), std::out_of_range, val);
}
FCF_TEST_TEST_ORDER("ThrowCheck ok", 1);
FCF_TEST_TEST_ORDER("ThrowCheck fail", 2);
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:
Performing the test: "ExceptionTests" -> "Basic" -> "ThrowCheck ok" ...
[SUCCESS] Test completed successfully (0.000`042`402 sec)
Performing the test: "ExceptionTests" -> "Basic" -> "ThrowCheck fail" ...
Test error: 'riskyFunction(val)' did not throw [FILE: PATH/main.cpp:24]
Values:
val: 5
[FAILED] Test failed (0.000`027`418 sec)
[FAILED] Testing completed with failures.
Tests: 1 passed, 1 failed, 0 skipped, 2 total
Duration: 0.000`069`820 sec
Example: Testing for any exception
Demonstrating how to use the ellipsis (...) to catch any exception type.
#define FCF_TEST_IMPLEMENTATION
#include <fcfTest/test.hpp>
void genericError() {
throw std::runtime_error("Something went wrong");
}
FCF_TEST_DECLARE("ExceptionTests", "Advanced", "CatchAll") {
// Using '...' tells the macro to catch any exception type.
FCF_TEST_THROW(genericError(), std::exception);
}
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:
Performing the test: "ExceptionTests" -> "Advanced" -> "CatchAll" ...
[SUCCESS] Test completed successfully (0.000`036`807 sec)
[SUCCESS] All tests were completed.
Tests: 1 passed, 0 failed, 0 skipped, 1 total
Duration: 0.000`036`807 sec