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 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 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."); }); }
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"); }
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(); }