1 // ***********************************************************************
2 // Copyright (c) 2009 Charlie Poole
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the
6 // "Software"), to deal in the Software without restriction, including
7 // without limitation the rights to use, copy, modify, merge, publish,
8 // distribute, sublicense, and/or sell copies of the Software, and to
9 // permit persons to whom the Software is furnished to do so, subject to
10 // the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 // ***********************************************************************
23 
24 using System;
25 using System.Collections;
26 using NUnit.Framework.Constraints;
27 
28 namespace NUnit.Framework
29 {
30     /// <summary>
31     /// Helper class with properties and methods that supply
32     /// a number of constraints used in Asserts.
33     /// </summary>
34     public class Is
35     {
36         #region Not
37 
38         /// <summary>
39         /// Returns a ConstraintExpression that negates any
40         /// following constraint.
41         /// </summary>
42         public static ConstraintExpression Not
43         {
44             get { return new ConstraintExpression().Not; }
45         }
46 
47         #endregion
48 
49         #region All
50 
51         /// <summary>
52         /// Returns a ConstraintExpression, which will apply
53         /// the following constraint to all members of a collection,
54         /// succeeding if all of them succeed.
55         /// </summary>
56         public static ConstraintExpression All
57         {
58             get { return new ConstraintExpression().All; }
59         }
60 
61         #endregion
62 
63         #region Null
64 
65         /// <summary>
66         /// Returns a constraint that tests for null
67         /// </summary>
68         public static NullConstraint Null
69         {
70             get { return new NullConstraint(); }
71         }
72 
73         #endregion
74 
75         #region True
76 
77         /// <summary>
78         /// Returns a constraint that tests for True
79         /// </summary>
80         public static TrueConstraint True
81         {
82             get { return new TrueConstraint(); }
83         }
84 
85         #endregion
86 
87         #region False
88 
89         /// <summary>
90         /// Returns a constraint that tests for False
91         /// </summary>
92         public static FalseConstraint False
93         {
94             get { return new FalseConstraint(); }
95         }
96 
97         #endregion
98 
99         #region Positive
100 
101         /// <summary>
102         /// Returns a constraint that tests for a positive value
103         /// </summary>
104         public static GreaterThanConstraint Positive
105         {
106             get { return new GreaterThanConstraint(0); }
107         }
108 
109         #endregion
110 
111         #region Negative
112 
113         /// <summary>
114         /// Returns a constraint that tests for a negative value
115         /// </summary>
116         public static LessThanConstraint Negative
117         {
118             get { return new LessThanConstraint(0); }
119         }
120 
121         #endregion
122 
123         #region NaN
124 
125         /// <summary>
126         /// Returns a constraint that tests for NaN
127         /// </summary>
128         public static NaNConstraint NaN
129         {
130             get { return new NaNConstraint(); }
131         }
132 
133         #endregion
134 
135         #region Empty
136 
137         /// <summary>
138         /// Returns a constraint that tests for empty
139         /// </summary>
140         public static EmptyConstraint Empty
141         {
142             get { return new EmptyConstraint(); }
143         }
144 
145         #endregion
146 
147         #region Unique
148 
149         /// <summary>
150         /// Returns a constraint that tests whether a collection
151         /// contains all unique items.
152         /// </summary>
153         public static UniqueItemsConstraint Unique
154         {
155             get { return new UniqueItemsConstraint(); }
156         }
157 
158         #endregion
159 
160         #region BinarySerializable
161 
162 #if !NETCF && !SILVERLIGHT
163         /// <summary>
164         /// Returns a constraint that tests whether an object graph is serializable in binary format.
165         /// </summary>
166         public static BinarySerializableConstraint BinarySerializable
167         {
168             get { return new BinarySerializableConstraint(); }
169         }
170 #endif
171 
172         #endregion
173 
174         #region XmlSerializable
175 
176 #if !SILVERLIGHT
177         /// <summary>
178         /// Returns a constraint that tests whether an object graph is serializable in xml format.
179         /// </summary>
180         public static XmlSerializableConstraint XmlSerializable
181         {
182             get { return new XmlSerializableConstraint(); }
183         }
184 #endif
185 
186         #endregion
187 
188         #region EqualTo
189 
190         /// <summary>
191         /// Returns a constraint that tests two items for equality
192         /// </summary>
EqualTo(object expected)193         public static EqualConstraint EqualTo(object expected)
194         {
195             return new EqualConstraint(expected);
196         }
197 
198         #endregion
199 
200         #region SameAs
201 
202         /// <summary>
203         /// Returns a constraint that tests that two references are the same object
204         /// </summary>
SameAs(object expected)205         public static SameAsConstraint SameAs(object expected)
206         {
207             return new SameAsConstraint(expected);
208         }
209 
210         #endregion
211 
212         #region GreaterThan
213 
214         /// <summary>
215         /// Returns a constraint that tests whether the
216         /// actual value is greater than the suppled argument
217         /// </summary>
GreaterThan(object expected)218         public static GreaterThanConstraint GreaterThan(object expected)
219         {
220             return new GreaterThanConstraint(expected);
221         }
222 
223         #endregion
224 
225         #region GreaterThanOrEqualTo
226 
227         /// <summary>
228         /// Returns a constraint that tests whether the
229         /// actual value is greater than or equal to the suppled argument
230         /// </summary>
GreaterThanOrEqualTo(object expected)231         public static GreaterThanOrEqualConstraint GreaterThanOrEqualTo(object expected)
232         {
233             return new GreaterThanOrEqualConstraint(expected);
234         }
235 
236         /// <summary>
237         /// Returns a constraint that tests whether the
238         /// actual value is greater than or equal to the suppled argument
239         /// </summary>
AtLeast(object expected)240         public static GreaterThanOrEqualConstraint AtLeast(object expected)
241         {
242             return new GreaterThanOrEqualConstraint(expected);
243         }
244 
245         #endregion
246 
247         #region LessThan
248 
249         /// <summary>
250         /// Returns a constraint that tests whether the
251         /// actual value is less than the suppled argument
252         /// </summary>
LessThan(object expected)253         public static LessThanConstraint LessThan(object expected)
254         {
255             return new LessThanConstraint(expected);
256         }
257 
258         #endregion
259 
260         #region LessThanOrEqualTo
261 
262         /// <summary>
263         /// Returns a constraint that tests whether the
264         /// actual value is less than or equal to the suppled argument
265         /// </summary>
LessThanOrEqualTo(object expected)266         public static LessThanOrEqualConstraint LessThanOrEqualTo(object expected)
267         {
268             return new LessThanOrEqualConstraint(expected);
269         }
270 
271         /// <summary>
272         /// Returns a constraint that tests whether the
273         /// actual value is less than or equal to the suppled argument
274         /// </summary>
AtMost(object expected)275         public static LessThanOrEqualConstraint AtMost(object expected)
276         {
277             return new LessThanOrEqualConstraint(expected);
278         }
279 
280         #endregion
281 
282         #region TypeOf
283 
284         /// <summary>
285         /// Returns a constraint that tests whether the actual
286         /// value is of the exact type supplied as an argument.
287         /// </summary>
TypeOf(Type expectedType)288         public static ExactTypeConstraint TypeOf(Type expectedType)
289         {
290             return new ExactTypeConstraint(expectedType);
291         }
292 
293 #if CLR_2_0 || CLR_4_0
294         /// <summary>
295         /// Returns a constraint that tests whether the actual
296         /// value is of the exact type supplied as an argument.
297         /// </summary>
TypeOf()298         public static ExactTypeConstraint TypeOf<T>()
299         {
300             return new ExactTypeConstraint(typeof(T));
301         }
302 #endif
303 
304         #endregion
305 
306         #region InstanceOf
307 
308         /// <summary>
309         /// Returns a constraint that tests whether the actual value
310         /// is of the type supplied as an argument or a derived type.
311         /// </summary>
InstanceOf(Type expectedType)312         public static InstanceOfTypeConstraint InstanceOf(Type expectedType)
313         {
314             return new InstanceOfTypeConstraint(expectedType);
315         }
316 
317 #if CLR_2_0 || CLR_4_0
318         /// <summary>
319         /// Returns a constraint that tests whether the actual value
320         /// is of the type supplied as an argument or a derived type.
321         /// </summary>
InstanceOf()322         public static InstanceOfTypeConstraint InstanceOf<T>()
323         {
324             return new InstanceOfTypeConstraint(typeof(T));
325         }
326 #endif
327 
328         #endregion
329 
330         #region AssignableFrom
331 
332         /// <summary>
333         /// Returns a constraint that tests whether the actual value
334         /// is assignable from the type supplied as an argument.
335         /// </summary>
AssignableFrom(Type expectedType)336         public static AssignableFromConstraint AssignableFrom(Type expectedType)
337         {
338             return new AssignableFromConstraint(expectedType);
339         }
340 
341 #if CLR_2_0 || CLR_4_0
342         /// <summary>
343         /// Returns a constraint that tests whether the actual value
344         /// is assignable from the type supplied as an argument.
345         /// </summary>
AssignableFrom()346         public static AssignableFromConstraint AssignableFrom<T>()
347         {
348             return new AssignableFromConstraint(typeof(T));
349         }
350 #endif
351 
352         #endregion
353 
354         #region AssignableTo
355 
356         /// <summary>
357         /// Returns a constraint that tests whether the actual value
358         /// is assignable from the type supplied as an argument.
359         /// </summary>
AssignableTo(Type expectedType)360         public static AssignableToConstraint AssignableTo(Type expectedType)
361         {
362             return new AssignableToConstraint(expectedType);
363         }
364 
365 #if CLR_2_0 || CLR_4_0
366         /// <summary>
367         /// Returns a constraint that tests whether the actual value
368         /// is assignable from the type supplied as an argument.
369         /// </summary>
AssignableTo()370         public static AssignableToConstraint AssignableTo<T>()
371         {
372             return new AssignableToConstraint(typeof(T));
373         }
374 #endif
375 
376         #endregion
377 
378         #region EquivalentTo
379 
380         /// <summary>
381         /// Returns a constraint that tests whether the actual value
382         /// is a collection containing the same elements as the
383         /// collection supplied as an argument.
384         /// </summary>
EquivalentTo(IEnumerable expected)385         public static CollectionEquivalentConstraint EquivalentTo(IEnumerable expected)
386         {
387             return new CollectionEquivalentConstraint(expected);
388         }
389 
390         #endregion
391 
392         #region SubsetOf
393 
394         /// <summary>
395         /// Returns a constraint that tests whether the actual value
396         /// is a subset of the collection supplied as an argument.
397         /// </summary>
SubsetOf(IEnumerable expected)398         public static CollectionSubsetConstraint SubsetOf(IEnumerable expected)
399         {
400             return new CollectionSubsetConstraint(expected);
401         }
402 
403         #endregion
404 
405         #region Ordered
406 
407         /// <summary>
408         /// Returns a constraint that tests whether a collection is ordered
409         /// </summary>
410         public static CollectionOrderedConstraint Ordered
411         {
412             get { return new CollectionOrderedConstraint(); }
413         }
414 
415         #endregion
416 
417         #region StringContaining
418 
419         /// <summary>
420         /// Returns a constraint that succeeds if the actual
421         /// value contains the substring supplied as an argument.
422         /// </summary>
StringContaining(string expected)423         public static SubstringConstraint StringContaining(string expected)
424         {
425             return new SubstringConstraint(expected);
426         }
427 
428         #endregion
429 
430         #region StringStarting
431 
432         /// <summary>
433         /// Returns a constraint that succeeds if the actual
434         /// value starts with the substring supplied as an argument.
435         /// </summary>
StringStarting(string expected)436         public static StartsWithConstraint StringStarting(string expected)
437         {
438             return new StartsWithConstraint(expected);
439         }
440 
441         #endregion
442 
443         #region StringEnding
444 
445         /// <summary>
446         /// Returns a constraint that succeeds if the actual
447         /// value ends with the substring supplied as an argument.
448         /// </summary>
StringEnding(string expected)449         public static EndsWithConstraint StringEnding(string expected)
450         {
451             return new EndsWithConstraint(expected);
452         }
453 
454         #endregion
455 
456         #region StringMatching
457 
458 #if !NETCF
459         /// <summary>
460         /// Returns a constraint that succeeds if the actual
461         /// value matches the regular expression supplied as an argument.
462         /// </summary>
StringMatching(string pattern)463         public static RegexConstraint StringMatching(string pattern)
464         {
465             return new RegexConstraint(pattern);
466         }
467 #endif
468 
469         #endregion
470 
471         #region SamePath
472 
473         /// <summary>
474         /// Returns a constraint that tests whether the path provided
475         /// is the same as an expected path after canonicalization.
476         /// </summary>
SamePath(string expected)477         public static SamePathConstraint SamePath(string expected)
478         {
479             return new SamePathConstraint(expected);
480         }
481 
482         #endregion
483 
484         #region SubPath
485 
486         /// <summary>
487         /// Returns a constraint that tests whether the path provided
488         /// is under an expected path after canonicalization.
489         /// </summary>
SubPath(string expected)490         public static SubPathConstraint SubPath(string expected)
491         {
492             return new SubPathConstraint(expected);
493         }
494 
495         #endregion
496 
497         #region SamePathOrUnder
498 
499         /// <summary>
500         /// Returns a constraint that tests whether the path provided
501         /// is the same path or under an expected path after canonicalization.
502         /// </summary>
SamePathOrUnder(string expected)503         public static SamePathOrUnderConstraint SamePathOrUnder(string expected)
504         {
505             return new SamePathOrUnderConstraint(expected);
506         }
507 
508         #endregion
509 
510         #region InRange
511 
512 #if CLR_2_0 || CLR_4_0
513         /// <summary>
514         /// Returns a constraint that tests whether the actual value falls
515         /// within a specified range.
516         /// </summary>
517         public static RangeConstraint<T> InRange<T>(T from, T to) where T : IComparable<T>
518         {
519             return new RangeConstraint<T>(from, to);
520         }
521 #else
522         /// <summary>
523         /// Returns a constraint that tests whether the actual value falls
524         /// within a specified range.
525         /// </summary>
InRange(IComparable from, IComparable to)526         public static RangeConstraint InRange(IComparable from, IComparable to)
527         {
528             return new RangeConstraint(from, to);
529         }
530 #endif
531 
532         #endregion
533 
534     }
535 }
536