catch3
Progmasoft Catch3: an independently maintained C++17 test framework with a parallel runner, property checks, snapshots and structured results.
Clonegit clone https://git.progmasoft.com/catch3
Updated documentation about stringifying UDTs
- Commit
31f5e2ed81fd38020bd9e0be2341f46556751821- Author
- Martin Hořeňovský ·
- Parents
85aa77070198
Changes
docs/tostring.md | 41 +++++++++-------------------------------- 1 file changed, 9 insertions(+), 32 deletions(-) diff --git a/docs/tostring.md b/docs/tostring.md index dbb6cb8d..f961ccc1 100644 --- a/docs/tostring.md +++ b/docs/tostring.md @@ -1,7 +1,7 @@ # String conversions Catch needs to be able to convert types you use in assertions and logging expressions into strings (for logging and reporting purposes). -Most built-in or std types are supported out of the box but there are three ways that you can tell Catch how to convert your own types (or other, third-party types) into strings. +Most built-in or std types are supported out of the box but there are two ways that you can tell Catch how to convert your own types (or other, third-party types) into strings. ## operator << overload for std::ostream @@ -16,42 +16,19 @@ std::ostream& operator << ( std::ostream& os, T const& value ) { (where ```T``` is your type and ```convertMyTypeToString``` is where you'll write whatever code is necessary to make your type printable - it doesn't have to be in another function). -You should put this function in the same namespace as your type. - -Alternatively you may prefer to write it as a member function: - -``` -std::ostream& T::operator << ( std::ostream& os ) const { - os << convertMyTypeToString( *this ); - return os; -} -``` - -## Catch::toString overload - -If you don't want to provide an ```operator <<``` overload, or you want to convert your type differently for testing purposes, you can provide an overload for ```Catch::toString()``` for your type. - -``` -namespace Catch { - std::string toString( T const& value ) { - return convertMyTypeToString( value ); - } -} -``` - -Again ```T``` is your type and ```convertMyTypeToString``` is where you'll write whatever code is necessary to make your type printable. Note that the function must be in the Catch namespace, which itself must be in the global namespace. +You should put this function in the same namespace as your type and have it declared before including Catch's header. ## Catch::StringMaker<T> specialisation - -There are some cases where overloading toString does not work as expected. Specialising StringMaker<T> gives you more precise, and reliable, control - but at the cost of slightly more code and complexity: +If you don't want to provide an ```operator <<``` overload, or you want to convert your type differently for testing purposes, you can provide a specialization for `Catch::StringMaker<T>`: ``` namespace Catch { - template<> struct StringMaker<T> { - static std::string convert( T const& value ) { - return convertMyTypeToString( value ); - } - }; + template<> + struct StringMaker<T> { + std::string operator()( T const& value ) { + return convertMyTypeToString( value ); + } + }; } ```