2014-11-14 18:50:42 +01:00
|
|
|
# Making Types Printable
|
|
|
|
|
2014-11-14 19:12:02 +01:00
|
|
|
Catch can print many standard types out of the box, but it is also capable of using user defined print functions and overloads.
|
2014-11-14 18:50:42 +01:00
|
|
|
|
2014-11-14 19:12:02 +01:00
|
|
|
## Using ostream and operator<<
|
2014-11-14 18:50:42 +01:00
|
|
|
|
2014-11-14 19:12:02 +01:00
|
|
|
By default, catch will attempt to display a variable of type `T` using an ostream overload if available.
|
2014-11-14 18:50:42 +01:00
|
|
|
|
|
|
|
```c++
|
2014-11-14 19:12:02 +01:00
|
|
|
std::ostream& operator<<(std::ostream&, T const&);
|
2014-11-14 18:50:42 +01:00
|
|
|
```
|
|
|
|
|
2014-11-14 19:12:02 +01:00
|
|
|
## Overloading Catch::toString
|
2014-11-14 18:50:42 +01:00
|
|
|
If you don't want your type to work with ostreams, or Catch is unable to find the
|
|
|
|
overload you provide, you may alternatively overload `Catch::toString`. The overload
|
|
|
|
must appear **above** the include of `catch.hpp`. For example, if I want to provide
|
|
|
|
a print functionality for `std::pair<int, char>` in my test, the top of my test file
|
|
|
|
would look like this:
|
|
|
|
|
|
|
|
```c++
|
|
|
|
#include <string>
|
|
|
|
#include <sstream>
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
namespace Catch {
|
2014-11-14 19:12:02 +01:00
|
|
|
std::string toString(std::pair<int, char> const& p) {
|
2014-11-14 18:50:42 +01:00
|
|
|
std::ostringstream oss;
|
|
|
|
oss << '{' << p.first << ", " << p.second << '}';
|
|
|
|
return oss.str();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "catch.hpp"
|
|
|
|
```
|
|
|
|
Note that the former approach is preferred, and the latter is more error-prone
|
|
|
|
due to possible typos when typing `Catch` or `toString`
|
|
|
|
|
|
|
|
## Default output
|
|
|
|
|
2014-11-14 19:12:02 +01:00
|
|
|
If neither of the two conditions has been met, and catch does not provide a string conversion for the type, catch will output `{?}` by default.
|