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;
32 namespace NUnit.Framework.Constraints
35 /// Helper class with properties and methods that supply
36 /// a number of constraints used in Asserts.
38 public class ConstraintFactory
43 /// Returns a ConstraintExpression that negates any
44 /// following constraint.
46 public ConstraintExpression Not
48 get { return Is.Not; }
52 /// Returns a ConstraintExpression that negates any
53 /// following constraint.
55 public ConstraintExpression No
57 get { return Has.No; }
65 /// Returns a ConstraintExpression, which will apply
66 /// the following constraint to all members of a collection,
67 /// succeeding if all of them succeed.
69 public ConstraintExpression All
71 get { return Is.All; }
79 /// Returns a ConstraintExpression, which will apply
80 /// the following constraint to all members of a collection,
81 /// succeeding if at least one of them succeeds.
83 public ConstraintExpression Some
85 get { return Has.Some; }
93 /// Returns a ConstraintExpression, which will apply
94 /// the following constraint to all members of a collection,
95 /// succeeding if all of them fail.
97 public ConstraintExpression None
99 get { return Has.None; }
107 /// Returns a ConstraintExpression, which will apply
108 /// the following constraint to all members of a collection,
109 /// succeeding only if a specified number of them succeed.
111 public static ConstraintExpression Exactly(int expectedCount)
113 return Has.Exactly(expectedCount);
121 /// Returns a new PropertyConstraintExpression, which will either
122 /// test for the existence of the named property on the object
123 /// being tested or apply any following constraint to that property.
125 public ResolvableConstraintExpression Property(string name)
127 return Has.Property(name);
135 /// Returns a new ConstraintExpression, which will apply the following
136 /// constraint to the Length property of the object being tested.
138 public ResolvableConstraintExpression Length
140 get { return Has.Length; }
148 /// Returns a new ConstraintExpression, which will apply the following
149 /// constraint to the Count property of the object being tested.
151 public ResolvableConstraintExpression Count
153 get { return Has.Count; }
161 /// Returns a new ConstraintExpression, which will apply the following
162 /// constraint to the Message property of the object being tested.
164 public ResolvableConstraintExpression Message
166 get { return Has.Message; }
171 #region InnerException
174 /// Returns a new ConstraintExpression, which will apply the following
175 /// constraint to the InnerException property of the object being tested.
177 public ResolvableConstraintExpression InnerException
179 get { return Has.InnerException; }
187 /// Returns a new AttributeConstraint checking for the
188 /// presence of a particular attribute on an object.
190 public ResolvableConstraintExpression Attribute(Type expectedType)
192 return Has.Attribute(expectedType);
196 /// Returns a new AttributeConstraint checking for the
197 /// presence of a particular attribute on an object.
199 public ResolvableConstraintExpression Attribute<TExpected>()
201 return Attribute(typeof(TExpected));
209 /// Returns a constraint that tests for null
211 public NullConstraint Null
213 get { return new NullConstraint(); }
221 /// Returns a constraint that tests for True
223 public TrueConstraint True
225 get { return new TrueConstraint(); }
233 /// Returns a constraint that tests for False
235 public FalseConstraint False
237 get { return new FalseConstraint(); }
245 /// Returns a constraint that tests for a positive value
247 public GreaterThanConstraint Positive
249 get { return new GreaterThanConstraint(0); }
257 /// Returns a constraint that tests for a negative value
259 public LessThanConstraint Negative
261 get { return new LessThanConstraint(0); }
269 /// Returns a constraint that tests for equality with zero
271 public EqualConstraint Zero
273 get { return new EqualConstraint(0); }
281 /// Returns a constraint that tests for NaN
283 public NaNConstraint NaN
285 get { return new NaNConstraint(); }
293 /// Returns a constraint that tests for empty
295 public EmptyConstraint Empty
297 get { return new EmptyConstraint(); }
305 /// Returns a constraint that tests whether a collection
306 /// contains all unique items.
308 public UniqueItemsConstraint Unique
310 get { return new UniqueItemsConstraint(); }
315 #region BinarySerializable
317 #if !NETCF && !SILVERLIGHT && !PORTABLE
319 /// Returns a constraint that tests whether an object graph is serializable in binary format.
321 public BinarySerializableConstraint BinarySerializable
323 get { return new BinarySerializableConstraint(); }
329 #region XmlSerializable
331 #if !SILVERLIGHT && !PORTABLE
333 /// Returns a constraint that tests whether an object graph is serializable in xml format.
335 public XmlSerializableConstraint XmlSerializable
337 get { return new XmlSerializableConstraint(); }
346 /// Returns a constraint that tests two items for equality
348 public EqualConstraint EqualTo(object expected)
350 return new EqualConstraint(expected);
358 /// Returns a constraint that tests that two references are the same object
360 public SameAsConstraint SameAs(object expected)
362 return new SameAsConstraint(expected);
370 /// Returns a constraint that tests whether the
371 /// actual value is greater than the supplied argument
373 public GreaterThanConstraint GreaterThan(object expected)
375 return new GreaterThanConstraint(expected);
380 #region GreaterThanOrEqualTo
383 /// Returns a constraint that tests whether the
384 /// actual value is greater than or equal to the supplied argument
386 public GreaterThanOrEqualConstraint GreaterThanOrEqualTo(object expected)
388 return new GreaterThanOrEqualConstraint(expected);
392 /// Returns a constraint that tests whether the
393 /// actual value is greater than or equal to the supplied argument
395 public GreaterThanOrEqualConstraint AtLeast(object expected)
397 return new GreaterThanOrEqualConstraint(expected);
405 /// Returns a constraint that tests whether the
406 /// actual value is less than the supplied argument
408 public LessThanConstraint LessThan(object expected)
410 return new LessThanConstraint(expected);
415 #region LessThanOrEqualTo
418 /// Returns a constraint that tests whether the
419 /// actual value is less than or equal to the supplied argument
421 public LessThanOrEqualConstraint LessThanOrEqualTo(object expected)
423 return new LessThanOrEqualConstraint(expected);
427 /// Returns a constraint that tests whether the
428 /// actual value is less than or equal to the supplied argument
430 public LessThanOrEqualConstraint AtMost(object expected)
432 return new LessThanOrEqualConstraint(expected);
440 /// Returns a constraint that tests whether the actual
441 /// value is of the exact type supplied as an argument.
443 public ExactTypeConstraint TypeOf(Type expectedType)
445 return new ExactTypeConstraint(expectedType);
449 /// Returns a constraint that tests whether the actual
450 /// value is of the exact type supplied as an argument.
452 public ExactTypeConstraint TypeOf<TExpected>()
454 return new ExactTypeConstraint(typeof(TExpected));
462 /// Returns a constraint that tests whether the actual value
463 /// is of the type supplied as an argument or a derived type.
465 public InstanceOfTypeConstraint InstanceOf(Type expectedType)
467 return new InstanceOfTypeConstraint(expectedType);
471 /// Returns a constraint that tests whether the actual value
472 /// is of the type supplied as an argument or a derived type.
474 public InstanceOfTypeConstraint InstanceOf<TExpected>()
476 return new InstanceOfTypeConstraint(typeof(TExpected));
481 #region AssignableFrom
484 /// Returns a constraint that tests whether the actual value
485 /// is assignable from the type supplied as an argument.
487 public AssignableFromConstraint AssignableFrom(Type expectedType)
489 return new AssignableFromConstraint(expectedType);
493 /// Returns a constraint that tests whether the actual value
494 /// is assignable from the type supplied as an argument.
496 public AssignableFromConstraint AssignableFrom<TExpected>()
498 return new AssignableFromConstraint(typeof(TExpected));
506 /// Returns a constraint that tests whether the actual value
507 /// is assignable from the type supplied as an argument.
509 public AssignableToConstraint AssignableTo(Type expectedType)
511 return new AssignableToConstraint(expectedType);
515 /// Returns a constraint that tests whether the actual value
516 /// is assignable from the type supplied as an argument.
518 public AssignableToConstraint AssignableTo<TExpected>()
520 return new AssignableToConstraint(typeof(TExpected));
528 /// Returns a constraint that tests whether the actual value
529 /// is a collection containing the same elements as the
530 /// collection supplied as an argument.
532 public CollectionEquivalentConstraint EquivalentTo(IEnumerable expected)
534 return new CollectionEquivalentConstraint(expected);
542 /// Returns a constraint that tests whether the actual value
543 /// is a subset of the collection supplied as an argument.
545 public CollectionSubsetConstraint SubsetOf(IEnumerable expected)
547 return new CollectionSubsetConstraint(expected);
555 /// Returns a constraint that tests whether the actual value
556 /// is a superset of the collection supplied as an argument.
558 public CollectionSupersetConstraint SupersetOf(IEnumerable expected)
560 return new CollectionSupersetConstraint(expected);
568 /// Returns a constraint that tests whether a collection is ordered
570 public CollectionOrderedConstraint Ordered
572 get { return new CollectionOrderedConstraint(); }
580 /// Returns a new CollectionContainsConstraint checking for the
581 /// presence of a particular object in the collection.
583 public CollectionContainsConstraint Member(object expected)
585 return new CollectionContainsConstraint(expected);
589 /// Returns a new CollectionContainsConstraint checking for the
590 /// presence of a particular object in the collection.
592 public CollectionContainsConstraint Contains(object expected)
594 return new CollectionContainsConstraint(expected);
602 /// Returns a new ContainsConstraint. This constraint
603 /// will, in turn, make use of the appropriate second-level
604 /// constraint, depending on the type of the actual argument.
605 /// This overload is only used if the item sought is a string,
606 /// since any other type implies that we are looking for a
607 /// collection member.
609 public ContainsConstraint Contains(string expected)
611 return new ContainsConstraint(expected);
616 #region StringContaining
619 /// Returns a constraint that succeeds if the actual
620 /// value contains the substring supplied as an argument.
622 [Obsolete("Deprecated, use Contains")]
623 public SubstringConstraint StringContaining(string expected)
625 return new SubstringConstraint(expected);
629 /// Returns a constraint that succeeds if the actual
630 /// value contains the substring supplied as an argument.
632 [Obsolete("Deprecated, use Contains")]
633 public SubstringConstraint ContainsSubstring(string expected)
635 return new SubstringConstraint(expected);
640 #region DoesNotContain
643 /// Returns a constraint that fails if the actual
644 /// value contains the substring supplied as an argument.
646 [Obsolete("Deprecated, use Does.Not.Contain")]
647 public SubstringConstraint DoesNotContain(string expected)
649 return new ConstraintExpression().Not.ContainsSubstring(expected);
657 /// Returns a constraint that succeeds if the actual
658 /// value starts with the substring supplied as an argument.
660 public StartsWithConstraint StartWith(string expected)
662 return new StartsWithConstraint(expected);
666 /// Returns a constraint that succeeds if the actual
667 /// value starts with the substring supplied as an argument.
669 public StartsWithConstraint StartsWith(string expected)
671 return new StartsWithConstraint(expected);
675 /// Returns a constraint that succeeds if the actual
676 /// value starts with the substring supplied as an argument.
678 [Obsolete("Deprecated, use Does.StartWith or StartsWith")]
679 public StartsWithConstraint StringStarting(string expected)
681 return new StartsWithConstraint(expected);
686 #region DoesNotStartWith
689 /// Returns a constraint that fails if the actual
690 /// value starts with the substring supplied as an argument.
692 [Obsolete("Deprecated, use Does.Not.StartWith")]
693 public StartsWithConstraint DoesNotStartWith(string expected)
695 return new ConstraintExpression().Not.StartsWith(expected);
703 /// Returns a constraint that succeeds if the actual
704 /// value ends with the substring supplied as an argument.
706 public EndsWithConstraint EndWith(string expected)
708 return new EndsWithConstraint(expected);
712 /// Returns a constraint that succeeds if the actual
713 /// value ends with the substring supplied as an argument.
715 public EndsWithConstraint EndsWith(string expected)
717 return new EndsWithConstraint(expected);
721 /// Returns a constraint that succeeds if the actual
722 /// value ends with the substring supplied as an argument.
724 [Obsolete("Deprecated, use Does.EndWith or EndsWith")]
725 public EndsWithConstraint StringEnding(string expected)
727 return new EndsWithConstraint(expected);
732 #region DoesNotEndWith
735 /// Returns a constraint that fails if the actual
736 /// value ends with the substring supplied as an argument.
738 [Obsolete("Deprecated, use Does.Not.EndWith")]
739 public EndsWithConstraint DoesNotEndWith(string expected)
741 return new ConstraintExpression().Not.EndsWith(expected);
749 /// Returns a constraint that succeeds if the actual
750 /// value matches the regular expression supplied as an argument.
752 public RegexConstraint Match(string pattern)
754 return new RegexConstraint(pattern);
758 /// Returns a constraint that succeeds if the actual
759 /// value matches the regular expression supplied as an argument.
761 public RegexConstraint Matches(string pattern)
763 return new RegexConstraint(pattern);
767 /// Returns a constraint that succeeds if the actual
768 /// value matches the regular expression supplied as an argument.
770 [Obsolete("Deprecated, use Does.Match or Matches")]
771 public RegexConstraint StringMatching(string pattern)
773 return new RegexConstraint(pattern);
781 /// Returns a constraint that fails if the actual
782 /// value matches the pattern supplied as an argument.
784 [Obsolete("Deprecated, use Does.Not.Match")]
785 public RegexConstraint DoesNotMatch(string pattern)
787 return new ConstraintExpression().Not.Matches(pattern);
796 /// Returns a constraint that tests whether the path provided
797 /// is the same as an expected path after canonicalization.
799 public SamePathConstraint SamePath(string expected)
801 return new SamePathConstraint(expected);
809 /// Returns a constraint that tests whether the path provided
810 /// is a subpath of the expected path after canonicalization.
812 public SubPathConstraint SubPathOf(string expected)
814 return new SubPathConstraint(expected);
819 #region SamePathOrUnder
822 /// Returns a constraint that tests whether the path provided
823 /// is the same path or under an expected path after canonicalization.
825 public SamePathOrUnderConstraint SamePathOrUnder(string expected)
827 return new SamePathOrUnderConstraint(expected);
836 /// Returns a constraint that tests whether the actual value falls
837 /// within a specified range.
839 public RangeConstraint InRange(IComparable from, IComparable to)
841 return new RangeConstraint(from, to);