How to test input and output overloaded operator in C++ Gtest

纵饮孤独 提交于 2019-12-02 08:23:33
Slava

Is there any way I can use EXPECT_EQ instead?

You can use a stringstream to print the results of operator<< to a string, then compare the string.

https://en.cppreference.com/w/cpp/io/basic_stringstream

TEST( Distance, Output )
{
    std::ostringstream out;
    Distance d;
    out << d;
    EXPECT_EQ( "F:0 I:0", out.str() );
}

Input test would be similar, just use std::istringtream instead.

What do you want to test about the operators?

  • That the stream is in a good state after writing to or reading from it.
    You can check for that.

  • That the output operator writes a particular string for a particular distance.
    You can do this by writing into a std::ostringstream and comparing the result of calling its str() member with your expectations.

  • That the input iterator reads a particular distance from a particular string.
    You can do this employing a std::istringstream initialized with the string, comparing the distance read from it with what you expect.

  • That the class eats its own dog food.
    Use a std::stringstream to write into, then read from it, and compare what you read with what you wrote.
    Note: This will currently fail.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!