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

data() method from fcf::NTest::State class

fcf::NTest::SharedPtrAny data(const char* a_key)
void data(const char* a_key, fcf::NTest::SharedPtrAny a_data)

Class: fcf::NTest::State

Package: fcfTest

File: test.hpp

Available from version: 1.2.1

Provides access to the user-defined data storage within the state.

The data method allows you to store and retrieve arbitrary data associated with the current test execution using string keys.

This is extremely useful for sharing information between different tests, fixtures, or even between the test runner and the logger (e.g., passing custom objects to a logger's prefix handler).

The data is stored using the fcf::NTest::SharedPtrAny type, which provides a type-safe way to handle heterogeneous data types.

Arguments

const char* a_key
- The unique string identifier for the data entry.

fcf::NTest::SharedPtrAny a_data
- The data object to be stored under the specified key.
Result
fcf::NTest::SharedPtrAny
- A fcf::NTest::SharedPtrAny object containing the data associated with the specified key.

Example: Storing and retrieving custom data

Demonstrating how to use the state to pass an integer value between a fixture and a test.

#define FCF_TEST_IMPLEMENTATION #include <fcfTest/test.hpp> // 1. A fixture that sets up some data FCF_TEST_BEFORE_DEFINE("DataDemo", "Fixture", "*", fcf::NTest::FL_GROUP) { fcf::NTest::state().data("shared_value", fcf::NTest::SharedPtrAny::make<int>(42)); fcf::NTest::log() << "Fixture: Data set to 42" << std::endl; } // 2. A test that retrieves and uses that data FCF_TEST_DEFINE("DataDemo", "Fixture", "Retrieve") { // Retrieve the data fcf::NTest::SharedPtrAny data = fcf::NTest::state().data("shared_value"); // Cast it back to the original type int* value = data.cast<int>(); FCF_TEST(*value == 42); fcf::NTest::log() << "Test: Retrieved value is " << *value << 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:

> Fixture: Data set to 42 Performing the test: "DataDemo" -> "Fixture" -> "Retrieve" ... > Test: Retrieved value is 42 [SUCCESS] Test completed successfully (0.000`005`435 sec) [SUCCESS] All tests were completed. Tests: 1 passed, 0 failed, 0 skipped, 1 total Duration: 0.000`005`435 sec