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

/src/NUnit/framework/Constraints/BinaryOperations.cs

#
C# | 148 lines | 71 code | 14 blank | 63 comment | 3 complexity | 7c50d2f6503dba6d5ad7dc1048f7a589 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	/// BinaryConstraint is the abstract base of all constraints
 13	/// that combine two other constraints in some fashion.
 14	/// </summary>
 15    public abstract class BinaryConstraint : Constraint
 16    {
 17		/// <summary>
 18		/// The first constraint being combined
 19		/// </summary>
 20		protected Constraint left;
 21		/// <summary>
 22		/// The second constraint being combined
 23		/// </summary>
 24		protected Constraint right;
 25
 26		/// <summary>
 27		/// Construct a BinaryConstraint from two other constraints
 28		/// </summary>
 29		/// <param name="left">The first constraint</param>
 30		/// <param name="right">The second constraint</param>
 31        public BinaryConstraint(Constraint left, Constraint right) : base(left, right)
 32        {
 33            this.left = left;
 34            this.right = right;
 35        }
 36    }
 37
 38    /// <summary>
 39    /// AndConstraint succeeds only if both members succeed.
 40    /// </summary>
 41	public class AndConstraint : BinaryConstraint
 42    {
 43        private enum FailurePoint
 44        {
 45            None,
 46            Left,
 47            Right
 48        };
 49
 50        private FailurePoint failurePoint;
 51
 52		/// <summary>
 53		/// Create an AndConstraint from two other constraints
 54		/// </summary>
 55		/// <param name="left">The first constraint</param>
 56		/// <param name="right">The second constraint</param>
 57		public AndConstraint(Constraint left, Constraint right) : base(left, right) { }
 58
 59		/// <summary>
 60		/// Apply both member constraints to an actual value, succeeding 
 61		/// succeeding only if both of them succeed.
 62		/// </summary>
 63		/// <param name="actual">The actual value</param>
 64		/// <returns>True if the constraints both succeeded</returns>
 65		public override bool Matches(object actual)
 66        {
 67            this.actual = actual;
 68
 69            failurePoint = left.Matches(actual)
 70                ? right.Matches(actual)
 71                    ? FailurePoint.None
 72                    : FailurePoint.Right
 73                : FailurePoint.Left;
 74
 75            return failurePoint == FailurePoint.None;
 76        }
 77
 78		/// <summary>
 79		/// Write a description for this contraint to a MessageWriter
 80		/// </summary>
 81		/// <param name="writer">The MessageWriter to receive the description</param>
 82		public override void WriteDescriptionTo(MessageWriter writer)
 83        {
 84            left.WriteDescriptionTo(writer);
 85            writer.WriteConnector("and");
 86            right.WriteDescriptionTo(writer);
 87        }
 88
 89        /// <summary>
 90        /// Write the actual value for a failing constraint test to a
 91        /// MessageWriter. The default implementation simply writes
 92        /// the raw value of actual, leaving it to the writer to
 93        /// perform any formatting.
 94        /// </summary>
 95        /// <param name="writer">The writer on which the actual value is displayed</param>
 96        public override void WriteActualValueTo(MessageWriter writer)
 97        {
 98            switch (failurePoint)
 99            {
100                case FailurePoint.Left:
101                    left.WriteActualValueTo(writer);
102                    break;
103                case FailurePoint.Right:
104                    right.WriteActualValueTo(writer);
105                    break;
106                default:
107                    base.WriteActualValueTo(writer);
108                    break;
109            }
110        }
111    }
112
113	/// <summary>
114	/// OrConstraint succeeds if either member succeeds
115	/// </summary>
116    public class OrConstraint : BinaryConstraint
117    {
118		/// <summary>
119		/// Create an OrConstraint from two other constraints
120		/// </summary>
121		/// <param name="left">The first constraint</param>
122		/// <param name="right">The second constraint</param>
123		public OrConstraint(Constraint left, Constraint right) : base(left, right) { }
124
125		/// <summary>
126		/// Apply the member constraints to an actual value, succeeding 
127		/// succeeding as soon as one of them succeeds.
128		/// </summary>
129		/// <param name="actual">The actual value</param>
130		/// <returns>True if either constraint succeeded</returns>
131		public override bool Matches(object actual)
132        {
133            this.actual = actual;
134            return left.Matches(actual) || right.Matches(actual);
135        }
136
137		/// <summary>
138		/// Write a description for this contraint to a MessageWriter
139		/// </summary>
140		/// <param name="writer">The MessageWriter to receive the description</param>
141		public override void WriteDescriptionTo(MessageWriter writer)
142        {
143            left.WriteDescriptionTo(writer);
144            writer.WriteConnector("or");
145            right.WriteDescriptionTo(writer);
146        }
147    }
148}