SpECTRE  v2024.09.29
TestHelpers::serialization Namespace Reference

Collection of classes and functions for testing serialization. More...

Functions

template<typename Compare = void, typename Serialize >
void test_versioning (const std::string &filename, const std::string &current_label, const Serialize &current_object, const std::unordered_map< std::string, std::variant< Serialize, std::string > > &old_objects={}, const bool generate_new_entry=false)
 Test serialization of a versioned class against old versions. More...
 

Detailed Description

Collection of classes and functions for testing serialization.

Function Documentation

◆ test_versioning()

template<typename Compare = void, typename Serialize >
void TestHelpers::serialization::test_versioning ( const std::string filename,
const std::string current_label,
const Serialize &  current_object,
const std::unordered_map< std::string, std::variant< Serialize, std::string > > &  old_objects = {},
const bool  generate_new_entry = false 
)

Test serialization of a versioned class against old versions.

Old serializations of the class are stored in filename (relative to the unit test source directory). Each stored version has a label, where the current label must match current_label. This function checks that

  • The last entry matches current_label and matches the result of serializing current_object.
  • Old lines in the file are successfully deserialized and give expected values.

By default, old versions are expected to deserialize to current_object, but if the object represented by the serialized entries has changed (for example, because the current_object has newer options enabled), objects can be passed in old_objects, keyed by the last version they should match. If deserialization support is dropped up to a specific entry, an error can be checked for by instead adding a string to old_objects.

By default, objects are directly compared for equality. If the objects being tested are stored in std::unique_ptrs the Compare template parameter can be passed in to dereference and dynamic_cast the contained value before comparing.

If generate_new_entry is true and current_label and current_object do not match the last serialization in the file, a new entry will be appended.