boostorg / boostorg/test

Provide more flexible ways for outputting the tested types

Open
#131 6 comments 0 reactions 1 assignee Claimed by @raffienficiaud View on GitHub
Dominant language
C++
Stars
213
Forks
149
Avg merge
18h 17m
Merged PRs (30d)
2

Description

According to the docs, if I want a custom output from tests for my type, I either have to provide an ADL-visible `operator<<` or ADL-visible function `boost_test_print_type`. This is not satisfactory in a number of cases.

### 1. Sometimes I operate on pointers or pairs:

```c++
struct Item
{
int uid;
string name;
string title;
};

using ItemPtr = std::shared_ptr;
using Count = std::pair;
```

And I would like to serialize `ItemPtr p` by `p->uid`.

I cannot do this because I would have to add a declaration to namespace `std` which is an undefined behavior.

### 2. Sometimes I want to display the same object in two different ways in two different test cases.

In a different test case I may want to display `ItemPtr p` as

```c++
"{"s + p->name + "," + p->title + "}"
```

Issue 1 could be addressed in two ways.

1. The library provides a class template `boost::test::print_type::print(T const&, std::ostream&)` which by default goes to `boost_test_print_type` but I can provide a specialization for my types.

2. You provide a function ADL-based interface, but one of the arguments is from `boost::test` namespace:

```c++
namespace boost { namespace test {

struct tag {};

template
void print_type(T const&, std::ostream&, tag); // default impl

}}
```

This way, I can always add overload into namespace `boost::test`:

```c++
namespace boost { namespace test {

void print_type(std::shared_ptr const&, std::ostream&, tag); // overload

}}
```

Tags could also solve the second issue, if they were somehow associated with test cases of test case fixtures. Or if rather than tags one would use fixture or test case namesnames:

```c++
namespace boost { namespace test {

template
struct tag {};

template
void print_type(T const&, std::ostream&, tag); // default impl

}}
```

```c++
namespace boost { namespace test {

// overloads:
void print_type(std::shared_ptr const&, std::ostream&, tag);
void print_type(std::shared_ptr const&, std::ostream&, tag);

}}
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.