source: BMXNET_RPMS_dotNET_UTILITIES-BMX/branch/IHS BMX Framework/lib/NUnit/NUnit-2.5.10.11092/doc/reusableConstraint.html@ 1146

Last change on this file since 1146 was 1146, checked in by Sam Habiel, 13 years ago

Initial Import of BMX4

File size: 5.6 KB
Line 
1<!-- saved from url=(0014)about:internet --><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
2<html>
3<!-- Standard Head Part -->
4<head>
5<title>NUnit - ReusableConstraint</title>
6<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
7<meta http-equiv="Content-Language" content="en-US">
8<link rel="stylesheet" type="text/css" href="nunit.css">
9<link rel="shortcut icon" href="favicon.ico">
10</head>
11<!-- End Standard Head Part -->
12
13<body>
14
15<!-- Standard Header for NUnit.org -->
16<div id="header">
17 <a id="logo" href="http://www.nunit.org"><img src="img/logo.gif" alt="NUnit.org" title="NUnit.org"></a>
18 <div id="nav">
19 <a href="http://www.nunit.org">NUnit</a>
20 <a class="active" href="index.html">Documentation</a>
21 </div>
22</div>
23<!-- End of Header -->
24
25<div id="content">
26
27<h2>ReusableConstraint (NUnit 2.5.6)</h2>
28
29<p>Normally constraints just work. However, attempting to reuse the
30same constraint in several places can lead to unexpected results.
31
32<p>Consider the following code as an example:
33
34<code><pre>
35 Constraint myConstraint = Is.Not.Null;
36 Assert.That("not a null", myConstraint); // Passes, of course
37 Assert.That("not a null", myConstraint); // Fails! What's that about?
38</pre></code>
39
40<p>We'll save the technical explanation for later and show the
41solution first:
42
43<code><pre>
44 ReusableConstraint myConstraint = Is.Not.Null;
45 Assert.That("not a null", myConstraint); // Passes
46 Assert.That("not a null", myConstraint); // Passes
47</pre></code>
48
49Or alternatively..
50
51<code><pre>
52 var myConstraint = new ReusableConstraint(Is.Not.Null);
53 Assert.That("not a null", myConstraint); // Passes
54 Assert.That("not a null", myConstraint); // Passes
55</pre></code>
56
57<h3>Technical Explanation</h3>
58
59<p>In the original example, the value assigned to myConstraint is
60known as an <b>unresolved</b> constraint. In fact, it's an
61unresolved NullConstraint, because that was the last constraint
62encountered in the expression. It's associated with a <b>Not</b>
63operator that has not yet been applied.
64
65<p>That's OK for use with Assert.That(), because the method
66knows how to resolve a constraint before using it. Assert.That()
67resolves this constraint to a NotConstraint referencing the
68original NullConstraint.
69
70<p>Of course, the original reference in myConstraint is left
71unchanged in all of this. But the EqualConstraint it points
72to has now been resolved. It is now a <b>resolved</b> constraint
73and can't be resolved again by the second Assert.That(), which
74only sees the NullConstraint and not the NotConstraint.
75
76<p>So, for reusability, what we want to save is the result
77of resolving the constraint, in this case
78
79<pre> NotConstraint => NullConstraint</pre>
80
81That's what <b>ReusableConstraint</b> does for us. It resolves
82the full expression and saves the result. Then it passes all
83operations on to that saved result.
84
85<h3>When to Use It</h3>
86
87<p>Use this constraint any time you want to reuse a constraint
88expression and you'll be safe.
89
90<p>If you like to take chances, you'll find that you can
91avoid using it in the following cases...
92
93<ol>
94<li> With a simple constraint involving no operators, like...
95
96<pre>
97 Constraint myConstraint = Is.Null;
98 Constraint myConstraint = Is.EqualTo(42);
99</pre>
100
101<li> With any constraint you construct using new, without
102using the "dotted" constraint syntax...
103
104<pre>
105 Constraint myConstraint = new NotConstraint(new NullConstraint());
106 Constraint myConstraint = new AndConstraint(
107 new GreaterThanConstraint(0),
108 new LessThanConstraint(100));
109</pre>
110
111<p>However, there is no significant penalty to using <b>ReusableConstraint</b>.
112It makes your intent much clearer and the exceptions listed are accidents of
113the internal implementation and could disappear in future releases.
114
115
116</div>
117
118<!-- Submenu -->
119<div id="subnav">
120<ul>
121<li><a href="index.html">NUnit 2.5.10</a></li>
122<ul>
123<li><a href="getStarted.html">Getting&nbsp;Started</a></li>
124<li><a href="assertions.html">Assertions</a></li>
125<li><a href="constraintModel.html">Constraints</a></li>
126<ul>
127<li><a href="equalConstraint.html">Equal&nbsp;Constraint</a></li>
128<li><a href="sameasConstraint.html">SameAs&nbsp;Constraint</a></li>
129<li><a href="conditionConstraints.html">Condition&nbsp;Constraints</a></li>
130<li><a href="comparisonConstraints.html">Comparison&nbsp;Constrants</a></li>
131<li><a href="pathConstraints.html">Path&nbsp;Constraints</a></li>
132<li><a href="typeConstraints.html">Type&nbsp;Constraints</a></li>
133<li><a href="stringConstraints.html">String&nbsp;Constraints</a></li>
134<li><a href="collectionConstraints.html">Collection&nbsp;Constraints</a></li>
135<li><a href="propertyConstraint.html">Property&nbsp;Constraint</a></li>
136<li><a href="throwsConstraint.html">Throws&nbsp;Constraint</a></li>
137<li><a href="compoundConstraints.html">Compound&nbsp;Constraints</a></li>
138<li><a href="delayedConstraint.html">Delayed&nbsp;Constraint</a></li>
139<li><a href="listMapper.html">List&nbsp;Mapper</a></li>
140<li id="current"><a href="reusableConstraint.html">Reusable&nbsp;Constraint</a></li>
141</ul>
142<li><a href="attributes.html">Attributes</a></li>
143<li><a href="runningTests.html">Running&nbsp;Tests</a></li>
144<li><a href="extensibility.html">Extensibility</a></li>
145<li><a href="releaseNotes.html">Release&nbsp;Notes</a></li>
146<li><a href="samples.html">Samples</a></li>
147<li><a href="license.html">License</a></li>
148</ul>
149</ul>
150</div>
151<!-- End of Submenu -->
152
153
154<!-- Standard Footer for NUnit.org -->
155<div id="footer">
156 Copyright &copy; 2010 Charlie Poole. All Rights Reserved.
157</div>
158<!-- End of Footer -->
159
160</body>
161</html>
Note: See TracBrowser for help on using the repository browser.