public void execute_async_throws_if_execution_context_is_null()
        {
            var sut = new ExerciseBuilder()
                      .Build();

            Assert.Throws <ArgumentNullException>(() => sut.ExecuteAsync(null));
        }
        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_updates_the_current_exercise_in_the_context()
        {
            var sut = new ExerciseBuilder()
                      .Build();
            var context = new ExecutionContext();

            sut.ExecuteAsync(context);

            Assert.Same(sut, context.CurrentExercise);
        }
        public void execute_async_says_exercise_name_first()
        {
            var speechService = new SpeechServiceMock(MockBehavior.Loose);
            var sut           = new ExerciseBuilder()
                                .WithName("some name")
                                .WithSpeechService(speechService)
                                .Build();

            sut.ExecuteAsync(new ExecutionContext());

            speechService
            .Verify(x => x.SpeakAsync("some name", It.IsAny <CancellationToken>()))
            .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_updates_the_current_set_in_the_context()
        {
            var sut = new ExerciseBuilder()
                      .WithSetCount(3)
                      .Build();
            var context     = new ExecutionContext();
            var currentSets = context
                              .ObservableForProperty(x => x.CurrentSet)
                              .Select(x => x.Value)
                              .CreateCollection();

            sut.ExecuteAsync(context);

            Assert.Equal(3, currentSets.Count);
            Assert.Equal(1, currentSets[0]);
            Assert.Equal(2, currentSets[1]);
            Assert.Equal(3, currentSets[2]);
        }
        public void execute_async_updates_the_current_repetitions_in_the_context()
        {
            var sut = new ExerciseBuilder()
                      .WithRepetitionCount(5)
                      .Build();
            var context            = new ExecutionContext();
            var currentRepetitions = context
                                     .ObservableForProperty(x => x.CurrentRepetition)
                                     .Select(x => x.Value)
                                     .CreateCollection();

            sut.ExecuteAsync(context);

            Assert.Equal(5, currentRepetitions.Count);
            Assert.Equal(1, currentRepetitions[0]);
            Assert.Equal(2, currentRepetitions[1]);
            Assert.Equal(3, currentRepetitions[2]);
            Assert.Equal(4, currentRepetitions[3]);
            Assert.Equal(5, currentRepetitions[4]);
        }
        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_says_exercise_name_first()
        {
            var speechService = new SpeechServiceMock(MockBehavior.Loose);
            var sut = new ExerciseBuilder()
                .WithName("some name")
                .WithSpeechService(speechService)
                .Build();

            sut.ExecuteAsync(new ExecutionContext());

            speechService
                .Verify(x => x.SpeakAsync("some name", It.IsAny<CancellationToken>()))
                .WasCalledExactlyOnce();
        }
        public void execute_async_updates_the_current_set_in_the_context()
        {
            var sut = new ExerciseBuilder()
                .WithSetCount(3)
                .Build();
            var context = new ExecutionContext();
            var currentSets = context
                .ObservableForProperty(x => x.CurrentSet)
                .Select(x => x.Value)
                .CreateCollection();

            sut.ExecuteAsync(context);

            Assert.Equal(3, currentSets.Count);
            Assert.Equal(1, currentSets[0]);
            Assert.Equal(2, currentSets[1]);
            Assert.Equal(3, currentSets[2]);
        }
        public void execute_async_updates_the_current_repetitions_in_the_context()
        {
            var sut = new ExerciseBuilder()
                .WithRepetitionCount(5)
                .Build();
            var context = new ExecutionContext();
            var currentRepetitions = context
                .ObservableForProperty(x => x.CurrentRepetition)
                .Select(x => x.Value)
                .CreateCollection();

            sut.ExecuteAsync(context);

            Assert.Equal(5, currentRepetitions.Count);
            Assert.Equal(1, currentRepetitions[0]);
            Assert.Equal(2, currentRepetitions[1]);
            Assert.Equal(3, currentRepetitions[2]);
            Assert.Equal(4, currentRepetitions[3]);
            Assert.Equal(5, currentRepetitions[4]);
        }
        public void execute_async_updates_the_current_exercise_in_the_context()
        {
            var sut = new ExerciseBuilder()
                .Build();
            var context = new ExecutionContext();

            sut.ExecuteAsync(context);

            Assert.Same(sut, context.CurrentExercise);
        }
        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_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_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_throws_if_execution_context_is_null()
 {
     var sut = new ExerciseBuilder()
         .Build();
     Assert.Throws<ArgumentNullException>(() => sut.ExecuteAsync(null));
 }