1 // ****************************************************************
\r
2 // Copyright 2007, Charlie Poole
\r
3 // This is free software licensed under the NUnit license. You may
\r
4 // obtain a copy of the license at http://nunit.org/?p=license&r=2.4
\r
5 // ****************************************************************
\r
9 namespace NUnit.Framework.Constraints
\r
12 /// BinaryOperation is the abstract base of all constraints
\r
13 /// that combine two other constraints in some fashion.
\r
15 public abstract class BinaryOperation : Constraint
\r
18 /// The first constraint being combined
\r
20 protected Constraint left;
\r
22 /// The second constraint being combined
\r
24 protected Constraint right;
\r
27 /// Construct a BinaryOperation from two other constraints
\r
29 /// <param name="left">The first constraint</param>
\r
30 /// <param name="right">The second constraint</param>
\r
31 public BinaryOperation(Constraint left, Constraint right)
\r
39 /// AndConstraint succeeds only if both members succeed.
\r
41 public class AndConstraint : BinaryOperation
\r
44 /// Create an AndConstraint from two other constraints
\r
46 /// <param name="left">The first constraint</param>
\r
47 /// <param name="right">The second constraint</param>
\r
48 public AndConstraint(Constraint left, Constraint right) : base(left, right) { }
\r
51 /// Apply both member constraints to an actual value, succeeding
\r
52 /// succeeding only if both of them succeed.
\r
54 /// <param name="actual">The actual value</param>
\r
55 /// <returns>True if the constraints both succeeded</returns>
\r
56 public override bool Matches(object actual)
\r
58 this.actual = actual;
\r
59 return left.Matches(actual) && right.Matches(actual);
\r
63 /// Write a description for this contraint to a MessageWriter
\r
65 /// <param name="writer">The MessageWriter to receive the description</param>
\r
66 public override void WriteDescriptionTo(MessageWriter writer)
\r
68 left.WriteDescriptionTo(writer);
\r
69 writer.WriteConnector("and");
\r
70 right.WriteDescriptionTo(writer);
\r
75 /// OrConstraint succeeds if either member succeeds
\r
77 public class OrConstraint : BinaryOperation
\r
80 /// Create an OrConstraint from two other constraints
\r
82 /// <param name="left">The first constraint</param>
\r
83 /// <param name="right">The second constraint</param>
\r
84 public OrConstraint(Constraint left, Constraint right) : base(left, right) { }
\r
87 /// Apply the member constraints to an actual value, succeeding
\r
88 /// succeeding as soon as one of them succeeds.
\r
90 /// <param name="actual">The actual value</param>
\r
91 /// <returns>True if either constraint succeeded</returns>
\r
92 public override bool Matches(object actual)
\r
94 this.actual = actual;
\r
95 return left.Matches(actual) || right.Matches(actual);
\r
99 /// Write a description for this contraint to a MessageWriter
\r
101 /// <param name="writer">The MessageWriter to receive the description</param>
\r
102 public override void WriteDescriptionTo(MessageWriter writer)
\r
104 left.WriteDescriptionTo(writer);
\r
105 writer.WriteConnector("or");
\r
106 right.WriteDescriptionTo(writer);
\r