File: custom_predicate_support.html

package info (click to toggle)
boost 1.34.1-14
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 116,412 kB
  • ctags: 259,566
  • sloc: cpp: 642,395; xml: 56,450; python: 17,612; ansic: 14,520; sh: 2,265; yacc: 858; perl: 481; makefile: 478; lex: 94; sql: 74; csh: 6
file content (97 lines) | stat: -rw-r--r-- 5,646 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<HTML>
<HEAD>
<TITLE>The Test Tools: custom predicate support</TITLE>
<LINK rel="stylesheet" type="text/css" href="../../style/btl.css" media="screen">
<LINK rel="stylesheet" type="text/css" href="../../style/btl-print.css" media="print">
<META http-equiv="Content-Language" content="en-us">
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</HEAD>
<BODY> 
<DIV class="header"> <A href="../../index.html">Boost.Test</A> &gt; <A href="../index.html">Components</A> &gt; <A href="index.html">The
    Test Tools</A> &gt; <SPAN class="current_article">custom predicate support </SPAN></DIV> 
<DIV class="body"> <IMG src="../../btl1.gif" width="252" height="43" alt="Boost Test logo"> 
  <H1 class="subtitle">The custom predicate support</H1> 
  <H2 class="first-line-indented">Synopsis</H2> 
  <PRE class="code"><SPAN class="reserv-word">class</SPAN> <SPAN class="new-term">extended_predicate_value</SPAN> {
<SPAN class="reserv-word">public</SPAN>:
    extended_predicate_value( <SPAN class="cpp-type">bool</SPAN> predicate_value_ );
    extended_predicate_value( extended_predicate_value const&amp; rhs );

    <SPAN class="cpp-type">bool</SPAN>    operator!() <SPAN class="reserv-word">const</SPAN>;
    <SPAN class="cpp-type">void</SPAN>    operator=( <SPAN class="cpp-type">bool</SPAN> predicate_value_ );

    readonly_property&lt;bool&gt;              p_predicate_value;
    boost::shared_ptr&lt;wrap_stringstream&gt; p_message;
};</PRE> 
  <H3>Description</H3> 
  <P class="first-line-indented">There two layers of custom predicate support
    implemented by the Test Tools:</P> 
  <UL> 
    <LI>Simple  predicate one or two arity without custom error message support</LI> 
    <LI>Arbitrary predicate with custom error message support</LI> 
  </UL> 
  <P class="first-line-indented">To use first layer use BOOST_CHECK_PREDICATE
    tool. You could implement any custom check and report the result by returning
    boolean value from your predicate. For detailed description of this see <A href="reference/index.html">reference</A>.</P>
  <P class="first-line-indented">To
      use second layer you predicate should be defined to return boost::test_tools::<SPAN class="new-term">extended_predicate_value</SPAN>.
      This class encapsulate both boolean result value and error/info message
      in case if result is true/false. Be aware that message returned in extended_predicate_value
      will be the only one logged by the framework in case of error (actually
      plus error location). Usual error notification gets skipped. </P>
  <P class="first-line-indented">Usually you construct the instance of extended_predicate_value
    inside your predicate and return it by value. The constructor expects one
    argument - the boolean result value. The constructor is implicit, meaning
    that you could simply return boolean value from your predicate - extended_predicate_value
    get implicitly constructed to hold your value and empty error message. In
    case if you want to update this value after construction use method <SPAN class="new-term">operator=(
    bool )</SPAN>.
    Would you need to check the current predicate value use method <SPAN class="new-term">operator!()</SPAN> or
    directly access public read only property  <SPAN class="code"><SPAN class="new-term">p_predicate_value</SPAN></SPAN>. To
    create/modify error message you should use public read/write property <SPAN class="new-term">p_message</SPAN>.
    p_message is a shared pointer to the <SPAN class="code">class
    wrap_stringstream</SPAN>. You will need to allocate instance of it dynamically
    in you predicate. It gets destroyed after error is logged. Use usual ostream
    interface to create the error message.</P> 
  <H3>Example</H3>
  <PRE class="code"><SPAN class="comment">// custom comparison predicate that only checks for lists size</SPAN>
extended_predicate_value
compare_lists( <SPAN class="cpp-type">std::list</SPAN>&lt;<SPAN class="cpp-type">int</SPAN>&gt; <SPAN class="reserv-word">const</SPAN>&amp; l1, <SPAN class="cpp-type">std::list</SPAN>&lt;<SPAN class="cpp-type">int</SPAN>&gt; <SPAN class="reserv-word">const</SPAN>&amp; l2 )
{
    <SPAN class="reserv-word">if</SPAN>( l1.size() != l2.size() ) {
        extended_predicate_value res( <SPAN class="reserv-word">false</SPAN> );

        res.p_message.reset( <SPAN class="reserv-word">new</SPAN> boost::wrap_stringstream );

        *res.p_message &lt;&lt; <SPAN class="literal">&quot; Different sizes [&quot;</SPAN> &lt;&lt; l1.size() &lt;&lt; <SPAN class="literal">&quot;!=&quot;</SPAN> &lt;&lt; l2.size() &lt;&lt; <SPAN class="literal">&quot;]&quot;</SPAN>;

        <SPAN class="reserv-word">return</SPAN> res;
    }

    <SPAN class="reserv-word">return</SPAN> <SPAN class="reserv-word">true</SPAN>;
}

...

<SPAN class="cpp-type">void</SPAN> my_test_case()
{
    ...
    BOOST_CHECK( compare_lists( l1, l2 ) );
}
</PRE>
</DIV> 
<DIV class="footer"> 
  <DIV class="footer-body"> 
    <P> &copy; <A name="Copyright">Copyright</A> <A href="mailto:boost-test%20at%20emailaccount%20dot%20com%20%28please%20unobscure%29">Gennadiy
        Rozental</A> 2001-2006. <BR> 
      Distributed under the Boost Software License, Version 1.0.
      (See accompanying file <A href="../../../../../LICENSE_1_0.txt">LICENSE_1_0.txt</A> or copy at
      <A href="http://www.boost.org/LICENSE_1_0.txt">www.boost.org/LICENSE_1_0.txt</A>)</P> 
    <P>Revised:
      <!-- #BeginDate format:Sw1 -->28 February, 2006<!-- #EndDate --> 
    </P> 
  </DIV> 
</DIV> 
</BODY>
</HTML>