Merge pull request #704 from jgagnon/master
[mono.git] / mcs / nunit24 / NUnitFramework / framework / Constraints / TypeConstraints.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     /// <summary>\r
12     /// TypeConstraint is the abstract base for constraints\r
13     /// that take a Type as their expected value.\r
14     /// </summary>\r
15     public abstract class TypeConstraint : Constraint\r
16     {\r
17         /// <summary>\r
18         /// The expected Type used by the constraint\r
19         /// </summary>\r
20         protected Type expectedType;\r
21 \r
22         /// <summary>\r
23         /// Construct a TypeConstraint for a given Type\r
24         /// </summary>\r
25         /// <param name="type"></param>\r
26         public TypeConstraint(Type type)\r
27         {\r
28             this.expectedType = type;\r
29         }\r
30 \r
31         /// <summary>\r
32         /// Write the actual value for a failing constraint test to a\r
33         /// MessageWriter. TypeCOnstraints override this method to write\r
34         /// the name of the type.\r
35         /// </summary>\r
36         /// <param name="writer">The writer on which the actual value is displayed</param>\r
37                 public override void WriteActualValueTo(MessageWriter writer)\r
38                 {\r
39                         writer.WriteActualValue( actual == null ? null : actual.GetType() ); \r
40                 }\r
41         }\r
42 \r
43     /// <summary>\r
44     /// ExactTypeConstraint is used to test that an object\r
45     /// is of the exact type provided in the constructor\r
46     /// </summary>\r
47     public class ExactTypeConstraint : TypeConstraint\r
48     {\r
49         /// <summary>\r
50         /// Construct an ExactTypeConstraint for a given Type\r
51         /// </summary>\r
52         /// <param name="type"></param>\r
53         public ExactTypeConstraint(Type type) : base( type ) { }\r
54 \r
55         /// <summary>\r
56         /// Test that an object is of the exact type specified\r
57         /// </summary>\r
58         /// <param name="actual"></param>\r
59         /// <returns></returns>\r
60         public override bool Matches(object actual)\r
61         {\r
62             this.actual = actual;\r
63             return actual != null && actual.GetType() == this.expectedType;\r
64         }\r
65 \r
66         /// <summary>\r
67         /// Write the description of this constraint to a MessageWriter\r
68         /// </summary>\r
69         /// <param name="writer"></param>\r
70         public override void WriteDescriptionTo(MessageWriter writer)\r
71         {\r
72             writer.WriteExpectedValue(expectedType);\r
73         }\r
74     }\r
75 \r
76     /// <summary>\r
77     /// InstanceOfTypeConstraint is used to test that an object\r
78     /// is of the same type provided or derived from it.\r
79     /// </summary>\r
80     public class InstanceOfTypeConstraint : TypeConstraint\r
81     {\r
82         /// <summary>\r
83         /// Construct an InstanceOfTypeConstraint for the type provided\r
84         /// </summary>\r
85         /// <param name="type"></param>\r
86         public InstanceOfTypeConstraint(Type type) : base(type) { }\r
87 \r
88         /// <summary>\r
89         /// Test whether an object is of the specified type or a derived type\r
90         /// </summary>\r
91         /// <param name="actual"></param>\r
92         /// <returns></returns>\r
93         public override bool Matches(object actual)\r
94         {\r
95             this.actual = actual;\r
96             return actual != null && expectedType.IsInstanceOfType(actual);\r
97         }\r
98 \r
99         /// <summary>\r
100         /// Write a description of this constraint to a MessageWriter\r
101         /// </summary>\r
102         /// <param name="writer"></param>\r
103         public override void WriteDescriptionTo(MessageWriter writer)\r
104         {\r
105             writer.WritePredicate("instance of");\r
106             writer.WriteExpectedValue(expectedType);\r
107         }\r
108         }\r
109 \r
110     /// <summary>\r
111     /// AssignableFromConstraint is used to test that an object\r
112     /// can be assigned from a given Type.\r
113     /// </summary>\r
114     public class AssignableFromConstraint : TypeConstraint\r
115     {\r
116         /// <summary>\r
117         /// Construct an AssignableFromConstraint for the type provided\r
118         /// </summary>\r
119         /// <param name="type"></param>\r
120         public AssignableFromConstraint(Type type) : base(type) { }\r
121 \r
122         /// <summary>\r
123         /// Test whether an object can be assigned from the specified type\r
124         /// </summary>\r
125         /// <param name="actual"></param>\r
126         /// <returns></returns>\r
127         public override bool Matches(object actual)\r
128         {\r
129                         this.actual = actual;\r
130             return actual != null && actual.GetType().IsAssignableFrom( expectedType );\r
131         }\r
132 \r
133         /// <summary>\r
134         /// Write a description of this constraint to a MessageWriter\r
135         /// </summary>\r
136         /// <param name="writer"></param>\r
137         public override void WriteDescriptionTo(MessageWriter writer)\r
138         {\r
139             writer.WritePredicate("Type assignable from");\r
140             writer.WriteExpectedValue(expectedType);\r
141         }\r
142     }\r
143 }\r