| Phil Nash | 27ce70c | 2014-12-09 18:54:35 +0000 | [diff] [blame] | 1 | # String conversions |
| 2 | |
| 3 | Catch needs to be able to convert types you use in assertions and logging expressions into strings (for logging and reporting purposes). |
| Phil Nash | 605d870 | 2015-05-20 18:12:40 +0100 | [diff] [blame] | 4 | 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. |
| Phil Nash | 27ce70c | 2014-12-09 18:54:35 +0000 | [diff] [blame] | 5 | |
| 6 | ## operator << overload for std::ostream |
| 7 | |
| 8 | This is the standard way of providing string conversions in C++ - and the chances are you may already provide this for your own purposes. If you're not familiar with this idiom it involves writing a free function of the form: |
| 9 | |
| Phil Nash | 82754c1 | 2014-12-12 08:29:21 +0000 | [diff] [blame] | 10 | ``` |
| 11 | std::ostream& operator << ( std::ostream& os, T const& value ) { |
| Phil Nash | 27ce70c | 2014-12-09 18:54:35 +0000 | [diff] [blame] | 12 | os << convertMyTypeToString( value ); |
| 13 | return os; |
| Phil Nash | 82754c1 | 2014-12-12 08:29:21 +0000 | [diff] [blame] | 14 | } |
| 15 | ``` |
| Phil Nash | 27ce70c | 2014-12-09 18:54:35 +0000 | [diff] [blame] | 16 | |
| 17 | (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). |
| 18 | |
| 19 | You should put this function in the same namespace as your type. |
| 20 | |
| 21 | Alternatively you may prefer to write it as a member function: |
| 22 | |
| Phil Nash | 82754c1 | 2014-12-12 08:29:21 +0000 | [diff] [blame] | 23 | ``` |
| 24 | std::ostream& T::operator << ( std::ostream& os ) const { |
| Phil Nash | 27ce70c | 2014-12-09 18:54:35 +0000 | [diff] [blame] | 25 | os << convertMyTypeToString( *this ); |
| 26 | return os; |
| Phil Nash | 82754c1 | 2014-12-12 08:29:21 +0000 | [diff] [blame] | 27 | } |
| 28 | ``` |
| Phil Nash | 27ce70c | 2014-12-09 18:54:35 +0000 | [diff] [blame] | 29 | |
| 30 | ## Catch::toString overload |
| 31 | |
| 32 | 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. |
| 33 | |
| Phil Nash | 82754c1 | 2014-12-12 08:29:21 +0000 | [diff] [blame] | 34 | ``` |
| 35 | namespace Catch { |
| Phil Nash | 27ce70c | 2014-12-09 18:54:35 +0000 | [diff] [blame] | 36 | std::string toString( T const& value ) { |
| 37 | return convertMyTypeToString( value ); |
| 38 | } |
| 39 | } |
| Phil Nash | 82754c1 | 2014-12-12 08:29:21 +0000 | [diff] [blame] | 40 | ``` |
| Phil Nash | 27ce70c | 2014-12-09 18:54:35 +0000 | [diff] [blame] | 41 | |
| 42 | 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. |
| 43 | |
| Phil Nash | 605d870 | 2015-05-20 18:12:40 +0100 | [diff] [blame] | 44 | ## Catch::StringMaker<T> specialisation |
| 45 | |
| 46 | 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: |
| 47 | |
| 48 | ``` |
| 49 | namespace Catch { |
| 50 | template<> struct StringMaker<T> { |
| 51 | static std::string convert( T const& value ) { |
| 52 | return convertMyTypeToString( value ); |
| 53 | } |
| 54 | }; |
| 55 | } |
| 56 | ``` |
| 57 | |
| Phil Nash | a49f088 | 2015-11-18 08:39:21 +0000 | [diff] [blame^] | 58 | ## Exceptions |
| 59 | |
| 60 | By default all exceptions deriving from `std::exception` will be translated to strings by calling the `what()` method. For exception types that do not derive from `std::exception` - or if `what()` does not return a suitable string - use `CATCH_TRANSLATE_EXCEPTION`. This defines a function that takes your exception type, by reference, and returns a string. It can appear anywhere in the code - it doesn't have to be in the same translation unit. For example: |
| 61 | |
| 62 | ``` |
| 63 | CATCH_TRANSLATE_EXCEPTION( MyType& ex ) { |
| 64 | return ex.message(); |
| 65 | } |
| 66 | ``` |
| 67 | |
| Phil Nash | 27ce70c | 2014-12-09 18:54:35 +0000 | [diff] [blame] | 68 | --- |
| 69 | |
| 70 | [Home](Readme.md) |