blob: cfca05331468a1acb23e8e3146beec4effdfe2c9 [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 ) {
Phil Nash27ce70c2014-12-09 18:54:35 +000013 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 {
Martin Hořeňovský31f5e2e2017-05-16 13:38:52 +020027 template<>
28 struct StringMaker<T> {
Martin Hořeňovský67914d82017-05-21 23:40:05 +020029 static std::string convert( T const& value ) {
Martin Hořeňovský31f5e2e2017-05-16 13:38:52 +020030 return convertMyTypeToString( value );
31 }
32 };
Phil Nash605d8702015-05-20 18:12:40 +010033}
34```
35
Phil Nasha49f0882015-11-18 08:39:21 +000036## Exceptions
37
38By 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:
39
40```
41CATCH_TRANSLATE_EXCEPTION( MyType& ex ) {
42 return ex.message();
43}
44```
45
Phil Nash27ce70c2014-12-09 18:54:35 +000046---
47
Martin Moene6c09b452017-08-24 15:33:38 +020048[Home](Readme.md#top)