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 execute_skips_child_actions_that_are_shorter_than_the_skip_ahead_even_if_the_context_is_paused()
        {
            var action1 = new ActionMock();
            var action2 = new ActionMock();
            var action3 = new ActionMock(MockBehavior.Loose);

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

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

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

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

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

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

            using (var context = new ExecutionContext(TimeSpan.FromSeconds(11)))
            {
                context.IsPaused = true;
                sut.Execute(context).Subscribe();

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

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

            var sut = new ExerciseBuilder()
                .AddMatcherWithAction(new MatcherWithAction(eventMatcher, action))
                .Build();

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

                action
                    .Verify(x => x.ExecuteAsync(executionContext))
                    .WasCalledExactlyOnce();
            }
        }
        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 execute_async_skips_exercises_that_are_shorter_than_the_skip_ahead()
        {
            var action1 = new ActionMock(MockBehavior.Loose);
            var action2 = new ActionMock(MockBehavior.Loose);
            var action3 = new ActionMock(MockBehavior.Loose);

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

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

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

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

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

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

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

                action3
                    .Verify(x => x.ExecuteAsync(executionContext))
                    .WasCalledExactlyOnce();
            }
        }
        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);
            }
        }
        public void execute_async_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()
                .AddChild(action1)
                .AddChild(action2)
                .AddChild(action3)
                .AddChild(action4)
                .Build();

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

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

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

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

                action4
                    .Verify(x => x.ExecuteAsync(It.IsAny<ExecutionContext>()))
                    .WasCalledExactlyOnce();
            }
        }
        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 execute_async_ensures_progress_of_child_actions_does_not_compound_when_skipping()
        {
            var action1 = new ActionMock();
            var action2 = new ActionMock();
            var action3 = new ActionMock();
            var action4 = new ActionMock();

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

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

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

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

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

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

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

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

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

            using (var context = new ExecutionContext(TimeSpan.FromSeconds(5)))
            {
                sut.ExecuteAsync(context);

                Assert.Equal(TimeSpan.FromSeconds(10), context.Progress);

                action1
                    .Verify(x => x.ExecuteAsync(It.IsAny<ExecutionContext>()))
                    .WasNotCalled();

                action4
                    .Verify(x => x.ExecuteAsync(It.IsAny<ExecutionContext>()))
                    .WasNotCalled();
            }
        }
        public void execute_async_correctly_handles_a_skip_ahead_value_that_exceeds_even_the_longest_child_actions_duration()
        {
            var action1 = new ActionMock(MockBehavior.Loose);
            var action2 = new ActionMock(MockBehavior.Loose);
            var action3 = new ActionMock(MockBehavior.Loose);

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

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

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

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

            using (var context = new ExecutionContext(TimeSpan.FromMinutes(3)))
            {
                sut.ExecuteAsync(context);

                action1
                    .Verify(x => x.ExecuteAsync(It.IsAny<ExecutionContext>()))
                    .WasNotCalled();

                action2
                    .Verify(x => x.ExecuteAsync(It.IsAny<ExecutionContext>()))
                    .WasNotCalled();

                action3
                    .Verify(x => x.ExecuteAsync(It.IsAny<ExecutionContext>()))
                    .WasNotCalled();

                Assert.Equal(TimeSpan.FromSeconds(71), context.Progress);
            }
        }
        public void execute_async_skips_actions_that_are_shorter_than_the_skip_ahead_even_if_the_execution_context_is_paused()
        {
            var action1 = new ActionMock(MockBehavior.Loose);
            var action2 = new ActionMock(MockBehavior.Loose);
            var action3 = new ActionMock(MockBehavior.Loose);

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

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

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

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

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

                action1
                    .Verify(x => x.ExecuteAsync(It.IsAny<ExecutionContext>()))
                    .WasNotCalled();

                action2
                    .Verify(x => x.ExecuteAsync(It.IsAny<ExecutionContext>()))
                    .WasNotCalled();

                action3
                    .Verify(x => x.ExecuteAsync(It.IsAny<ExecutionContext>()))
                    .WasCalledExactlyOnce();
            }
        }
        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 execute_async_skips_child_actions_that_are_shorter_than_the_skip_ahead()
        {
            var action1 = new ActionMock();
            var action2 = new ActionMock();
            var action3 = new ActionMock(MockBehavior.Loose);

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

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

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

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

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

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

            using (var context = new ExecutionContext(TimeSpan.FromSeconds(11)))
            {
                sut.ExecuteAsync(context);

                action3
                    .Verify(x => x.ExecuteAsync(context))
                    .WasCalledExactlyOnce();
            }
        }