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>
|
---|
42 | int[] iarray = new int[] { 1, 2, 3 };
|
---|
43 |
|
---|
44 | Assert.That( 5, Is.SubsetOf( iarray ) ); // Fails in early releases
|
---|
45 | Assert.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>
|
---|
55 | AllItemsConstraint(Constraint itemConstraint)
|
---|
56 | </pre></div>
|
---|
57 |
|
---|
58 | <h4>Syntax</h4>
|
---|
59 | <div class="code"><pre>
|
---|
60 | Is.All...
|
---|
61 | Has.All...
|
---|
62 | </pre></div>
|
---|
63 |
|
---|
64 | <h4>Examples of Use</h4>
|
---|
65 | <div class="code"><pre>
|
---|
66 | int[] iarray = new int[] { 1, 2, 3 };
|
---|
67 | string[] sarray = new string[] { "a", "b", "c" };
|
---|
68 |
|
---|
69 | Assert.That( iarray, Is.All.Not.Null );
|
---|
70 | Assert.That( sarray, Is.All.InstanceOf<string>() );
|
---|
71 | Assert.That( iarray, Is.All.GreaterThan(0) );
|
---|
72 | Assert.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>
|
---|
82 | SomeItemsConstraint(Constraint itemConstraint)
|
---|
83 | </pre></div>
|
---|
84 |
|
---|
85 | <h4>Syntax</h4>
|
---|
86 | <div class="code"><pre>
|
---|
87 | Has.Some...
|
---|
88 | </pre></div>
|
---|
89 |
|
---|
90 | <h4>Examples of Use</h4>
|
---|
91 | <div class="code"><pre>
|
---|
92 | int[] iarray = new int[] { 1, 2, 3 };
|
---|
93 | string[] sarray = new string[] { "a", "b", "c" };
|
---|
94 |
|
---|
95 | Assert.That( iarray, Has.Some.GreaterThan(2) );
|
---|
96 | Assert.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>
|
---|
106 | NoItemConstraint(Constraint itemConstraint)
|
---|
107 | </pre></div>
|
---|
108 |
|
---|
109 | <h4>Syntax</h4>
|
---|
110 | <div class="code"><pre>
|
---|
111 | Has.None...
|
---|
112 | Has.No...
|
---|
113 | </pre></div>
|
---|
114 |
|
---|
115 | <h4>Examples of Use</h4>
|
---|
116 | <div class="code"><pre>
|
---|
117 | int[] iarray = new int[] { 1, 2, 3 };
|
---|
118 | string[] sarray = new string[] { "a", "b", "c" };
|
---|
119 |
|
---|
120 | Assert.That( iarray, Has.None.Null );
|
---|
121 | Assert.That( iarray, Has.No.Null );
|
---|
122 | Assert.That( sarray, Has.None.EqualTo("d") );
|
---|
123 | Assert.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>
|
---|
133 | UniqueItemsConstraint()
|
---|
134 | </pre></div>
|
---|
135 |
|
---|
136 | <h4>Syntax</h4>
|
---|
137 | <div class="code"><pre>
|
---|
138 | Is.Unique
|
---|
139 | </pre></div>
|
---|
140 |
|
---|
141 | <h4>Example of Use</h4>
|
---|
142 | <div class="code"><pre>
|
---|
143 | string[] sarray = new string[] { "a", "b", "c" };
|
---|
144 |
|
---|
145 | Assert.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>
|
---|
160 | CollectionContainsConstraint( object )
|
---|
161 | </pre></div>
|
---|
162 |
|
---|
163 | <h4>Syntax</h4>
|
---|
164 | <div class="code"><pre>
|
---|
165 | Has.Member( object )
|
---|
166 | Contains.Item( object )
|
---|
167 | </pre></div>
|
---|
168 |
|
---|
169 | <h4>Modifiers</h4>
|
---|
170 | <div class="code"><pre>
|
---|
171 | ...Using(IComparer comparer)
|
---|
172 | ...Using<T>(IComparer<T> comparer)
|
---|
173 | ...Using<T>(Comparison<T> comparer)
|
---|
174 | </pre></div>
|
---|
175 |
|
---|
176 | <h4>Examples of Use</h4>
|
---|
177 | <div class="code"><pre>
|
---|
178 | int[] iarray = new int[] { 1, 2, 3 };
|
---|
179 | string[] sarray = new string[] { "a", "b", "c" };
|
---|
180 |
|
---|
181 | Assert.That( iarray, Has.Member(3) );
|
---|
182 | Assert.That( sarray, Has.Member("b") );
|
---|
183 | Assert.That( sarray, Contains.Item("c") );
|
---|
184 | Assert.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
|
---|
190 | collection. 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
|
---|
198 | the same items, in any order.
|
---|
199 |
|
---|
200 | <h4>Constructor</h4>
|
---|
201 | <div class="code"><pre>
|
---|
202 | CollectionEquivalentConstraint( IEnumerable other )
|
---|
203 | </pre></div>
|
---|
204 |
|
---|
205 | <h4>Syntax</h4>
|
---|
206 | <div class="code"><pre>
|
---|
207 | Is.EquivalentTo( IEnumerable other )
|
---|
208 | </pre></div>
|
---|
209 |
|
---|
210 | <h4>Examples of Use</h4>
|
---|
211 | <div class="code"><pre>
|
---|
212 | int[] iarray = new int[] { 1, 2, 3 };
|
---|
213 | string[] sarray = new string[] { "a", "b", "c" };
|
---|
214 |
|
---|
215 | Assert.That( new string[] { "c", "a", "b" }, Is.EquivalentTo( sarray ) );
|
---|
216 | Assert.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>
|
---|
231 | CollectionSubsetConstraint( ICollection )
|
---|
232 | </pre></div>
|
---|
233 |
|
---|
234 | <h4>Syntax</h4>
|
---|
235 | <div class="code"><pre>
|
---|
236 | Is.SubsetOf( IEnumerable )
|
---|
237 | </pre></div>
|
---|
238 |
|
---|
239 | <h4>Example of Use</h4>
|
---|
240 | <div class="code"><pre>
|
---|
241 | int[] iarray = new int[] { 1, 2, 3 };
|
---|
242 |
|
---|
243 | Assert.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>
|
---|
253 | CollectionOrderedConstraint()
|
---|
254 | </pre></div>
|
---|
255 |
|
---|
256 | <h4>Syntax</h4>
|
---|
257 | <div class="code"><pre>
|
---|
258 | Is.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<T> comparer)
|
---|
267 | ...Using<T>(Comparison<T> comparer)
|
---|
268 | </pre></div>
|
---|
269 |
|
---|
270 | <h4>Examples of Use</h4>
|
---|
271 | <div class="code"><pre>
|
---|
272 | int[] iarray = new int[] { 1, 2, 3 };
|
---|
273 | string[] sarray = new string[] { "c", "b", "a" };
|
---|
274 | string[] sarray2 = new string[] ( "a", "aa", "aaa" );
|
---|
275 |
|
---|
276 | Assert.That( iarray, Is.Ordered );
|
---|
277 | Assert.That( sarray, Is.Ordered.Descending );
|
---|
278 | Assert.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
|
---|
284 | same 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 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 Constraint</a></li>
|
---|
300 | <li><a href="sameasConstraint.html">SameAs Constraint</a></li>
|
---|
301 | <li><a href="conditionConstraints.html">Condition Constraints</a></li>
|
---|
302 | <li><a href="comparisonConstraints.html">Comparison Constrants</a></li>
|
---|
303 | <li><a href="pathConstraints.html">Path Constraints</a></li>
|
---|
304 | <li><a href="typeConstraints.html">Type Constraints</a></li>
|
---|
305 | <li><a href="stringConstraints.html">String Constraints</a></li>
|
---|
306 | <li id="current"><a href="collectionConstraints.html">Collection Constraints</a></li>
|
---|
307 | <li><a href="propertyConstraint.html">Property Constraint</a></li>
|
---|
308 | <li><a href="throwsConstraint.html">Throws Constraint</a></li>
|
---|
309 | <li><a href="compoundConstraints.html">Compound Constraints</a></li>
|
---|
310 | <li><a href="delayedConstraint.html">Delayed Constraint</a></li>
|
---|
311 | <li><a href="listMapper.html">List Mapper</a></li>
|
---|
312 | <li><a href="reusableConstraint.html">Reusable Constraint</a></li>
|
---|
313 | </ul>
|
---|
314 | <li><a href="attributes.html">Attributes</a></li>
|
---|
315 | <li><a href="runningTests.html">Running Tests</a></li>
|
---|
316 | <li><a href="extensibility.html">Extensibility</a></li>
|
---|
317 | <li><a href="releaseNotes.html">Release 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 © 2010 Charlie Poole. All Rights Reserved.
|
---|
329 | </div>
|
---|
330 | <!-- End of Footer -->
|
---|
331 |
|
---|
332 | </body>
|
---|
333 | </html>
|
---|