public async Task AnyReturnsValueOfFirstPromiseFinished() { int result = 73, result2 = 37; IPromise <int> resolvingPromise = new SharpPromise.Promise <int>((resolve) => { Task.Delay(500) .ContinueWith(_ => resolve(result)); }); IPromise <int> neverEndingPromise = new SharpPromise.Promise <int>((resolve) => { Task.Delay(700) .ContinueWith(_ => resolve(result2)); }); IPromise <int> neverEndingStory = new SharpPromise.Promise <int>((Action <Action <int>, Action>)((resolve, reject) => { })); bool didFinish = false; var promise = SharpPromise.Promise <int> .Any(resolvingPromise, neverEndingPromise, neverEndingStory) .Then(val => { didFinish = true; val.ShouldBe(result); }); var task = promise.AsTask(); task.Wait(1000); didFinish.ShouldBeTrue(); }
public async Task ReturnedValueFromOneThenIsPassedToNextThen() { const int value = 42; IPromise <int> promise = new SharpPromise.Promise <int>(resolve => { resolve(value); }); await promise.Then(result => result *result) .Then(val => val.ShouldBe(value * value)); }
public async Task ChainedThensExecuteInCorrectOrder() { var count = 1; const int expected = 42; IPromise <int> promise = new SharpPromise.Promise <int>(resolve => { Task.Delay(200).Wait(); resolve(expected); }); await promise.Then(i => count ++.ShouldBe(1)) .Then(() => count++.ShouldBe(2)); }
public void SetsCorrectStateOnEmptyResolve() { Action <int> resolver = null; const int expected = 42; IPromise <int> promise = new SharpPromise.Promise <int>((resolve) => { resolver = resolve; }); promise.State.ShouldBe(PromiseState.Pending); resolver.ShouldNotBeNull(); resolver(expected); promise.State.ShouldBe(PromiseState.Fulfilled); }
public void SetsCorrectStateOnEmptyReject() { Action rejecter = null; void callback(Action <int> resolve, Action reject) { rejecter = reject; } IPromise <int> promise = new SharpPromise.Promise <int>(callback); promise.State.ShouldBe(PromiseState.Pending); rejecter.ShouldNotBeNull(); rejecter(); promise.State.ShouldBe(PromiseState.Rejected); }
public void SetsCorrectStateOnParameterizedReject() { Action <Exception> rejecter = null; void callback(Action <int> resolve, Action <Exception> reject) { rejecter = reject; } IPromise <int> promise = new SharpPromise.Promise <int>(callback); promise.State.ShouldBe(PromiseState.Pending); rejecter.ShouldNotBeNull(); rejecter(new Exception("Testing")); promise.State.ShouldBe(PromiseState.Rejected); }
public async Task AllMethodWaitsForAllPromisesToFullfill() { var promise2Resolve = false; var promise3Resolved = false; const int prom1Result = 42, prom2Result = 73, prom3Result = 13; var promise1 = SharpPromise.Promise <int> .Resolve(prom1Result); var promise2 = new SharpPromise.Promise <int>((resolve) => { Task.Delay(1000).ContinueWith(_ => { promise2Resolve = true; resolve(prom2Result); }); }); var promise3 = new SharpPromise.Promise <int>((resolve) => { Task.Delay(2000).ContinueWith(_ => { promise3Resolved = true; resolve(prom3Result); }); }); await SharpPromise.Promise <int> .All(promise1, promise2, promise3) .Then(results => { promise1.State.ShouldBe(PromiseState.Fulfilled); promise2Resolve.ShouldBeTrue("Promise 2 did not resolve in time"); promise3Resolved.ShouldBeTrue("Promise 3 did not resolve in time"); results.Length.ShouldBe(3); results[0].ShouldBe(prom1Result); results[1].ShouldBe(prom2Result); results[2].ShouldBe(prom3Result); }); }
public async Task ThenFiresAfterResolutionOnAlreadyCompletedPromise() { Action <int> resolver = null; var wasCalled = false; const int expected = 42; IPromise <int> promise = new SharpPromise.Promise <int>(resolve => resolver = resolve); resolver.ShouldNotBeNull(); resolver(expected); var other = promise.Then(i => { wasCalled = true; return(3); }); await promise; await other; wasCalled.ShouldBeTrue(); }
public void ThrowsExceptionIfNoCallbackPassedForActionActionException() { var promise = new SharpPromise.Promise <int>((Action <Action <int>, Action <Exception> >)null); Assert.Fail("No exception was thrown on passing null to the constructor"); }
public void ThrowsExceptionIfNoCallbackPassedForTask() { var promise = new SharpPromise.Promise <int>((Task <int>)null); Assert.Fail("No exception was thrown on passing null to the constructor"); }