source: BMXNET_RPMS_dotNET_UTILITIES-BMX/branch/IHS BMX Framework/lib/NUnit/NUnit-2.5.10.11092/doc/collectionConstraints.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: 9.1 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 - CollectionConstraints</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>Collection Constraints (NUnit 2.4 / 2.5)</h2>
28
29<p>Collection constraints perform tests that are specific to collections.
30 The following collection constraints are provided. Before NUnit 2.4.6,
31 these constraints only operated on true Collections. Beginning with 2.4.6,
32 they can work with any object that implements IEnumerable.
33
34<p>Beginning with NUnit 2.4.2, use of an improper argument type caused tests
35 to fail. Later releases give an error rather than a failure, so that negated
36 constraints will not appear to succeed.
37
38<p>For example, both of the following statements give an error in later releases,
39 but the second would have succeeded in earlier versions of NUnit.
40
41<div class="code"><pre>
42int[] iarray = new int[] { 1, 2, 3 };
43
44Assert.That( 5, Is.SubsetOf( iarray ) ); // Fails in early releases
45Assert.That( 5, Is.Not.SubsetOf( iarray ) ); /
46</pre></div>
47
48<h3>AllItemsConstraint</h3>
49
50<h4>Action</h4>
51<p>Applies a constraint to each item in a collection, succeeding only if all of them succeed.
52
53<h4>Constructor</h4>
54<div class="code"><pre>
55AllItemsConstraint(Constraint itemConstraint)
56</pre></div>
57
58<h4>Syntax</h4>
59<div class="code"><pre>
60Is.All...
61Has.All...
62</pre></div>
63
64<h4>Examples of Use</h4>
65<div class="code"><pre>
66int[] iarray = new int[] { 1, 2, 3 };
67string[] sarray = new string[] { "a", "b", "c" };
68
69Assert.That( iarray, Is.All.Not.Null );
70Assert.That( sarray, Is.All.InstanceOf<string>() );
71Assert.That( iarray, Is.All.GreaterThan(0) );
72Assert.That( iarray, Has.All.GreaterThan(0) );
73</pre></div>
74
75<h3>SomeItemsConstraint</h3>
76
77<h4>Action</h4>
78<p>Applies a constraint to each item in a collection, succeeding if at least one of them succeeds.
79
80<h4>Constructor</h4>
81<div class="code"><pre>
82SomeItemsConstraint(Constraint itemConstraint)
83</pre></div>
84
85<h4>Syntax</h4>
86<div class="code"><pre>
87Has.Some...
88</pre></div>
89
90<h4>Examples of Use</h4>
91<div class="code"><pre>
92int[] iarray = new int[] { 1, 2, 3 };
93string[] sarray = new string[] { "a", "b", "c" };
94
95Assert.That( iarray, Has.Some.GreaterThan(2) );
96Assert.That( sarray, Has.Some.Length(1) );
97</pre></div>
98
99<h3>NoItemConstraint</h3>
100
101<h4>Action</h4>
102<p>Applies a constraint to each item in a collection, succeeding only if all of them fail.
103
104<h4>Constructor</h4>
105<div class="code"><pre>
106NoItemConstraint(Constraint itemConstraint)
107</pre></div>
108
109<h4>Syntax</h4>
110<div class="code"><pre>
111Has.None...
112Has.No...
113</pre></div>
114
115<h4>Examples of Use</h4>
116<div class="code"><pre>
117int[] iarray = new int[] { 1, 2, 3 };
118string[] sarray = new string[] { "a", "b", "c" };
119
120Assert.That( iarray, Has.None.Null );
121Assert.That( iarray, Has.No.Null );
122Assert.That( sarray, Has.None.EqualTo("d") );
123Assert.That( iarray, Has.None.LessThan(0) );
124</pre></div>
125
126<h3>UniqueItemsConstraint</h3>
127
128<h4>Action</h4>
129<p>Tests that a collection contains only unique items.
130
131<h4>Constructor</h4>
132<div class="code"><pre>
133UniqueItemsConstraint()
134</pre></div>
135
136<h4>Syntax</h4>
137<div class="code"><pre>
138Is.Unique
139</pre></div>
140
141<h4>Example of Use</h4>
142<div class="code"><pre>
143string[] sarray = new string[] { "a", "b", "c" };
144
145Assert.That( sarray, Is.Unique );
146</pre></div>
147
148<h4>Notes</h4>
149<ol>
150<li>??
151</ol>
152
153<h3>CollectionContainsConstraint</h3>
154
155<h4>Action</h4>
156<p>Tests that a collection contains an object.
157
158<h4>Constructor</h4>
159<div class="code"><pre>
160CollectionContainsConstraint( object )
161</pre></div>
162
163<h4>Syntax</h4>
164<div class="code"><pre>
165Has.Member( object )
166Contains.Item( object )
167</pre></div>
168
169<h4>Modifiers</h4>
170<div class="code"><pre>
171...Using(IComparer comparer)
172...Using<T>(IComparer&lt;T&gt; comparer)
173...Using<T>(Comparison&lt;T&gt; comparer)
174</pre></div>
175
176<h4>Examples of Use</h4>
177<div class="code"><pre>
178int[] iarray = new int[] { 1, 2, 3 };
179string[] sarray = new string[] { "a", "b", "c" };
180
181Assert.That( iarray, Has.Member(3) );
182Assert.That( sarray, Has.Member("b") );
183Assert.That( sarray, Contains.Item("c") );
184Assert.That( sarray, Has.No.Member("x") );
185</pre></div>
186
187<h4>Notes</h4>
188<ol>
189<li>For references, <b>Has.Member</b> uses object equality to find a member in a
190collection. To check for an object equal to an item the collection, use
191<b>Has.Some.EqualTo(...)</b>.
192</ol>
193
194<h3>CollectionEquivalentConstraint</h3>
195
196<h4>Action</h4>
197<p>Tests that two collections are equivalent - that they contain
198the same items, in any order.
199
200<h4>Constructor</h4>
201<div class="code"><pre>
202CollectionEquivalentConstraint( IEnumerable other )
203</pre></div>
204
205<h4>Syntax</h4>
206<div class="code"><pre>
207Is.EquivalentTo( IEnumerable other )
208</pre></div>
209
210<h4>Examples of Use</h4>
211<div class="code"><pre>
212int[] iarray = new int[] { 1, 2, 3 };
213string[] sarray = new string[] { "a", "b", "c" };
214
215Assert.That( new string[] { "c", "a", "b" }, Is.EquivalentTo( sarray ) );
216Assert.That( new int[] { 1, 2, 2 }, Is.Not.EquivalentTo( iarray ) );
217</pre></div>
218
219<h4>Notes</h4>
220<ol>
221<li>To compare collections for equality, use Is.EqualTo().
222</ol>
223
224<h3>CollectionSubsetConstraint</h3>
225
226<h4>Action</h4>
227<p>Tests that one collection is a subset of another.
228
229<h4>Constructor</h4>
230<div class="code"><pre>
231CollectionSubsetConstraint( ICollection )
232</pre></div>
233
234<h4>Syntax</h4>
235<div class="code"><pre>
236Is.SubsetOf( IEnumerable )
237</pre></div>
238
239<h4>Example of Use</h4>
240<div class="code"><pre>
241int[] iarray = new int[] { 1, 2, 3 };
242
243Assert.That( new int[] { 1, 3 }, Is.SubsetOf( iarray ) );
244</pre></div>
245
246<h3>CollectionOrderedConstraint (NUnit 2.5)</h3>
247
248<h4>Action</h4>
249<p>Tests that a collection is ordered.
250
251<h4>Constructor</h4>
252<div class="code"><pre>
253CollectionOrderedConstraint()
254</pre></div>
255
256<h4>Syntax</h4>
257<div class="code"><pre>
258Is.Ordered
259</pre></div>
260
261<h4>Modifiers</h4>
262<div class="code"><pre>
263...Descending
264...By(string propertyName)
265...Using(IComparer comparer)
266...Using<T>(IComparer&lt;T&gt; comparer)
267...Using<T>(Comparison&lt;T&gt; comparer)
268</pre></div>
269
270<h4>Examples of Use</h4>
271<div class="code"><pre>
272int[] iarray = new int[] { 1, 2, 3 };
273string[] sarray = new string[] { "c", "b", "a" };
274string[] sarray2 = new string[] ( "a", "aa", "aaa" );
275
276Assert.That( iarray, Is.Ordered );
277Assert.That( sarray, Is.Ordered.Descending );
278Assert.That( sarray2, Is.Ordered.By("Length");
279</pre></div>
280
281<h4>Notes</h4>
282<ol>
283<li>Modifiers may be combined and may appear in any order. If the
284same modifier is used more than once, the result is undefined.
285<li>The syntax of Is.Ordered has changed from earlier betas.
286</ol>
287
288</div>
289
290<!-- Submenu -->
291<div id="subnav">
292<ul>
293<li><a href="index.html">NUnit 2.5.10</a></li>
294<ul>
295<li><a href="getStarted.html">Getting&nbsp;Started</a></li>
296<li><a href="assertions.html">Assertions</a></li>
297<li><a href="constraintModel.html">Constraints</a></li>
298<ul>
299<li><a href="equalConstraint.html">Equal&nbsp;Constraint</a></li>
300<li><a href="sameasConstraint.html">SameAs&nbsp;Constraint</a></li>
301<li><a href="conditionConstraints.html">Condition&nbsp;Constraints</a></li>
302<li><a href="comparisonConstraints.html">Comparison&nbsp;Constrants</a></li>
303<li><a href="pathConstraints.html">Path&nbsp;Constraints</a></li>
304<li><a href="typeConstraints.html">Type&nbsp;Constraints</a></li>
305<li><a href="stringConstraints.html">String&nbsp;Constraints</a></li>
306<li id="current"><a href="collectionConstraints.html">Collection&nbsp;Constraints</a></li>
307<li><a href="propertyConstraint.html">Property&nbsp;Constraint</a></li>
308<li><a href="throwsConstraint.html">Throws&nbsp;Constraint</a></li>
309<li><a href="compoundConstraints.html">Compound&nbsp;Constraints</a></li>
310<li><a href="delayedConstraint.html">Delayed&nbsp;Constraint</a></li>
311<li><a href="listMapper.html">List&nbsp;Mapper</a></li>
312<li><a href="reusableConstraint.html">Reusable&nbsp;Constraint</a></li>
313</ul>
314<li><a href="attributes.html">Attributes</a></li>
315<li><a href="runningTests.html">Running&nbsp;Tests</a></li>
316<li><a href="extensibility.html">Extensibility</a></li>
317<li><a href="releaseNotes.html">Release&nbsp;Notes</a></li>
318<li><a href="samples.html">Samples</a></li>
319<li><a href="license.html">License</a></li>
320</ul>
321</ul>
322</div>
323<!-- End of Submenu -->
324
325
326<!-- Standard Footer for NUnit.org -->
327<div id="footer">
328 Copyright &copy; 2010 Charlie Poole. All Rights Reserved.
329</div>
330<!-- End of Footer -->
331
332</body>
333</html>
Note: See TracBrowser for help on using the repository browser.