public async Task AllMethodWaitsForAllPromisesToFullfill()
        {
            var promise2Resolve  = false;
            var promise3Resolved = false;

            var promise1 = SharpPromise.Promise.Resolve();
            var promise2 = new SharpPromise.Promise((resolve) =>
            {
                Task.Delay(1000).ContinueWith(_ => {
                    promise2Resolve = true;
                    resolve();
                });
            });
            var promise3 = new SharpPromise.Promise((resolve) =>
            {
                Task.Delay(2000).ContinueWith(_ =>
                {
                    promise3Resolved = true;
                    resolve();
                });
            });

            await SharpPromise.Promise.All(promise1, promise2, promise3)
            .Then(() =>
            {
                promise2Resolve.ShouldBeTrue("Promise 2 did not resolve in time");
                promise3Resolved.ShouldBeTrue("Promise 3 did not resolve in time");
            });
        }
        public void AnyReturnsIfAnyPromiseFinishes()
        {
            IPromise resolvingPromise = new SharpPromise.Promise((resolve) =>
            {
                Task.Delay(500)
                .ContinueWith(_ => resolve());
            });

            IPromise neverEndingPromise = new SharpPromise.Promise((resolve) =>
            {
            });

            IPromise neverEndingStory = new SharpPromise.Promise((resolve) =>
            {
            });

            bool result = false;

            var promise = SharpPromise.Promise.Any(resolvingPromise, neverEndingPromise, neverEndingStory)
                          .Then(() => result = true);

            var task = promise.AsTask();

            task.Wait(700);

            result.ShouldBeTrue();
        }
        public async Task CatchNotCalledWhenHandledInThenCallback()
        {
            var othersWereCalled1 = false;
            var handlerCalled1    = false;
            var catchCalled1      = false;

            var othersWereCalled2 = false;
            var catchCalled2      = false;

            var exception = new InternalTestFailureException();

            var promise = new SharpPromise.Promise(resolve => resolve());

            var prom = promise.Then(() => { })
                       .Then(() => { })
                       .Then((Action)(() => throw exception));

            var run1 = prom.Then(() => { }, ex => { handlerCalled1 = true; ex.ShouldBe(exception, "Exceptions were not the same."); })
                       .Then(() => { othersWereCalled1 = true; })
                       .Catch(ex => { catchCalled1 = true; });

            var run2 = prom.Then(() => { othersWereCalled2 = true; })
                       .Catch(ex => { catchCalled2 = true; });

            await SharpPromise.Promise.All(run1, run2);

            othersWereCalled1.ShouldBeTrue("Subsequent \"Then\" calls were not executed after exception was handled.");
            handlerCalled1.ShouldBeTrue("Handler was not called after exception.");
            catchCalled1.ShouldBeFalse("Catch was still called after exception was handled.");

            othersWereCalled2.ShouldBeFalse();
            catchCalled2.ShouldBeTrue();
        }
        public async Task ReturnedValueFromOneThenIsPassedToNextThen()
        {
            var      value   = 42;
            IPromise promise = new SharpPromise.Promise(resolve =>
            {
                resolve();
            });

            await promise.Then(() => value)
            .Then(val => val.ShouldBe(value));
        }
        public async Task ChainedThensExecuteInCorrectOrder()
        {
            int      count   = 1;
            IPromise promise = new SharpPromise.Promise(resolve =>
            {
                Thread.Sleep(200);
                resolve();
            });

            await promise.Then(() => count ++.ShouldBe(1))
            .Then(() => count++.ShouldBe(2));
        }
        public void CastTaskToPromise()
        {
            var completionSource = new TaskCompletionSource <int>();
            var task             = completionSource.Task;

            SharpPromise.Promise promise = task;

            promise.State.ShouldBe(PromiseState.Pending);

            completionSource.SetResult(0);

            promise.State.ShouldBe(PromiseState.Fulfilled);
        }
Beispiel #7
0
        public async Task IfReturnValueIsAPromiseReturnValueOfThatPromiseIsChained()
        {
            Action <int> returnedResolver = null;
            const int    expected         = 42;
            var          returnedPromise  = new Promise <int>(resolve => returnedResolver = resolve);
            var          testedPromise    = new SharpPromise.Promise(resolve => resolve());
            var          resultPromise    = testedPromise.Then(() => returnedPromise)
                                            .Then(result => result.ShouldBe(expected));

            returnedResolver(expected);

            await resultPromise;
        }
        public void CastPromiseToTask()
        {
            Action resolver = null;
            var    promise  = new SharpPromise.Promise(r => resolver = r);

            Task test = promise;

            test.ShouldNotBeNull();
            test.IsCompleted.ShouldBeFalse();

            resolver();

            test.IsCompleted.ShouldBeTrue();
        }
        public async Task ExceptionGetsHandledInThenCallbackFromRejectedTask()
        {
            var message = "Task failed, but that's what we wanted.";
            var ex      = new InternalTestFailureException(message);

            var testTask = new SharpPromise.Promise(Task.FromException(ex));

            await testTask.Then(() =>
            {
                Assert.Fail("Promise resolved, but should not have.");
            },
                                e =>
            {
                e.Message.ShouldBe(message, "Exception messages did not match.");
            });
        }
Beispiel #10
0
        public void SetsCorrectStateOnEmptyResolve()
        {
            Action resolver = null;

            IPromise promise = new SharpPromise.Promise((resolve) =>
            {
                resolver = resolve;
            });

            promise.State.ShouldBe(PromiseState.Pending);
            resolver.ShouldNotBeNull();

            resolver();

            promise.State.ShouldBe(PromiseState.Fulfilled);
        }
Beispiel #11
0
        public async Task CatchDealsWithExceptionFurtherUpTheChain()
        {
            var othersWereCalled = false;

            var exception = new TaskCanceledException();

            var promise = new SharpPromise.Promise(resolve => resolve());

            await promise.Then(() => { })
            .Then(() => { })
            .Then((Action)(() => throw exception))
            .Then(() => { othersWereCalled = true; })
            .Then(() => { othersWereCalled = true; })
            .Catch(ex => ex.ShouldBeAssignableTo <TaskCanceledException>());

            othersWereCalled.ShouldBeFalse("Then calls after exception should not be called");
        }
Beispiel #12
0
        public async Task ThenFiresAfterResolutionOnAlreadyCompletedPromise()
        {
            Action resolver  = null;
            var    wasCalled = false;

            IPromise promise = new SharpPromise.Promise(resolve => resolver = resolve);

            resolver.ShouldNotBeNull();

            resolver();

            var other = promise.Then(() => wasCalled = true);

            await promise;
            await other;

            wasCalled.ShouldBeTrue();
        }
Beispiel #13
0
        public void SetsCorrectStateOnParameterizedReject()
        {
            Action <Exception> rejecter = null;

            void callback(Action resolve, Action <Exception> reject)
            {
                rejecter = reject;
            }

            IPromise promise = new SharpPromise.Promise(callback);

            promise.State.ShouldBe(PromiseState.Pending);
            rejecter.ShouldNotBeNull();

            rejecter(new Exception("Testing"));

            promise.State.ShouldBe(PromiseState.Rejected);
        }
Beispiel #14
0
        public void SetsCorrectStateOnEmptyReject()
        {
            Action rejecter = null;

            void callback(Action resolve, Action reject)
            {
                rejecter = reject;
            }

            IPromise promise = new SharpPromise.Promise(callback);

            promise.State.ShouldBe(PromiseState.Pending);
            rejecter.ShouldNotBeNull();

            rejecter();

            promise.State.ShouldBe(PromiseState.Rejected);
        }
Beispiel #15
0
#pragma warning disable CC0061 // Asynchronous method can be terminated with the 'Async' keyword.
        public static Task AsTask(this Promise promise) => promise;
Beispiel #16
0
        public void ThrowsExceptionIfNoCallbackPassedForTask()
        {
            var promise = new SharpPromise.Promise((Task)null);

            Assert.Fail("No exception was thrown on passing null to the constructor");
        }
Beispiel #17
0
        public void ThrowsExceptionIfNoCallbackPassedForActionActionException()
        {
            var promise = new SharpPromise.Promise((Action <Action, Action <Exception> >)null);

            Assert.Fail("No exception was thrown on passing null to the constructor");
        }
Beispiel #18
0
#pragma warning disable CC0061 // Asynchronous method can be terminated with the 'Async' keyword.
        public static Task <T> AsTask <T>(this Promise <T> promise) => promise;