Crate string_io_and_mock

Source
Expand description

This crate provides a struct FileTextHandler that acts as a mockable layer over a file system. It provides write and read operations that are required by the TextIOHandler trait :

  • method write_text writes String content to a file or file system simulator;
  • method read_text reads String content from a file or file system simulator;

The Text in the names of the trait and structs mean that these entities are only meant to handle String content, as is evident from the signatures of the trait’s methods.

For unit tests - or for other applications - a mock MockTextHandler is available that also implements the TextIOHandler trait, but doesn’t access any file system. It stores it texts in a HashMap instead.

This means that MockTextHandler is more than a mere mock: with its internal persistence, it can serve as an application component in its own right, providing string storage in memory where file storage isn’t needed.

Structs§

FileTextHandler
FileTextHandler provides string read and write operations to file system files. It has no internal persistence, as this is provided by the underlying file system. Even so, calling it’s write_text method still requires a FileTextHandler object to be declared as mutable, as the TextIOHandler trait imposes this so as to enable mocks to use internal persistence to write to their encapsulated state.
MockTextHandler
MockTextHandler allows FileTextHandler objects to be replaced by a mock in unit tests. MockTextHandler stores strings written to it in a private HashMap.

Traits§

TextIOHandler
Implementors provide the ability to accept std::string::String content associated with an std::ffi::OsStr name, as can be expected from entities mediating a file system or their mocks and simulators.