PageRenderTime 18ms CodeModel.GetById 10ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/src/NUnit/framework/Constraints/PrefixConstraints.cs

#
C# | 226 lines | 112 code | 25 blank | 89 comment | 8 complexity | 1c95c8cea6f0c0692aa30215b34ec0c9 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;
  8using System.Collections;
  9
 10namespace NUnit.Framework.Constraints
 11{
 12	#region PrefixConstraint
 13	/// <summary>
 14	/// Abstract base class used for prefixes
 15	/// </summary>
 16    public abstract class PrefixConstraint : Constraint
 17    {
 18        /// <summary>
 19        /// The base constraint
 20        /// </summary>
 21        protected Constraint baseConstraint;
 22
 23        /// <summary>
 24        /// Construct given a base constraint
 25        /// </summary>
 26        /// <param name="resolvable"></param>
 27        protected PrefixConstraint(IResolveConstraint resolvable) : base(resolvable)
 28        {
 29            if ( resolvable != null )
 30                this.baseConstraint = resolvable.Resolve();
 31        }
 32    }
 33	#endregion
 34
 35	#region NotConstraint
 36    /// <summary>
 37	/// NotConstraint negates the effect of some other constraint
 38	/// </summary>
 39	public class NotConstraint : PrefixConstraint
 40	{
 41		/// <summary>
 42		/// Initializes a new instance of the <see cref="T:NotConstraint"/> class.
 43		/// </summary>
 44		/// <param name="baseConstraint">The base constraint to be negated.</param>
 45		public NotConstraint(Constraint baseConstraint)
 46			: base( baseConstraint ) { }
 47
 48		/// <summary>
 49		/// Test whether the constraint is satisfied by a given value
 50		/// </summary>
 51		/// <param name="actual">The value to be tested</param>
 52		/// <returns>True for if the base constraint fails, false if it succeeds</returns>
 53		public override bool Matches(object actual)
 54		{
 55			this.actual = actual;
 56			return !baseConstraint.Matches(actual);
 57		}
 58
 59		/// <summary>
 60		/// Write the constraint description to a MessageWriter
 61		/// </summary>
 62		/// <param name="writer">The writer on which the description is displayed</param>
 63		public override void WriteDescriptionTo( MessageWriter writer )
 64		{
 65			writer.WritePredicate( "not" );
 66			baseConstraint.WriteDescriptionTo( writer );
 67		}
 68
 69		/// <summary>
 70		/// Write the actual value for a failing constraint test to a MessageWriter.
 71		/// </summary>
 72		/// <param name="writer">The writer on which the actual value is displayed</param>
 73		public override void WriteActualValueTo(MessageWriter writer)
 74		{
 75			baseConstraint.WriteActualValueTo (writer);
 76		}
 77	}
 78	#endregion
 79
 80	#region AllItemsConstraint
 81	/// <summary>
 82	/// AllItemsConstraint applies another constraint to each
 83	/// item in a collection, succeeding if they all succeed.
 84	/// </summary>
 85	public class AllItemsConstraint : PrefixConstraint
 86	{
 87		/// <summary>
 88		/// Construct an AllItemsConstraint on top of an existing constraint
 89		/// </summary>
 90		/// <param name="itemConstraint"></param>
 91		public AllItemsConstraint(Constraint itemConstraint)
 92			: base( itemConstraint )
 93        {
 94            this.DisplayName = "all";
 95        }
 96
 97		/// <summary>
 98		/// Apply the item constraint to each item in the collection,
 99		/// failing if any item fails.
100		/// </summary>
101		/// <param name="actual"></param>
102		/// <returns></returns>
103		public override bool Matches(object actual)
104		{
105			this.actual = actual;
106
107			if ( !(actual is IEnumerable) )
108				throw new ArgumentException( "The actual value must be an IEnumerable", "actual" );
109
110			foreach(object item in (IEnumerable)actual)
111				if (!baseConstraint.Matches(item))
112					return false;
113
114			return true;
115		}
116
117		/// <summary>
118		/// Write a description of this constraint to a MessageWriter
119		/// </summary>
120		/// <param name="writer"></param>
121		public override void WriteDescriptionTo(MessageWriter writer)
122		{
123			writer.WritePredicate("all items");
124			baseConstraint.WriteDescriptionTo(writer);
125		}
126	}
127	#endregion
128
129	#region SomeItemsConstraint
130	/// <summary>
131	/// SomeItemsConstraint applies another constraint to each
132	/// item in a collection, succeeding if any of them succeeds.
133	/// </summary>
134	public class SomeItemsConstraint : PrefixConstraint
135	{
136		/// <summary>
137		/// Construct a SomeItemsConstraint on top of an existing constraint
138		/// </summary>
139		/// <param name="itemConstraint"></param>
140		public SomeItemsConstraint(Constraint itemConstraint)
141			: base( itemConstraint ) 
142        {
143            this.DisplayName = "some";
144        }
145
146		/// <summary>
147		/// Apply the item constraint to each item in the collection,
148		/// succeeding if any item succeeds.
149		/// </summary>
150		/// <param name="actual"></param>
151		/// <returns></returns>
152		public override bool Matches(object actual)
153		{
154			this.actual = actual;
155
156			if ( !(actual is IEnumerable) )
157				throw new ArgumentException( "The actual value must be an IEnumerable", "actual" );
158
159			foreach(object item in (IEnumerable)actual)
160				if (baseConstraint.Matches(item))
161					return true;
162
163			return false;
164		}
165
166		/// <summary>
167		/// Write a description of this constraint to a MessageWriter
168		/// </summary>
169		/// <param name="writer"></param>
170		public override void WriteDescriptionTo(MessageWriter writer)
171		{
172			writer.WritePredicate("some item");
173			baseConstraint.WriteDescriptionTo(writer);
174		}
175	}
176	#endregion
177
178	#region NoItemConstraint
179    /// <summary>
180    /// NoItemConstraint applies another constraint to each
181    /// item in a collection, failing if any of them succeeds.
182    /// </summary>
183    public class NoItemConstraint : PrefixConstraint
184	{
185		/// <summary>
186		/// Construct a SomeItemsConstraint on top of an existing constraint
187		/// </summary>
188		/// <param name="itemConstraint"></param>
189		public NoItemConstraint(Constraint itemConstraint)
190			: base( itemConstraint ) 
191        {
192            this.DisplayName = "none";
193        }
194
195		/// <summary>
196		/// Apply the item constraint to each item in the collection,
197		/// failing if any item fails.
198		/// </summary>
199		/// <param name="actual"></param>
200		/// <returns></returns>
201		public override bool Matches(object actual)
202		{
203			this.actual = actual;
204
205			if ( !(actual is IEnumerable) )
206				throw new ArgumentException( "The actual value must be an IEnumerable", "actual" );
207
208			foreach(object item in (IEnumerable)actual)
209				if (baseConstraint.Matches(item))
210					return false;
211
212			return true;
213		}
214
215		/// <summary>
216		/// Write a description of this constraint to a MessageWriter
217		/// </summary>
218		/// <param name="writer"></param>
219		public override void WriteDescriptionTo(MessageWriter writer)
220		{
221			writer.WritePredicate("no item");
222			baseConstraint.WriteDescriptionTo(writer);
223		}
224	}
225	#endregion
226}