public void skip_backwards_command_is_disabled_if_on_first_exercise()
        {
            var action = new ActionMock(MockBehavior.Loose);

            action
            .When(x => x.Execute(It.IsAny <ExecutionContext>()))
            .Return <ExecutionContext>(
                ec =>
            {
                ec.IsPaused = true;
                return(ec.WaitWhilePaused());
            });

            var sut = new ExerciseProgramViewModelBuilder()
                      .WithModel(new ExerciseProgramBuilder()
                                 .WithExercise(new ExerciseBuilder()
                                               .WithBeforeExerciseAction(action)))
                      .Build();

            sut
            .StartCommand
            .Execute()
            .Subscribe();

            Assert.False(sut.SkipBackwardsCommand.CanExecute.FirstAsync().Wait());
        }
Esempio n. 2
0
        public void execute_logs_any_error_raised_by_the_inner_action()
        {
            var waitHandle    = new ManualResetEventSlim();
            var logger        = new LoggerMock(MockBehavior.Loose);
            var loggerService = new LoggerServiceMock(MockBehavior.Loose);
            var action        = new ActionMock(MockBehavior.Loose);

            logger
            .When(x => x.Log(LogLevel.Error, It.IsAny <string>()))
            .Do(waitHandle.Set);

            loggerService
            .When(x => x.GetLogger(It.IsAny <Type>()))
            .Return(logger);

            action
            .When(x => x.Execute(It.IsAny <ExecutionContext>()))
            .Return(Observable.Throw <Unit>(new InvalidOperationException("Something bad happened")));

            var sut = new DoNotAwaitActionBuilder()
                      .WithLoggerService(loggerService)
                      .WithInnerAction(action)
                      .Build();

            sut.Execute(new ExecutionContext());

            Assert.True(waitHandle.Wait(TimeSpan.FromSeconds(3)));
        }
        public void execute_executes_each_child_action_in_order()
        {
            var childCount          = 10;
            var childExecutionOrder = new int[childCount];
            var executionOrder      = 0;
            var childActions        = Enumerable
                                      .Range(0, childCount)
                                      .Select(
                (i, _) =>
            {
                var childAction = new ActionMock(MockBehavior.Loose);
                childAction
                .When(x => x.Execute(It.IsAny <ExecutionContext>()))
                .Do(() => childExecutionOrder[i] = Interlocked.Increment(ref executionOrder))
                .Return(Observable.Return(Unit.Default));
                return(childAction);
            })
                                      .ToList();

            var sut = new SequenceActionBuilder()
                      .WithChildren(childActions)
                      .Build();

            using (var context = new ExecutionContext())
            {
                sut.Execute(context).Subscribe();

                for (var i = 0; i < childExecutionOrder.Length; ++i)
                {
                    Assert.Equal(i + 1, childExecutionOrder[i]);
                }
            }
        }
        public void execute_executes_each_child_action()
        {
            var action1 = new ActionMock(MockBehavior.Loose);
            var action2 = new ActionMock(MockBehavior.Loose);
            var action3 = new ActionMock(MockBehavior.Loose);
            var sut     = new SequenceActionBuilder()
                          .WithChild(action1)
                          .WithChild(action2)
                          .WithChild(action3)
                          .Build();

            using (var context = new ExecutionContext())
            {
                sut.Execute(context).Subscribe();

                action1
                .Verify(x => x.Execute(context))
                .WasCalledExactlyOnce();

                action2
                .Verify(x => x.Execute(context))
                .WasCalledExactlyOnce();

                action3
                .Verify(x => x.Execute(context))
                .WasCalledExactlyOnce();
            }
        }
        public void skip_backwards_command_is_enabled_if_sufficient_progress_has_been_made_through_first_exercise()
        {
            var action = new ActionMock(MockBehavior.Loose);

            action
            .When(x => x.Execute(It.IsAny <ExecutionContext>()))
            .Return <ExecutionContext>(
                ec =>
            {
                ec.AddProgress(TimeSpan.FromSeconds(1));
                ec.IsPaused = true;
                return(ec.WaitWhilePaused());
            });

            var sut = new ExerciseProgramViewModelBuilder()
                      .WithModel(new ExerciseProgramBuilder()
                                 .WithExercise(new ExerciseBuilder()
                                               .WithBeforeExerciseAction(action)))
                      .Build();

            sut
            .StartCommand
            .Execute()
            .Subscribe();

            Assert.True(sut.SkipBackwardsCommand.CanExecute.FirstAsync().Wait());
        }
        public void execute_context_can_be_cancelled_by_longest_child_action()
        {
            var action1 = new ActionMock(MockBehavior.Loose);
            var action2 = new ActionMock(MockBehavior.Loose);

            action1
            .When(x => x.Duration)
            .Return(TimeSpan.FromSeconds(13));

            action2
            .When(x => x.Duration)
            .Return(TimeSpan.FromSeconds(8));

            action1
            .When(x => x.Execute(It.IsAny <ExecutionContext>()))
            .Do <ExecutionContext>(ec => ec.Cancel())
            .Return(Observable.Return(Unit.Default));

            var sut = new ParallelActionBuilder()
                      .WithChild(action1)
                      .WithChild(action2)
                      .Build();

            using (var context = new ExecutionContext())
            {
                sut.Execute(context);

                // can't assume certain actions did not execute because actions run in parallel, but we can check the context is cancelled
                Assert.True(context.IsCancelled);
            }
        }
        public void execute_async_stops_executing_if_the_context_is_cancelled()
        {
            var action1 = new ActionMock(MockBehavior.Loose);
            var action2 = new ActionMock(MockBehavior.Loose);
            var action3 = new ActionMock(MockBehavior.Loose);
            var sut     = new SequenceActionBuilder()
                          .AddChild(action1)
                          .AddChild(action2)
                          .AddChild(action3)
                          .Build();

            using (var context = new ExecutionContext())
            {
                action2
                .When(x => x.ExecuteAsync(It.IsAny <ExecutionContext>()))
                .Do(() => context.Cancel())
                .Return(Observable.Return(Unit.Default));

                Assert.ThrowsAsync <OperationCanceledException>(async() => await sut.ExecuteAsync(context));

                action1
                .Verify(x => x.ExecuteAsync(context))
                .WasCalledExactlyOnce();

                action2
                .Verify(x => x.ExecuteAsync(context))
                .WasCalledExactlyOnce();

                action3
                .Verify(x => x.ExecuteAsync(context))
                .WasNotCalled();
            }
        }
        public void progress_is_calculated_based_on_duration_and_progress_time_span(int durationInMs, int progressInMs, double expectedProgress)
        {
            var scheduler = new TestScheduler();
            var action    = new ActionMock(MockBehavior.Loose);

            action
            .When(x => x.Duration)
            .Return(TimeSpan.FromMilliseconds(durationInMs));

            var model = new ExerciseBuilder()
                        .WithBeforeExerciseAction(action)
                        .Build();

            var executionContext = new ExecutionContext();
            var sut = new ExerciseViewModelBuilder()
                      .WithSchedulerService(scheduler)
                      .WithExecutionContext(executionContext)
                      .WithModel(model)
                      .Build();

            executionContext.SetCurrentExercise(model);
            executionContext.AddProgress(TimeSpan.FromMilliseconds(progressInMs));

            scheduler.AdvanceMinimal();

            Assert.Equal(expectedProgress, sut.Progress);
        }
Esempio n. 9
0
        public void execute_context_can_be_paused_by_child_action_that_is_not_the_longest()
        {
            var action1 = new ActionMock(MockBehavior.Loose);
            var action2 = new ActionMock(MockBehavior.Loose);

            action1
            .When(x => x.Duration)
            .Return(TimeSpan.FromSeconds(8));

            action2
            .When(x => x.Duration)
            .Return(TimeSpan.FromSeconds(13));

            action1
            .When(x => x.Execute(It.IsAny <ExecutionContext>()))
            .Do <ExecutionContext>(ec => ec.IsPaused = true)
            .Return(Observables.Unit);

            var sut = new ParallelActionBuilder()
                      .WithChild(action1)
                      .WithChild(action2)
                      .Build();
            var context = new ExecutionContext();

            sut.Execute(context);

            // can't assume certain actions did not execute because actions run in parallel, but we can check the context is paused
            Assert.True(context.IsPaused);
        }
Esempio n. 10
0
        public void Solve_should_solve_solvable_problem()
        {
            var action1      = new ActionMock("action 1");
            var action2      = new ActionMock("action 2");
            var initialState = new StateMock("initial state");
            var state1       = new StateMock("state 1");
            var state2       = new StateMock("state 2");
            var problem      = A.Fake <ISearchProblem <StateMock, ActionMock> >();

            A.CallTo(() => problem.InitialState).Returns(initialState);
            A.CallTo(() => problem.GetActions(initialState)).Returns(new [] { action1 });
            A.CallTo(() => problem.DoAction(initialState, action1)).Returns(state1);
            A.CallTo(() => problem.GetActions(state1)).Returns(new [] { action2 });
            A.CallTo(() => problem.DoAction(state1, action2)).Returns(state2);
            A.CallTo(() => problem.IsGoal(state2)).Returns(true);

            var bfs = new BreadthFirstSearch <StateMock, ActionMock>(problem);

            bfs.Solve();

            Assert.IsTrue(bfs.IsFinished);
            Assert.IsTrue(bfs.IsSolved);

            var solution = bfs.GetSolutionTo(state2).ToList();

            Assert.AreEqual(new[] { action1, action2 }, solution);
        }
        public void duration_is_calculated_as_the_sum_of_child_durations()
        {
            var action1 = new ActionMock();
            var action2 = new ActionMock();
            var action3 = new ActionMock();

            action1
            .When(x => x.Duration)
            .Return(TimeSpan.FromSeconds(10));

            action2
            .When(x => x.Duration)
            .Return(TimeSpan.FromSeconds(1));

            action3
            .When(x => x.Duration)
            .Return(TimeSpan.FromSeconds(7));

            var sut = new SequenceActionBuilder()
                      .WithChild(action1)
                      .WithChild(action2)
                      .WithChild(action3)
                      .Build();

            Assert.Equal(TimeSpan.FromSeconds(18), sut.Duration);
        }
        public void execute_executes_each_exercise()
        {
            var action1 = new ActionMock(MockBehavior.Loose);
            var action2 = new ActionMock(MockBehavior.Loose);

            var sut = new ExerciseProgramBuilder()
                      .WithExercise(new ExerciseBuilder()
                                    .WithBeforeExerciseAction(action1))
                      .WithExercise(new ExerciseBuilder()
                                    .WithBeforeExerciseAction(action2))
                      .Build();
            var executionContext = new ExecutionContext();

            sut
            .Execute(executionContext)
            .Subscribe();

            action1
            .Verify(x => x.Execute(executionContext))
            .WasCalledExactlyOnce();

            action2
            .Verify(x => x.Execute(executionContext))
            .WasCalledExactlyOnce();
        }
Esempio n. 13
0
        public void execute_async_executes_each_exercise()
        {
            var action1 = new ActionMock(MockBehavior.Loose);
            var action2 = new ActionMock(MockBehavior.Loose);

            var sut = new ExerciseProgramBuilder()
                      .AddExercise(new ExerciseBuilder()
                                   .WithBeforeExerciseAction(action1))
                      .AddExercise(new ExerciseBuilder()
                                   .WithBeforeExerciseAction(action2))
                      .Build();

            using (var executionContext = new ExecutionContext())
            {
                sut.ExecuteAsync(executionContext);

                action1
                .Verify(x => x.ExecuteAsync(executionContext))
                .WasCalledExactlyOnce();

                action2
                .Verify(x => x.ExecuteAsync(executionContext))
                .WasCalledExactlyOnce();
            }
        }
        public void is_pause_visible_cycles_correctly_if_start_command_is_executed()
        {
            var scheduler = new TestSchedulerService();
            var action    = new ActionMock(MockBehavior.Loose);

            action
            .When(x => x.Duration)
            .Return(TimeSpan.FromMinutes(1));
            action
            .When(x => x.ExecuteAsync(It.IsAny <ExecutionContext>()))
            .Return <ExecutionContext>(
                ec =>
                Observable
                .Return(Unit.Default)
                .Do(_ => { })
                .Delay(TimeSpan.FromMinutes(1), scheduler.DefaultScheduler)
                .Do(_ => ec.AddProgress(TimeSpan.FromMinutes(1))));
            var sut = new ExerciseProgramViewModelBuilder()
                      .WithModel(
                new ExerciseProgramBuilder()
                .AddExercise(
                    new ExerciseBuilder()
                    .WithBeforeExerciseAction(action)))
                      .WithSchedulerService(scheduler)
                      .Build();

            Assert.False(sut.IsPauseVisible);

            sut.StartCommand.Execute(null);
            scheduler.AdvanceMinimal();
            Assert.True(sut.IsPauseVisible);

            scheduler.AdvanceBy(TimeSpan.FromMinutes(10));
            Assert.False(sut.IsPauseVisible);
        }
        public void skip_backwards_command_is_disabled_if_on_first_exercise()
        {
            var scheduler = new TestSchedulerService();
            var action    = new ActionMock(MockBehavior.Loose);

            action
            .When(x => x.ExecuteAsync(It.IsAny <ExecutionContext>()))
            .Return <ExecutionContext>(
                ec =>
            {
                ec.IsPaused = true;
                return(ec.WaitWhilePausedAsync());
            });

            var sut = new ExerciseProgramViewModelBuilder()
                      .WithModel(new ExerciseProgramBuilder()
                                 .AddExercise(new ExerciseBuilder()
                                              .WithBeforeExerciseAction(action)))
                      .WithSchedulerService(scheduler)
                      .Build();

            sut.StartCommand.Execute(null);
            scheduler.AdvanceMinimal();

            Assert.False(sut.SkipBackwardsCommand.CanExecute(null));
        }
Esempio n. 16
0
        public void duration_is_the_maximum_duration_from_all_children()
        {
            var action1 = new ActionMock();
            var action2 = new ActionMock();
            var action3 = new ActionMock();
            var action4 = new ActionMock();

            action1
            .When(x => x.Duration)
            .Return(TimeSpan.FromMilliseconds(150));

            action2
            .When(x => x.Duration)
            .Return(TimeSpan.FromSeconds(3));

            action3
            .When(x => x.Duration)
            .Return(TimeSpan.FromSeconds(5));

            action4
            .When(x => x.Duration)
            .Return(TimeSpan.FromMilliseconds(550));

            var sut = new ParallelActionBuilder()
                      .WithChild(action1)
                      .WithChild(action2)
                      .WithChild(action3)
                      .WithChild(action4)
                      .Build();

            Assert.Equal(TimeSpan.FromSeconds(5), sut.Duration);
        }
Esempio n. 17
0
        public void GivenNonDeterministicAction_ShouldReachGoal()
        {
            var state1 = new StateMock("state 1");
            var state2 = new StateMock("state 2");
            var goalState = new StateMock("goal state");
            var action = new ActionMock("action");

            A.CallTo(() => _problem.IsGoal(goalState)).Returns(true);
            // available actions
            A.CallTo(() => _problem.GetActions(_initialState)).Returns(new [] {action});
            A.CallTo(() => _problem.GetActions(state1)).Returns(new [] {action});
            A.CallTo(() => _problem.GetActions(state2)).Returns(new [] {action});
            // action outcomes
            A.CallTo(() => _problem.DoAction(_initialState, action)).Returns(new[]
            {
                state1,
                state2
            });
            A.CallTo(() => _problem.DoAction(state1, action)).Returns(new[] {goalState});
            A.CallTo(() => _problem.DoAction(state2, action)).Returns(new[] {goalState});

            // act
            var solution = CreateSearchAlgorithm(_problem, _initialState).GetSolution();
            
            // assert
            Assert.AreEqual(action, solution.NextAction(_initialState));
            Assert.AreEqual(action, solution.NextAction(state1));
        }
        public void execute_ensures_progress_of_child_actions_does_not_compound()
        {
            var action1 = new ActionMock();
            var action2 = new ActionMock();
            var action3 = new ActionMock();
            var action4 = new ActionMock();

            action1
            .When(x => x.Duration)
            .Return(TimeSpan.FromSeconds(1));

            action2
            .When(x => x.Duration)
            .Return(TimeSpan.FromSeconds(1));

            action3
            .When(x => x.Duration)
            .Return(TimeSpan.FromSeconds(2));

            action4
            .When(x => x.Duration)
            .Return(TimeSpan.FromSeconds(1));

            action1
            .When(x => x.Execute(It.IsAny <ExecutionContext>()))
            .Do <ExecutionContext>(ec => ec.AddProgress(action1.Duration))
            .Return(Observable.Return(Unit.Default));

            action2
            .When(x => x.Execute(It.IsAny <ExecutionContext>()))
            .Do <ExecutionContext>(ec => ec.AddProgress(action2.Duration))
            .Return(Observable.Return(Unit.Default));

            action3
            .When(x => x.Execute(It.IsAny <ExecutionContext>()))
            .Do <ExecutionContext>(ec => ec.AddProgress(action3.Duration))
            .Return(Observable.Return(Unit.Default));

            action4
            .When(x => x.Execute(It.IsAny <ExecutionContext>()))
            .Do <ExecutionContext>(ec => ec.AddProgress(action4.Duration))
            .Return(Observable.Return(Unit.Default));

            var sut = new ParallelActionBuilder()
                      .WithChild(action1)
                      .WithChild(action2)
                      .WithChild(action3)
                      .WithChild(action4)
                      .Build();

            using (var context = new ExecutionContext())
            {
                sut.Execute(context);

                Assert.Equal(TimeSpan.FromSeconds(2), context.Progress);
            }
        }
        public void current_exercise_reflects_that_in_the_execution_context()
        {
            var scheduler = new TestSchedulerService();
            var action    = new ActionMock();

            action
            .When(x => x.Duration)
            .Return(TimeSpan.FromSeconds(10));

            action
            .When(x => x.ExecuteAsync(It.IsAny <ExecutionContext>()))
            .Return <ExecutionContext>(
                ec =>
            {
                ec.IsPaused = true;
                return(ec.WaitWhilePausedAsync());
            });

            var exercise1 = new ExerciseBuilder()
                            .WithName("Exercise 1")
                            .WithBeforeExerciseAction(action)
                            .Build();

            var exercise2 = new ExerciseBuilder()
                            .WithName("Exercise 2")
                            .WithBeforeExerciseAction(action)
                            .Build();

            var exercise3 = new ExerciseBuilder()
                            .WithName("Exercise 3")
                            .WithBeforeExerciseAction(action)
                            .Build();

            var sut = new ExerciseProgramViewModelBuilder()
                      .WithModel(new ExerciseProgramBuilder()
                                 .AddExercise(exercise1)
                                 .AddExercise(exercise2)
                                 .AddExercise(exercise3))
                      .WithSchedulerService(scheduler)
                      .Build();

            sut.StartCommand.Execute(null);
            scheduler.AdvanceMinimal();
            Assert.Equal("Exercise 1", sut.CurrentExercise?.Name);

            sut.SkipForwardsCommand.Execute(null);
            scheduler.AdvanceMinimal();
            Assert.Equal("Exercise 2", sut.CurrentExercise?.Name);

            sut.SkipForwardsCommand.Execute(null);
            scheduler.AdvanceMinimal();
            Assert.Equal("Exercise 3", sut.CurrentExercise?.Name);

            sut.SkipBackwardsCommand.Execute(null);
            scheduler.AdvanceMinimal();
            Assert.Equal("Exercise 2", sut.CurrentExercise?.Name);
        }
        public void skip_backwards_command_restarts_the_execution_context_from_the_start_of_the_previous_exercise_if_the_current_exercise_if_only_recently_started()
        {
            var action = new ActionMock();

            action
            .When(x => x.Duration)
            .Return(TimeSpan.FromSeconds(10));

            action
            .When(x => x.Execute(It.IsAny <ExecutionContext>()))
            .Return <ExecutionContext>(
                ec =>
            {
                ec.AddProgress(TimeSpan.FromSeconds(0.5));
                ec.IsPaused = true;

                return(ec.WaitWhilePaused());
            });

            var exercise1 = new ExerciseBuilder()
                            .WithName("Exercise 1")
                            .WithBeforeExerciseAction(action)
                            .Build();

            var exercise2 = new ExerciseBuilder()
                            .WithName("Exercise 2")
                            .WithBeforeExerciseAction(action)
                            .Build();

            var sut = new ExerciseProgramViewModelBuilder()
                      .WithModel(new ExerciseProgramBuilder()
                                 .WithExercise(exercise1)
                                 .WithExercise(exercise2))
                      .Build();

            var progress = sut
                           .WhenAnyValue(x => x.ProgressTimeSpan)
                           .CreateCollection();

            // start from the second exercise
            sut
            .StartCommand
            .Execute(exercise1.Duration)
            .Subscribe();

            sut
            .SkipBackwardsCommand
            .Execute()
            .Subscribe();

            Assert.Equal(5, progress.Count);
            Assert.Equal(TimeSpan.Zero, progress[0]);
            Assert.Equal(TimeSpan.FromSeconds(10), progress[1]);
            Assert.Equal(TimeSpan.FromSeconds(10.5), progress[2]);
            Assert.Equal(TimeSpan.Zero, progress[3]);
            Assert.Equal(TimeSpan.FromSeconds(10), progress[4]);
        }
        public void execute_async_skips_actions_that_are_shorter_than_the_skip_ahead_even_if_the_context_is_paused()
        {
            var action1       = new ActionMock(MockBehavior.Loose);
            var action2       = new ActionMock(MockBehavior.Loose);
            var action3       = new ActionMock(MockBehavior.Loose);
            var eventMatcher1 = new EventMatcherMock();
            var eventMatcher2 = new EventMatcherMock();
            var eventMatcher3 = new EventMatcherMock();

            action1
            .When(x => x.Duration)
            .Return(TimeSpan.FromSeconds(10));

            action2
            .When(x => x.Duration)
            .Return(TimeSpan.FromSeconds(3));

            action3
            .When(x => x.Duration)
            .Return(TimeSpan.FromSeconds(1));

            action1
            .When(x => x.ExecuteAsync(It.IsAny <ExecutionContext>()))
            .Throw();

            action2
            .When(x => x.ExecuteAsync(It.IsAny <ExecutionContext>()))
            .Throw();

            eventMatcher1
            .When(x => x.Matches(It.IsAny <IEvent>()))
            .Return((IEvent @event) => @event is BeforeExerciseEvent);

            eventMatcher2
            .When(x => x.Matches(It.IsAny <IEvent>()))
            .Return((IEvent @event) => @event is BeforeExerciseEvent);

            eventMatcher3
            .When(x => x.Matches(It.IsAny <IEvent>()))
            .Return((IEvent @event) => @event is BeforeExerciseEvent);

            var sut = new ExerciseBuilder()
                      .AddMatcherWithAction(new MatcherWithAction(eventMatcher1, action1))
                      .AddMatcherWithAction(new MatcherWithAction(eventMatcher2, action2))
                      .AddMatcherWithAction(new MatcherWithAction(eventMatcher3, action3))
                      .Build();

            using (var executionContext = new ExecutionContext(TimeSpan.FromSeconds(13)))
            {
                executionContext.IsPaused = true;
                sut.ExecuteAsync(executionContext);

                action3
                .Verify(x => x.ExecuteAsync(executionContext))
                .WasCalledExactlyOnce();
            }
        }
        public void skip_forwards_command_is_disabled_if_on_last_exercise()
        {
            var action = new ActionMock();

            action
            .When(x => x.Duration)
            .Return(TimeSpan.FromSeconds(10));

            action
            .When(x => x.Execute(It.IsAny <ExecutionContext>()))
            .Return <ExecutionContext>(
                ec =>
            {
                ec.IsPaused = true;

                return(ec.WaitWhilePaused());
            });

            var exercise1 = new ExerciseBuilder()
                            .WithName("Exercise 1")
                            .WithBeforeExerciseAction(action)
                            .Build();

            var exercise2 = new ExerciseBuilder()
                            .WithName("Exercise 2")
                            .WithBeforeExerciseAction(action)
                            .Build();

            var sut = new ExerciseProgramViewModelBuilder()
                      .WithModel(new ExerciseProgramBuilder()
                                 .WithExercise(exercise1)
                                 .WithExercise(exercise2))
                      .Build();

            var canExecute = sut
                             .SkipForwardsCommand
                             .CanExecute
                             .CreateCollection();

            sut
            .StartCommand
            .Execute()
            .Subscribe();

            sut
            .SkipForwardsCommand
            .Execute()
            .Subscribe();

            Assert.Equal(5, canExecute.Count);
            Assert.False(canExecute[0]);
            Assert.True(canExecute[1]);
            Assert.False(canExecute[2]);
            Assert.True(canExecute[3]);
            Assert.False(canExecute[4]);
        }
Esempio n. 23
0
        public void ExecuteNotGenericWithNotNull()
        {
            bool executed = false;

            var eventMock = new ActionMock();

            eventMock.TestAction += () => executed = true;

            eventMock.InvokeAction();

            Assert.IsTrue(executed);
        }
        public void skip_forwards_command_skips_to_the_next_exercise()
        {
            var action = new ActionMock();

            action
            .When(x => x.Duration)
            .Return(TimeSpan.FromSeconds(10));

            action
            .When(x => x.Execute(It.IsAny <ExecutionContext>()))
            .Return <ExecutionContext>(
                ec =>
            {
                ec.IsPaused = true;

                return(ec.WaitWhilePaused());
            });

            var exercise1 = new ExerciseBuilder()
                            .WithName("Exercise 1")
                            .WithBeforeExerciseAction(action)
                            .Build();

            var exercise2 = new ExerciseBuilder()
                            .WithName("Exercise 2")
                            .WithBeforeExerciseAction(action)
                            .Build();

            var sut = new ExerciseProgramViewModelBuilder()
                      .WithModel(new ExerciseProgramBuilder()
                                 .WithExercise(exercise1)
                                 .WithExercise(exercise2))
                      .Build();

            var progress = sut
                           .WhenAnyValue(x => x.ProgressTimeSpan)
                           .CreateCollection();

            sut
            .StartCommand
            .Execute()
            .Subscribe();

            sut
            .SkipForwardsCommand
            .Execute()
            .Subscribe();

            Assert.Equal(2, progress.Count);
            Assert.Equal(TimeSpan.Zero, progress[0]);
            Assert.Equal(TimeSpan.FromSeconds(10), progress[1]);
        }
        public void execute_executes_each_child_action()
        {
            var action1 = new ActionMock(MockBehavior.Loose);
            var action2 = new ActionMock(MockBehavior.Loose);
            var action3 = new ActionMock(MockBehavior.Loose);
            var action4 = new ActionMock(MockBehavior.Loose);

            action1
            .When(x => x.Duration)
            .Return(TimeSpan.FromSeconds(1));

            action2
            .When(x => x.Duration)
            .Return(TimeSpan.FromSeconds(2));

            action3
            .When(x => x.Duration)
            .Return(TimeSpan.FromSeconds(3));

            action4
            .When(x => x.Duration)
            .Return(TimeSpan.FromSeconds(4));

            var sut = new ParallelActionBuilder()
                      .WithChild(action1)
                      .WithChild(action2)
                      .WithChild(action3)
                      .WithChild(action4)
                      .Build();

            using (var context = new ExecutionContext())
            {
                sut.Execute(context);

                action1
                .Verify(x => x.Execute(It.IsAny <ExecutionContext>()))
                .WasCalledExactlyOnce();

                action2
                .Verify(x => x.Execute(It.IsAny <ExecutionContext>()))
                .WasCalledExactlyOnce();

                action3
                .Verify(x => x.Execute(It.IsAny <ExecutionContext>()))
                .WasCalledExactlyOnce();

                action4
                .Verify(x => x.Execute(It.IsAny <ExecutionContext>()))
                .WasCalledExactlyOnce();
            }
        }
Esempio n. 26
0
        public void duration_always_returns_zero_regardless_of_inner_action_duration()
        {
            var action = new ActionMock();

            action
            .When(x => x.Duration)
            .Return(TimeSpan.FromSeconds(3));

            var sut = new DoNotAwaitActionBuilder()
                      .WithInnerAction(action)
                      .Build();

            Assert.Equal(TimeSpan.Zero, sut.Duration);
        }
        public void duration_returns_duration_in_model(int durationInMs)
        {
            var action = new ActionMock(MockBehavior.Loose);

            action
            .When(x => x.Duration)
            .Return(TimeSpan.FromMilliseconds(durationInMs));

            var sut = new ExerciseViewModelBuilder()
                      .WithModel(new ExerciseBuilder()
                                 .WithBeforeExerciseAction(action))
                      .Build();

            Assert.Equal(TimeSpan.FromMilliseconds(durationInMs), sut.Duration);
        }
Esempio n. 28
0
        public void GivenOnlyActionLeadsToGoal_FirstSolutionActionShouldGoToGoal()
        {
            var goalState = new StateMock("goal state");
            var goToGoal = new ActionMock("go to goal");

            A.CallTo(() => _problem.IsGoal(_initialState)).Returns(false);
            A.CallTo(() => _problem.IsGoal(goalState)).Returns(true);
            A.CallTo(() => _problem.GetActions(_initialState)).Returns(new [] {goToGoal});
            A.CallTo(() => _problem.DoAction(_initialState, goToGoal)).Returns(new[] {goalState});
            
            // act
            var solution = CreateSearchAlgorithm(_problem, _initialState).GetSolution();
            
            // assert
            Assert.AreEqual(goToGoal, solution.NextAction(_initialState));
        }
        public void execute_async_executes_all_appropriate_actions()
        {
            var action1       = new ActionMock(MockBehavior.Loose);
            var action2       = new ActionMock(MockBehavior.Loose);
            var action3       = new ActionMock(MockBehavior.Loose);
            var eventMatcher1 = new EventMatcherMock();
            var eventMatcher2 = new EventMatcherMock();
            var eventMatcher3 = new EventMatcherMock();

            eventMatcher1
            .When(x => x.Matches(It.IsAny <IEvent>()))
            .Return((IEvent @event) => @event is BeforeExerciseEvent);

            eventMatcher2
            .When(x => x.Matches(It.IsAny <IEvent>()))
            .Return((IEvent @event) => @event is DuringRepetitionEvent);

            eventMatcher3
            .When(x => x.Matches(It.IsAny <IEvent>()))
            .Return((IEvent @event) => @event is AfterSetEvent);

            var sut = new ExerciseBuilder()
                      .WithSetCount(2)
                      .WithRepetitionCount(3)
                      .AddMatcherWithAction(new MatcherWithAction(eventMatcher1, action1))
                      .AddMatcherWithAction(new MatcherWithAction(eventMatcher2, action2))
                      .AddMatcherWithAction(new MatcherWithAction(eventMatcher3, action3))
                      .Build();

            using (var executionContext = new ExecutionContext())
            {
                sut.ExecuteAsync(executionContext);

                action1
                .Verify(x => x.ExecuteAsync(executionContext))
                .WasCalledExactlyOnce();

                action2
                .Verify(x => x.ExecuteAsync(executionContext))
                .WasCalledExactly(times: 6);

                action3
                .Verify(x => x.ExecuteAsync(executionContext))
                .WasCalledExactly(times: 2);
            }
        }
Esempio n. 30
0
        public void GivenOneActionRepeatsAndOneGoesToGoal_ShouldReturnPlanToGoal()
        {
            var goalState = new StateMock("goal state");
            var repeat = new ActionMock("repeat");
            var goToGoal = new ActionMock("go to goal");

            A.CallTo(() => _problem.IsGoal(_initialState)).Returns(false);
            A.CallTo(() => _problem.IsGoal(goalState)).Returns(true);
            A.CallTo(() => _problem.GetActions(_initialState)).Returns(new [] {repeat, goToGoal});
            A.CallTo(() => _problem.DoAction(_initialState, repeat)).Returns(new[] {_initialState});
            A.CallTo(() => _problem.DoAction(_initialState, goToGoal)).Returns(new[] {goalState});
            
            // act
            var solution = CreateSearchAlgorithm(_problem, _initialState).GetSolution();
            
            // assert
            Assert.AreEqual(goToGoal, solution.NextAction(_initialState));
        }
Esempio n. 31
0
 protected void StartPollingApp(string aAppName, string aUrl, DateTime aDateTime, out Mock<IAction> aAvailableActionMock, out Mock<IAction> aFailedActionMock)
 {
     var availableAction = new ActionMock();
     var failedAction = new ActionMock();
     iPollManager.StartPollingApp(aAppName, aUrl, aDateTime, availableAction.Action, failedAction.Action);
     aAvailableActionMock = availableAction.Mock;
     aFailedActionMock = failedAction.Mock;
 }