google / google/googletest

Best printer selection for containers is a pain

Open
#3,458 4 comments 1 reaction 1 assignee Claimed by @sbenzaquen View on GitHub
bug
Dominant language
C++
Stars
39.6k
Forks
10.9k
Avg merge
6d 13h
Merged PRs (30d)
1

Description

**Describe the bug**

I hit a wall when testing a custom container. A stream operator for that container exists and should be used to pretty print the container in test reports. AFAIK this is what the Google Test documentation says.
However it didn't work. Google test ignored my operator and printed the container in a (sub-optimal) way using internal, generic code.

I checked why this happens. The `PrintWithFallback()` function documentation says:

```
// Select the best printer in the following order:
// - Print containers (they have begin/end/etc).
// - Print function pointers.
// - Print object pointers.
// - Use the stream operator, if available.
// .... some more lines
```

So obviously internal code has higher priority than custom stream operators. This seems not useful to me. A stream operator should always be preferred if it exists.

**Steps to reproduce the bug**

Create a custom container. Implement a stream operator for it. Then write an equality test for the container that fails. You will see that the stream operator is not used to print actual and expected container values.

**Does the bug persist in the most recent commit?**

Yes

**What operating system and version are you using?**

Kubuntu 20.04.

**What compiler and version are you using?**

GCC 10

**What build system are you using?**

qmake

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.