1 // ***********************************************************************
2 // Copyright (c) 2007 Charlie Poole
4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the
6 // "Software"), to deal in the Software without restriction, including
7 // without limitation the rights to use, copy, modify, merge, publish,
8 // distribute, sublicense, and/or sell copies of the Software, and to
9 // permit persons to whom the Software is furnished to do so, subject to
10 // the following conditions:
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 // ***********************************************************************
25 #define NUNIT_FRAMEWORK
29 namespace NUnit.Framework.Constraints
32 /// AndConstraint succeeds only if both members succeed.
34 public class AndConstraint : BinaryConstraint
36 //private enum FailurePoint
43 //private FailurePoint failurePoint;
46 /// Create an AndConstraint from two other constraints
48 /// <param name="left">The first constraint</param>
49 /// <param name="right">The second constraint</param>
50 public AndConstraint(IConstraint left, IConstraint right) : base(left, right) { }
53 /// Gets text describing a constraint
55 public override string Description
57 get { return Left.Description + " and " + Right.Description; }
61 /// Apply both member constraints to an actual value, succeeding
62 /// succeeding only if both of them succeed.
64 /// <param name="actual">The actual value</param>
65 /// <returns>True if the constraints both succeeded</returns>
66 public override ConstraintResult ApplyTo<TActual>(TActual actual)
68 var leftResult = Left.ApplyTo(actual);
69 var rightResult = leftResult.IsSuccess
70 ? Right.ApplyTo(actual)
71 : new ConstraintResult(Right, actual);
73 return new AndConstraintResult(this, actual, leftResult, rightResult);
76 #region Nested Result Class
78 class AndConstraintResult : ConstraintResult
80 private ConstraintResult leftResult;
81 private ConstraintResult rightResult;
83 public AndConstraintResult(AndConstraint constraint, object actual, ConstraintResult leftResult, ConstraintResult rightResult)
84 : base(constraint, actual, leftResult.IsSuccess && rightResult.IsSuccess)
86 this.leftResult = leftResult;
87 this.rightResult = rightResult;
91 /// Write the actual value for a failing constraint test to a
92 /// MessageWriter. The default implementation simply writes
93 /// the raw value of actual, leaving it to the writer to
94 /// perform any formatting.
96 /// <param name="writer">The writer on which the actual value is displayed</param>
97 public override void WriteActualValueTo(MessageWriter writer)
100 base.WriteActualValueTo(writer);
101 else if (!leftResult.IsSuccess)
102 leftResult.WriteActualValueTo(writer);
104 rightResult.WriteActualValueTo(writer);