blob: 933f2e619fe3cb5ceae815ac83fbff60488a7e99 [file] [log] [blame] [view]
Martin Moenee8225052017-08-24 15:21:36 +02001<a id="top"></a>
Phil Nash27ce70c2014-12-09 18:54:35 +00002# String conversions
3
Martin Moenea81c01d2018-09-05 10:01:54 +02004**Contents**<br>
Martin Hořeňovský46e99e22018-09-09 16:16:49 +02005[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 Moenea81c01d2018-09-05 10:01:54 +02008[Exceptions](#exceptions)<br>
9
Phil Nash27ce70c2014-12-09 18:54:35 +000010Catch 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ý31f5e2e2017-05-16 13:38:52 +020011Most 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 Nash27ce70c2014-12-09 18:54:35 +000012
13## operator << overload for std::ostream
14
15This 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 Nash82754c12014-12-12 08:29:21 +000017```
18std::ostream& operator << ( std::ostream& os, T const& value ) {
Axel Huebl7667a7d2018-08-28 16:17:37 +020019 os << convertMyTypeToString( value );
20 return os;
Phil Nash82754c12014-12-12 08:29:21 +000021}
22```
Phil Nash27ce70c2014-12-09 18:54:35 +000023
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
thecppzoo3f3238e2018-11-16 16:45:13 -080026You should put this function in the same namespace as your type, or the global namespace, and have it declared before including Catch's header.
Phil Nash27ce70c2014-12-09 18:54:35 +000027
Martin Hořeňovský46e99e22018-09-09 16:16:49 +020028## Catch::StringMaker specialisation
Martin Hořeňovský31f5e2e2017-05-16 13:38:52 +020029If 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 Nash605d8702015-05-20 18:12:40 +010030
31```
32namespace Catch {
Axel Huebl7667a7d2018-08-28 16:17:37 +020033 template<>
Martin Hořeňovský31f5e2e2017-05-16 13:38:52 +020034 struct StringMaker<T> {
Axel Huebl7667a7d2018-08-28 16:17:37 +020035 static std::string convert( T const& value ) {
36 return convertMyTypeToString( value );
Martin Hořeňovský31f5e2e2017-05-16 13:38:52 +020037 }
38 };
Phil Nash605d8702015-05-20 18:12:40 +010039}
40```
41
Martin Hořeňovský46e99e22018-09-09 16:16:49 +020042## Catch::is_range specialisation
Martin Hořeňovský5e484862018-02-01 20:29:18 +010043As 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
45as a range. For certain types this can lead to infinite recursion, so
46it can be disabled by specializing `Catch::is_range` like so:
47
48```cpp
49namespace Catch {
50 template<>
51 struct is_range<T> {
52 static const bool value = false;
53 };
54}
55
56```
57
58
Phil Nasha49f0882015-11-18 08:39:21 +000059## Exceptions
60
61By 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```
64CATCH_TRANSLATE_EXCEPTION( MyType& ex ) {
Axel Huebl7667a7d2018-08-28 16:17:37 +020065 return ex.message();
Phil Nasha49f0882015-11-18 08:39:21 +000066}
67```
68
Phil Nash27ce70c2014-12-09 18:54:35 +000069---
70
Martin Moene6c09b452017-08-24 15:33:38 +020071[Home](Readme.md#top)