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 using NUnit.Framework.Constraints;
26 using NUnit.Framework.Internal;
27 using NUnit.Framework.TUnit;
29 namespace NUnit.Framework
31 public partial class Assert
38 /// Asserts that a condition is true. If the condition is false the method throws
39 /// an <see cref="AssertionException"/>.
41 /// <param name="condition">The evaluated condition</param>
42 /// <param name="message">The message to display if the condition is false</param>
43 /// <param name="args">Arguments to be used in formatting the message</param>
44 static public void That(bool condition, string message, params object[] args)
46 Assert.That(condition, Is.True, message, args);
50 /// Asserts that a condition is true. If the condition is false the method throws
51 /// an <see cref="AssertionException"/>.
53 /// <param name="condition">The evaluated condition</param>
54 static public void That(bool condition)
56 Assert.That(condition, Is.True, null, null);
61 #region Lambda returning Boolean
64 /// Asserts that a condition is true. If the condition is false the method throws
65 /// an <see cref="AssertionException"/>.
67 /// <param name="condition">A lambda that returns a Boolean</param>
68 /// <param name="message">The message to display if the condition is false</param>
69 /// <param name="args">Arguments to be used in formatting the message</param>
70 static public void That(Func<bool> condition, string message, params object[] args)
72 Assert.That(condition.Invoke(), Is.True, message, args);
76 /// Asserts that a condition is true. If the condition is false the method throws
77 /// an <see cref="AssertionException"/>.
79 /// <param name="condition">A lambda that returns a Boolean</param>
80 static public void That(Func<bool> condition)
82 Assert.That(condition.Invoke(), Is.True, null, null);
87 #region ActualValueDelegate
90 /// Apply a constraint to an actual value, succeeding if the constraint
91 /// is satisfied and throwing an assertion exception on failure.
93 /// <param name="expr">A Constraint expression to be applied</param>
94 /// <param name="del">An ActualValueDelegate returning the value to be tested</param>
95 static public void That<TActual>(ActualValueDelegate<TActual> del, IResolveConstraint expr)
97 Assert.That(del, expr.Resolve(), null, null);
101 /// Apply a constraint to an actual value, succeeding if the constraint
102 /// is satisfied and throwing an assertion exception on failure.
104 /// <param name="del">An ActualValueDelegate returning the value to be tested</param>
105 /// <param name="expr">A Constraint expression to be applied</param>
106 /// <param name="message">The message that will be displayed on failure</param>
107 /// <param name="args">Arguments to be used in formatting the message</param>
108 static public void That<TActual>(ActualValueDelegate<TActual> del, IResolveConstraint expr, string message, params object[] args)
110 var constraint = expr.Resolve();
112 IncrementAssertCount();
113 var result = constraint.ApplyTo(del);
114 if (!result.IsSuccess)
116 MessageWriter writer = new TextMessageWriter(message, args);
117 result.WriteMessageTo(writer);
118 throw new AssertionException(writer.ToString());
127 /// Asserts that the code represented by a delegate throws an exception
128 /// that satisfies the constraint provided.
130 /// <param name="code">A TestDelegate to be executed</param>
131 /// <param name="constraint">A ThrowsConstraint used in the test</param>
132 static public void That(TestDelegate code, IResolveConstraint constraint)
134 Assert.That(code, constraint, null, null);
138 /// Asserts that the code represented by a delegate throws an exception
139 /// that satisfies the constraint provided.
141 /// <param name="code">A TestDelegate to be executed</param>
142 /// <param name="constraint">A ThrowsConstraint used in the test</param>
143 /// <param name="message">The message that will be displayed on failure</param>
144 /// <param name="args">Arguments to be used in formatting the message</param>
145 static public void That(TestDelegate code, IResolveConstraint constraint, string message, params string[] args)
147 Assert.That((object)code, constraint, message, args);
154 #region Assert.That<TActual>
157 /// Apply a constraint to an actual value, succeeding if the constraint
158 /// is satisfied and throwing an assertion exception on failure.
160 /// <param name="expression">A Constraint to be applied</param>
161 /// <param name="actual">The actual value to test</param>
162 static public void That<TActual>(TActual actual, IResolveConstraint expression)
164 Assert.That(actual, expression, null, null);
168 /// Apply a constraint to an actual value, succeeding if the constraint
169 /// is satisfied and throwing an assertion exception on failure.
171 /// <param name="expression">A Constraint expression to be applied</param>
172 /// <param name="actual">The actual value to test</param>
173 /// <param name="message">The message that will be displayed on failure</param>
174 /// <param name="args">Arguments to be used in formatting the message</param>
175 static public void That<TActual>(TActual actual, IResolveConstraint expression, string message, params object[] args)
177 var constraint = expression.Resolve();
179 IncrementAssertCount();
180 var result = constraint.ApplyTo(actual);
181 if (!result.IsSuccess)
183 // [DuongNT][BEGIN]: Write console logs
184 TLogger.Write(message);
185 // [DuongNT][END]: Write console logs
187 MessageWriter writer = new TextMessageWriter(message, args);
188 result.WriteMessageTo(writer);
189 throw new AssertionException(writer.ToString());
198 /// Apply a constraint to an actual value, succeeding if the constraint
199 /// is satisfied and throwing an assertion exception on failure.
200 /// Used as a synonym for That in rare cases where a private setter
201 /// causes a Visual Basic compilation error.
203 /// <param name="expression">A Constraint to be applied</param>
204 /// <param name="actual">The actual value to test</param>
205 static public void ByVal(object actual, IResolveConstraint expression)
207 Assert.That(actual, expression, null, null);
211 /// Apply a constraint to an actual value, succeeding if the constraint
212 /// is satisfied and throwing an assertion exception on failure.
213 /// Used as a synonym for That in rare cases where a private setter
214 /// causes a Visual Basic compilation error.
217 /// This method is provided for use by VB developers needing to test
218 /// the value of properties with private setters.
220 /// <param name="expression">A Constraint expression to be applied</param>
221 /// <param name="actual">The actual value to test</param>
222 /// <param name="message">The message that will be displayed on failure</param>
223 /// <param name="args">Arguments to be used in formatting the message</param>
224 static public void ByVal(object actual, IResolveConstraint expression, string message, params object[] args)
226 Assert.That(actual, expression, message, args);