1
// ****************************************************************
2
// Copyright 2009, Charlie Poole
3
// This is free software licensed under the NUnit license. You may
4
// obtain a copy of the license at http://nunit.org
5
// ****************************************************************
8
using System.Collections;
9
using NUnit.Framework.Constraints;
11
namespace NUnit.Framework
14
/// Helper class with properties and methods that supply
15
/// a number of constraints used in Asserts.
22
/// Returns a ConstraintExpression that negates any
23
/// following constraint.
25
public static ConstraintExpression Not
27
get { return new ConstraintExpression().Not; }
35
/// Returns a ConstraintExpression, which will apply
36
/// the following constraint to all members of a collection,
37
/// succeeding if all of them succeed.
39
public static ConstraintExpression All
41
get { return new ConstraintExpression().All; }
49
/// Returns a constraint that tests for null
51
public static NullConstraint Null
53
get { return new NullConstraint(); }
61
/// Returns a constraint that tests for True
63
public static TrueConstraint True
65
get { return new TrueConstraint(); }
73
/// Returns a constraint that tests for False
75
public static FalseConstraint False
77
get { return new FalseConstraint(); }
85
/// Returns a constraint that tests for a positive value
87
public static GreaterThanConstraint Positive
89
get { return new GreaterThanConstraint(0); }
97
/// Returns a constraint that tests for a negative value
99
public static LessThanConstraint Negative
101
get { return new LessThanConstraint(0); }
109
/// Returns a constraint that tests for NaN
111
public static NaNConstraint NaN
113
get { return new NaNConstraint(); }
121
/// Returns a constraint that tests for empty
123
public static EmptyConstraint Empty
125
get { return new EmptyConstraint(); }
133
/// Returns a constraint that tests whether a collection
134
/// contains all unique items.
136
public static UniqueItemsConstraint Unique
138
get { return new UniqueItemsConstraint(); }
143
#region BinarySerializable
146
/// Returns a constraint that tests whether an object graph is serializable in binary format.
148
public static BinarySerializableConstraint BinarySerializable
150
get { return new BinarySerializableConstraint(); }
155
#region XmlSerializable
158
/// Returns a constraint that tests whether an object graph is serializable in xml format.
160
public static XmlSerializableConstraint XmlSerializable
162
get { return new XmlSerializableConstraint(); }
170
/// Returns a constraint that tests two items for equality
172
public static EqualConstraint EqualTo(object expected)
174
return new EqualConstraint(expected);
182
/// Returns a constraint that tests that two references are the same object
184
public static SameAsConstraint SameAs(object expected)
186
return new SameAsConstraint(expected);
194
/// Returns a constraint that tests whether the
195
/// actual value is greater than the suppled argument
197
public static GreaterThanConstraint GreaterThan(object expected)
199
return new GreaterThanConstraint(expected);
204
#region GreaterThanOrEqualTo
207
/// Returns a constraint that tests whether the
208
/// actual value is greater than or equal to the suppled argument
210
public static GreaterThanOrEqualConstraint GreaterThanOrEqualTo(object expected)
212
return new GreaterThanOrEqualConstraint(expected);
216
/// Returns a constraint that tests whether the
217
/// actual value is greater than or equal to the suppled argument
219
public static GreaterThanOrEqualConstraint AtLeast(object expected)
221
return new GreaterThanOrEqualConstraint(expected);
229
/// Returns a constraint that tests whether the
230
/// actual value is less than the suppled argument
232
public static LessThanConstraint LessThan(object expected)
234
return new LessThanConstraint(expected);
239
#region LessThanOrEqualTo
242
/// Returns a constraint that tests whether the
243
/// actual value is less than or equal to the suppled argument
245
public static LessThanOrEqualConstraint LessThanOrEqualTo(object expected)
247
return new LessThanOrEqualConstraint(expected);
251
/// Returns a constraint that tests whether the
252
/// actual value is less than or equal to the suppled argument
254
public static LessThanOrEqualConstraint AtMost(object expected)
256
return new LessThanOrEqualConstraint(expected);
264
/// Returns a constraint that tests whether the actual
265
/// value is of the exact type supplied as an argument.
267
public static ExactTypeConstraint TypeOf(Type expectedType)
269
return new ExactTypeConstraint(expectedType);
272
#if CLR_2_0 || CLR_4_0
274
/// Returns a constraint that tests whether the actual
275
/// value is of the exact type supplied as an argument.
277
public static ExactTypeConstraint TypeOf<T>()
279
return new ExactTypeConstraint(typeof(T));
288
/// Returns a constraint that tests whether the actual value
289
/// is of the type supplied as an argument or a derived type.
291
public static InstanceOfTypeConstraint InstanceOf(Type expectedType)
293
return new InstanceOfTypeConstraint(expectedType);
296
#if CLR_2_0 || CLR_4_0
298
/// Returns a constraint that tests whether the actual value
299
/// is of the type supplied as an argument or a derived type.
301
public static InstanceOfTypeConstraint InstanceOf<T>()
303
return new InstanceOfTypeConstraint(typeof(T));
308
/// Returns a constraint that tests whether the actual value
309
/// is of the type supplied as an argument or a derived type.
311
[Obsolete("Use InstanceOf(expectedType)")]
312
public static InstanceOfTypeConstraint InstanceOfType(Type expectedType)
314
return new InstanceOfTypeConstraint(expectedType);
317
#if CLR_2_0 || CLR_4_0
319
/// Returns a constraint that tests whether the actual value
320
/// is of the type supplied as an argument or a derived type.
322
[Obsolete("Use InstanceOf<T>()")]
323
public static InstanceOfTypeConstraint InstanceOfType<T>()
325
return new InstanceOfTypeConstraint(typeof(T));
331
#region AssignableFrom
334
/// Returns a constraint that tests whether the actual value
335
/// is assignable from the type supplied as an argument.
337
public static AssignableFromConstraint AssignableFrom(Type expectedType)
339
return new AssignableFromConstraint(expectedType);
342
#if CLR_2_0 || CLR_4_0
344
/// Returns a constraint that tests whether the actual value
345
/// is assignable from the type supplied as an argument.
347
public static AssignableFromConstraint AssignableFrom<T>()
349
return new AssignableFromConstraint(typeof(T));
358
/// Returns a constraint that tests whether the actual value
359
/// is assignable from the type supplied as an argument.
361
public static AssignableToConstraint AssignableTo(Type expectedType)
363
return new AssignableToConstraint(expectedType);
366
#if CLR_2_0 || CLR_4_0
368
/// Returns a constraint that tests whether the actual value
369
/// is assignable from the type supplied as an argument.
371
public static AssignableToConstraint AssignableTo<T>()
373
return new AssignableToConstraint(typeof(T));
382
/// Returns a constraint that tests whether the actual value
383
/// is a collection containing the same elements as the
384
/// collection supplied as an argument.
386
public static CollectionEquivalentConstraint EquivalentTo(IEnumerable expected)
388
return new CollectionEquivalentConstraint(expected);
396
/// Returns a constraint that tests whether the actual value
397
/// is a subset of the collection supplied as an argument.
399
public static CollectionSubsetConstraint SubsetOf(IEnumerable expected)
401
return new CollectionSubsetConstraint(expected);
409
/// Returns a constraint that tests whether a collection is ordered
411
public static CollectionOrderedConstraint Ordered
413
get { return new CollectionOrderedConstraint(); }
418
#region StringContaining
421
/// Returns a constraint that succeeds if the actual
422
/// value contains the substring supplied as an argument.
424
public static SubstringConstraint StringContaining(string expected)
426
return new SubstringConstraint(expected);
431
#region StringStarting
434
/// Returns a constraint that succeeds if the actual
435
/// value starts with the substring supplied as an argument.
437
public static StartsWithConstraint StringStarting(string expected)
439
return new StartsWithConstraint(expected);
447
/// Returns a constraint that succeeds if the actual
448
/// value ends with the substring supplied as an argument.
450
public static EndsWithConstraint StringEnding(string expected)
452
return new EndsWithConstraint(expected);
457
#region StringMatching
460
/// Returns a constraint that succeeds if the actual
461
/// value matches the Regex pattern supplied as an argument.
463
public static RegexConstraint StringMatching(string pattern)
465
return new RegexConstraint(pattern);
473
/// Returns a constraint that tests whether the path provided
474
/// is the same as an expected path after canonicalization.
476
public static SamePathConstraint SamePath(string expected)
478
return new SamePathConstraint(expected);
486
/// Returns a constraint that tests whether the path provided
487
/// is the same path or under an expected path after canonicalization.
489
public static SubPathConstraint SubPath(string expected)
491
return new SubPathConstraint(expected);
496
#region SamePathOrUnder
499
/// Returns a constraint that tests whether the path provided
500
/// is the same path or under an expected path after canonicalization.
502
public static SamePathOrUnderConstraint SamePathOrUnder(string expected)
504
return new SamePathOrUnderConstraint(expected);
511
#if !CLR_2_0 && !CLR_4_0
513
/// Returns a constraint that tests whether the actual value falls
514
/// within a specified range.
516
public static RangeConstraint InRange(IComparable from, IComparable to)
518
return new RangeConstraint(from, to);
526
#if CLR_2_0 || CLR_4_0
528
/// Returns a constraint that tests whether the actual value falls
529
/// within a specified range.
531
public static RangeConstraint<T> InRange<T>(T from, T to) where T : IComparable<T>
533
return new RangeConstraint<T>(from, to);
1
// ****************************************************************
2
// Copyright 2009, Charlie Poole
3
// This is free software licensed under the NUnit license. You may
4
// obtain a copy of the license at http://nunit.org
5
// ****************************************************************
8
using System.Collections;
9
using NUnit.Framework.Constraints;
11
namespace NUnit.Framework
14
/// Helper class with properties and methods that supply
15
/// a number of constraints used in Asserts.
22
/// Returns a ConstraintExpression that negates any
23
/// following constraint.
25
public static ConstraintExpression Not
27
get { return new ConstraintExpression().Not; }
35
/// Returns a ConstraintExpression, which will apply
36
/// the following constraint to all members of a collection,
37
/// succeeding if all of them succeed.
39
public static ConstraintExpression All
41
get { return new ConstraintExpression().All; }
49
/// Returns a constraint that tests for null
51
public static NullConstraint Null
53
get { return new NullConstraint(); }
61
/// Returns a constraint that tests for True
63
public static TrueConstraint True
65
get { return new TrueConstraint(); }
73
/// Returns a constraint that tests for False
75
public static FalseConstraint False
77
get { return new FalseConstraint(); }
85
/// Returns a constraint that tests for a positive value
87
public static GreaterThanConstraint Positive
89
get { return new GreaterThanConstraint(0); }
97
/// Returns a constraint that tests for a negative value
99
public static LessThanConstraint Negative
101
get { return new LessThanConstraint(0); }
109
/// Returns a constraint that tests for NaN
111
public static NaNConstraint NaN
113
get { return new NaNConstraint(); }
121
/// Returns a constraint that tests for empty
123
public static EmptyConstraint Empty
125
get { return new EmptyConstraint(); }
133
/// Returns a constraint that tests whether a collection
134
/// contains all unique items.
136
public static UniqueItemsConstraint Unique
138
get { return new UniqueItemsConstraint(); }
143
#region BinarySerializable
146
/// Returns a constraint that tests whether an object graph is serializable in binary format.
148
public static BinarySerializableConstraint BinarySerializable
150
get { return new BinarySerializableConstraint(); }
155
#region XmlSerializable
158
/// Returns a constraint that tests whether an object graph is serializable in xml format.
160
public static XmlSerializableConstraint XmlSerializable
162
get { return new XmlSerializableConstraint(); }
170
/// Returns a constraint that tests two items for equality
172
public static EqualConstraint EqualTo(object expected)
174
return new EqualConstraint(expected);
182
/// Returns a constraint that tests that two references are the same object
184
public static SameAsConstraint SameAs(object expected)
186
return new SameAsConstraint(expected);
194
/// Returns a constraint that tests whether the
195
/// actual value is greater than the suppled argument
197
public static GreaterThanConstraint GreaterThan(object expected)
199
return new GreaterThanConstraint(expected);
204
#region GreaterThanOrEqualTo
207
/// Returns a constraint that tests whether the
208
/// actual value is greater than or equal to the suppled argument
210
public static GreaterThanOrEqualConstraint GreaterThanOrEqualTo(object expected)
212
return new GreaterThanOrEqualConstraint(expected);
216
/// Returns a constraint that tests whether the
217
/// actual value is greater than or equal to the suppled argument
219
public static GreaterThanOrEqualConstraint AtLeast(object expected)
221
return new GreaterThanOrEqualConstraint(expected);
229
/// Returns a constraint that tests whether the
230
/// actual value is less than the suppled argument
232
public static LessThanConstraint LessThan(object expected)
234
return new LessThanConstraint(expected);
239
#region LessThanOrEqualTo
242
/// Returns a constraint that tests whether the
243
/// actual value is less than or equal to the suppled argument
245
public static LessThanOrEqualConstraint LessThanOrEqualTo(object expected)
247
return new LessThanOrEqualConstraint(expected);
251
/// Returns a constraint that tests whether the
252
/// actual value is less than or equal to the suppled argument
254
public static LessThanOrEqualConstraint AtMost(object expected)
256
return new LessThanOrEqualConstraint(expected);
264
/// Returns a constraint that tests whether the actual
265
/// value is of the exact type supplied as an argument.
267
public static ExactTypeConstraint TypeOf(Type expectedType)
269
return new ExactTypeConstraint(expectedType);
272
#if CLR_2_0 || CLR_4_0
274
/// Returns a constraint that tests whether the actual
275
/// value is of the exact type supplied as an argument.
277
public static ExactTypeConstraint TypeOf<T>()
279
return new ExactTypeConstraint(typeof(T));
288
/// Returns a constraint that tests whether the actual value
289
/// is of the type supplied as an argument or a derived type.
291
public static InstanceOfTypeConstraint InstanceOf(Type expectedType)
293
return new InstanceOfTypeConstraint(expectedType);
296
#if CLR_2_0 || CLR_4_0
298
/// Returns a constraint that tests whether the actual value
299
/// is of the type supplied as an argument or a derived type.
301
public static InstanceOfTypeConstraint InstanceOf<T>()
303
return new InstanceOfTypeConstraint(typeof(T));
308
/// Returns a constraint that tests whether the actual value
309
/// is of the type supplied as an argument or a derived type.
311
[Obsolete("Use InstanceOf(expectedType)")]
312
public static InstanceOfTypeConstraint InstanceOfType(Type expectedType)
314
return new InstanceOfTypeConstraint(expectedType);
317
#if CLR_2_0 || CLR_4_0
319
/// Returns a constraint that tests whether the actual value
320
/// is of the type supplied as an argument or a derived type.
322
[Obsolete("Use InstanceOf<T>()")]
323
public static InstanceOfTypeConstraint InstanceOfType<T>()
325
return new InstanceOfTypeConstraint(typeof(T));
331
#region AssignableFrom
334
/// Returns a constraint that tests whether the actual value
335
/// is assignable from the type supplied as an argument.
337
public static AssignableFromConstraint AssignableFrom(Type expectedType)
339
return new AssignableFromConstraint(expectedType);
342
#if CLR_2_0 || CLR_4_0
344
/// Returns a constraint that tests whether the actual value
345
/// is assignable from the type supplied as an argument.
347
public static AssignableFromConstraint AssignableFrom<T>()
349
return new AssignableFromConstraint(typeof(T));
358
/// Returns a constraint that tests whether the actual value
359
/// is assignable from the type supplied as an argument.
361
public static AssignableToConstraint AssignableTo(Type expectedType)
363
return new AssignableToConstraint(expectedType);
366
#if CLR_2_0 || CLR_4_0
368
/// Returns a constraint that tests whether the actual value
369
/// is assignable from the type supplied as an argument.
371
public static AssignableToConstraint AssignableTo<T>()
373
return new AssignableToConstraint(typeof(T));
382
/// Returns a constraint that tests whether the actual value
383
/// is a collection containing the same elements as the
384
/// collection supplied as an argument.
386
public static CollectionEquivalentConstraint EquivalentTo(IEnumerable expected)
388
return new CollectionEquivalentConstraint(expected);
396
/// Returns a constraint that tests whether the actual value
397
/// is a subset of the collection supplied as an argument.
399
public static CollectionSubsetConstraint SubsetOf(IEnumerable expected)
401
return new CollectionSubsetConstraint(expected);
409
/// Returns a constraint that tests whether a collection is ordered
411
public static CollectionOrderedConstraint Ordered
413
get { return new CollectionOrderedConstraint(); }
418
#region StringContaining
421
/// Returns a constraint that succeeds if the actual
422
/// value contains the substring supplied as an argument.
424
public static SubstringConstraint StringContaining(string expected)
426
return new SubstringConstraint(expected);
431
#region StringStarting
434
/// Returns a constraint that succeeds if the actual
435
/// value starts with the substring supplied as an argument.
437
public static StartsWithConstraint StringStarting(string expected)
439
return new StartsWithConstraint(expected);
447
/// Returns a constraint that succeeds if the actual
448
/// value ends with the substring supplied as an argument.
450
public static EndsWithConstraint StringEnding(string expected)
452
return new EndsWithConstraint(expected);
457
#region StringMatching
460
/// Returns a constraint that succeeds if the actual
461
/// value matches the regular expression supplied as an argument.
463
public static RegexConstraint StringMatching(string pattern)
465
return new RegexConstraint(pattern);
473
/// Returns a constraint that tests whether the path provided
474
/// is the same as an expected path after canonicalization.
476
public static SamePathConstraint SamePath(string expected)
478
return new SamePathConstraint(expected);
486
/// Returns a constraint that tests whether the path provided
487
/// is under an expected path after canonicalization.
489
public static SubPathConstraint SubPath(string expected)
491
return new SubPathConstraint(expected);
496
#region SamePathOrUnder
499
/// Returns a constraint that tests whether the path provided
500
/// is the same path or under an expected path after canonicalization.
502
public static SamePathOrUnderConstraint SamePathOrUnder(string expected)
504
return new SamePathOrUnderConstraint(expected);
511
#if CLR_2_0 || CLR_4_0
513
/// Returns a constraint that tests whether the actual value falls
514
/// within a specified range.
516
public static RangeConstraint<T> InRange<T>(T from, T to) where T : IComparable<T>
518
return new RangeConstraint<T>(from, to);
522
/// Returns a constraint that tests whether the actual value falls
523
/// within a specified range.
525
public static RangeConstraint InRange(IComparable from, IComparable to)
527
return new RangeConstraint(from, to);