有没有一种方法可以使用 catch 框架来比较流或文件?
Is there a way with catch framework to compare stream or files?
我在boost测试工具里看到宏:
BOOST_<level>_EQUAL_COLLECTION(left_begin, left_end, right_begin, right_end)
可以通过使用 ifstream_iterator.
适用于流
Catch 框架是否提供这种比较方式streams/files?
不是内置的,但它并不打算。
为此您自己编写 matcher。
这是文档中整数范围检查的示例:
// The matcher class
class IntRange : public Catch::MatcherBase<int> {
int m_begin, m_end;
public:
IntRange( int begin, int end ) : m_begin( begin ), m_end( end ) {}
// Performs the test for this matcher
virtual bool match( int const& i ) const override {
return i >= m_begin && i <= m_end;
}
// Produces a string describing what this matcher does. It should
// include any provided data (the begin/ end in this case) and
// be written as if it were stating a fact (in the output it will be
// preceded by the value under test).
virtual std::string describe() const {
std::ostringstream ss;
ss << "is between " << m_begin << " and " << m_end;
return ss.str();
}
};
// The builder function
inline IntRange IsBetween( int begin, int end ) {
return IntRange( begin, end );
}
// ...
// Usage
TEST_CASE("Integers are within a range")
{
CHECK_THAT( 3, IsBetween( 1, 10 ) );
CHECK_THAT( 100, IsBetween( 1, 10 ) );
}
很明显,您可以调整它来执行您需要的任何检查。
我在boost测试工具里看到宏:
BOOST_<level>_EQUAL_COLLECTION(left_begin, left_end, right_begin, right_end)
可以通过使用 ifstream_iterator.
适用于流Catch 框架是否提供这种比较方式streams/files?
不是内置的,但它并不打算。
为此您自己编写 matcher。
这是文档中整数范围检查的示例:
// The matcher class
class IntRange : public Catch::MatcherBase<int> {
int m_begin, m_end;
public:
IntRange( int begin, int end ) : m_begin( begin ), m_end( end ) {}
// Performs the test for this matcher
virtual bool match( int const& i ) const override {
return i >= m_begin && i <= m_end;
}
// Produces a string describing what this matcher does. It should
// include any provided data (the begin/ end in this case) and
// be written as if it were stating a fact (in the output it will be
// preceded by the value under test).
virtual std::string describe() const {
std::ostringstream ss;
ss << "is between " << m_begin << " and " << m_end;
return ss.str();
}
};
// The builder function
inline IntRange IsBetween( int begin, int end ) {
return IntRange( begin, end );
}
// ...
// Usage
TEST_CASE("Integers are within a range")
{
CHECK_THAT( 3, IsBetween( 1, 10 ) );
CHECK_THAT( 100, IsBetween( 1, 10 ) );
}
很明显,您可以调整它来执行您需要的任何检查。