1 // ***********************************************************************
2 // Copyright (c) 2009 Charlie Poole
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:
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
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 // ***********************************************************************
25 #define NUNIT_FRAMEWORK
30 using System.Collections;
31 using NUnit.Framework.Constraints;
33 namespace NUnit.Framework
36 /// Helper class with properties and methods that supply
37 /// a number of constraints used in Asserts.
44 /// Returns a ConstraintExpression that negates any
45 /// following constraint.
47 public static ConstraintExpression Not
49 get { return new ConstraintExpression().Not; }
57 /// Returns a ConstraintExpression, which will apply
58 /// the following constraint to all members of a collection,
59 /// succeeding if all of them succeed.
61 public static ConstraintExpression All
63 get { return new ConstraintExpression().All; }
71 /// Returns a constraint that tests for null
73 public static NullConstraint Null
75 get { return new NullConstraint(); }
83 /// Returns a constraint that tests for True
85 public static TrueConstraint True
87 get { return new TrueConstraint(); }
95 /// Returns a constraint that tests for False
97 public static FalseConstraint False
99 get { return new FalseConstraint(); }
107 /// Returns a constraint that tests for a positive value
109 public static GreaterThanConstraint Positive
111 get { return new GreaterThanConstraint(0); }
119 /// Returns a constraint that tests for a negative value
121 public static LessThanConstraint Negative
123 get { return new LessThanConstraint(0); }
131 /// Returns a constraint that tests for equality with zero
133 public static EqualConstraint Zero
135 get { return new EqualConstraint(0); }
143 /// Returns a constraint that tests for NaN
145 public static NaNConstraint NaN
147 get { return new NaNConstraint(); }
155 /// Returns a constraint that tests for empty
157 public static EmptyConstraint Empty
159 get { return new EmptyConstraint(); }
167 /// Returns a constraint that tests whether a collection
168 /// contains all unique items.
170 public static UniqueItemsConstraint Unique
172 get { return new UniqueItemsConstraint(); }
177 #region BinarySerializable
179 #if !NETCF && !SILVERLIGHT && !PORTABLE
181 /// Returns a constraint that tests whether an object graph is serializable in binary format.
183 public static BinarySerializableConstraint BinarySerializable
185 get { return new BinarySerializableConstraint(); }
191 #region XmlSerializable
193 #if !SILVERLIGHT && !PORTABLE
195 /// Returns a constraint that tests whether an object graph is serializable in xml format.
197 public static XmlSerializableConstraint XmlSerializable
199 get { return new XmlSerializableConstraint(); }
208 /// Returns a constraint that tests two items for equality
210 public static EqualConstraint EqualTo(object expected)
212 return new EqualConstraint(expected);
220 /// Returns a constraint that tests that two references are the same object
222 public static SameAsConstraint SameAs(object expected)
224 return new SameAsConstraint(expected);
232 /// Returns a constraint that tests whether the
233 /// actual value is greater than the supplied argument
235 public static GreaterThanConstraint GreaterThan(object expected)
237 return new GreaterThanConstraint(expected);
242 #region GreaterThanOrEqualTo
245 /// Returns a constraint that tests whether the
246 /// actual value is greater than or equal to the supplied argument
248 public static GreaterThanOrEqualConstraint GreaterThanOrEqualTo(object expected)
250 return new GreaterThanOrEqualConstraint(expected);
254 /// Returns a constraint that tests whether the
255 /// actual value is greater than or equal to the supplied argument
257 public static GreaterThanOrEqualConstraint AtLeast(object expected)
259 return new GreaterThanOrEqualConstraint(expected);
267 /// Returns a constraint that tests whether the
268 /// actual value is less than the supplied argument
270 public static LessThanConstraint LessThan(object expected)
272 return new LessThanConstraint(expected);
277 #region LessThanOrEqualTo
280 /// Returns a constraint that tests whether the
281 /// actual value is less than or equal to the supplied argument
283 public static LessThanOrEqualConstraint LessThanOrEqualTo(object expected)
285 return new LessThanOrEqualConstraint(expected);
289 /// Returns a constraint that tests whether the
290 /// actual value is less than or equal to the supplied argument
292 public static LessThanOrEqualConstraint AtMost(object expected)
294 return new LessThanOrEqualConstraint(expected);
302 /// Returns a constraint that tests whether the actual
303 /// value is of the exact type supplied as an argument.
305 public static ExactTypeConstraint TypeOf(Type expectedType)
307 return new ExactTypeConstraint(expectedType);
311 /// Returns a constraint that tests whether the actual
312 /// value is of the exact type supplied as an argument.
314 public static ExactTypeConstraint TypeOf<TExpected>()
316 return new ExactTypeConstraint(typeof(TExpected));
324 /// Returns a constraint that tests whether the actual value
325 /// is of the type supplied as an argument or a derived type.
327 public static InstanceOfTypeConstraint InstanceOf(Type expectedType)
329 return new InstanceOfTypeConstraint(expectedType);
333 /// Returns a constraint that tests whether the actual value
334 /// is of the type supplied as an argument or a derived type.
336 public static InstanceOfTypeConstraint InstanceOf<TExpected>()
338 return new InstanceOfTypeConstraint(typeof(TExpected));
343 #region AssignableFrom
346 /// Returns a constraint that tests whether the actual value
347 /// is assignable from the type supplied as an argument.
349 public static AssignableFromConstraint AssignableFrom(Type expectedType)
351 return new AssignableFromConstraint(expectedType);
355 /// Returns a constraint that tests whether the actual value
356 /// is assignable from the type supplied as an argument.
358 public static AssignableFromConstraint AssignableFrom<TExpected>()
360 return new AssignableFromConstraint(typeof(TExpected));
368 /// Returns a constraint that tests whether the actual value
369 /// is assignable to the type supplied as an argument.
371 public static AssignableToConstraint AssignableTo(Type expectedType)
373 return new AssignableToConstraint(expectedType);
377 /// Returns a constraint that tests whether the actual value
378 /// is assignable to the type supplied as an argument.
380 public static AssignableToConstraint AssignableTo<TExpected>()
382 return new AssignableToConstraint(typeof(TExpected));
390 /// Returns a constraint that tests whether the actual value
391 /// is a collection containing the same elements as the
392 /// collection supplied as an argument.
394 public static CollectionEquivalentConstraint EquivalentTo(IEnumerable expected)
396 return new CollectionEquivalentConstraint(expected);
404 /// Returns a constraint that tests whether the actual value
405 /// is a subset of the collection supplied as an argument.
407 public static CollectionSubsetConstraint SubsetOf(IEnumerable expected)
409 return new CollectionSubsetConstraint(expected);
417 /// Returns a constraint that tests whether the actual value
418 /// is a superset of the collection supplied as an argument.
420 public static CollectionSupersetConstraint SupersetOf(IEnumerable expected)
422 return new CollectionSupersetConstraint(expected);
430 /// Returns a constraint that tests whether a collection is ordered
432 public static CollectionOrderedConstraint Ordered
434 get { return new CollectionOrderedConstraint(); }
439 #region StringContaining
442 /// Returns a constraint that succeeds if the actual
443 /// value contains the substring supplied as an argument.
445 [Obsolete("Deprecated, use Does.Contain")]
446 public static SubstringConstraint StringContaining(string expected)
448 return new SubstringConstraint(expected);
453 #region StringStarting
456 /// Returns a constraint that succeeds if the actual
457 /// value starts with the substring supplied as an argument.
459 [Obsolete("Deprecated, use Does.StartWith")]
460 public static StartsWithConstraint StringStarting(string expected)
462 return new StartsWithConstraint(expected);
470 /// Returns a constraint that succeeds if the actual
471 /// value ends with the substring supplied as an argument.
473 [Obsolete("Deprecated, use Does.EndWith")]
474 public static EndsWithConstraint StringEnding(string expected)
476 return new EndsWithConstraint(expected);
481 #region StringMatching
484 /// Returns a constraint that succeeds if the actual
485 /// value matches the regular expression supplied as an argument.
487 [Obsolete("Deprecated, use Does.Match")]
488 public static RegexConstraint StringMatching(string pattern)
490 return new RegexConstraint(pattern);
499 /// Returns a constraint that tests whether the path provided
500 /// is the same as an expected path after canonicalization.
502 public static SamePathConstraint SamePath(string expected)
504 return new SamePathConstraint(expected);
512 /// Returns a constraint that tests whether the path provided
513 /// is a subpath of the expected path after canonicalization.
515 public static SubPathConstraint SubPathOf(string expected)
517 return new SubPathConstraint(expected);
522 #region SamePathOrUnder
525 /// Returns a constraint that tests whether the path provided
526 /// is the same path or under an expected path after canonicalization.
528 public static SamePathOrUnderConstraint SamePathOrUnder(string expected)
530 return new SamePathOrUnderConstraint(expected);
539 /// Returns a constraint that tests whether the actual value falls
540 /// inclusively within a specified range.
542 /// <remarks>from must be less than or equal to true</remarks>
543 /// <param name="from">Inclusive beginning of the range. Must be less than or equal to to.</param>
544 /// <param name="to">Inclusive end of the range. Must be greater than or equal to from.</param>
545 /// <returns></returns>
546 public static RangeConstraint InRange(IComparable from, IComparable to)
548 return new RangeConstraint(from, to);