/// <summary>
        /// Configure synchronous cancellation for the given command
        /// </summary>
        public static CommandBuilderContext <TArgument> WithCancellation <TArgument>(this CommandBuilderContext <TArgument> context)
        {
            ValdiateContextForNull(context);

            context.CancelCommand = new CancelCommand(context.CommandManager);
            return(context);
        }
Exemple #2
0
        public void Build_ShouldThrowForNullBusyStack()
        {
            var commandManager = Utils.GetTestCommandManager();
            var context        = new CommandBuilderContext <object>(commandManager, Utils.GetTestExceptionHandler(), Utils.TestBusyStackFactory, Utils.TestExecute, Utils.TestCanExecute);

            Assert.Throws <ArgumentNullException>(() => context.WithBusyNotification(null));
        }
Exemple #3
0
        public void Ctor_ShouldInitializeDependencyProperties()
        {
            var commandManager = Utils.GetTestCommandManager();
            var context        = new CommandBuilderContext <object>(commandManager, Utils.GetTestExceptionHandler(), Utils.TestBusyStackFactory, Utils.TestExecute, Utils.TestCanExecute);

            Assert.IsNotNull(context.CommandManager);
            Assert.AreEqual(commandManager, context.CommandManager);
        }
Exemple #4
0
        public void Build_ShouldCreateValidCommandInstance()
        {
            var commandManager = Utils.GetTestCommandManager();
            var context        = new CommandBuilderContext <object>(commandManager, Utils.GetTestExceptionHandler(), Utils.TestBusyStackFactory, Utils.TestExecute, Utils.TestCanExecute);

            var command = context.Build();

            Assert.IsNotNull(command);
        }
Exemple #5
0
        public void Ctor_ShouldNotInitializeAllProperties()
        {
            var commandManager = Utils.GetTestCommandManager();
            var context        = new CommandBuilderContext <object>(commandManager, Utils.GetTestExceptionHandler(), Utils.TestBusyStackFactory, Utils.TestExecute, Utils.TestCanExecute);

            // should not be initialized by default, since they represent optional features
            Assert.IsNull(context.BusyStack);
            Assert.IsNull(context.CancelCommand);
        }
Exemple #6
0
        public void Build_ShouldAddSingleExecutionDecorator()
        {
            var commandManager = Utils.GetTestCommandManager();
            var command        = new CommandBuilderContext <object>(commandManager, Utils.GetTestExceptionHandler(), Utils.TestBusyStackFactory, Utils.TestExecute, Utils.TestCanExecute)
                                 .WithSingleExecution()
                                 .Build();

            Assert.IsInstanceOf <SequentialAsyncCommandDecorator>(command);
        }
Exemple #7
0
        public void Build_ShouldAddCancellationSupport()
        {
            var commandManager = Utils.GetTestCommandManager();
            var command        = new CommandBuilderContext <object>(commandManager, Utils.GetTestExceptionHandler(), Utils.TestBusyStackFactory, Utils.TestExecute, Utils.TestCanExecute)
                                 .WithCancellation()
                                 .Build();

            Assert.IsNotNull(command.CancelCommand);
            Assert.IsInstanceOf <CancelCommand>(command.CancelCommand);
        }
Exemple #8
0
        public void Build_ShouldAssignBusyStack()
        {
            var commandManager  = Utils.GetTestCommandManager();
            var customBusyStack = NSubstitute.Substitute.For <IBusyStack>();
            var context         = new CommandBuilderContext <object>(commandManager, Utils.GetTestExceptionHandler(), Utils.TestBusyStackFactory, Utils.TestExecute, Utils.TestCanExecute);

            context.WithBusyNotification(customBusyStack);

            Assert.IsInstanceOf <IBusyStack>(context.BusyStack);
        }
Exemple #9
0
        public void Build_ShouldCreateDefaultCommandInstance()
        {
            var commandManager = Utils.GetTestCommandManager();
            var context        = new CommandBuilderContext <object>(commandManager, Utils.GetTestExceptionHandler(), Utils.TestBusyStackFactory, Utils.TestExecute, Utils.TestCanExecute);

            var command = context.Build();

            Assert.IsNotNull(command.CancelCommand);
            Assert.IsInstanceOf <NoCancellationCommand>(command.CancelCommand);
            Assert.AreEqual(command.IsBusy, false);
            Assert.AreEqual(command.Completion, Task.CompletedTask);
        }
Exemple #10
0
        public void Build_ShouldCallAllDecorators()
        {
            var commandManager = Utils.GetTestCommandManager();
            var context        = new CommandBuilderContext <object>(commandManager, Utils.GetTestExceptionHandler(), Utils.TestBusyStackFactory, Utils.TestExecute, Utils.TestCanExecute);

            var count          = 0;
            var decoratorCount = 20;

            for (var i = 0; i < decoratorCount; i++)
            {
                context.AddDecorator(DecoratorFactory);
            }

            var command = context.Build();

            Assert.AreEqual(decoratorCount, count);

            ConcurrentCommandBase DecoratorFactory(ConcurrentCommandBase command)
            {
                count++;
                return(command);
            }
        }
Exemple #11
0
        public void Build_ShouldAddAllDecoratorsInCorrectOrder()
        {
            var commandManager = Utils.GetTestCommandManager();
            var context        = new CommandBuilderContext <object>(commandManager, Utils.GetTestExceptionHandler(), Utils.TestBusyStackFactory, Utils.TestExecute, Utils.TestCanExecute);

            var count          = 0;
            var decoratorCount = 3;

            context.AddDecorator(DecoratorFactory1);
            context.AddDecorator(DecoratorFactory2);
            context.AddDecorator(DecoratorFactory3);

            var command = context.Build();

            Assert.AreEqual(decoratorCount, count);

            ConcurrentCommandBase DecoratorFactory1(ConcurrentCommandBase command)
            {
                Assert.AreEqual(0, count);
                count++;
                return(command);
            }

            ConcurrentCommandBase DecoratorFactory2(ConcurrentCommandBase command)
            {
                Assert.AreEqual(1, count);
                count++;
                return(command);
            }

            ConcurrentCommandBase DecoratorFactory3(ConcurrentCommandBase command)
            {
                Assert.AreEqual(2, count);
                count++;
                return(command);
            }
        }
 /// <summary>
 /// Provide a custom implementation of <see cref="ICancelCommand"/>
 /// </summary>
 /// <exception cref="ArgumentNullException"></exception>
 public static CommandBuilderContext <TArgument> WithCustomCancellation <TArgument>(this CommandBuilderContext <TArgument> context, in ICancelCommand cancelCommand)
        /// <summary>
        /// Configure task based cancellation for the given command
        /// </summary>
        public static CommandBuilderContext <TArgument> WithAsyncCancellation <TArgument>(this CommandBuilderContext <TArgument> context)
        {
            ValdiateContextForNull(context);

            context.CancelCommand = new ConcurrentCancelCommand(context.CommandManager, context.ExceptionHandler);
            return(context);
        }
Exemple #14
0
 public void Ctor_DoesntThrow()
 {
     var _ = new CommandBuilderContext <object>(Utils.GetTestCommandManager(), Utils.GetTestExceptionHandler(), Utils.TestBusyStackFactory, Utils.TestExecute, Utils.TestCanExecute);
 }