public void OnResolvedWillBeInvokedWithCapturedValue0()
        {
            var deferred = Promise.NewDeferred();

            string expected = "expected";
            bool   invoked  = false;

            TestHelper.AddResolveCallbacks <int, string>(deferred.Promise,
                                                         captureValue: expected,
                                                         onResolveCapture: cv =>
            {
                Assert.AreEqual(expected, cv);
                invoked = true;
            }
                                                         );
            TestHelper.AddCallbacks <int, object, string>(deferred.Promise,
                                                          captureValue: expected,
                                                          onResolveCapture: cv =>
            {
                Assert.AreEqual(expected, cv);
                invoked = true;
            }
                                                          );

            deferred.Resolve();
            Promise.Manager.HandleCompletes();

            Assert.AreEqual(true, invoked);

            TestHelper.Cleanup();
        }
        public void OnResolvedWillBeInvokedWithCapturedValue1()
        {
            var deferred = Promise.NewDeferred <int>();

            string expected = "expected";
            bool   invoked  = false;

            TestHelper.AddResolveCallbacks <int, bool, string>(deferred.Promise,
                                                               captureValue: expected,
                                                               onResolveCapture: cv =>
            {
                Assert.AreEqual(expected, cv);
                invoked = true;
            }
                                                               );
            TestHelper.AddCallbacks <int, bool, object, string>(deferred.Promise,
                                                                captureValue: expected,
                                                                onResolveCapture: cv =>
            {
                Assert.AreEqual(expected, cv);
                invoked = true;
            }
                                                                );

            deferred.Resolve(50);
            Promise.Manager.HandleCompletes();

            Assert.AreEqual(true, invoked);

            // Clean up.
            GC.Collect();
            Promise.Manager.HandleCompletesAndProgress();
            LogAssert.NoUnexpectedReceived();
        }
Beispiel #3
0
        public void AccessingRethrowInOnResolvedThrows()
        {
            var promise1 = Promise.Resolved();
            var promise2 = Promise.Resolved(100);

            int voidErrors = 0;
            int intErrors  = 0;

            Action <Promise> callback = p =>
            {
                p.Catch((object e) =>
                {
                    Assert.IsInstanceOf(typeof(InvalidOperationException), e);
                    ++voidErrors;
                });
                var _ = Promise.Rethrow;
            };
            Action <Promise> callbackT = p =>
            {
                p.Catch((object e) =>
                {
                    Assert.IsInstanceOf(typeof(InvalidOperationException), e);
                    ++intErrors;
                });
                var _ = Promise.Rethrow;
            };

            TestHelper.AddResolveCallbacks <int, string>(promise1,
                                                         promiseToVoid: callback,
                                                         promiseToConvert: p => { callback(p); return(0); },
                                                         promiseToPromise: p => { callback(p); return(Promise.Resolved()); },
                                                         promiseToPromiseConvert: p => { callback(p); return(Promise.Resolved(0)); }
                                                         );
            TestHelper.AddCallbacks <int, object, string>(promise1,
                                                          promiseToVoid: callback,
                                                          promiseToConvert: p => { callback(p); return(0); },
                                                          promiseToPromise: p => { callback(p); return(Promise.Resolved()); },
                                                          promiseToPromiseConvert: p => { callback(p); return(Promise.Resolved(0)); }
                                                          );
            TestHelper.AddContinueCallbacks <int, string>(promise1,
                                                          promiseToVoid: callback,
                                                          promiseToConvert: p => { callback(p); return(0); },
                                                          promiseToPromise: p => { callback(p); return(Promise.Resolved()); },
                                                          promiseToPromiseConvert: p => { callback(p); return(Promise.Resolved(0)); }
                                                          );

            TestHelper.AddResolveCallbacks <int, string>(promise2,
                                                         promiseToVoid: callbackT,
                                                         promiseToConvert: p => { callbackT(p); return(0); },
                                                         promiseToPromise: p => { callbackT(p); return(Promise.Resolved()); },
                                                         promiseToPromiseConvert: p => { callbackT(p); return(Promise.Resolved(0)); }
                                                         );
            TestHelper.AddCallbacks <int, bool, object, string>(promise2,
                                                                promiseToVoid: callbackT,
                                                                promiseToConvert: p => { callbackT(p); return(false); },
                                                                promiseToPromise: p => { callbackT(p); return(Promise.Resolved()); },
                                                                promiseToPromiseConvert: p => { callbackT(p); return(Promise.Resolved(false)); }
                                                                );
            TestHelper.AddContinueCallbacks <int, int, string>(promise2,
                                                               promiseToVoid: callbackT,
                                                               promiseToConvert: p => { callbackT(p); return(0); },
                                                               promiseToPromise: p => { callbackT(p); return(Promise.Resolved()); },
                                                               promiseToPromiseConvert: p => { callbackT(p); return(Promise.Resolved(0)); }
                                                               );

            Promise.Manager.HandleCompletes();

            Assert.AreEqual(TestHelper.resolveVoidVoidCallbacks + TestHelper.resolveVoidConvertCallbacks +
                            TestHelper.resolveVoidPromiseVoidCallbacks + TestHelper.resolveVoidPromiseConvertCallbacks +
                            TestHelper.continueVoidVoidCallbacks + TestHelper.continueVoidConvertCallbacks +
                            TestHelper.continueVoidPromiseVoidCallbacks + TestHelper.continueVoidPromiseConvertCallbacks,
                            voidErrors);
            Assert.AreEqual(TestHelper.resolveTVoidCallbacks + TestHelper.resolveTConvertCallbacks +
                            TestHelper.resolveTPromiseVoidCallbacks + TestHelper.resolveTPromiseConvertCallbacks +
                            TestHelper.continueTVoidCallbacks + TestHelper.continueTConvertCallbacks +
                            TestHelper.continueTPromiseVoidCallbacks + TestHelper.continueTPromiseConvertCallbacks,
                            intErrors);

            // Clean up.
            GC.Collect();
            Promise.Manager.HandleCompletesAndProgress();
            LogAssert.NoUnexpectedReceived();
        }
Beispiel #4
0
        public void ThrowingCancelExceptionInOnResolvedCancelsThePromiseWithTheGivenValue()
        {
            var promise1 = Promise.Resolved();
            var promise2 = Promise.Resolved(100);

            int    voidCancelations = 0;
            int    intCancelations  = 0;
            string expected         = "Cancel!";

            Action <Promise> callback = p =>
            {
                p.CatchCancelation(reason =>
                {
                    Assert.AreEqual(expected, reason.Value);
                    ++voidCancelations;
                });
                throw Promise.CancelException(expected);
            };
            Action <Promise> callbackT = p =>
            {
                p.CatchCancelation(reason =>
                {
                    Assert.AreEqual(expected, reason.Value);
                    ++intCancelations;
                });
                throw Promise.CancelException(expected);
            };

            TestHelper.AddResolveCallbacks <int, string>(promise1,
                                                         promiseToVoid: callback,
                                                         promiseToConvert: p => { callback(p); return(0); },
                                                         promiseToPromise: p => { callback(p); return(Promise.Resolved()); },
                                                         promiseToPromiseConvert: p => { callback(p); return(Promise.Resolved(0)); }
                                                         );
            TestHelper.AddCallbacks <int, object, string>(promise1,
                                                          promiseToVoid: callback,
                                                          promiseToConvert: p => { callback(p); return(0); },
                                                          promiseToPromise: p => { callback(p); return(Promise.Resolved()); },
                                                          promiseToPromiseConvert: p => { callback(p); return(Promise.Resolved(0)); }
                                                          );
            TestHelper.AddContinueCallbacks <int, string>(promise1,
                                                          promiseToVoid: callback,
                                                          promiseToConvert: p => { callback(p); return(0); },
                                                          promiseToPromise: p => { callback(p); return(Promise.Resolved()); },
                                                          promiseToPromiseConvert: p => { callback(p); return(Promise.Resolved(0)); }
                                                          );

            TestHelper.AddResolveCallbacks <int, string>(promise2,
                                                         promiseToVoid: callbackT,
                                                         promiseToConvert: p => { callbackT(p); return(0); },
                                                         promiseToPromise: p => { callbackT(p); return(Promise.Resolved()); },
                                                         promiseToPromiseConvert: p => { callbackT(p); return(Promise.Resolved(0)); }
                                                         );
            TestHelper.AddCallbacks <int, bool, object, string>(promise2,
                                                                promiseToVoid: callbackT,
                                                                promiseToConvert: p => { callbackT(p); return(false); },
                                                                promiseToPromise: p => { callbackT(p); return(Promise.Resolved()); },
                                                                promiseToPromiseConvert: p => { callbackT(p); return(Promise.Resolved(false)); }
                                                                );
            TestHelper.AddContinueCallbacks <int, int, string>(promise2,
                                                               promiseToVoid: callbackT,
                                                               promiseToConvert: p => { callbackT(p); return(0); },
                                                               promiseToPromise: p => { callbackT(p); return(Promise.Resolved()); },
                                                               promiseToPromiseConvert: p => { callbackT(p); return(Promise.Resolved(0)); }
                                                               );

            Promise.Manager.HandleCompletes();

            Assert.AreEqual(TestHelper.resolveVoidVoidCallbacks + TestHelper.resolveVoidConvertCallbacks +
                            TestHelper.resolveVoidPromiseVoidCallbacks + TestHelper.resolveVoidPromiseConvertCallbacks +
                            TestHelper.continueVoidVoidCallbacks + TestHelper.continueVoidConvertCallbacks +
                            TestHelper.continueVoidPromiseVoidCallbacks + TestHelper.continueVoidPromiseConvertCallbacks,
                            voidCancelations);
            Assert.AreEqual(TestHelper.resolveTVoidCallbacks + TestHelper.resolveTConvertCallbacks +
                            TestHelper.resolveTPromiseVoidCallbacks + TestHelper.resolveTPromiseConvertCallbacks +
                            TestHelper.continueTVoidCallbacks + TestHelper.continueTConvertCallbacks +
                            TestHelper.continueTPromiseVoidCallbacks + TestHelper.continueTPromiseConvertCallbacks,
                            intCancelations);

            // Clean up.
            GC.Collect();
            Promise.Manager.HandleCompletesAndProgress();
            LogAssert.NoUnexpectedReceived();
        }
        public void ThrowingRejectExceptionInOnResolvedRejectsThePromiseWithTheGivenValue()
        {
            var promise1 = Promise.Resolved();
            var promise2 = Promise.Resolved(100);

            int    voidRejections = 0;
            int    intRejections  = 0;
            string expected       = "Reject!";

            Action <Promise> callback = p =>
            {
                p.Catch((string e) =>
                {
                    Assert.AreEqual(expected, e);
                    ++voidRejections;
                });
                throw Promise.RejectException(expected);
            };
            Action <Promise> callbackT = p =>
            {
                p.Catch((string e) =>
                {
                    Assert.AreEqual(expected, e);
                    ++intRejections;
                });
                throw Promise.RejectException(expected);
            };

            TestHelper.AddResolveCallbacks <int, string>(promise1,
                                                         promiseToVoid: callback,
                                                         promiseToConvert: p => { callback(p); return(0); },
                                                         promiseToPromise: p => { callback(p); return(Promise.Resolved()); },
                                                         promiseToPromiseConvert: p => { callback(p); return(Promise.Resolved(0)); }
                                                         );
            TestHelper.AddCallbacks <int, object, string>(promise1,
                                                          promiseToVoid: callback,
                                                          promiseToConvert: p => { callback(p); return(0); },
                                                          promiseToPromise: p => { callback(p); return(Promise.Resolved()); },
                                                          promiseToPromiseConvert: p => { callback(p); return(Promise.Resolved(0)); }
                                                          );
            TestHelper.AddContinueCallbacks <int, string>(promise1,
                                                          promiseToVoid: callback,
                                                          promiseToConvert: p => { callback(p); return(0); },
                                                          promiseToPromise: p => { callback(p); return(Promise.Resolved()); },
                                                          promiseToPromiseConvert: p => { callback(p); return(Promise.Resolved(0)); }
                                                          );

            TestHelper.AddResolveCallbacks <int, string>(promise2,
                                                         promiseToVoid: callbackT,
                                                         promiseToConvert: p => { callbackT(p); return(0); },
                                                         promiseToPromise: p => { callbackT(p); return(Promise.Resolved()); },
                                                         promiseToPromiseConvert: p => { callbackT(p); return(Promise.Resolved(0)); }
                                                         );
            TestHelper.AddCallbacks <int, bool, object, string>(promise2,
                                                                promiseToVoid: callbackT,
                                                                promiseToConvert: p => { callbackT(p); return(false); },
                                                                promiseToPromise: p => { callbackT(p); return(Promise.Resolved()); },
                                                                promiseToPromiseConvert: p => { callbackT(p); return(Promise.Resolved(false)); }
                                                                );
            TestHelper.AddContinueCallbacks <int, int, string>(promise2,
                                                               promiseToVoid: callbackT,
                                                               promiseToConvert: p => { callbackT(p); return(0); },
                                                               promiseToPromise: p => { callbackT(p); return(Promise.Resolved()); },
                                                               promiseToPromiseConvert: p => { callbackT(p); return(Promise.Resolved(0)); }
                                                               );

            Promise.Manager.HandleCompletes();

            Assert.AreEqual(
                (TestHelper.resolveVoidVoidCallbacks + TestHelper.resolveVoidConvertCallbacks +
                 TestHelper.resolveVoidPromiseVoidCallbacks + TestHelper.resolveVoidPromiseConvertCallbacks +
                 TestHelper.continueVoidVoidCallbacks + TestHelper.continueVoidConvertCallbacks +
                 TestHelper.continueVoidPromiseVoidCallbacks + TestHelper.continueVoidPromiseConvertCallbacks) * 2,
                voidRejections
                );
            Assert.AreEqual(
                (TestHelper.resolveTVoidCallbacks + TestHelper.resolveTConvertCallbacks +
                 TestHelper.resolveTPromiseVoidCallbacks + TestHelper.resolveTPromiseConvertCallbacks +
                 TestHelper.continueTVoidCallbacks + TestHelper.continueTConvertCallbacks +
                 TestHelper.continueTPromiseVoidCallbacks + TestHelper.continueTPromiseConvertCallbacks) * 2,
                intRejections
                );

            TestHelper.Cleanup();
        }