/src/NUnit/framework/Constraints/TypeConstraints.cs
C# | 183 lines | 77 code | 16 blank | 90 comment | 10 complexity | a8e4f1bf731402d3415308a9473edb74 MD5 | raw file
Possible License(s): GPL-2.0
- // ****************************************************************
- // 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
- // ****************************************************************
-
- using System;
-
- namespace NUnit.Framework.Constraints
- {
- /// <summary>
- /// TypeConstraint is the abstract base for constraints
- /// that take a Type as their expected value.
- /// </summary>
- public abstract class TypeConstraint : Constraint
- {
- /// <summary>
- /// The expected Type used by the constraint
- /// </summary>
- protected Type expectedType;
-
- /// <summary>
- /// Construct a TypeConstraint for a given Type
- /// </summary>
- /// <param name="type"></param>
- public TypeConstraint(Type type) : base(type)
- {
- this.expectedType = type;
- }
-
- /// <summary>
- /// Write the actual value for a failing constraint test to a
- /// MessageWriter. TypeConstraints override this method to write
- /// the name of the type.
- /// </summary>
- /// <param name="writer">The writer on which the actual value is displayed</param>
- public override void WriteActualValueTo(MessageWriter writer)
- {
- writer.WriteActualValue( actual == null ? null : actual.GetType() );
- }
- }
-
- /// <summary>
- /// ExactTypeConstraint is used to test that an object
- /// is of the exact type provided in the constructor
- /// </summary>
- public class ExactTypeConstraint : TypeConstraint
- {
- /// <summary>
- /// Construct an ExactTypeConstraint for a given Type
- /// </summary>
- /// <param name="type">The expected Type.</param>
- public ExactTypeConstraint(Type type) : base( type )
- {
- this.DisplayName = "typeof";
- }
-
- /// <summary>
- /// Test that an object is of the exact type specified
- /// </summary>
- /// <param name="actual">The actual value.</param>
- /// <returns>True if the tested object is of the exact type provided, otherwise false.</returns>
- public override bool Matches(object actual)
- {
- this.actual = actual;
- return actual != null && actual.GetType() == this.expectedType;
- }
-
- /// <summary>
- /// Write the description of this constraint to a MessageWriter
- /// </summary>
- /// <param name="writer">The MessageWriter to use</param>
- public override void WriteDescriptionTo(MessageWriter writer)
- {
- writer.WriteExpectedValue(expectedType);
- }
- }
-
- /// <summary>
- /// InstanceOfTypeConstraint is used to test that an object
- /// is of the same type provided or derived from it.
- /// </summary>
- public class InstanceOfTypeConstraint : TypeConstraint
- {
- /// <summary>
- /// Construct an InstanceOfTypeConstraint for the type provided
- /// </summary>
- /// <param name="type">The expected Type</param>
- public InstanceOfTypeConstraint(Type type) : base(type)
- {
- this.DisplayName = "instanceof";
- }
-
- /// <summary>
- /// Test whether an object is of the specified type or a derived type
- /// </summary>
- /// <param name="actual">The object to be tested</param>
- /// <returns>True if the object is of the provided type or derives from it, otherwise false.</returns>
- public override bool Matches(object actual)
- {
- this.actual = actual;
- return actual != null && expectedType.IsInstanceOfType(actual);
- }
-
- /// <summary>
- /// Write a description of this constraint to a MessageWriter
- /// </summary>
- /// <param name="writer">The MessageWriter to use</param>
- public override void WriteDescriptionTo(MessageWriter writer)
- {
- writer.WritePredicate("instance of");
- writer.WriteExpectedValue(expectedType);
- }
- }
-
- /// <summary>
- /// AssignableFromConstraint is used to test that an object
- /// can be assigned from a given Type.
- /// </summary>
- public class AssignableFromConstraint : TypeConstraint
- {
- /// <summary>
- /// Construct an AssignableFromConstraint for the type provided
- /// </summary>
- /// <param name="type"></param>
- public AssignableFromConstraint(Type type) : base(type) { }
-
- /// <summary>
- /// Test whether an object can be assigned from the specified type
- /// </summary>
- /// <param name="actual">The object to be tested</param>
- /// <returns>True if the object can be assigned a value of the expected Type, otherwise false.</returns>
- public override bool Matches(object actual)
- {
- this.actual = actual;
- return actual != null && actual.GetType().IsAssignableFrom(expectedType);
- }
-
- /// <summary>
- /// Write a description of this constraint to a MessageWriter
- /// </summary>
- /// <param name="writer">The MessageWriter to use</param>
- public override void WriteDescriptionTo(MessageWriter writer)
- {
- writer.WritePredicate("assignable from");
- writer.WriteExpectedValue(expectedType);
- }
- }
-
- /// <summary>
- /// AssignableToConstraint is used to test that an object
- /// can be assigned to a given Type.
- /// </summary>
- public class AssignableToConstraint : TypeConstraint
- {
- /// <summary>
- /// Construct an AssignableToConstraint for the type provided
- /// </summary>
- /// <param name="type"></param>
- public AssignableToConstraint(Type type) : base(type) { }
-
- /// <summary>
- /// Test whether an object can be assigned to the specified type
- /// </summary>
- /// <param name="actual">The object to be tested</param>
- /// <returns>True if the object can be assigned a value of the expected Type, otherwise false.</returns>
- public override bool Matches(object actual)
- {
- this.actual = actual;
- return actual != null && expectedType.IsAssignableFrom(actual.GetType());
- }
-
- /// <summary>
- /// Write a description of this constraint to a MessageWriter
- /// </summary>
- /// <param name="writer">The MessageWriter to use</param>
- public override void WriteDescriptionTo(MessageWriter writer)
- {
- writer.WritePredicate("assignable to");
- writer.WriteExpectedValue(expectedType);
- }
- }
- }