Ejemplo n.º 1
0
        public void itShouldRunEnabledCommandsOnly()
        {
            var commands = new List <BaseCommand>();
            var command1 = new MockSucceedingCommand();

            command1.Enabled = true;
            commands.Add(command1);

            var command2 = new MockSucceedingCommand();

            command2.Enabled = false;
            commands.Add(command2);

            var runner = new CommandsRunner(commands);
            var result = runner.Run();

            Assert.IsTrue(result);
            Assert.IsTrue(runner.WasCommandStarted(command1));
            Assert.IsFalse(runner.WasCommandStarted(command2));
        }
Ejemplo n.º 2
0
        public void itShouldSupportCancellation()
        {
            var commands = new List <BaseCommand>();
            var command1 = new MockSucceedingCommand();

            commands.Add(command1);

            var command2 = new MockSleepingCommand(2000);

            commands.Add(command2);

            var command3 = new MockSucceedingCommand();

            commands.Add(command3);

            var  runner = new CommandsRunner(commands);
            bool result = true;

            ThreadStart starter = new ThreadStart(() => {
                result = runner.Run();
            });

            var thread = new Thread(starter);

            thread.Start();
            // Sleep to give time for command1 to succeed.
            Thread.Sleep(500);
            // Cancel before command2 completes
            runner.Cancel();

            thread.Join();

            // Cancelled means failed result
            Assert.IsFalse(result);
            Assert.IsTrue(runner.WasCommandStarted(command1));
            // Cancellation should trigger Undo
            Assert.IsTrue(runner.Reports.Any(r => r.Reporter == command1 && r.ReportType.IsUndoReport()));
            Assert.IsTrue(runner.WasCommandStarted(command2));
            // Cancellation should not run next command in the chain.
            Assert.IsFalse(runner.WasCommandStarted(command3));
        }
Ejemplo n.º 3
0
        public void itShouldSetWasCommandStartedOnRanCommandsOnly()
        {
            var commands = new List <BaseCommand>();
            var command1 = new MockSucceedingCommand();

            commands.Add(command1);

            var failingCmd = new MockFailingCommand();

            commands.Add(failingCmd);

            var command2 = new MockUndoableCommand();

            commands.Add(command2);

            var runner = new CommandsRunner(commands);
            var result = runner.Run();

            Assert.IsFalse(result);
            Assert.IsTrue(runner.WasCommandStarted(command1));
            Assert.IsTrue(runner.WasCommandStarted(failingCmd));
            Assert.IsFalse(runner.WasCommandStarted(command2));
        }
Ejemplo n.º 4
0
        public void itShouldCallCleanupOfInvokedCommandsOnly()
        {
            var commands = new List <BaseCommand>();
            var command1 = new MockCommandWithCleanup();

            commands.Add(command1);
            var command2 = new MockFailingCommand();

            commands.Add(command2);
            var command3 = new MockCommandWithCleanup();

            commands.Add(command3);

            var runner = new CommandsRunner(commands);
            var result = runner.Run();

            Assert.IsFalse(result);
            Assert.IsTrue(runner.WasCommandStarted(command1));
            Assert.IsTrue(runner.Reports.Any(r => r.ReportType.IsCleanupReport() && r.Reporter.Id == command1.Id));


            Assert.IsTrue(!runner.WasCommandStarted(command3));
            Assert.IsFalse(runner.Reports.Any(r => r.ReportType.IsCleanupReport() && r.Reporter.Id == command3.Id));
        }
Ejemplo n.º 5
0
        public void itShouldDiscontinueWhenCommandFails()
        {
            var commands       = new List <BaseCommand>();
            var successfulCmd1 = new MockSucceedingCommand();

            commands.Add(successfulCmd1);
            var failingCmd = new MockFailingCommand();

            commands.Add(failingCmd);
            var successfulCmd2 = new MockSucceedingCommand();

            commands.Add(successfulCmd2);

            var runner = new CommandsRunner(commands);
            var result = runner.Run();

            Assert.IsFalse(result);

            // There should be no report from successfulCmd2 whatsoever because of failingCmd.
            Assert.IsFalse(runner.WasCommandStarted(successfulCmd2));
        }