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

前端 未结 2 1794
广开言路
广开言路 2021-01-26 04:48

I am using following example from here

Consider I have following class

#include 

class Distance {
private:
  int feet;             
  i         


        
2条回答
  •  清酒与你
    2021-01-26 05:08

    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.

提交回复
热议问题