Skip to content

Commit

Permalink
Document stringizers
Browse files Browse the repository at this point in the history
Resolves #20
  • Loading branch information
sbeyer committed Apr 3, 2017
1 parent fceb83f commit 0333bc2
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ Whenever Snowhouse prints an error message for a type, it will use the stream op
as a placeholder.
```cpp
struct MyType { /*...*/ };
struct MyType { int x; char c; };
AssertThat(myType, Fulfills(MyConstraint());
```
Expand All @@ -392,16 +392,43 @@ If we add a stream operator:
```cpp
std::ostream& operator<<(std::ostream& stream, const MyType& a)
{
stream << "MyType( x = " << a.x << " )";
stream << a.c << a.x;
return stream;
}
```

the output will be a bit more readable:

```
Expected: To fullfill my constraint
Actual: MyType( x = 23 )
Expected: To fulfill my constraint
Actual: f23
```

If it is necessary to print an object in a different manner than the
usual output stream operator provides, for example, to output more detailed
information, we can use a specialization of the Stringizer class template:

```cpp
namespace snowhouse
{
template<>
struct Stringizer<MyType>
{
static std::string ToString(const MyType& a)
{
std::stringstream stream;
stream << "MyType(x = " << a.x << ", c = " << int(a.c) << "('" << a.c << "'))";
return stream.str();
}
};
}
```

with output:

```
Expected: To fulfill my constraint
Actual: MyType(x = 23, c = 102('f'))
```

## Configurable Failure Handlers
Expand Down

0 comments on commit 0333bc2

Please sign in to comment.