PageRenderTime 12ms CodeModel.GetById 1ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/src/NUnit/framework/Constraints/TypeConstraints.cs

#
C# | 183 lines | 77 code | 16 blank | 90 comment | 10 complexity | a8e4f1bf731402d3415308a9473edb74 MD5 | raw file
  1// ****************************************************************
  2// Copyright 2007, Charlie Poole
  3// This is free software licensed under the NUnit license. You may
  4// obtain a copy of the license at http://nunit.org
  5// ****************************************************************
  6
  7using System;
  8
  9namespace NUnit.Framework.Constraints
 10{
 11    /// <summary>
 12    /// TypeConstraint is the abstract base for constraints
 13    /// that take a Type as their expected value.
 14    /// </summary>
 15    public abstract class TypeConstraint : Constraint
 16    {
 17        /// <summary>
 18        /// The expected Type used by the constraint
 19        /// </summary>
 20        protected Type expectedType;
 21
 22        /// <summary>
 23        /// Construct a TypeConstraint for a given Type
 24        /// </summary>
 25        /// <param name="type"></param>
 26        public TypeConstraint(Type type) : base(type)
 27        {
 28            this.expectedType = type;
 29        }
 30
 31        /// <summary>
 32        /// Write the actual value for a failing constraint test to a
 33        /// MessageWriter. TypeConstraints override this method to write
 34        /// the name of the type.
 35        /// </summary>
 36        /// <param name="writer">The writer on which the actual value is displayed</param>
 37		public override void WriteActualValueTo(MessageWriter writer)
 38		{
 39			writer.WriteActualValue( actual == null ? null : actual.GetType() ); 
 40		}
 41	}
 42
 43    /// <summary>
 44    /// ExactTypeConstraint is used to test that an object
 45    /// is of the exact type provided in the constructor
 46    /// </summary>
 47    public class ExactTypeConstraint : TypeConstraint
 48    {
 49        /// <summary>
 50        /// Construct an ExactTypeConstraint for a given Type
 51        /// </summary>
 52        /// <param name="type">The expected Type.</param>
 53        public ExactTypeConstraint(Type type) : base( type ) 
 54        {
 55            this.DisplayName = "typeof";
 56        }
 57
 58        /// <summary>
 59        /// Test that an object is of the exact type specified
 60        /// </summary>
 61        /// <param name="actual">The actual value.</param>
 62        /// <returns>True if the tested object is of the exact type provided, otherwise false.</returns>
 63        public override bool Matches(object actual)
 64        {
 65            this.actual = actual;
 66            return actual != null && actual.GetType() == this.expectedType;
 67        }
 68
 69        /// <summary>
 70        /// Write the description of this constraint to a MessageWriter
 71        /// </summary>
 72        /// <param name="writer">The MessageWriter to use</param>
 73        public override void WriteDescriptionTo(MessageWriter writer)
 74        {
 75            writer.WriteExpectedValue(expectedType);
 76        }
 77    }
 78
 79    /// <summary>
 80    /// InstanceOfTypeConstraint is used to test that an object
 81    /// is of the same type provided or derived from it.
 82    /// </summary>
 83    public class InstanceOfTypeConstraint : TypeConstraint
 84    {
 85        /// <summary>
 86        /// Construct an InstanceOfTypeConstraint for the type provided
 87        /// </summary>
 88        /// <param name="type">The expected Type</param>
 89        public InstanceOfTypeConstraint(Type type) : base(type) 
 90        {
 91            this.DisplayName = "instanceof";
 92        }
 93
 94        /// <summary>
 95        /// Test whether an object is of the specified type or a derived type
 96        /// </summary>
 97        /// <param name="actual">The object to be tested</param>
 98        /// <returns>True if the object is of the provided type or derives from it, otherwise false.</returns>
 99        public override bool Matches(object actual)
100        {
101            this.actual = actual;
102            return actual != null && expectedType.IsInstanceOfType(actual);
103        }
104
105        /// <summary>
106        /// Write a description of this constraint to a MessageWriter
107        /// </summary>
108        /// <param name="writer">The MessageWriter to use</param>
109        public override void WriteDescriptionTo(MessageWriter writer)
110        {
111            writer.WritePredicate("instance of");
112            writer.WriteExpectedValue(expectedType);
113        }
114	}
115
116    /// <summary>
117    /// AssignableFromConstraint is used to test that an object
118    /// can be assigned from a given Type.
119    /// </summary>
120    public class AssignableFromConstraint : TypeConstraint
121    {
122        /// <summary>
123        /// Construct an AssignableFromConstraint for the type provided
124        /// </summary>
125        /// <param name="type"></param>
126        public AssignableFromConstraint(Type type) : base(type) { }
127
128        /// <summary>
129        /// Test whether an object can be assigned from the specified type
130        /// </summary>
131        /// <param name="actual">The object to be tested</param>
132        /// <returns>True if the object can be assigned a value of the expected Type, otherwise false.</returns>
133        public override bool Matches(object actual)
134        {
135            this.actual = actual;
136            return actual != null && actual.GetType().IsAssignableFrom(expectedType);
137        }
138
139        /// <summary>
140        /// Write a description of this constraint to a MessageWriter
141        /// </summary>
142        /// <param name="writer">The MessageWriter to use</param>
143        public override void WriteDescriptionTo(MessageWriter writer)
144        {
145            writer.WritePredicate("assignable from");
146            writer.WriteExpectedValue(expectedType);
147        }
148    }
149
150    /// <summary>
151    /// AssignableToConstraint is used to test that an object
152    /// can be assigned to a given Type.
153    /// </summary>
154    public class AssignableToConstraint : TypeConstraint
155    {
156        /// <summary>
157        /// Construct an AssignableToConstraint for the type provided
158        /// </summary>
159        /// <param name="type"></param>
160        public AssignableToConstraint(Type type) : base(type) { }
161
162        /// <summary>
163        /// Test whether an object can be assigned to the specified type
164        /// </summary>
165        /// <param name="actual">The object to be tested</param>
166        /// <returns>True if the object can be assigned a value of the expected Type, otherwise false.</returns>
167        public override bool Matches(object actual)
168        {
169            this.actual = actual;
170            return actual != null && expectedType.IsAssignableFrom(actual.GetType());
171        }
172
173        /// <summary>
174        /// Write a description of this constraint to a MessageWriter
175        /// </summary>
176        /// <param name="writer">The MessageWriter to use</param>
177        public override void WriteDescriptionTo(MessageWriter writer)
178        {
179            writer.WritePredicate("assignable to");
180            writer.WriteExpectedValue(expectedType);
181        }
182    }
183}