| Martin Moene | e822505 | 2017-08-24 15:21:36 +0200 | [diff] [blame] | 1 | <a id="top"></a> |
| Phil Nash | 27ce70c | 2014-12-09 18:54:35 +0000 | [diff] [blame] | 2 | # String conversions |
| 3 | |
| Martin Moene | a81c01d | 2018-09-05 10:01:54 +0200 | [diff] [blame] | 4 | **Contents**<br> |
| Martin Hořeňovský | 46e99e2 | 2018-09-09 16:16:49 +0200 | [diff] [blame^] | 5 | [operator << overload for std::ostream](#operator--overload-for-stdostream)<br> |
| 6 | [Catch::StringMaker specialisation](#catchstringmaker-specialisation)<br> |
| 7 | [Catch::is_range specialisation](#catchis_range-specialisation)<br> |
| Martin Moene | a81c01d | 2018-09-05 10:01:54 +0200 | [diff] [blame] | 8 | [Exceptions](#exceptions)<br> |
| 9 | |
| Phil Nash | 27ce70c | 2014-12-09 18:54:35 +0000 | [diff] [blame] | 10 | Catch needs to be able to convert types you use in assertions and logging expressions into strings (for logging and reporting purposes). |
| Martin Hořeňovský | 31f5e2e | 2017-05-16 13:38:52 +0200 | [diff] [blame] | 11 | 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. |
| Phil Nash | 27ce70c | 2014-12-09 18:54:35 +0000 | [diff] [blame] | 12 | |
| 13 | ## operator << overload for std::ostream |
| 14 | |
| 15 | 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: |
| 16 | |
| Phil Nash | 82754c1 | 2014-12-12 08:29:21 +0000 | [diff] [blame] | 17 | ``` |
| 18 | std::ostream& operator << ( std::ostream& os, T const& value ) { |
| Axel Huebl | 7667a7d | 2018-08-28 16:17:37 +0200 | [diff] [blame] | 19 | os << convertMyTypeToString( value ); |
| 20 | return os; |
| Phil Nash | 82754c1 | 2014-12-12 08:29:21 +0000 | [diff] [blame] | 21 | } |
| 22 | ``` |
| Phil Nash | 27ce70c | 2014-12-09 18:54:35 +0000 | [diff] [blame] | 23 | |
| 24 | (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). |
| 25 | |
| Martin Hořeňovský | 31f5e2e | 2017-05-16 13:38:52 +0200 | [diff] [blame] | 26 | You should put this function in the same namespace as your type and have it declared before including Catch's header. |
| Phil Nash | 27ce70c | 2014-12-09 18:54:35 +0000 | [diff] [blame] | 27 | |
| Martin Hořeňovský | 46e99e2 | 2018-09-09 16:16:49 +0200 | [diff] [blame^] | 28 | ## Catch::StringMaker specialisation |
| Martin Hořeňovský | 31f5e2e | 2017-05-16 13:38:52 +0200 | [diff] [blame] | 29 | 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>`: |
| Phil Nash | 605d870 | 2015-05-20 18:12:40 +0100 | [diff] [blame] | 30 | |
| 31 | ``` |
| 32 | namespace Catch { |
| Axel Huebl | 7667a7d | 2018-08-28 16:17:37 +0200 | [diff] [blame] | 33 | template<> |
| Martin Hořeňovský | 31f5e2e | 2017-05-16 13:38:52 +0200 | [diff] [blame] | 34 | struct StringMaker<T> { |
| Axel Huebl | 7667a7d | 2018-08-28 16:17:37 +0200 | [diff] [blame] | 35 | static std::string convert( T const& value ) { |
| 36 | return convertMyTypeToString( value ); |
| Martin Hořeňovský | 31f5e2e | 2017-05-16 13:38:52 +0200 | [diff] [blame] | 37 | } |
| 38 | }; |
| Phil Nash | 605d870 | 2015-05-20 18:12:40 +0100 | [diff] [blame] | 39 | } |
| 40 | ``` |
| 41 | |
| Martin Hořeňovský | 46e99e2 | 2018-09-09 16:16:49 +0200 | [diff] [blame^] | 42 | ## Catch::is_range specialisation |
| Martin Hořeňovský | 5e48486 | 2018-02-01 20:29:18 +0100 | [diff] [blame] | 43 | As a fallback, Catch attempts to detect if the type can be iterated |
| 44 | (`begin(T)` and `end(T)` are valid) and if it can be, it is stringified |
| 45 | as a range. For certain types this can lead to infinite recursion, so |
| 46 | it can be disabled by specializing `Catch::is_range` like so: |
| 47 | |
| 48 | ```cpp |
| 49 | namespace Catch { |
| 50 | template<> |
| 51 | struct is_range<T> { |
| 52 | static const bool value = false; |
| 53 | }; |
| 54 | } |
| 55 | |
| 56 | ``` |
| 57 | |
| 58 | |
| Phil Nash | a49f088 | 2015-11-18 08:39:21 +0000 | [diff] [blame] | 59 | ## Exceptions |
| 60 | |
| 61 | 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: |
| 62 | |
| 63 | ``` |
| 64 | CATCH_TRANSLATE_EXCEPTION( MyType& ex ) { |
| Axel Huebl | 7667a7d | 2018-08-28 16:17:37 +0200 | [diff] [blame] | 65 | return ex.message(); |
| Phil Nash | a49f088 | 2015-11-18 08:39:21 +0000 | [diff] [blame] | 66 | } |
| 67 | ``` |
| 68 | |
| Phil Nash | 27ce70c | 2014-12-09 18:54:35 +0000 | [diff] [blame] | 69 | --- |
| 70 | |
| Martin Moene | 6c09b45 | 2017-08-24 15:33:38 +0200 | [diff] [blame] | 71 | [Home](Readme.md#top) |