public void Defer <T>([PexAssumeNotNull] IEnumerable <T> source) { var stateful = default(StatefulEnumerable <T>); var enumerable = EnumerableEx.Defer(() => { stateful = new StatefulEnumerable <T>(source); return(stateful); }); PexAssert.IsNull(stateful); var enumerator = enumerable.GetEnumerator(); PexAssert.IsNotNull(stateful); var sourceEnumerator = source.GetEnumerator(); var expectedState = new List <EnumerableState>() { EnumerableState.Fresh, EnumerableState.CreatingEnumerator, EnumerableState.CreatedEnumerator, EnumerableState.MovingNext }; if (!sourceEnumerator.MoveNext()) { PexAssert.IsFalse(enumerator.MoveNext()); expectedState.Add(EnumerableState.AtEnd); PexAssert.AreElementsEqual(expectedState, stateful.States, (a, b) => object.Equals(a, b)); } else { PexAssert.IsTrue(enumerator.MoveNext()); expectedState.Add(EnumerableState.MovedNext); PexAssert.AreElementsEqual(expectedState, stateful.States, (a, b) => object.Equals(a, b)); expectedState.Add(EnumerableState.GettingCurrent); PexAssert.AreEqual(sourceEnumerator.Current, enumerator.Current); expectedState.Add(EnumerableState.GotCurrent); PexAssert.AreElementsEqual(expectedState, stateful.States, (a, b) => object.Equals(a, b)); var count = 1; while (sourceEnumerator.MoveNext()) { count++; expectedState.Add(EnumerableState.MovingNext); PexAssert.IsTrue(enumerator.MoveNext()); expectedState.Add(EnumerableState.MovedNext); PexAssert.AreElementsEqual(expectedState, stateful.States, (a, b) => object.Equals(a, b)); expectedState.Add(EnumerableState.GettingCurrent); PexAssert.AreEqual(sourceEnumerator.Current, enumerator.Current); expectedState.Add(EnumerableState.GotCurrent); PexAssert.AreElementsEqual(expectedState, stateful.States, (a, b) => object.Equals(a, b)); PexAssert.AreEqual(count, stateful.EnumerationCount); } PexAssert.IsFalse(enumerator.MoveNext()); } }
public void Dematerialize <T>([PexAssumeNotNull] IEnumerable <Notification <T> > source) { PexAssume.AreElementsNotNull(source); var dispatched = source.Dematerialize(); var enumerable = new StatefulEnumerable <T>(dispatched) { ShouldRethrow = false }; var enumerator = enumerable.GetEnumerator(); var sourceEnumerator = source.GetEnumerator(); while (sourceEnumerator.MoveNext()) { var notification = sourceEnumerator.Current; var onNext = notification as Notification <T> .OnNext; if (onNext != null) { PexAssert.IsTrue(enumerator.MoveNext()); PexAssert.AreEqual(EnumerableState.MovedNext, enumerable.CurrentState); PexAssert.AreEqual(onNext.Value, enumerator.Current); PexAssert.AreEqual(EnumerableState.GotCurrent, enumerable.CurrentState); } else { var onError = notification as Notification <T> .OnError; if (onError != null) { PexAssert.IsFalse(enumerator.MoveNext()); PexAssert.AreEqual(EnumerableState.MovingNext | EnumerableState.Threw, enumerable.CurrentState); PexAssert.AreEqual(onError.Exception, enumerable.Exception); return; } var onCompleted = (Notification <T> .OnCompleted)notification; PexAssert.IsFalse(enumerator.MoveNext()); PexAssert.AreEqual(EnumerableState.AtEnd, enumerable.CurrentState); return; } } PexAssert.IsFalse(enumerator.MoveNext()); }
public StatefulEnumerator(StatefulEnumerable <T> statefulEnumerable, IEnumerator <T> source) { m_source = source; m_statefulEnumerable = statefulEnumerable; }