如何使用重载的相等(==)运算符向测试用例添加描述?
How to add description to a test case using overloaded equality(==) operator?
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 方法,以便它打印出我的类型而不是 {?} == {?}
我也试过通过匹配器方法
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 的运算符 <<,
但是,我不知道在我超载运算符后我应该做什么。
提前感谢您的回答
编辑:
Point Object 的 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)
不同
有两种常用的方法。两者都在您提到的 documentation 中进行了描述。
You should put this function in the same namespace as your type, or
the global namespace, and have it declared before including Catch's
header.
所以,你的运算符重载没问题,只要把它放在#include "catch.hpp"
之前即可。
#define CATCH_CONFIG_MAIN
#include <array>
#include "Point.h"
#include "Square.h"
std::ostream &operator<<(std::ostream &os, Point const& p)
{
os << "Point (" << p.get_X() << ", " << p.get_Y() << ")";
return os;
}
#include "catch.hpp"
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)));
}
}
2。 Catch::StringMaker specialisation with additional stringstream 用于正确格式化输出。
#define CATCH_CONFIG_MAIN
#include <array>
#include <sstream>
#include "Point.h"
#include "Square.h"
#include "catch.hpp"
namespace Catch
{
template <> struct StringMaker<Point>
{
static std::string convert(Point const& p)
{
std::stringstream buf;
buf << "Point (" << p.get_X() << ", " << p.get_Y() << ")";
return << buf.str();
}
};
}
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)));
}
}
这种方法避免了全局 namespace pollution 并让您屏蔽任何其他先前定义的 <<
运算符。 StringMaker
首先使用专业
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 方法,以便它打印出我的类型而不是 {?} == {?}
我也试过通过匹配器方法
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 的运算符 <<, 但是,我不知道在我超载运算符后我应该做什么。
提前感谢您的回答
编辑: Point Object 的 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)
不同有两种常用的方法。两者都在您提到的 documentation 中进行了描述。
You should put this function in the same namespace as your type, or the global namespace, and have it declared before including Catch's header.
所以,你的运算符重载没问题,只要把它放在#include "catch.hpp"
之前即可。
#define CATCH_CONFIG_MAIN
#include <array>
#include "Point.h"
#include "Square.h"
std::ostream &operator<<(std::ostream &os, Point const& p)
{
os << "Point (" << p.get_X() << ", " << p.get_Y() << ")";
return os;
}
#include "catch.hpp"
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)));
}
}
2。 Catch::StringMaker specialisation with additional stringstream 用于正确格式化输出。
#define CATCH_CONFIG_MAIN
#include <array>
#include <sstream>
#include "Point.h"
#include "Square.h"
#include "catch.hpp"
namespace Catch
{
template <> struct StringMaker<Point>
{
static std::string convert(Point const& p)
{
std::stringstream buf;
buf << "Point (" << p.get_X() << ", " << p.get_Y() << ")";
return << buf.str();
}
};
}
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)));
}
}
这种方法避免了全局 namespace pollution 并让您屏蔽任何其他先前定义的 <<
运算符。 StringMaker
首先使用专业