blob: 00048bbb31f4c188d2f2f35fbfe4914250d5da81 [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
4Catch 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 +02005Most 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 +00006
7## operator << overload for std::ostream
8
9This 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:
10
Phil Nash82754c12014-12-12 08:29:21 +000011```
12std::ostream& operator << ( std::ostream& os, T const& value ) {
Axel Huebl7667a7d2018-08-28 16:17:37 +020013 os << convertMyTypeToString( value );
14 return os;
Phil Nash82754c12014-12-12 08:29:21 +000015}
16```
Phil Nash27ce70c2014-12-09 18:54:35 +000017
18(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).
19
Martin Hořeňovský31f5e2e2017-05-16 13:38:52 +020020You should put this function in the same namespace as your type and have it declared before including Catch's header.
Phil Nash27ce70c2014-12-09 18:54:35 +000021
Phil Nash605d8702015-05-20 18:12:40 +010022## Catch::StringMaker<T> specialisation
Martin Hořeňovský31f5e2e2017-05-16 13:38:52 +020023If 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 +010024
25```
26namespace Catch {
Axel Huebl7667a7d2018-08-28 16:17:37 +020027 template<>
Martin Hořeňovský31f5e2e2017-05-16 13:38:52 +020028 struct StringMaker<T> {
Axel Huebl7667a7d2018-08-28 16:17:37 +020029 static std::string convert( T const& value ) {
30 return convertMyTypeToString( value );
Martin Hořeňovský31f5e2e2017-05-16 13:38:52 +020031 }
32 };
Phil Nash605d8702015-05-20 18:12:40 +010033}
34```
35
Martin Hořeňovský5e484862018-02-01 20:29:18 +010036## Catch::is_range<T> specialisation
37As a fallback, Catch attempts to detect if the type can be iterated
38(`begin(T)` and `end(T)` are valid) and if it can be, it is stringified
39as a range. For certain types this can lead to infinite recursion, so
40it can be disabled by specializing `Catch::is_range` like so:
41
42```cpp
43namespace Catch {
44 template<>
45 struct is_range<T> {
46 static const bool value = false;
47 };
48}
49
50```
51
52
Phil Nasha49f0882015-11-18 08:39:21 +000053## Exceptions
54
55By 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:
56
57```
58CATCH_TRANSLATE_EXCEPTION( MyType& ex ) {
Axel Huebl7667a7d2018-08-28 16:17:37 +020059 return ex.message();
Phil Nasha49f0882015-11-18 08:39:21 +000060}
61```
62
Phil Nash27ce70c2014-12-09 18:54:35 +000063---
64
Martin Moene6c09b452017-08-24 15:33:38 +020065[Home](Readme.md#top)