Home > The Unit Test Framework > Testing tools > Custom predicate support
PrevNext

Custom predicate support

Even though supplied testing tools cover wide range of possible checks and provide detailed report on cause of error in some cases you may want to implement and use custom predicate that perform complex check and produce intelligent report on failure. To satisfy this need testing tools implement custom predicate support. There two layers of custom predicate support implemented by testing tools toolbox: with and without custom error message generation.
雖然提供的測試工具提供了很廣範圍的檢查,並在錯誤時提供詳細的報告,但在某些情況下,你可以需要實現自定義的斷言, 執行複雜的檢查,並在失敗時產生智能的報告。為了滿足這個需要,測試工具實現了自定義斷言的支持。 測試工具工具箱為自定義斷言提供了兩層支持:生成自定義錯誤信息,不生成自定義錯誤信息。

The first layer is supported by BOOST_CHECK_PREDICATE family of testing tools. You can use it to check any custom predicate that reports the result as boolean value. The values of the predicate arguments are reported by the tool automatically in case of failure.
第一層支持通過 BOOST_CHECK_PRECIATE 族測試工具提供。你可以通過它檢查任何返回布爾值作為結果報告的自定義斷言。 失敗時斷言參數的值自動被工具報告。

Example 36. Custom predicate support using BOOST_CHECK_PREDICATE

#define BOOST_TEST_MODULE example
#include <boost/test/included/unit_test.hpp>

//____________________________________________________________________________//

bool is_even( int i )        { return i%2 == 0;  }

BOOST_AUTO_TEST_CASE( test_is_even )
{
    BOOST_CHECK_PREDICATE( is_even, (14) );

    int i = 17;
    BOOST_CHECK_PREDICATE( is_even, (i) );
}

//____________________________________________________________________________//

Source code | Show output
> example
Running 1 test case...
test.cpp(13): error in "test_is_even": check is_even( i ) failed for ( 17 )

*** 1 failure detected in test suite "example"

To use second layer your predicate have to return boost::test_tools::predicate_result. This class encapsulates boolean result value along with any error or information message you opt to report.
要使用第二層支持,你的斷言需要返回 boost::test_tools::predicate_result。 這個類封裝了布爾結果值和你要報告的錯誤或信息。

Usually you construct the instance of class boost::test_tools::predicate_result inside your predicate function and return it by value. The constructor expects one argument - the boolean result value. The constructor is implicit, so you can simply return boolean value from your predicate and boost::test_tools::predicate_result is constructed automatically to hold your value and empty message. You can also assign boolean value to the constructed instance. You can check the current predicate value by using operator!() or directly accessing public read-only property p_predicate_value. The error message is stored in public read-write property p_message.
通常,你在自己的斷言函數中構造類 boost::test_tools::predicate_result 的實例並按值返回。 構造函數需要一個參數 - 布爾結果值。構造函數是隱式的,所以你可以簡單地從斷言中返回布爾值, boost::test_tools::predicate_result 被自動構造,持有你的值和空的信息。 你也可以將布爾值賦給被構造的實例中。你可以使用 operator!() 或直接訪問公有只讀屬性 p_predicate_value 來檢查當前的斷言值。 錯誤信息存儲在公有只讀屬性 p_message 中。

Example 37. Custom predicate support using class predicate_result

#define BOOST_TEST_MODULE example
#include <boost/test/included/unit_test.hpp>

//____________________________________________________________________________//

boost::test_tools::predicate_result
compare_lists( std::list<int> const& l1, std::list<int> const& l2 )
{
    if( l1.size() != l2.size() ) {
        boost::test_tools::predicate_result res( false );

        res.message() << "Different sizes [" << l1.size() << "!=" << l2.size() << "]";

        return res;
    }

    return true;
}

//____________________________________________________________________________//

BOOST_AUTO_TEST_CASE( test_list_comparizon )
{
    std::list<int> l1, l2;
    l1.push_back( 1 );
    l1.push_back( 2 );

    BOOST_CHECK( compare_lists( l1, l2 ) );
}

//____________________________________________________________________________//

Source code | Show output
Running 1 test case...
test.cpp(28): error in "test_list_comparizon": check compare_lists( l1, l2 ) failed. Different sizes [2!=0]

*** 1 failure detected in test suite "example"


PrevUpHomeNext