Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using NUnit.Framework;
- using NUnit.Framework.Constraints;
- using NUnit.Framework.Internal;
- namespace Basware.P2P.UI.Lib.Common
- {
- /// <summary>
- /// Wrapper for NUnit's Assert class for page objects
- ///
- /// Main difference is not setting the test as failed if assertion exception
- /// is catched later. This enables using Bassert in try-catch blocks.
- ///
- /// Once all page objects are updated to use a better mechanism for
- /// condition checks, this class becomes obsolete.
- /// </summary>
- public class Bassert
- {
- #region Direct methods
- public static void Fail(string message)
- {
- Assert.Fail(message);
- }
- public static void Fail(string message, params object[] args)
- {
- Assert.Fail(message, args);
- }
- #endregion
- public static void AreEqual(object expected, object actual)
- {
- That<object>(actual, (IResolveConstraint) Is.EqualTo(expected), (string) null, (object[]) null);
- }
- public static void AreNotEqual(object expected, object actual)
- {
- That<object>(actual, (IResolveConstraint) Is.Not.EqualTo(expected), null, null);
- }
- public static void AreNotEqual(object expected,
- object actual,
- string message,
- params object[] args)
- {
- That<object>(actual, (IResolveConstraint) Is.Not.EqualTo(expected), message, args);
- }
- public static void AreEqual(object expected, object actual, string message, params object[] args)
- {
- That<object>(actual, (IResolveConstraint) Is.EqualTo(expected), message, args);
- }
- public static void False(bool condition)
- {
- That<bool>(condition, (IResolveConstraint) Is.False, (string) null, (object[]) null);
- }
- public static void IsFalse(bool condition)
- {
- That<bool>(condition, (IResolveConstraint) Is.False, (string) null, (object[]) null);
- }
- public static void IsFalse(bool condition, string message, params object[] args)
- {
- That<bool>(condition, (IResolveConstraint) Is.False, message, args);
- }
- public static void IsTrue(bool condition)
- {
- That<bool>(condition, (IResolveConstraint) Is.True, (string) null, (object[]) null);
- }
- public static void IsTrue(bool condition, string message, params object[] args)
- {
- That<bool>(condition, (IResolveConstraint) Is.True, message, args);
- }
- public static void IsNotNull(object anObject)
- {
- That<object>(anObject, (IResolveConstraint) Is.Not.Null, (string) null, (object[]) null);
- }
- public static void NotNull(object anObject)
- {
- That<object>(anObject, (IResolveConstraint) Is.Null, (string) null, (object[]) null);
- }
- public static void Greater(int arg1, int arg2)
- {
- That<int>(arg1, (IResolveConstraint) Is.GreaterThan((object) arg2), (string) null, (object[]) null);
- }
- public static void Less(int arg1, int arg2)
- {
- That<int>(arg1, (IResolveConstraint) Is.LessThan((object) arg2), (string) null, (object[]) null);
- }
- #region Assert.That
- #region Boolean
- /// <summary>
- /// Asserts that a condition is true. If the condition is false the method throws
- /// an <see cref="AssertionException"/>.
- /// </summary>
- /// <param name="condition">The evaluated condition</param>
- /// <param name="message">The message to display if the condition is false</param>
- /// <param name="args">Arguments to be used in formatting the message</param>
- public static void That(bool condition, string message, params object[] args)
- {
- That(condition, Is.True, message, args);
- }
- /// <summary>
- /// Asserts that a condition is true. If the condition is false the method throws
- /// an <see cref="AssertionException"/>.
- /// </summary>
- /// <param name="condition">The evaluated condition</param>
- public static void That(bool condition)
- {
- That(condition, Is.True, null, null);
- }
- /// <summary>
- /// Asserts that a condition is true. If the condition is false the method throws
- /// an <see cref="AssertionException"/>.
- /// </summary>
- /// <param name="condition">The evaluated condition</param>
- /// <param name="getExceptionMessage">A function to build the message included with the Exception</param>
- public static void That(bool condition, Func<string> getExceptionMessage)
- {
- That(condition, Is.True, getExceptionMessage);
- }
- #endregion
- #region Lambda returning Boolean
- /// <summary>
- /// Asserts that a condition is true. If the condition is false the method throws
- /// an <see cref="AssertionException"/>.
- /// </summary>
- /// <param name="condition">A lambda that returns a Boolean</param>
- /// <param name="message">The message to display if the condition is false</param>
- /// <param name="args">Arguments to be used in formatting the message</param>
- public static void That(Func<bool> condition, string message, params object[] args)
- {
- That(condition.Invoke(), Is.True, message, args);
- }
- /// <summary>
- /// Asserts that a condition is true. If the condition is false the method throws
- /// an <see cref="AssertionException"/>.
- /// </summary>
- /// <param name="condition">A lambda that returns a Boolean</param>
- public static void That(Func<bool> condition)
- {
- That(condition.Invoke(), Is.True, null, null);
- }
- /// <summary>
- /// Asserts that a condition is true. If the condition is false the method throws
- /// an <see cref="AssertionException"/>.
- /// </summary>
- /// <param name="condition">A lambda that returns a Boolean</param>
- /// <param name="getExceptionMessage">A function to build the message included with the Exception</param>
- public static void That(Func<bool> condition, Func<string> getExceptionMessage)
- {
- That(condition.Invoke(), Is.True, getExceptionMessage);
- }
- #endregion
- #region ActualValueDelegate
- /// <summary>
- /// Apply a constraint to an actual value, succeeding if the constraint
- /// is satisfied and throwing an assertion exception on failure.
- /// </summary>
- /// <typeparam name="TActual">The Type being compared.</typeparam>
- /// <param name="del">An ActualValueDelegate returning the value to be tested</param>
- /// <param name="expr">A Constraint expression to be applied</param>
- public static void That<TActual>(ActualValueDelegate<TActual> del, IResolveConstraint expr)
- {
- That(del, expr.Resolve(), null, null);
- }
- /// <summary>
- /// Apply a constraint to an actual value, succeeding if the constraint
- /// is satisfied and throwing an assertion exception on failure.
- /// </summary>
- /// <typeparam name="TActual">The Type being compared.</typeparam>
- /// <param name="del">An ActualValueDelegate returning the value to be tested</param>
- /// <param name="expr">A Constraint expression to be applied</param>
- /// <param name="message">The message that will be displayed on failure</param>
- /// <param name="args">Arguments to be used in formatting the message</param>
- public static void That<TActual>(ActualValueDelegate<TActual> del, IResolveConstraint expr, string message, params object[] args)
- {
- var constraint = expr.Resolve();
- IncrementAssertCount();
- var result = constraint.ApplyTo(del);
- if (!result.IsSuccess)
- ReportFailure(result, message, args);
- }
- /// <summary>
- /// Apply a constraint to an actual value, succeeding if the constraint
- /// is satisfied and throwing an assertion exception on failure.
- /// </summary>
- /// <typeparam name="TActual">The Type being compared.</typeparam>
- /// <param name="del">An ActualValueDelegate returning the value to be tested</param>
- /// <param name="expr">A Constraint expression to be applied</param>
- /// <param name="getExceptionMessage">A function to build the message included with the Exception</param>
- public static void That<TActual>(
- ActualValueDelegate<TActual> del,
- IResolveConstraint expr,
- Func<string> getExceptionMessage)
- {
- var constraint = expr.Resolve();
- IncrementAssertCount();
- var result = constraint.ApplyTo(del);
- if (!result.IsSuccess)
- ReportFailure(result, getExceptionMessage());
- }
- #endregion
- #region TestDelegate
- /// <summary>
- /// Asserts that the code represented by a delegate throws an exception
- /// that satisfies the constraint provided.
- /// </summary>
- /// <param name="code">A TestDelegate to be executed</param>
- /// <param name="constraint">A Constraint expression to be applied</param>
- public static void That(TestDelegate code, IResolveConstraint constraint)
- {
- That(code, constraint, null, null);
- }
- /// <summary>
- /// Asserts that the code represented by a delegate throws an exception
- /// that satisfies the constraint provided.
- /// </summary>
- /// <param name="code">A TestDelegate to be executed</param>
- /// <param name="constraint">A Constraint expression to be applied</param>
- /// <param name="message">The message that will be displayed on failure</param>
- /// <param name="args">Arguments to be used in formatting the message</param>
- public static void That(TestDelegate code, IResolveConstraint constraint, string message, params object[] args)
- {
- That((object)code, constraint, message, args);
- }
- /// <summary>
- /// Asserts that the code represented by a delegate throws an exception
- /// that satisfies the constraint provided.
- /// </summary>
- /// <param name="code">A TestDelegate to be executed</param>
- /// <param name="constraint">A Constraint expression to be applied</param>
- /// <param name="getExceptionMessage">A function to build the message included with the Exception</param>
- public static void That(TestDelegate code, IResolveConstraint constraint, Func<string> getExceptionMessage)
- {
- That((object)code, constraint, getExceptionMessage);
- }
- #endregion
- #endregion
- #region Assert.That<TActual>
- /// <summary>
- /// Apply a constraint to an actual value, succeeding if the constraint
- /// is satisfied and throwing an assertion exception on failure.
- /// </summary>
- /// <typeparam name="TActual">The Type being compared.</typeparam>
- /// <param name="actual">The actual value to test</param>
- /// <param name="expression">A Constraint expression to be applied</param>
- public static void That<TActual>(TActual actual, IResolveConstraint expression)
- {
- That(actual, expression, null, null);
- }
- /// <summary>
- /// Apply a constraint to an actual value, succeeding if the constraint
- /// is satisfied and throwing an assertion exception on failure.
- /// </summary>
- /// <typeparam name="TActual">The Type being compared.</typeparam>
- /// <param name="actual">The actual value to test</param>
- /// <param name="expression">A Constraint expression to be applied</param>
- /// <param name="message">The message that will be displayed on failure</param>
- /// <param name="args">Arguments to be used in formatting the message</param>
- public static void That<TActual>(TActual actual, IResolveConstraint expression, string message, params object[] args)
- {
- var constraint = expression.Resolve();
- IncrementAssertCount();
- var result = constraint.ApplyTo(actual);
- if (!result.IsSuccess)
- ReportFailure(result, message, args);
- }
- /// <summary>
- /// Apply a constraint to an actual value, succeeding if the constraint
- /// is satisfied and throwing an assertion exception on failure.
- /// </summary>
- /// <typeparam name="TActual">The Type being compared.</typeparam>
- /// <param name="actual">The actual value to test</param>
- /// <param name="expression">A Constraint expression to be applied</param>
- /// <param name="getExceptionMessage">A function to build the message included with the Exception</param>
- public static void That<TActual>(
- TActual actual,
- IResolveConstraint expression,
- Func<string> getExceptionMessage)
- {
- var constraint = expression.Resolve();
- IncrementAssertCount();
- var result = constraint.ApplyTo(actual);
- if (!result.IsSuccess)
- ReportFailure(result, getExceptionMessage());
- }
- #endregion
- #region Assert.ByVal
- /// <summary>
- /// Apply a constraint to an actual value, succeeding if the constraint
- /// is satisfied and throwing an assertion exception on failure.
- /// Used as a synonym for That in rare cases where a private setter
- /// causes a Visual Basic compilation error.
- /// </summary>
- /// <param name="actual">The actual value to test</param>
- /// <param name="expression">A Constraint expression to be applied</param>
- public static void ByVal(object actual, IResolveConstraint expression)
- {
- That(actual, expression, null, null);
- }
- /// <summary>
- /// Apply a constraint to an actual value, succeeding if the constraint
- /// is satisfied and throwing an assertion exception on failure.
- /// Used as a synonym for That in rare cases where a private setter
- /// causes a Visual Basic compilation error.
- /// </summary>
- /// <remarks>
- /// This method is provided for use by VB developers needing to test
- /// the value of properties with private setters.
- /// </remarks>
- /// <param name="actual">The actual value to test</param>
- /// <param name="expression">A Constraint expression to be applied</param>
- /// <param name="message">The message that will be displayed on failure</param>
- /// <param name="args">Arguments to be used in formatting the message</param>
- public static void ByVal(object actual, IResolveConstraint expression, string message, params object[] args)
- {
- That(actual, expression, message, args);
- }
- #endregion
- #region Assert.cs hacks
- private static void ReportFailure(ConstraintResult result, string message)
- {
- ReportFailure(result, message, null);
- }
- private static void ReportFailure(ConstraintResult result, string message, params object[] args)
- {
- MessageWriter writer = new TextMessageWriter(message, args);
- result.WriteMessageTo(writer);
- ReportFailure(writer.ToString());
- }
- /// <summary>
- /// Hack : This method has switched to a throwing method
- /// </summary>
- /// <param name="message"></param>
- private static void ReportFailure(string message)
- {
- // Record the failure in an <assertion> element
- var result = TestExecutionContext.CurrentContext.CurrentResult;
- // NOTE: These commented lines below are the hack
- //result.RecordAssertion(AssertionStatus.Failed, message, GetStackTrace());
- //result.RecordTestCompletion();
- //// If we are outside any multiple assert block, then throw
- //if (TestExecutionContext.CurrentContext.MultipleAssertLevel == 0)
- // throw new AssertionException(result.Message);
- throw new AssertionException(message);
- }
- private static void IncrementAssertCount()
- {
- TestExecutionContext.CurrentContext.IncrementAssertCount();
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement