1 // ****************************************************************
\r
2 // Copyright 2007, Charlie Poole
\r
3 // This is free software licensed under the NUnit license. You may
\r
4 // obtain a copy of the license at http://nunit.org/?p=license&r=2.4
\r
5 // ****************************************************************
\r
9 namespace NUnit.Framework.Constraints
\r
13 /// ContainsConstraint tests a whether a string contains a substring
\r
14 /// or a collection contains an object. It postpones the decision of
\r
15 /// which test to use until the type of the actual argument is known.
\r
16 /// This allows testing whether a string is contained in a collection
\r
17 /// or as a substring of another string using the same syntax.
\r
19 public class ContainsConstraint : Constraint
\r
22 Constraint realConstraint;
\r
24 private Constraint RealConstraint
\r
28 if ( realConstraint == null )
\r
30 if ( actual is string )
\r
31 this.realConstraint = new SubstringConstraint( (string)expected );
\r
33 this.realConstraint = new CollectionContainsConstraint( expected );
\r
36 return realConstraint;
\r
40 realConstraint = value;
\r
45 /// Initializes a new instance of the <see cref="T:ContainsConstraint"/> class.
\r
47 /// <param name="expected">The expected.</param>
\r
48 public ContainsConstraint( object expected )
\r
50 this.expected = expected;
\r
54 /// Test whether the constraint is satisfied by a given value
\r
56 /// <param name="actual">The value to be tested</param>
\r
57 /// <returns>True for success, false for failure</returns>
\r
58 public override bool Matches(object actual)
\r
60 this.actual = actual;
\r
62 if ( this.caseInsensitive )
\r
63 this.RealConstraint = RealConstraint.IgnoreCase;
\r
65 return this.RealConstraint.Matches( actual );
\r
69 /// Write the constraint description to a MessageWriter
\r
71 /// <param name="writer">The writer on which the description is displayed</param>
\r
72 public override void WriteDescriptionTo(MessageWriter writer)
\r
74 this.RealConstraint.WriteDescriptionTo(writer);
\r