1 // ***********************************************************************
2 // Copyright (c) 2007 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 using System.Collections;
30 using NUnit.Framework.Constraints;
32 namespace NUnit.Framework
35 /// AssertionHelper is an optional base class for user tests,
36 /// allowing the use of shorter ids for constraints and
37 /// asserts and avoiding conflict with the definition of
38 /// <see cref="Is"/>, from which it inherits much of its
39 /// behavior, in certain mock object frameworks.
41 public class AssertionHelper : ConstraintFactory
48 /// Asserts that a condition is true. If the condition is false the method throws
49 /// an <see cref="AssertionException"/>. Works Identically to
50 /// <see cref="Assert.That(bool, string, object[])"/>.
52 /// <param name="condition">The evaluated condition</param>
53 /// <param name="message">The message to display if the condition is false</param>
54 /// <param name="args">Arguments to be used in formatting the message</param>
55 public void Expect(bool condition, string message, params object[] args)
57 Assert.That(condition, Is.True, message, args);
61 /// Asserts that a condition is true. If the condition is false the method throws
62 /// an <see cref="AssertionException"/>. Works Identically to <see cref="Assert.That(bool)"/>.
64 /// <param name="condition">The evaluated condition</param>
65 public void Expect(bool condition)
67 Assert.That(condition, Is.True, null, null);
72 #region ActualValueDelegate
74 /// Apply a constraint to an actual value, succeeding if the constraint
75 /// is satisfied and throwing an assertion exception on failure.
77 /// <param name="expr">A Constraint expression to be applied</param>
78 /// <param name="del">An ActualValueDelegate returning the value to be tested</param>
79 public void Expect<TActual>(ActualValueDelegate<TActual> del, IResolveConstraint expr)
81 Assert.That(del, expr.Resolve(), null, null);
85 /// Apply a constraint to an actual value, succeeding if the constraint
86 /// is satisfied and throwing an assertion exception on failure.
88 /// <param name="del">An ActualValueDelegate returning the value to be tested</param>
89 /// <param name="expr">A Constraint expression to be applied</param>
90 /// <param name="message">The message that will be displayed on failure</param>
91 /// <param name="args">Arguments to be used in formatting the message</param>
92 public void Expect<TActual>(ActualValueDelegate<TActual> del, IResolveConstraint expr, string message, params object[] args)
94 Assert.That(del, expr, message, args);
101 /// Asserts that the code represented by a delegate throws an exception
102 /// that satisfies the constraint provided.
104 /// <param name="code">A TestDelegate to be executed</param>
105 /// <param name="constraint">A ThrowsConstraint used in the test</param>
106 public void Expect(TestDelegate code, IResolveConstraint constraint)
108 Assert.That((object)code, constraint);
115 #region Expect<TActual>
118 /// Apply a constraint to an actual value, succeeding if the constraint
119 /// is satisfied and throwing an assertion exception on failure.
121 /// <param name="expression">A Constraint to be applied</param>
122 /// <param name="actual">The actual value to test</param>
123 static public void Expect<TActual>(TActual actual, IResolveConstraint expression)
125 Assert.That(actual, expression, null, null);
129 /// Apply a constraint to an actual value, succeeding if the constraint
130 /// is satisfied and throwing an assertion exception on failure.
132 /// <param name="expression">A Constraint expression to be applied</param>
133 /// <param name="actual">The actual value to test</param>
134 /// <param name="message">The message that will be displayed on failure</param>
135 /// <param name="args">Arguments to be used in formatting the message</param>
136 static public void Expect<TActual>(TActual actual, IResolveConstraint expression, string message, params object[] args)
138 Assert.That(actual, expression, message, args);
145 /// Returns a ListMapper based on a collection.
147 /// <param name="original">The original collection</param>
148 /// <returns></returns>
149 public ListMapper Map( ICollection original )
151 return new ListMapper( original );