// **************************************************************** // Copyright 2007, Charlie Poole // This is free software licensed under the NUnit license. You may // obtain a copy of the license at http://nunit.org/?p=license&r=2.4 // **************************************************************** using System; namespace NUnit.Framework.Constraints { /// /// TypeConstraint is the abstract base for constraints /// that take a Type as their expected value. /// public abstract class TypeConstraint : Constraint { /// /// The expected Type used by the constraint /// protected Type expectedType; /// /// Construct a TypeConstraint for a given Type /// /// public TypeConstraint(Type type) { this.expectedType = type; } /// /// Write the actual value for a failing constraint test to a /// MessageWriter. TypeCOnstraints override this method to write /// the name of the type. /// /// The writer on which the actual value is displayed public override void WriteActualValueTo(MessageWriter writer) { writer.WriteActualValue( actual == null ? null : actual.GetType() ); } } /// /// ExactTypeConstraint is used to test that an object /// is of the exact type provided in the constructor /// public class ExactTypeConstraint : TypeConstraint { /// /// Construct an ExactTypeConstraint for a given Type /// /// public ExactTypeConstraint(Type type) : base( type ) { } /// /// Test that an object is of the exact type specified /// /// /// public override bool Matches(object actual) { this.actual = actual; return actual != null && actual.GetType() == this.expectedType; } /// /// Write the description of this constraint to a MessageWriter /// /// public override void WriteDescriptionTo(MessageWriter writer) { writer.WriteExpectedValue(expectedType); } } /// /// InstanceOfTypeConstraint is used to test that an object /// is of the same type provided or derived from it. /// public class InstanceOfTypeConstraint : TypeConstraint { /// /// Construct an InstanceOfTypeConstraint for the type provided /// /// public InstanceOfTypeConstraint(Type type) : base(type) { } /// /// Test whether an object is of the specified type or a derived type /// /// /// public override bool Matches(object actual) { this.actual = actual; return actual != null && expectedType.IsInstanceOfType(actual); } /// /// Write a description of this constraint to a MessageWriter /// /// public override void WriteDescriptionTo(MessageWriter writer) { writer.WritePredicate("instance of"); writer.WriteExpectedValue(expectedType); } } /// /// AssignableFromConstraint is used to test that an object /// can be assigned from a given Type. /// public class AssignableFromConstraint : TypeConstraint { /// /// Construct an AssignableFromConstraint for the type provided /// /// public AssignableFromConstraint(Type type) : base(type) { } /// /// Test whether an object can be assigned from the specified type /// /// /// public override bool Matches(object actual) { this.actual = actual; return actual != null && actual.GetType().IsAssignableFrom( expectedType ); } /// /// Write a description of this constraint to a MessageWriter /// /// public override void WriteDescriptionTo(MessageWriter writer) { writer.WritePredicate("Type assignable from"); writer.WriteExpectedValue(expectedType); } } }