// **************************************************************** // Copyright 2007, Charlie Poole // This is free software licensed under the NUnit license. You may // obtain a copy of the license at http://nunit.org/?p=license&r=2.4 // **************************************************************** using System; namespace NUnit.Framework.Constraints { /// /// BinaryOperation is the abstract base of all constraints /// that combine two other constraints in some fashion. /// public abstract class BinaryOperation : Constraint { /// /// The first constraint being combined /// protected Constraint left; /// /// The second constraint being combined /// protected Constraint right; /// /// Construct a BinaryOperation from two other constraints /// /// The first constraint /// The second constraint public BinaryOperation(Constraint left, Constraint right) { this.left = left; this.right = right; } } /// /// AndConstraint succeeds only if both members succeed. /// public class AndConstraint : BinaryOperation { /// /// Create an AndConstraint from two other constraints /// /// The first constraint /// The second constraint public AndConstraint(Constraint left, Constraint right) : base(left, right) { } /// /// Apply both member constraints to an actual value, succeeding /// succeeding only if both of them succeed. /// /// The actual value /// True if the constraints both succeeded public override bool Matches(object actual) { this.actual = actual; return left.Matches(actual) && right.Matches(actual); } /// /// Write a description for this contraint to a MessageWriter /// /// The MessageWriter to receive the description public override void WriteDescriptionTo(MessageWriter writer) { left.WriteDescriptionTo(writer); writer.WriteConnector("and"); right.WriteDescriptionTo(writer); } } /// /// OrConstraint succeeds if either member succeeds /// public class OrConstraint : BinaryOperation { /// /// Create an OrConstraint from two other constraints /// /// The first constraint /// The second constraint public OrConstraint(Constraint left, Constraint right) : base(left, right) { } /// /// Apply the member constraints to an actual value, succeeding /// succeeding as soon as one of them succeeds. /// /// The actual value /// True if either constraint succeeded public override bool Matches(object actual) { this.actual = actual; return left.Matches(actual) || right.Matches(actual); } /// /// Write a description for this contraint to a MessageWriter /// /// The MessageWriter to receive the description public override void WriteDescriptionTo(MessageWriter writer) { left.WriteDescriptionTo(writer); writer.WriteConnector("or"); right.WriteDescriptionTo(writer); } } }