Merge branch 'master' of git://github.com/mono/mono
[mono.git] / mcs / nunit24 / NUnitFramework / framework / Constraints / ContainsConstraint.cs
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
6 \r
7 using System;\r
8 \r
9 namespace NUnit.Framework.Constraints\r
10 {\r
11         // TODO Needs tests\r
12         /// <summary>\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
18         /// </summary>\r
19         public class ContainsConstraint : Constraint\r
20         {\r
21                 object expected;\r
22                 Constraint realConstraint;\r
23 \r
24                 private Constraint RealConstraint\r
25                 {\r
26                         get \r
27                         {\r
28                                 if ( realConstraint == null )\r
29                                 {\r
30                                         if ( actual is string )\r
31                                                 this.realConstraint = new SubstringConstraint( (string)expected );\r
32                                         else\r
33                                                 this.realConstraint = new CollectionContainsConstraint( expected );\r
34                                 }\r
35 \r
36                                 return realConstraint;\r
37                         }\r
38                         set \r
39                         { \r
40                                 realConstraint = value; \r
41                         }\r
42                 }\r
43 \r
44         /// <summary>\r
45         /// Initializes a new instance of the <see cref="T:ContainsConstraint"/> class.\r
46         /// </summary>\r
47         /// <param name="expected">The expected.</param>\r
48                 public ContainsConstraint( object expected )\r
49                 {\r
50                         this.expected = expected;\r
51                 }\r
52 \r
53         /// <summary>\r
54         /// Test whether the constraint is satisfied by a given value\r
55         /// </summary>\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
59                 {\r
60                         this.actual = actual;\r
61 \r
62                         if ( this.caseInsensitive )\r
63                                 this.RealConstraint = RealConstraint.IgnoreCase;\r
64 \r
65                         return this.RealConstraint.Matches( actual );\r
66                 }\r
67 \r
68         /// <summary>\r
69         /// Write the constraint description to a MessageWriter\r
70         /// </summary>\r
71         /// <param name="writer">The writer on which the description is displayed</param>\r
72                 public override void WriteDescriptionTo(MessageWriter writer)\r
73                 {\r
74                         this.RealConstraint.WriteDescriptionTo(writer);\r
75                 }\r
76         }\r
77 }\r