static IEnumerable <CommandBaseTests.TestArgs <AsyncCommand> > GetTestArgs( [NotNull] CommandBaseTests.ReentrancyContainer <AsyncCommand> container, [NotNull] Func <Task> execute, [NotNull] Func <CancellationToken, Task> executeWithCancellationToken, [NotNull] Func <CommandExecutionController, CancellationToken, Task> executeWithController) { var nonDefaultOptions = new CommandOptions(false); yield return(CommandBaseTests.TestArgs.Create(new AsyncCommand(execute, options: nonDefaultOptions), true, false, container)); yield return(CommandBaseTests.TestArgs.Create(new AsyncCommand(execute, () => true, nonDefaultOptions), true, false, container)); yield return(CommandBaseTests.TestArgs.Create(new AsyncCommand(execute, () => false, nonDefaultOptions), false, false, container)); yield return(CommandBaseTests.TestArgs.Create(new AsyncCommand(execute), true, true, container)); yield return(CommandBaseTests.TestArgs.Create(new AsyncCommand(execute, () => true), true, true, container)); yield return(CommandBaseTests.TestArgs.Create(new AsyncCommand(execute, () => false), false, true, container)); yield return (CommandBaseTests.TestArgs.Create(new AsyncCommand(executeWithCancellationToken, options: nonDefaultOptions), true, false, container)); yield return (CommandBaseTests.TestArgs.Create( new AsyncCommand(executeWithCancellationToken, () => true, nonDefaultOptions), true, false, container)); yield return (CommandBaseTests.TestArgs.Create( new AsyncCommand(executeWithCancellationToken, () => false, nonDefaultOptions), false, false, container)); yield return(CommandBaseTests.TestArgs.Create(new AsyncCommand(executeWithCancellationToken), true, true, container)); yield return(CommandBaseTests.TestArgs.Create(new AsyncCommand(executeWithCancellationToken, () => true), true, true, container)); yield return(CommandBaseTests.TestArgs.Create(new AsyncCommand(executeWithCancellationToken, () => false), false, true, container)); yield return(CommandBaseTests.TestArgs.Create(new AsyncCommand(executeWithController, options: nonDefaultOptions), true, false, container) ); yield return (CommandBaseTests.TestArgs.Create(new AsyncCommand(executeWithController, () => true, nonDefaultOptions), true, false, container)); yield return (CommandBaseTests.TestArgs.Create(new AsyncCommand(executeWithController, () => false, nonDefaultOptions), false, false, container)); yield return(CommandBaseTests.TestArgs.Create(new AsyncCommand(executeWithController), true, true, container)); yield return(CommandBaseTests.TestArgs.Create(new AsyncCommand(executeWithController, () => true), true, true, container)); yield return(CommandBaseTests.TestArgs.Create(new AsyncCommand(executeWithController, () => false), false, true, container)); }
static IEnumerable <CommandBaseTests.TestArgs <AsyncCommand <T> > > _ExecutionCanceled <T>() { var counter = new CommandBaseTests.CommandExecutionCounter(); var reentrancyContainer = new CommandBaseTests.ReentrancyContainer <AsyncCommand <T> >(); async Task Execute(T arg) { await Task.Yield(); reentrancyContainer.Command.RunningExecution.CancelCommand.Execute(); throw new OperationCanceledException(); } async Task ExecuteWithCancellationToken(T arg, CancellationToken cancellationToken) { try { await Execute(arg); } finally { if (reentrancyContainer.IsCancelCommandEnabled) { if (cancellationToken.IsCancellationRequested) { counter.CancellationRequest++; } } else { if (!cancellationToken.IsCancellationRequested) { counter.CancellationRequest++; } } } } async Task ExecuteWithController(T arg, CommandExecutionController controller, CancellationToken cancellationToken) { await Task.Yield(); controller.ReportProgress(0.4f); await ExecuteWithCancellationToken(arg, cancellationToken); } foreach (var args in GetTestArgs(reentrancyContainer, Execute, ExecuteWithCancellationToken, ExecuteWithController)) { yield return(args); } Assert.AreEqual(reentrancyContainer.ExecutionCount / 3 * 2, counter.CancellationRequest); }
static IEnumerable <CommandBaseTests.TestArgs <Command <T> > > GetTestArgs <T>( [NotNull] CommandBaseTests.ReentrancyContainer <Command <T> > container, [NotNull] Action <T> execute, [NotNull] Action <T, CancellationToken> executeWithCancellationToken, [NotNull] Action <T, CommandExecutionController, CancellationToken> executeWithController) { var nonDefaultOptions = new CommandOptions(true); yield return(CommandBaseTests.TestArgs.Create(new Command <T>(execute), true, false, container)); yield return(CommandBaseTests.TestArgs.Create(new Command <T>(execute, arg => true), true, false, container)); yield return(CommandBaseTests.TestArgs.Create(new Command <T>(execute, arg => false), false, false, container)); yield return(CommandBaseTests.TestArgs.Create(new Command <T>(execute, options: nonDefaultOptions), true, true, container)); yield return(CommandBaseTests.TestArgs.Create(new Command <T>(execute, arg => true, nonDefaultOptions), true, true, container)); yield return(CommandBaseTests.TestArgs.Create(new Command <T>(execute, arg => false, nonDefaultOptions), false, true, container)); yield return(CommandBaseTests.TestArgs.Create(new Command <T>(executeWithCancellationToken), true, false, container)); yield return(CommandBaseTests.TestArgs.Create(new Command <T>(executeWithCancellationToken, arg => true), true, false, container)); yield return(CommandBaseTests.TestArgs.Create(new Command <T>(executeWithCancellationToken, arg => false), false, false, container)); yield return (CommandBaseTests.TestArgs.Create(new Command <T>(executeWithCancellationToken, options: nonDefaultOptions), true, true, container)); yield return (CommandBaseTests.TestArgs.Create(new Command <T>(executeWithCancellationToken, arg => true, nonDefaultOptions), true, true, container)); yield return (CommandBaseTests.TestArgs.Create( new Command <T>(executeWithCancellationToken, arg => false, nonDefaultOptions), false, true, container)); yield return(CommandBaseTests.TestArgs.Create(new Command <T>(executeWithController), true, false, container)); yield return(CommandBaseTests.TestArgs.Create(new Command <T>(executeWithController, arg => true), true, false, container)); yield return(CommandBaseTests.TestArgs.Create(new Command <T>(executeWithController, arg => false), false, false, container)); yield return(CommandBaseTests.TestArgs.Create(new Command <T>(executeWithController, options: nonDefaultOptions), true, true, container)); yield return (CommandBaseTests.TestArgs.Create(new Command <T>(executeWithController, arg => true, nonDefaultOptions), true, true, container)); yield return (CommandBaseTests.TestArgs.Create(new Command <T>(executeWithController, arg => false, nonDefaultOptions), false, true, container)); }
static IEnumerable <CommandBaseTests.TestArgs <Command> > _ExecutionCanceled() { var counter = new CommandBaseTests.CommandExecutionCounter(); var reentrancyContainer = new CommandBaseTests.ReentrancyContainer <Command>(); void Execute() { reentrancyContainer.Command.RunningExecution.CancelCommand.Execute(); throw new OperationCanceledException(); } void ExecuteWithCancellationToken(CancellationToken cancellationToken) { try { Execute(); } finally { if (reentrancyContainer.IsCancelCommandEnabled) { if (cancellationToken.IsCancellationRequested) { counter.CancellationRequest++; } } else { if (!cancellationToken.IsCancellationRequested) { counter.CancellationRequest++; } } } } void ExecuteWithController(CommandExecutionController controller, CancellationToken cancellationToken) { controller.ReportProgress(0.4f); ExecuteWithCancellationToken(cancellationToken); } foreach (var args in GetTestArgs(reentrancyContainer, Execute, ExecuteWithCancellationToken, ExecuteWithController)) { yield return(args); } Assert.AreEqual(reentrancyContainer.ExecutionCount / 3 * 2, counter.CancellationRequest); }
static IEnumerable <CommandBaseTests.TestArgs <Command <T> > > _ExecutionFailed <T>() { var reentrancyContainer = new CommandBaseTests.ReentrancyContainer <Command <T> >(); void Execute(T arg) => throw new InvalidOperationException(); void ExecuteWithCancellationToken(T arg, CancellationToken cancellationToken) => Execute(arg); void ExecuteWithController(T arg, CommandExecutionController controller, CancellationToken cancellationToken) { controller.ReportProgress(0.4f); ExecuteWithCancellationToken(arg, cancellationToken); } return(GetTestArgs(reentrancyContainer, Execute, ExecuteWithCancellationToken, ExecuteWithController)); }
static IEnumerable <CommandBaseTests.TestArgs <AsyncCommand> > _ExecutionFailed() { var reentrancyContainer = new CommandBaseTests.ReentrancyContainer <AsyncCommand>(); async Task Execute() { await Task.Yield(); throw new InvalidOperationException(); } Task ExecuteWithCancellationToken(CancellationToken cancellationToken) => Execute(); async Task ExecuteWithController(CommandExecutionController controller, CancellationToken cancellationToken) { await Task.Yield(); controller.ReportProgress(0.4f); await ExecuteWithCancellationToken(cancellationToken); } return(GetTestArgs(reentrancyContainer, Execute, ExecuteWithCancellationToken, ExecuteWithController)); }
static IEnumerable <CommandBaseTests.TestArgs <AsyncCommand <T> > > _ExecutionSuccessful <T>() { var counter = new CommandBaseTests.CommandExecutionCounter(); var reentrancyContainer = new CommandBaseTests.ReentrancyContainer <AsyncCommand <T> >(); async Task Execute(T arg) { await Task.Yield(); counter.Execution++; if (reentrancyContainer.Command.RunningExecution != null) { counter.RunningExecution++; if (reentrancyContainer.Command.RunningExecution.CancelCommand != null) { counter.CancelCommand++; if (reentrancyContainer.Command.RunningExecution.CancelCommand.CanExecute() == reentrancyContainer.IsCancelCommandEnabled) { counter.IsCancelCommandEnabled++; } if (Math.Abs(reentrancyContainer.Command.RunningExecution.Progress) < float.Epsilon) { counter.Progress++; } } } if (!reentrancyContainer.Command.CanExecute(default(T))) { counter.CanExecuteWhileExecuting++; } // nested execution await reentrancyContainer.Command.Execute(arg); } async Task ExecuteWithCancellationToken(T arg, CancellationToken cancellationToken) { await Task.Yield(); if (cancellationToken.CanBeCanceled) { counter.CanBeCanceled++; } if (!cancellationToken.IsCancellationRequested) { counter.CancellationRequest++; } await Execute(arg); } async Task ExecuteWithController(T arg, CommandExecutionController controller, CancellationToken cancellationToken) { await Task.Yield(); if (controller != null) { counter.Controller++; } if (controller.Execution == reentrancyContainer.Command.RunningExecution) { counter.SameRunningExecution++; } await ExecuteWithCancellationToken(arg, cancellationToken); ExceptionAssert.Throws <ArgumentOutOfRangeException>(() => controller.ReportProgress(-0.1f), "value"); ExceptionAssert.Throws <ArgumentOutOfRangeException>(() => controller.ReportProgress(1.1f), "value"); controller.ReportProgress(0.3f); if (Math.Abs(controller.Execution.Progress - 0.3f) < float.Epsilon) { counter.ProgressReporting++; } ((IProgress <float>)controller).Report(-0.1f); ((IProgress <float>)controller).Report(1.1f); ((IProgress <float>)controller).Report(0.35f); if (Math.Abs(controller.Execution.Progress - 0.35f) < float.Epsilon) { counter.ProgressReportingAsIProgress++; } controller.DisableCancelCommand(); if (!controller.Execution.CancelCommand.CanExecute()) { counter.DisabledCancelCommand++; } reentrancyContainer.CapturedController = controller; } foreach (var args in GetTestArgs(reentrancyContainer, Execute, ExecuteWithCancellationToken, ExecuteWithController)) { yield return(args); } Assert.AreEqual(reentrancyContainer.ExecutionCount, counter.Execution); Assert.AreEqual(counter.Execution, counter.RunningExecution); Assert.AreEqual(counter.Execution, counter.CancelCommand); Assert.AreEqual(counter.Execution, counter.IsCancelCommandEnabled); Assert.AreEqual(counter.Execution, counter.Progress); Assert.AreEqual(counter.Execution, counter.CanExecuteWhileExecuting); Assert.AreEqual(reentrancyContainer.ExecutionCount / 3 * 2, counter.CanBeCanceled); Assert.AreEqual(counter.CanBeCanceled, counter.CancellationRequest); Assert.AreEqual(reentrancyContainer.ExecutionCount / 3, counter.Controller); Assert.AreEqual(counter.Controller, counter.SameRunningExecution); Assert.AreEqual(counter.Controller, counter.ProgressReporting); Assert.AreEqual(counter.Controller, counter.ProgressReportingAsIProgress); Assert.AreEqual(counter.Controller, counter.DisabledCancelCommand); }