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
12 /// TypeConstraint is the abstract base for constraints
\r
13 /// that take a Type as their expected value.
\r
15 public abstract class TypeConstraint : Constraint
\r
18 /// The expected Type used by the constraint
\r
20 protected Type expectedType;
\r
23 /// Construct a TypeConstraint for a given Type
\r
25 /// <param name="type"></param>
\r
26 public TypeConstraint(Type type)
\r
28 this.expectedType = type;
\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
36 /// <param name="writer">The writer on which the actual value is displayed</param>
\r
37 public override void WriteActualValueTo(MessageWriter writer)
\r
39 writer.WriteActualValue( actual == null ? null : actual.GetType() );
\r
44 /// ExactTypeConstraint is used to test that an object
\r
45 /// is of the exact type provided in the constructor
\r
47 public class ExactTypeConstraint : TypeConstraint
\r
50 /// Construct an ExactTypeConstraint for a given Type
\r
52 /// <param name="type"></param>
\r
53 public ExactTypeConstraint(Type type) : base( type ) { }
\r
56 /// Test that an object is of the exact type specified
\r
58 /// <param name="actual"></param>
\r
59 /// <returns></returns>
\r
60 public override bool Matches(object actual)
\r
62 this.actual = actual;
\r
63 return actual != null && actual.GetType() == this.expectedType;
\r
67 /// Write the description of this constraint to a MessageWriter
\r
69 /// <param name="writer"></param>
\r
70 public override void WriteDescriptionTo(MessageWriter writer)
\r
72 writer.WriteExpectedValue(expectedType);
\r
77 /// InstanceOfTypeConstraint is used to test that an object
\r
78 /// is of the same type provided or derived from it.
\r
80 public class InstanceOfTypeConstraint : TypeConstraint
\r
83 /// Construct an InstanceOfTypeConstraint for the type provided
\r
85 /// <param name="type"></param>
\r
86 public InstanceOfTypeConstraint(Type type) : base(type) { }
\r
89 /// Test whether an object is of the specified type or a derived type
\r
91 /// <param name="actual"></param>
\r
92 /// <returns></returns>
\r
93 public override bool Matches(object actual)
\r
95 this.actual = actual;
\r
96 return actual != null && expectedType.IsInstanceOfType(actual);
\r
100 /// Write a description of this constraint to a MessageWriter
\r
102 /// <param name="writer"></param>
\r
103 public override void WriteDescriptionTo(MessageWriter writer)
\r
105 writer.WritePredicate("instance of");
\r
106 writer.WriteExpectedValue(expectedType);
\r
111 /// AssignableFromConstraint is used to test that an object
\r
112 /// can be assigned from a given Type.
\r
114 public class AssignableFromConstraint : TypeConstraint
\r
117 /// Construct an AssignableFromConstraint for the type provided
\r
119 /// <param name="type"></param>
\r
120 public AssignableFromConstraint(Type type) : base(type) { }
\r
123 /// Test whether an object can be assigned from the specified type
\r
125 /// <param name="actual"></param>
\r
126 /// <returns></returns>
\r
127 public override bool Matches(object actual)
\r
129 this.actual = actual;
\r
130 return actual != null && actual.GetType().IsAssignableFrom( expectedType );
\r
134 /// Write a description of this constraint to a MessageWriter
\r
136 /// <param name="writer"></param>
\r
137 public override void WriteDescriptionTo(MessageWriter writer)
\r
139 writer.WritePredicate("Type assignable from");
\r
140 writer.WriteExpectedValue(expectedType);
\r