예제 #1
0
            public void _2_1_2_2_MustHaveAValueWhichMustNotChange()
            {
                var deferred = Promise.NewDeferred <int>();

                Assert.AreEqual(Promise.State.Pending, deferred.State);

                deferred.Retain();
                int result   = -1;
                int expected = 0;

                TestHelper.AddCallbacks <int, bool, object, string>(deferred.Promise,
                                                                    onResolve: num => { Assert.AreEqual(expected, num); result = num; },
                                                                    onReject: s => Assert.Fail("Promise was rejected when it should have been resolved."),
                                                                    onUnknownRejection: () => Assert.Fail("Promise was rejected when it should have been resolved.")
                                                                    );
                deferred.Resolve(expected);
                Promise.Manager.HandleCompletes();

                Assert.AreEqual(expected, result);

                TestHelper.AddCallbacks <int, bool, object, string>(deferred.Promise,
                                                                    onResolve: num => { Assert.AreEqual(expected, num); result = num; },
                                                                    onReject: s => Assert.Fail("Promise was rejected when it should have been resolved."),
                                                                    onUnknownRejection: () => Assert.Fail("Promise was rejected when it should have been resolved.")
                                                                    );
                TestHelper.ExpectWarning("Deferred.Resolve - Deferred is not in the pending state.");
                deferred.Resolve(1);
                Promise.Manager.HandleCompletes();

                Assert.AreEqual(expected, result);

                deferred.Release();

                TestHelper.Cleanup();
            }
예제 #2
0
        public void AccessingCancelExceptionOrRejectExceptionInOnResolvedDoesNotThrow()
        {
            var promise1 = Promise.Resolved();
            var promise2 = Promise.Resolved(100);

            TestHelper.AddCallbacks <int, object, string>(promise1,
                                                          onResolve: () =>
            {
#if PROMISE_CANCEL
                Promise.CancelException();
                Promise.CancelException("Cancel!");
#endif
                Promise.RejectException("Reject!");
            });
            TestHelper.AddCallbacks <int, bool, object, string>(promise2,
                                                                onResolve: v =>
            {
#if PROMISE_CANCEL
                Promise.CancelException();
                Promise.CancelException("Cancel!");
#endif
                Promise.RejectException("Reject!");
            });

            Promise.Manager.HandleCompletes();

            // Clean up.
            GC.Collect();
            Promise.Manager.HandleCompletesAndProgress();
            LogAssert.NoUnexpectedReceived();
        }
        public void OnRejectedWillBeInvokedWithCapturedValue1()
        {
            var deferred = Promise.NewDeferred <int>();

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

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

            deferred.Reject("Reject");
            Promise.Manager.HandleCompletes();

            Assert.AreEqual(true, invoked);

            // Clean up.
            GC.Collect();
            Promise.Manager.HandleCompletesAndProgress();
            LogAssert.NoUnexpectedReceived();
        }
        public void IfPromiseIsCanceledOnResolveAndOnRejectedMustNotBeInvoked()
        {
            CancelationSource cancelationSource1 = CancelationSource.New();
            var deferred = Promise.NewDeferred(cancelationSource1.Token);

            Assert.AreEqual(Promise.State.Pending, deferred.State);
            CancelationSource cancelationSource2 = CancelationSource.New();
            var deferredInt = Promise.NewDeferred <int>(cancelationSource2.Token);

            Assert.AreEqual(Promise.State.Pending, deferredInt.State);

            Action resolveAssert = () => Assert.Fail("Promise was resolved when it should have been canceled.");
            Action rejectAssert  = () => Assert.Fail("Promise was rejected when it should have been canceled.");

            TestHelper.AddCallbacks <int, object, string>(deferred.Promise,
                                                          onResolve: resolveAssert,
                                                          onReject: _ => rejectAssert(),
                                                          onUnknownRejection: rejectAssert);
            TestHelper.AddCallbacks <int, bool, object, string>(deferredInt.Promise,
                                                                onResolve: _ => resolveAssert(),
                                                                onReject: _ => rejectAssert(),
                                                                onUnknownRejection: rejectAssert);

            cancelationSource1.Cancel();
            cancelationSource2.Cancel();
            Promise.Manager.HandleCompletes();

            cancelationSource1.Dispose();
            cancelationSource2.Dispose();
            TestHelper.Cleanup();
        }
예제 #5
0
        public void OnRejectedWillBeInvokedWithCapturedValue0()
        {
            var deferred = Promise.NewDeferred();

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

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

            deferred.Reject("Reject");
            Promise.Manager.HandleCompletes();

            Assert.AreEqual(true, invoked);

            TestHelper.Cleanup();
        }
예제 #6
0
        public void IfPromiseIsCanceledOnResolveAndOnRejectedMustNotBeInvoked()
        {
            var deferred = Promise.NewDeferred();

            Assert.AreEqual(Promise.State.Pending, deferred.State);
            var deferredInt = Promise.NewDeferred <int>();

            Assert.AreEqual(Promise.State.Pending, deferredInt.State);

            Action resolveAssert = () => Assert.Fail("Promise was resolved when it should have been canceled.");
            Action rejectAssert  = () => Assert.Fail("Promise was rejected when it should have been canceled.");

            TestHelper.AddCallbacks <int, object, string>(deferred.Promise,
                                                          onResolve: resolveAssert,
                                                          onReject: _ => rejectAssert(),
                                                          onUnknownRejection: rejectAssert);
            TestHelper.AddCallbacks <int, bool, object, string>(deferredInt.Promise,
                                                                onResolve: _ => resolveAssert(),
                                                                onReject: _ => rejectAssert(),
                                                                onUnknownRejection: rejectAssert);

            deferred.Cancel();
            deferredInt.Cancel();
            Promise.Manager.HandleCompletes();

            // Clean up.
            GC.Collect();
            Promise.Manager.HandleCompletesAndProgress();
            LogAssert.NoUnexpectedReceived();
        }
예제 #7
0
        public void ThrowingRethrowInOnRejectedRejectsThePromiseWithTheSameReason()
        {
            string expected = "Reject!";

            var promise1 = Promise.Rejected(expected);
            var promise2 = Promise.Rejected <int, string>(expected);

            int voidRejections = 0;
            int intRejections  = 0;

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

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

            Promise.Manager.HandleCompletes();

            Assert.AreEqual(
                (TestHelper.rejectVoidVoidCallbacks + TestHelper.rejectVoidConvertCallbacks +
                 TestHelper.rejectVoidPromiseVoidCallbacks + TestHelper.rejectVoidPromiseConvertCallbacks) * 2,
                voidRejections
                );
            Assert.AreEqual(
                (TestHelper.rejectTVoidCallbacks + TestHelper.rejectTConvertCallbacks +
                 TestHelper.rejectTPromiseVoidCallbacks + TestHelper.rejectTPromiseConvertCallbacks) * 2,
                intRejections
                );

            TestHelper.Cleanup();
        }
예제 #8
0
            public void _2_1_3_2_MustHaveAReasonWhichMustNotChange_0()
            {
                var deferred = Promise.NewDeferred();

                Assert.AreEqual(Promise.State.Pending, deferred.State);

                deferred.Retain();
                string rejection = null;
                string expected  = "Fail Value";

                TestHelper.AddCallbacks <int, string, string>(deferred.Promise,
                                                              onResolve: () => Assert.Fail("Promise was resolved when it should have been rejected."),
                                                              onReject: failValue =>
                {
                    rejection = failValue;

                    Assert.AreEqual(expected, failValue);
                });
                deferred.Reject(expected);
                Promise.Manager.HandleCompletes();

                Assert.AreEqual(expected, rejection);

                TestHelper.AddCallbacks <int, string, string>(deferred.Promise,
                                                              onResolve: () => Assert.Fail("Promise was resolved when it should have been rejected."),
                                                              onReject: failValue =>
                {
                    rejection = failValue;

                    Assert.AreEqual(expected, failValue);
                });

                LogAssert.Expect(UnityEngine.LogType.Warning, "Deferred.Reject - Deferred is not in the pending state.");

                deferred.Reject("Different Fail Value");
                deferred.Release();
                // The second rejection will be added to the unhandled rejection queue instead of set as the promise's reason.
                Assert.Throws <AggregateException>(Promise.Manager.HandleCompletes);

                Assert.AreEqual(expected, rejection);

                // Clean up.
                GC.Collect();
                Promise.Manager.HandleCompletesAndProgress();
                LogAssert.NoUnexpectedReceived();
            }
            public void MustHaveAReasonWhichMustNotChange_0()
            {
                CancelationSource cancelationSource = CancelationSource.New();
                var deferred = Promise.NewDeferred(cancelationSource.Token);

                Assert.AreEqual(Promise.State.Pending, deferred.State);

                deferred.Retain();
                object cancelation = null;
                string expected    = "Cancel Value";

                Action rejectAssert  = () => Assert.Fail("Promise was rejected when it should have been resolved.");
                Action resolveAssert = () => Assert.Fail("Promise was resolved when it should have been rejected.");

                TestHelper.AddCallbacks <int, object, string>(deferred.Promise,
                                                              onResolve: resolveAssert,
                                                              onReject: failValue => rejectAssert(),
                                                              onUnknownRejection: rejectAssert);
                deferred.Promise.CatchCancelation(cancelValue => Assert.AreEqual(expected, cancelation = cancelValue.Value));
                cancelationSource.Cancel(expected);
                Promise.Manager.HandleCompletes();

                Assert.AreEqual(expected, cancelation);

                TestHelper.AddCallbacks <int, object, string>(deferred.Promise,
                                                              onResolve: resolveAssert,
                                                              onReject: failValue => rejectAssert(),
                                                              onUnknownRejection: rejectAssert);
                deferred.Promise.CatchCancelation(cancelValue => Assert.AreEqual(expected, cancelation = cancelValue.Value));

                Assert.Throws <InvalidOperationException>(() =>
                                                          cancelationSource.Cancel("Different Cancel Value")
                                                          );

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

                Assert.AreEqual(expected, cancelation);

                cancelationSource.Dispose();
                TestHelper.Cleanup();
            }
예제 #10
0
            public void MustHaveAReasonWhichMustNotChange_1()
            {
                var deferred = Promise.NewDeferred <int>();

                Assert.AreEqual(Promise.State.Pending, deferred.State);

                deferred.Retain();
                object cancelation = null;
                string expected    = "Cancel Value";

                Action rejectAssert  = () => Assert.Fail("Promise was rejected when it should have been resolved.");
                Action resolveAssert = () => Assert.Fail("Promise was resolved when it should have been rejected.");

                TestHelper.AddCallbacks <int, bool, object, string>(deferred.Promise,
                                                                    onResolve: _ => resolveAssert(),
                                                                    onReject: _ => rejectAssert(),
                                                                    onUnknownRejection: rejectAssert);
                deferred.Promise.CatchCancelation(cancelValue => Assert.AreEqual(expected, cancelation = cancelValue.Value));
                deferred.Cancel(expected);
                Promise.Manager.HandleCompletes();

                Assert.AreEqual(expected, cancelation);

                TestHelper.AddCallbacks <int, bool, object, string>(deferred.Promise,
                                                                    onResolve: _ => resolveAssert(),
                                                                    onReject: _ => rejectAssert(),
                                                                    onUnknownRejection: rejectAssert);
                deferred.Promise.CatchCancelation(cancelValue => Assert.AreEqual(expected, cancelation = cancelValue.Value));

                LogAssert.Expect(UnityEngine.LogType.Warning, "Deferred.Cancel - Deferred is not in the pending state.");

                deferred.Cancel("Different Cancel Value");
                deferred.Release();
                Promise.Manager.HandleCompletes();

                Assert.AreEqual(expected, cancelation);

                // Clean up.
                GC.Collect();
                Promise.Manager.HandleCompletesAndProgress();
                LogAssert.NoUnexpectedReceived();
            }
예제 #11
0
        public void AccessingCancelExceptionOrRejectExceptionInOnRejectedDoesNotThrow()
        {
            var promise1 = Promise.Rejected("Reject!");
            var promise2 = Promise.Rejected <int, string>("Reject!");

            TestHelper.AddCallbacks <int, string, string>(promise1,
                                                          onReject: (string rej) =>
            {
                Promise.CancelException();
                Promise.CancelException("Cancel!");
                Promise.RejectException("Reject!");
            },
                                                          onUnknownRejection: () =>
            {
                Promise.CancelException();
                Promise.CancelException("Cancel!");
                Promise.RejectException("Reject!");
            });
            TestHelper.AddCallbacks <int, bool, string, string>(promise2,
                                                                onReject: (string rej) =>
            {
                Promise.CancelException();
                Promise.CancelException("Cancel!");
                Promise.RejectException("Reject!");
            },
                                                                onUnknownRejection: () =>
            {
                Promise.CancelException();
                Promise.CancelException("Cancel!");
                Promise.RejectException("Reject!");
            });

            Promise.Manager.HandleCompletes();

            TestHelper.Cleanup();
        }
예제 #12
0
        public void AccessingCancelExceptionOrRejectExceptionInOnResolvedDoesNotThrow()
        {
            var promise1 = Promise.Resolved();
            var promise2 = Promise.Resolved(100);

            TestHelper.AddCallbacks <int, object, string>(promise1,
                                                          onResolve: () =>
            {
                Promise.CancelException();
                Promise.CancelException("Cancel!");
                Promise.RejectException("Reject!");
            });
            TestHelper.AddCallbacks <int, bool, object, string>(promise2,
                                                                onResolve: v =>
            {
                Promise.CancelException();
                Promise.CancelException("Cancel!");
                Promise.RejectException("Reject!");
            });

            Promise.Manager.HandleCompletes();

            TestHelper.Cleanup();
        }
예제 #13
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();
        }
예제 #14
0
        public void ThrowingCancelExceptionInOnRejectedCancelsThePromiseWithTheGivenValue()
        {
            var promise1 = Promise.Rejected("Rejected");
            var promise2 = Promise.Rejected <int, string>("Rejected");

            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.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.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.rejectVoidVoidCallbacks + TestHelper.rejectVoidConvertCallbacks +
                            TestHelper.rejectVoidPromiseVoidCallbacks + TestHelper.rejectVoidPromiseConvertCallbacks +
                            TestHelper.continueVoidVoidCallbacks + TestHelper.continueVoidConvertCallbacks +
                            TestHelper.continueVoidPromiseVoidCallbacks + TestHelper.continueVoidPromiseConvertCallbacks,
                            voidCancelations);
            Assert.AreEqual(TestHelper.rejectTVoidCallbacks + TestHelper.rejectTConvertCallbacks +
                            TestHelper.rejectTPromiseVoidCallbacks + TestHelper.rejectTPromiseConvertCallbacks +
                            TestHelper.continueTVoidCallbacks + TestHelper.continueTConvertCallbacks +
                            TestHelper.continueTPromiseVoidCallbacks + TestHelper.continueTPromiseConvertCallbacks,
                            intCancelations);

            // Clean up.
            GC.Collect();
            Promise.Manager.HandleCompletesAndProgress();
            LogAssert.NoUnexpectedReceived();
        }