示例#1
0
        public IEnumerator ToYieldInstruction_Throws_WhenThrowOnErrorTrue()
        {
            this.coroutineRunner.StartCoroutine(this.CompleteAfterOneFrame());

            var exception        = new Exception("Test exception");
            var yieldInstruction = Responsibly
                                   .WaitForCondition("may complete", () => this.mayComplete)
                                   .Select <object, int>(_ => throw exception)
                                   .ExpectWithinSeconds(1)
                                   .ToYieldInstruction(this.Executor, throwOnError: true);

            TestFailureException failureException;

            while (true)
            {
                try
                {
                    yieldInstruction.MoveNext();
                }
                catch (TestFailureException e)
                {
                    failureException = e;
                    break;
                }

                yield return(null);

                this.mayComplete = true;
            }

            Assert.AreEqual(exception, failureException.InnerException);
        }
        public IEnumerator Polling_HappensOncePerFrame()
        {
            using (var executor = new UnityTestInstructionExecutor(logErrors: false))
            {
                var pollCount = 0;

                Responsibly
                .WaitForCondition("Never", () =>
                {
                    ++pollCount;
                    return(false);
                })
                .ExpectWithinSeconds(1)
                .ToYieldInstruction(executor);

                Assert.AreEqual(1, pollCount, "Should poll once synchronously");

                yield return(null);

                Assert.AreEqual(2, pollCount, "Should poll once per frame");

                yield return(null);

                Assert.AreEqual(3, pollCount, "Should poll once per frame");
            }
        }
        public void LoggingError_CausesFailureSynchronously()
        {
            using (var executor = new UnityTestInstructionExecutor(logErrors: false))
            {
                var yieldInstruction = Responsibly
                                       .WaitForCondition("Never", () => false)
                                       .ExpectWithinSeconds(1)
                                       .ToYieldInstruction(executor);

                Debug.LogError("Should fail the instruction");

                Assert.IsTrue(yieldInstruction.CompletedWithError);
            }
        }
示例#4
0
        public IEnumerator ToYieldInstruction_CompletesAsExpected_WhenConstructedFromWait()
        {
            this.coroutineRunner.StartCoroutine(this.CompleteAfterOneFrame());

            var yieldInstruction = Responsibly
                                   .WaitForCondition("may complete", () => this.mayComplete)
                                   .CreateState()
                                   .ToYieldInstruction(this.Executor);

            yield return(yieldInstruction);

            // Completes one frame "late", because of Update ordering
            Assert.AreEqual(this.completedOnFrame + 1, Time.frameCount);
            Assert.IsTrue(yieldInstruction.CompletedSuccessfully);
        }
        public void LoggingError_DoesNotFail_WhenUsingExpectLog()
        {
            using (var executor = new UnityTestInstructionExecutor(logErrors: false))
            {
                var yieldInstruction = Responsibly
                                       .WaitForCondition("Never", () => false)
                                       .ExpectWithinSeconds(1)
                                       .ToYieldInstruction(executor);

                var message = "Should not fail the instruction";
                executor.ExpectLog(LogType.Error, new Regex(message));
                Debug.LogError(message);

                Assert.IsFalse(yieldInstruction.WasCanceled);
                Assert.IsFalse(yieldInstruction.CompletedSuccessfully);
                Assert.IsFalse(yieldInstruction.CompletedWithError);
            }
        }
示例#6
0
        public IEnumerator ToYieldInstruction_CompletesAsExpected()
        {
            this.coroutineRunner.StartCoroutine(this.CompleteAfterOneFrame());

            var yieldInstruction = Responsibly
                                   .WaitForCondition("may complete", () => this.mayComplete)
                                   .ExpectWithinSeconds(1)
                                   .ToYieldInstruction(this.Executor);

            yield return(yieldInstruction);

            // Completes one frame after
            Assert.AreEqual(this.completedOnFrame + 1, Time.frameCount);

            object unused;

            Assert.IsFalse(yieldInstruction.WasCanceled);
            Assert.IsFalse(yieldInstruction.CompletedWithError);
            Assert.IsTrue(yieldInstruction.CompletedSuccessfully);
            Assert.IsNotNull(yieldInstruction.Result);
            Assert.Throws <InvalidOperationException>(() => unused = yieldInstruction.Error);
        }
        public IEnumerator Polling_IsStopped_WhenDisposed()
        {
            var pollCount = 0;

            using (var executor = new UnityTestInstructionExecutor(logErrors: false))
            {
                Responsibly
                .WaitForCondition("Never", () =>
                {
                    ++pollCount;
                    return(false);
                })
                .ExpectWithinSeconds(1)
                .ToYieldInstruction(executor);
            }

            Assert.AreEqual(1, pollCount, "Should poll once synchronously");

            yield return(null);

            Assert.AreEqual(1, pollCount, "Should not poll after being disposed");
        }
示例#8
0
        public IEnumerator ToYieldInstruction_ErrorsAsExpected()
        {
            this.coroutineRunner.StartCoroutine(this.CompleteAfterOneFrame());

            var exception        = new Exception("Test exception");
            var yieldInstruction = Responsibly
                                   .WaitForCondition("may complete", () => this.mayComplete)
                                   .Select <object, int>(_ => throw exception)
                                   .ExpectWithinSeconds(1)
                                   .ToYieldInstruction(this.Executor, false);

            yield return(yieldInstruction);

            // Completes one frame "late", because of Update ordering
            Assert.AreEqual(this.completedOnFrame + 1, Time.frameCount);

            object unused;

            Assert.IsFalse(yieldInstruction.WasCanceled);
            Assert.IsTrue(yieldInstruction.CompletedWithError);
            Assert.IsFalse(yieldInstruction.CompletedSuccessfully);
            Assert.Throws <InvalidOperationException>(() => unused = yieldInstruction.Result);
            Assert.AreSame(exception, yieldInstruction.Error.InnerException);
        }
示例#9
0
        public void Instruction_IsCanceled_WhenExternalResult([Values] bool error)
        {
            var polled = false;
            var unused = Responsibly
                         .WaitForCondition("Fake condition", () =>
            {
                polled = true;
                return(false);
            })
                         .ExpectWithinSeconds(1);

            if (error)
            {
                this.completionSource.SetException(new Exception());
            }
            else
            {
                this.completionSource.SetResult(1);
            }

            this.AdvanceDefaultFrame();

            Assert.IsFalse(polled);
        }