public void NestedEnumerableWithCoroutinesWantingToTerminate()
        {
            var subCoroutine = new CoroutineStackTestCoroutine(true);
            var steps        = new List <Step>
            {
                Step.DataStep(new EnumeratorWrapper {
                    IsCoroutine  = true,
                    SubCoroutine = subCoroutine,
                })
            };
            var root = new CoroutineStackTestCoroutine();
            var cst  = new CoroutineStack(steps.GetEnumerator(), root);

            Assert.IsTrue(cst.MoveNext());
            var current = cst.Current as Step;

            Assert.IsTrue(current.HasData);
            Assert.IsInstanceOfType(current.Data, typeof(EnumeratorWrapper));
            var enumData = current.Data as EnumeratorWrapper;

            Assert.AreEqual(subCoroutine, enumData.SubCoroutine);
            subCoroutine.Run().ToList();
            Assert.IsTrue(cst.MoveNext());
            Assert.IsFalse(cst.MoveNext());
        }
        public void NestedEnumerableWithCoroutinesShoulBeConsideredWithException()
        {
            var ex           = new Exception();
            var subCoroutine = new CoroutineStackTestCoroutine(ex);
            var steps        = new List <Step>
            {
                Step.DataStep(new EnumeratorWrapper {
                    IsCoroutine  = true,
                    SubCoroutine = subCoroutine
                })
            };

            try
            {
                subCoroutine.Run().ToList();
            }
            catch (Exception exx)
            {
                subCoroutine.OnError(exx);
            }
            var root = new CoroutineStackTestCoroutine();
            var cst  = new CoroutineStack(steps.GetEnumerator(), root);

            Assert.IsTrue(cst.MoveNext());
            var current = cst.Current as Step;

            Assert.IsTrue(current.HasData);
            Assert.IsInstanceOfType(current.Data, typeof(EnumeratorWrapper));
            var enumData = current.Data as EnumeratorWrapper;

            Assert.AreEqual(subCoroutine, enumData.SubCoroutine);
            Assert.IsTrue(cst.MoveNext());
            Assert.IsFalse(cst.MoveNext());
        }
        public void NestedEnumerableWithCoroutinesShoulBeConsidered()
        {
            var subCoroutine = new CoroutineStackTestCoroutine();
            var steps        = new List <Step>
            {
                Step.DataStep(new EnumeratorWrapper {
                    IsCoroutine  = true,
                    SubCoroutine = subCoroutine,
                })
            };

            subCoroutine.Run().ToList();
            var root = new CoroutineStackTestCoroutine();
            var cst  = new CoroutineStack(steps.GetEnumerator(), root);

            Assert.IsTrue(cst.MoveNext());
            var current = cst.Current as Step;

            Assert.IsTrue(current.HasData);
            Assert.IsInstanceOfType(current.Data, typeof(EnumeratorWrapper));
            var enumData = current.Data as EnumeratorWrapper;

            Assert.AreEqual(subCoroutine, enumData.SubCoroutine);
            Assert.IsTrue(cst.MoveNext());

            //Normally invoked by the thread
            subCoroutine.ShouldTerminate = true;
            Assert.IsTrue(cst.MoveNext());
            Assert.IsFalse(cst.MoveNext());
        }
        public void ExceptionsInTasksShouldBeHandled()
        {
            var taskInstance = Task.Run(() =>
            {
                throw new NotFiniteNumberException();
            });
            var steps = new List <Step>
            {
                Step.DataStep(new EnumeratorWrapper {
                    IsTask       = true,
                    TaskInstance = taskInstance
                })
            };
            var root = new CoroutineStackTestCoroutine();
            var cst  = new CoroutineStack(steps.GetEnumerator(), root);

            Thread.Sleep(100);
            Assert.IsTrue(cst.MoveNext());
            var current = cst.Current as Step;

            Assert.IsTrue(current.HasData);
            Assert.IsInstanceOfType(current.Data, typeof(EnumeratorWrapper));
            var enumData = current.Data as EnumeratorWrapper;

            Assert.AreEqual(taskInstance, enumData.TaskInstance);

            cst.MoveNext();
        }
        public void NestedEnumerableWithTasksShoulBeConsidered()
        {
            var taskInstance = Task.Run(() =>
            {
                Thread.Sleep(50);
            });
            var steps = new List <Step>
            {
                Step.DataStep(new EnumeratorWrapper {
                    IsTask       = true,
                    TaskInstance = taskInstance
                })
            };
            var root = new CoroutineStackTestCoroutine();
            var cst  = new CoroutineStack(steps.GetEnumerator(), root);

            Thread.Sleep(100);
            Assert.IsTrue(cst.MoveNext());
            var current = cst.Current as Step;

            Assert.IsTrue(current.HasData);
            Assert.IsInstanceOfType(current.Data, typeof(EnumeratorWrapper));
            var enumData = current.Data as EnumeratorWrapper;

            Assert.AreEqual(taskInstance, enumData.TaskInstance);
            Assert.IsTrue(cst.MoveNext());
            Assert.IsFalse(cst.MoveNext());
        }
        public void MoveNextShouldStopWithEmptyStack()
        {
            var steps = new List <Step>
            {
                Step.DataStep(new EnumeratorWrapper {
                    Enum = new List <Step>().GetEnumerator()
                })
            };
            var root = new CoroutineStackTestCoroutine();
            var cst  = new CoroutineStack(steps.GetEnumerator(), root);

            Assert.IsTrue(cst.MoveNext());
            Assert.IsFalse(cst.MoveNext());
        }
        public void NestedEnumerableWithoutCoroutineOrTasksShoulBeConsidered()
        {
            var internalEnum = new List <Step>().GetEnumerator();
            var steps        = new List <Step>
            {
                Step.DataStep(new EnumeratorWrapper {
                    Enum = internalEnum
                })
            };
            var root = new CoroutineStackTestCoroutine();
            var cst  = new CoroutineStack(steps.GetEnumerator(), root);

            Assert.IsTrue(cst.MoveNext());
            var current = cst.Current as Step;

            Assert.IsTrue(current.HasData);
            Assert.IsInstanceOfType(current.Data, typeof(EnumeratorWrapper));
            var enumData = current.Data as EnumeratorWrapper;

            Assert.AreEqual(internalEnum, enumData.Enum);
            Assert.IsFalse(cst.MoveNext());
        }