public void Enumerate_SingleEntry_JustOneEntry() { var subject = new SmartEnumerable <string>(new[] { "x" }); Assert.That(subject, Must.Be.Constrained( Must.Be.Entry(0, "x", isFirst: true, isLast: true))); }
/// <summary> /// Returns the penultimate element in the sequence or <c>default(T)</c> /// </summary> public static T PenultimateOrDefault <T>(this IEnumerable <T> source) { var enumerable = new SmartEnumerable <T>(source); var lastItem = enumerable.LastOrDefault(); return(lastItem != null ? lastItem.Previous : default(T)); }
public void TripleEntryEnumerable() { List <string> list = new List <string>(); list.Add("x"); list.Add("y"); list.Add("z"); SmartEnumerable <string> subject = new SmartEnumerable <string>(list); using (IEnumerator <SmartEnumerable <string> .Entry> iterator = subject.GetEnumerator()) { Assert.IsTrue(iterator.MoveNext()); Assert.IsTrue(iterator.Current.IsFirst); Assert.IsFalse(iterator.Current.IsLast); Assert.AreEqual("x", iterator.Current.Value); Assert.AreEqual(0, iterator.Current.Index); Assert.IsTrue(iterator.MoveNext()); Assert.IsFalse(iterator.Current.IsFirst); Assert.IsFalse(iterator.Current.IsLast); Assert.AreEqual("y", iterator.Current.Value); Assert.AreEqual(1, iterator.Current.Index); Assert.IsTrue(iterator.MoveNext()); Assert.IsFalse(iterator.Current.IsFirst); Assert.IsTrue(iterator.Current.IsLast); Assert.AreEqual("z", iterator.Current.Value); Assert.AreEqual(2, iterator.Current.Index); Assert.IsFalse(iterator.MoveNext()); } }
public void SingleEntryEnumerableViaCreate() { List <string> list = new List <string>(); list.Add("x"); TestSingleEntry(SmartEnumerable.Create(list)); }
public void SmartEnumerableInitialisesWithEmptyCollection() { IList <string> inner = new List <string>(); SmartEnumerable <string> smart = new SmartEnumerable <string>(inner); Assert.IsNotNull(smart); }
public void Enumerate_DoubleEntry_TwoEntries() { var subject = new SmartEnumerable <string>(new[] { "x", "y" }); Assert.That(subject, Must.Be.Constrained( Must.Be.Entry(0, "x", isFirst: true, isLast: false), Must.Be.Entry(1, "y", isFirst: false, isLast: true))); }
public void Enumerate_TripleEntry_ThreeEntries() { IEnumerable <SmartEntry <string> > subject = new SmartEnumerable <string>(new[] { "x", "y", "z" }); Assert.That(subject, Must.Be.Constrained( Must.Be.Entry(0, "x", isFirst: true, isLast: false), Must.Be.Entry(1, "y", isFirst: false, isLast: false), Must.Be.Entry(2, "z", isFirst: false, isLast: true))); }
public void EmptyEnumerable() { List<string> emptyList = new List<string>(); SmartEnumerable<string> subject = new SmartEnumerable<string>(emptyList); using (IEnumerator<SmartEnumerable<string>.Entry> iterator = subject.GetEnumerator()) { Assert.IsFalse(iterator.MoveNext()); } }
public void SmartEnumerableInitialises() { IList <string> inner = new List <string> { "a", "b", "c" }; SmartEnumerable <string> smart = new SmartEnumerable <string>(inner); Assert.IsNotNull(smart); }
public void SmartEnumerableDoesNotIterateForEmptyCollection() { IList <string> inner = new List <string>(); SmartEnumerable <string> smart = new SmartEnumerable <string>(inner); using (IEnumerator <SmartEnumerable <string> .Entry> iterator = smart.GetEnumerator()) { Assert.IsFalse(iterator.MoveNext()); } }
public void EmptyEnumerable() { List <string> emptyList = new List <string>(); SmartEnumerable <string> subject = new SmartEnumerable <string>(emptyList); using (IEnumerator <SmartEnumerable <string> .Entry> iterator = subject.GetEnumerator()) { Assert.IsFalse(iterator.MoveNext()); } }
private static void TestSingleEntry(SmartEnumerable <string> subject) { using (IEnumerator <SmartEnumerable <string> .Entry> iterator = subject.GetEnumerator()) { Assert.IsTrue(iterator.MoveNext()); Assert.IsTrue(iterator.Current.IsFirst); Assert.IsTrue(iterator.Current.IsLast); Assert.AreEqual("x", iterator.Current.Value); Assert.AreEqual(0, iterator.Current.Index); Assert.IsFalse(iterator.MoveNext()); } }
private static void TestSingleEntry(SmartEnumerable<string> subject) { using (IEnumerator<SmartEnumerable<string>.Entry> iterator = subject.GetEnumerator()) { Assert.IsTrue(iterator.MoveNext()); Assert.IsTrue(iterator.Current.IsFirst); Assert.IsTrue(iterator.Current.IsLast); Assert.AreEqual("x", iterator.Current.Value); Assert.AreEqual(0, iterator.Current.Index); Assert.IsFalse(iterator.MoveNext()); } }
public void SmartEnumerableIteratesForCollectionWithSingleEntry() { List <string> inner = new List <string> { "a" }; SmartEnumerable <string> smart = new SmartEnumerable <string>(inner); using (IEnumerator <SmartEnumerable <string> .Entry> iterator = smart.GetEnumerator()) { iterator.MoveNext(); var current = iterator.Current; Assert.IsNotNull(current); } }
public void SmartEnumerableForCollectionWithMoreThanOneEntryStopsIterating() { List <string> inner = new List <string> { "a", "b" }; SmartEnumerable <string> smart = new SmartEnumerable <string>(inner); using (IEnumerator <SmartEnumerable <string> .Entry> iterator = smart.GetEnumerator()) { iterator.MoveNext(); iterator.MoveNext(); Assert.IsFalse(iterator.MoveNext()); } }
public void SingleEntryUntypedEnumerable() { List<string> list = new List<string>(); list.Add("x"); IEnumerable subject = new SmartEnumerable<string>(list); int index = 0; foreach (SmartEnumerable<string>.Entry item in subject) { // only expecting 1 Assert.AreEqual(0, index++); Assert.AreEqual("x", item.Value); Assert.IsTrue(item.IsFirst); Assert.IsTrue(item.IsLast); Assert.AreEqual(0, item.Index); } Assert.AreEqual(1, index); }
public void SmartEnumerableIteratorWithMoreThanOneEntryReturnsFirstEntryWithExpectedIndex() { List <string> inner = new List <string> { "a", "b" }; SmartEnumerable <string> smart = new SmartEnumerable <string>(inner); using (IEnumerator <SmartEnumerable <string> .Entry> iterator = smart.GetEnumerator()) { iterator.MoveNext(); var first = iterator.Current; Assert.AreEqual(0, first.Index); } }
public void SmartEnumerableIteratorForCollectionWithMoreThanOneEntryReturnsEntryThatIsFirstButNotLast() { List <string> inner = new List <string> { "a", "b" }; SmartEnumerable <string> smart = new SmartEnumerable <string>(inner); using (IEnumerator <SmartEnumerable <string> .Entry> iterator = smart.GetEnumerator()) { iterator.MoveNext(); var first = iterator.Current; Assert.IsTrue(first.IsFirst); Assert.IsFalse(first.IsLast); } }
public void SmartEnumerableIteratorReturnsSingleEntryThatIsFirstAndLastEntry() { List <string> inner = new List <string> { "a" }; SmartEnumerable <string> smart = new SmartEnumerable <string>(inner); using (IEnumerator <SmartEnumerable <string> .Entry> iterator = smart.GetEnumerator()) { iterator.MoveNext(); var current = iterator.Current; Assert.IsTrue(current.IsFirst); Assert.IsTrue(current.IsLast); } }
public void SmartEnumerableIteratorReturnsSingleEntryWithExpectedValue() { const string expected = "a"; List <string> inner = new List <string> { "a" }; SmartEnumerable <string> smart = new SmartEnumerable <string>(inner); using (IEnumerator <SmartEnumerable <string> .Entry> iterator = smart.GetEnumerator()) { iterator.MoveNext(); var current = iterator.Current; Assert.AreEqual(expected, current.Value); } }
public void SingleEntryUntypedEnumerable() { List <string> list = new List <string>(); list.Add("x"); IEnumerable subject = new SmartEnumerable <string>(list); int index = 0; foreach (SmartEnumerable <string> .Entry item in subject) { // only expecting 1 Assert.AreEqual(0, index++); Assert.AreEqual("x", item.Value); Assert.IsTrue(item.IsFirst); Assert.IsTrue(item.IsLast); Assert.AreEqual(0, item.Index); } Assert.AreEqual(1, index); }
public void DoubleEntryEnumerable() { List<string> list = new List<string>(); list.Add("x"); list.Add("y"); SmartEnumerable<string> subject = new SmartEnumerable<string>(list); using (IEnumerator<SmartEnumerable<string>.Entry> iterator = subject.GetEnumerator()) { Assert.IsTrue(iterator.MoveNext()); Assert.IsTrue(iterator.Current.IsFirst); Assert.IsFalse(iterator.Current.IsLast); Assert.AreEqual("x", iterator.Current.Value); Assert.AreEqual(0, iterator.Current.Index); Assert.IsTrue(iterator.MoveNext()); Assert.IsFalse(iterator.Current.IsFirst); Assert.IsTrue(iterator.Current.IsLast); Assert.AreEqual("y", iterator.Current.Value); Assert.AreEqual(1, iterator.Current.Index); Assert.IsFalse(iterator.MoveNext()); } }
public void Enumerate_EmptyEnumerable_Empty() { var subject = new SmartEnumerable <string>(Enumerable.Empty <string>()); Assert.That(subject, Is.Empty); }