TEST_CASE("Functions of Square")
{
std::array<Point, 4> a_Vertices{Point(2, 2), Point(2, 4), Point(4, 2), Point(4, 4)};
Square a_Square(a_Vertices, true);
SECTION("Square is initialized properly")
{
REQUIRE(a_Square.get_Vertex_0() == Point(2, 1));
}
}
使用 catch2 单元测试框架,我想覆盖 catch2 describe 方法,以便它打印出我的类型而不是 {?} == {?}
我也试过通过 matcher 方法来做
class Point_Matcher : public Catch::MatcherBase<Point>
{
private:
Point p_;
public:
Point_Matcher(const Point &a_Point)
: p_(a_Point) {}
bool match(Point const &a_Point) const override
{
return a_Point == p_;
}
virtual std::string describe() const override
{
std::ostringstream oss;
oss << "not the same as Point (" << p_.get_X() << ", " << p_.get_Y() << ")";
return oss.str();
}
};
inline Point_Matcher is_Same(const Point &a_Point)
{
return Point_Matcher(a_Point);
}
TEST_CASE("Functions of Square")
{
std::array<Point, 4> a_Vertices{Point(2, 2), Point(2, 4), Point(4, 2), Point(4, 4)};
Square a_Square(a_Vertices, true);
SECTION("Square is initialized properly")
{
REQUIRE_THAT(a_Square.get_Vertex_0(), is_Same(Point(2, 1)));
}
}
但是,它只会显示指定为测试的对象,而不会显示正在测试的对象。输出将是 {?} 与 Point(2, 1) 不同
在https://github.com/catchorg/Catch2/blob/master/docs/tostring.md#top 建议的方法是覆盖 std::ostream 的运算符<<,但是,我不知道重载运算符后我应该做什么。
预先感谢您的回答
编辑: 点对象的 operator<< 的重载如下
std::ostream &operator<<(std::ostream &os, const Point &p)
{
os << "Point (" << p.get_X() << ", " << p.get_Y();
return os;
}
换句话说,在这种情况下,我的目标输出特别是 Point(x, y) 与 Point(x,y) 不同