Mercurial > silverbladetech
view SilverlightValidation/SilverlightValidation.Tests/TestSupport/CommandCanExecuteAssertHelper.cs @ 97:1adc1ae981ea
Tests added to SilverlightValidation.Tests
author | stevenhollidge <stevenhollidge@hotmail.com> |
---|---|
date | Sat, 05 May 2012 16:39:00 +0100 |
parents | |
children |
line wrap: on
line source
using System; using System.Windows.Input; using NUnit.Framework; namespace SilverlightValidation.Tests.TestSupport { public static class CommandCanExecuteAssertHelper { #region Assert methods (using helper) #region Can/cannot execute /// <summary> /// Raises an assertion if the supplied command's CanExecute method returns false. /// </summary> /// <remarks> /// CanExecute is passed null. /// </remarks> /// <param name="command">The command.</param> public static void AssertCanExecute(this ICommand command) { if (command == null) throw new ArgumentNullException("command"); Assert.IsTrue(command.CanExecute(null)); } /// <summary> /// Raises an assertion if the supplied command's CanExecute method returns false. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="command">The command.</param> /// <param name="value">A value to pass to CanExecute.</param> public static void AssertCanExecute<T>(this ICommand command, T value) { if (command == null) throw new ArgumentNullException("command"); Assert.IsTrue(command.CanExecute(value)); } /// <summary> /// Raises an assertion if the supplied command's CanExecute method returns true. /// </summary> /// <remarks> /// CanExecute is passed null. /// </remarks> /// <param name="command">The command.</param> public static void AssertCannotExecute(this ICommand command) { if (command == null) throw new ArgumentNullException("command"); Assert.IsTrue(!command.CanExecute(null)); } /// <summary> /// Raises an assertion if the supplied command's CanExecute method returns true. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="command">The command.</param> /// <param name="value">A value to pass to CanExecute.</param> public static void AssertCannotExecute<T>(this ICommand command, T value) { if (command == null) throw new ArgumentNullException("command"); Assert.IsTrue(!command.CanExecute(value)); } #endregion #region Assert is raised/Assert is not raised (CommandCanExecute) /// <summary> /// Raises an assertion if the action is invoked and the supplied command /// raises a CommandCanExecuteChanged event. /// </summary> /// <param name = "command">The command.</param> /// <param name = "action">The action to invoke.</param> public static void AssertIsNotRaised(this ICommand command, Action action) { Assert.IsTrue(IsNotRaised(command, action)); } /// <summary> /// Raises an assertion if the action is invoked and the supplied command /// does not raise at least one CommandCanExecuteChanged event. /// </summary> /// <param name = "command">The command.</param> /// <param name = "action">The action to invoke.</param> public static void AssertIsRaised(this ICommand command, Action action) { Assert.IsTrue(IsRaised(command, action)); } /// <summary> /// Raises an assertion if the action is invoked and the supplied command /// does raise the number of CommandCanExecuteChanged events as defined by the /// supplied predicate. /// </summary> /// <param name = "command">The command.</param> /// <param name = "predicate">The predicate.</param> /// <param name = "action">The action to invoke.</param> public static void AssertIsRaised(this ICommand command, Predicate<int> predicate, Action action) { Assert.IsTrue(IsRaised(command, predicate, action)); } #endregion #endregion #region Is raised/Is not raised methods (CommandCanExecute) /// <summary> /// Determines if the specified CommandCanExecuteChanged event is /// not raised when the supplied action is invoked. /// </summary> /// <param name = "command">The command.</param> /// <param name = "action">The action to invoke.</param> public static bool IsNotRaised(this ICommand command, Action action) { return !IsRaised(command, action); } /// <summary> /// Determines if the specified CommandCanExecuteChanged event is /// raised one or more times when the supplied action is invoked. /// </summary> /// <param name = "command">The command.</param> /// <param name = "action">The action to invoke.</param> /// <returns> /// <c>true</c> if the specified command is raised; otherwise, <c>false</c>. /// </returns> public static bool IsRaised(this ICommand command, Action action) { return IsRaised(command, count => count > 0, action); } /// <summary> /// Determines if the specified CommandCanExecuteChanged event is /// raised the number of times as defined by the predicate when the supplied action is invoked. /// </summary> /// <param name = "command">The command.</param> /// <param name = "predicate">A predicate.</param> /// <param name = "action">The action to invoke.</param> /// <returns> /// <c>true</c> if the specified command's CanExecuteChanged event is raised; otherwise, <c>false</c>. /// </returns> public static bool IsRaised(this ICommand command, Predicate<int> predicate, Action action) { if (predicate == null) throw new ArgumentNullException("predicate"); if (action == null) throw new ArgumentNullException("action"); using (var watcher = new CommandCanExecuteChangedEventWatcher(command)) { action(); return predicate(watcher.RaisedCount); } } #endregion } }