When both iterators and operator<< are available for class, gtest selects iterators for printing value
- Dominant language
- C++
- Stars
- 39.5k
- Forks
- 10.9k
- Avg merge
- 6d 13h
- Merged PRs (30d)
- 1
Description
**Describe the bug**
I have custom string class with both `operator<<`, plus all typedefs and methods for iterators. When Gtest/Gmock needs to print it, it treats it as a container and uses iterators instead of calling defined `operator<<`.
I had to add PrintTo in order to print it as a string. This helped for sample code below. However in my real code values in "Actual" lines for mocks still are printed as containers. I still need to find out why this part does not work as expected in my code.
**Steps to reproduce the bug**
```cpp
#include
#include
#include
#include
class MyString
{
public:
typedef std::string::traits_type traits_type;
typedef std::string::value_type value_type;
typedef std::string::allocator_type allocator_type;
typedef std::string::size_type size_type;
typedef std::string::difference_type difference_type;
typedef std::string::reference reference;
typedef std::string::const_reference const_reference;
typedef std::string::pointer pointer;
typedef std::string::const_pointer const_pointer;
typedef std::string::iterator iterator;
typedef std::string::const_iterator const_iterator;
typedef std::string::reverse_iterator reverse_iterator;
typedef std::string::const_reverse_iterator const_reverse_iterator;
MyString() = default;
MyString(const MyString&) = default;
MyString(MyString&&) = default;
MyString& operator=(const MyString&) = default;
MyString& operator=(MyString&&) = default;
MyString(const char* s) : str(s) {}
iterator begin() noexcept { return str.begin(); }
const_iterator begin() const noexcept { return str.begin(); }
const_iterator cbegin() const noexcept { return str.cbegin(); }
iterator end() noexcept { return str.end(); }
const_iterator end() const noexcept { return str.end(); }
const_iterator cend() const noexcept { return str.cend(); }
reverse_iterator rbegin() noexcept { return str.rbegin(); }
const_reverse_iterator rbegin() const noexcept { return str.rbegin(); }
const_reverse_iterator crbegin() const noexcept { return str.crbegin(); }
reverse_iterator rend() noexcept { return str.rend(); }
const_reverse_iterator rend() const noexcept { return str.rend(); }
const_reverse_iterator crend() const noexcept { return str.crend(); }
bool operator==(const MyString& s) const { return str == s.str; }
bool operator==(const std::string& s) const { return str == s; }
bool operator==(const char* s) const { return str == s; }
bool operator!=(const MyString& s) const { return str != s.str; }
bool operator!=(const std::string& s) const { return str != s; }
bool operator!=(const char* s) const { return str != s; }
friend std::ostream& operator<<(std::ostream& os, const MyString& str) { return os << str.str; }
private:
std::string str;
};
inline bool operator==(const std::string& s1, const MyString& s2) { return s2 == s1; }
inline bool operator==(const char* s1, const MyString& s2) { return s2 == s1; }
inline bool operator!=(const std::string& s1, const MyString& s2) { return s2 != s1; }
inline bool operator!=(const char* s1, const MyString& s2) { return s2 != s1; }
class MyMock
{
public:
MOCK_METHOD1(foo, void(const MyString&));
};
//inline void PrintTo(const MyString& s, std::ostream* os) { *os << s; }
TEST(FooTest, test)
{
MyMock mock;
EXPECT_CALL(mock, foo(MyString("foo")));
EXPECT_CALL(mock, foo(MyString("bar")));
mock.foo("baz");
}
```
**Does the bug persist in the most recent commit?**
I did not try. I use gtest-1.10.0-3.el7.x86_64
**What operating system and version are you using?**
CentOS Linux release 7.9.2009 (Core)
**What compiler and version are you using?**
gcc (GCC) 10.2.1 20200804 (Red Hat 10.2.1-2)
**What build system are you using?**
GNU Make 4.2.1
Contributor guide
Assessment
This issue has not been assessed yet.