1 // ***********************************************************************
2 // Copyright (c) 2011 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 /// ConstraintStatus represents the status of a ConstraintResult
33 /// returned by a Constraint being applied to an actual value.
35 public enum ConstraintStatus
38 /// The status has not yet been set
43 /// The constraint succeeded
48 /// The constraint failed
53 /// An error occured in applying the constraint (reserved for future use)
59 /// Contain the result of matching a <see cref="Constraint"/> against an actual value.
61 public class ConstraintResult
63 IConstraint _constraint;
68 /// Constructs a <see cref="ConstraintResult"/> for a particular <see cref="Constraint"/>.
70 /// <param name="constraint">The Constraint to which this result applies.</param>
71 /// <param name="actualValue">The actual value to which the Constraint was applied.</param>
72 public ConstraintResult(IConstraint constraint, object actualValue)
74 _constraint = constraint;
75 ActualValue = actualValue;
79 /// Constructs a <see cref="ConstraintResult"/> for a particular <see cref="Constraint"/>.
81 /// <param name="constraint">The Constraint to which this result applies.</param>
82 /// <param name="actualValue">The actual value to which the Constraint was applied.</param>
83 /// <param name="status">The status of the new ConstraintResult.</param>
84 public ConstraintResult(IConstraint constraint, object actualValue, ConstraintStatus status)
85 : this(constraint, actualValue)
91 /// Constructs a <see cref="ConstraintResult"/> for a particular <see cref="Constraint"/>.
93 /// <param name="constraint">The Constraint to which this result applies.</param>
94 /// <param name="actualValue">The actual value to which the Constraint was applied.</param>
95 /// <param name="isSuccess">If true, applies a status of Success to the result, otherwise Failure.</param>
96 public ConstraintResult(IConstraint constraint, object actualValue, bool isSuccess)
97 : this(constraint, actualValue)
99 Status = isSuccess ? ConstraintStatus.Success : ConstraintStatus.Failure;
107 /// The actual value that was passed to the <see cref="Constraint.ApplyTo{TActual}(TActual)"/> method.
109 public object ActualValue { get; private set; }
112 /// Gets and sets the ResultStatus for this result.
114 public ConstraintStatus Status { get; set; }
117 /// True if actual value meets the Constraint criteria otherwise false.
119 public virtual bool IsSuccess
121 get { return Status == ConstraintStatus.Success; }
125 /// Display friendly name of the constraint.
127 public string Name { get { return _constraint.DisplayName; } }
130 /// Description of the constraint may be affected by the state the constraint had
131 /// when <see cref="Constraint.ApplyTo{TActual}(TActual)"/> was performed against the actual value.
133 public string Description { get { return _constraint.Description; } }
137 #region Write Methods
140 /// Write the failure message to the MessageWriter provided
141 /// as an argument. The default implementation simply passes
142 /// the result and the actual value to the writer, which
143 /// then displays the constraint description and the value.
145 /// Constraints that need to provide additional details,
146 /// such as where the error occured can override this.
148 /// <param name="writer">The MessageWriter on which to display the message</param>
149 public virtual void WriteMessageTo(MessageWriter writer)
151 writer.DisplayDifferences(this);
155 /// Write the actual value for a failing constraint test to a
156 /// MessageWriter. The default implementation simply writes
157 /// the raw value of actual, leaving it to the writer to
158 /// perform any formatting.
160 /// <param name="writer">The writer on which the actual value is displayed</param>
161 public virtual void WriteActualValueTo(MessageWriter writer)
163 writer.WriteActualValue(ActualValue);