catch框架有没有办法比较流或文件?

时间:2018-10-30 13:21:44

标签: c++ boost stream comparison catch-unit-test

我在增强测试工具中看到了宏:

BOOST_<level>_EQUAL_COLLECTION(left_begin, left_end, right_begin, right_end)

可以通过使用ifstream_iterator用于流。

Catch框架提供了一种比较流/文件的方法吗?

1 个答案:

答案 0 :(得分:1)

不是内置的,但实际上并非如此。

为此,您编写自己的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 ) );
}

很明显,您可以对其进行调整以执行所需的任何检查。