Example #1
0
 public async Task It_will_await_all_forked_function()
 {
     var result = Task.FromResult(Outcome.Any())
                  .Fork(() => new Task[] {
         ExecuteAsync("step 1"),
         ExecuteAsync("step 2"),
         ExecuteAsync("step 3")
     })
                  .Join(results => {
         var(step1, step2, step3) = results;
     })
 }
Example #2
0
        public async Task It_can_return_an_async_failure_without_original_failure()
        {
            var t = await Outcome.Any()
                    .Map(AsyncMethodThatThrowsException)

                    // sig: Outcome<T> Catch<T>(this Task<Outcome<T>> @this, Func<Failure, Task<Outcome<T>> handler)
                    .Catch(GetFailedOutcomeAsync);

            t.IsSuccessful.Should().BeFalse();

            var f = t.FailureOrNull();

            f.Should().NotBeNull();
            f.Reason.Should().Be("Test Async Failure");
        }
Example #3
0
        public async Task It_can_return_a_failure()
        {
            var t = await Outcome.Any()
                    .Map(AsyncMethodThatThrowsException)

                    // sig: Task<Outcome<T>> Catch<T>(this Task<Outcome<T>> @this, Func<Failure, Outcome<T>> handler)
                    .Catch(failure => Outcome <string> .Reject("Test Failure", failure));

            t.IsSuccessful.Should().BeFalse();

            var f = t.FailureOrNull();

            f.Should().NotBeNull();
            f.Reason.Should().Be("Test Failure");
        }
Example #4
0
        public void Executes_the_Then_lambda_only_after_the_outer_function_has_executed_successfully()
        {
            int i = 0;

            var firstSpy = A.Fake <Action>();

            A.CallTo(() => firstSpy()).Invokes(() => i = 1);

            var secondSpy = A.Fake <Action>();

            A.CallTo(() => secondSpy()).Invokes(() => i *= 10);

            var outcome = Outcome.Any().Then(firstSpy).Then(secondSpy);

            A.CallTo(firstSpy).MustHaveHappenedOnceExactly();
            A.CallTo(secondSpy).MustHaveHappenedOnceExactly();

            i.Should().Be(10); // If it didn't happen in sequence, the value would have been 0
        }
Example #5
0
        public void Executes_the_Then_lambda_function_if_the_outer_Outcome_has_suceeded()
        {
            var successOutcome = Outcome.Any();

            successOutcome.IsSuccessful.Should().BeTrue();

            int i   = 0;
            var spy = A.Fake <Func <int> >();

            A.CallTo(() => spy()).ReturnsLazily(() => ++ i);

            var operationOutcome = successOutcome.Then(spy);

            A.CallTo(spy).MustHaveHappenedOnceExactly();
            i.Should().Be(1);

            operationOutcome.IsSuccessful.Should().BeTrue();
            operationOutcome.ResultOrDefault().Should().Be(1);
        }
        public void It_will_be_treated_as_a_failure_of_the_chain()
        {
            Func <int, bool> handler = A.Fake <Func <int, bool> >();
            var oc = Outcome.Any()
                     .Map(() => 100)
                     .TapWhen(true, () => Outcome <string> .Reject("Failed!"))
                     .Map(handler);

            oc.IsSuccessful.Should().BeFalse();
            A.CallTo(() => handler.Invoke(A <int> .Ignored)).MustNotHaveHappened();

            oc = Outcome.Any()
                 .Map(() => 100)
                 .TapWhen(() => true, () => Outcome <string> .Reject("Failed!"))
                 .Map(handler);

            oc.IsSuccessful.Should().BeFalse();
            A.CallTo(() => handler.Invoke(A <int> .Ignored)).MustNotHaveHappened();
        }