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)));
        }
예제 #2
0
        /// <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));
        }
예제 #3
0
        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());
            }
        }
예제 #4
0
        public void SingleEntryEnumerableViaCreate()
        {
            List <string> list = new List <string>();

            list.Add("x");

            TestSingleEntry(SmartEnumerable.Create(list));
        }
예제 #5
0
        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)));
        }
예제 #8
0
        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());
            }
        }
예제 #9
0
        public void SmartEnumerableInitialises()
        {
            IList <string> inner = new List <string> {
                "a", "b", "c"
            };

            SmartEnumerable <string> smart = new SmartEnumerable <string>(inner);

            Assert.IsNotNull(smart);
        }
예제 #10
0
        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());
            }
        }
예제 #11
0
        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());
            }
        }
예제 #12
0
 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());
     }
 }
예제 #13
0
 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());
     }
 }
예제 #14
0
        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);
            }
        }
예제 #15
0
        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());
            }
        }
예제 #16
0
 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);
 }
예제 #17
0
        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);
            }
        }
예제 #18
0
        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);
            }
        }
예제 #19
0
        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);
            }
        }
예제 #20
0
        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);
            }
        }
예제 #21
0
        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);
        }
예제 #22
0
        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);
        }