Merge pull request #704 from jgagnon/master
[mono.git] / mcs / nunit24 / NUnitFramework / framework / Constraints / BinaryOperations.cs
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
6 \r
7 using System;\r
8 \r
9 namespace NUnit.Framework.Constraints\r
10 {\r
11         /// <summary>\r
12         /// BinaryOperation is the abstract base of all constraints\r
13         /// that combine two other constraints in some fashion.\r
14         /// </summary>\r
15     public abstract class BinaryOperation : Constraint\r
16     {\r
17                 /// <summary>\r
18                 /// The first constraint being combined\r
19                 /// </summary>\r
20                 protected Constraint left;\r
21                 /// <summary>\r
22                 /// The second constraint being combined\r
23                 /// </summary>\r
24                 protected Constraint right;\r
25 \r
26                 /// <summary>\r
27                 /// Construct a BinaryOperation from two other constraints\r
28                 /// </summary>\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
32         {\r
33             this.left = left;\r
34             this.right = right;\r
35         }\r
36     }\r
37 \r
38     /// <summary>\r
39     /// AndConstraint succeeds only if both members succeed.\r
40     /// </summary>\r
41         public class AndConstraint : BinaryOperation\r
42     {\r
43                 /// <summary>\r
44                 /// Create an AndConstraint from two other constraints\r
45                 /// </summary>\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
49 \r
50                 /// <summary>\r
51                 /// Apply both member constraints to an actual value, succeeding \r
52                 /// succeeding only if both of them succeed.\r
53                 /// </summary>\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
57         {\r
58             this.actual = actual;\r
59             return left.Matches(actual) && right.Matches(actual);\r
60         }\r
61 \r
62                 /// <summary>\r
63                 /// Write a description for this contraint to a MessageWriter\r
64                 /// </summary>\r
65                 /// <param name="writer">The MessageWriter to receive the description</param>\r
66                 public override void WriteDescriptionTo(MessageWriter writer)\r
67         {\r
68             left.WriteDescriptionTo(writer);\r
69             writer.WriteConnector("and");\r
70             right.WriteDescriptionTo(writer);\r
71         }\r
72     }\r
73 \r
74         /// <summary>\r
75         /// OrConstraint succeeds if either member succeeds\r
76         /// </summary>\r
77     public class OrConstraint : BinaryOperation\r
78     {\r
79                 /// <summary>\r
80                 /// Create an OrConstraint from two other constraints\r
81                 /// </summary>\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
85 \r
86                 /// <summary>\r
87                 /// Apply the member constraints to an actual value, succeeding \r
88                 /// succeeding as soon as one of them succeeds.\r
89                 /// </summary>\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
93         {\r
94             this.actual = actual;\r
95             return left.Matches(actual) || right.Matches(actual);\r
96         }\r
97 \r
98                 /// <summary>\r
99                 /// Write a description for this contraint to a MessageWriter\r
100                 /// </summary>\r
101                 /// <param name="writer">The MessageWriter to receive the description</param>\r
102                 public override void WriteDescriptionTo(MessageWriter writer)\r
103         {\r
104             left.WriteDescriptionTo(writer);\r
105             writer.WriteConnector("or");\r
106             right.WriteDescriptionTo(writer);\r
107         }\r
108     }\r
109 }\r